Why Filter::Indent::HereDoc complain when blank line in middle of HereDoc

146 views Asked by At

I am trying Filter::Indent::HereDoc which allows one to indent the HereDocument. This is very useful, to be able to have HereDoc that flows with the code logic.

From the above link

When a 'here document' is used, the document text and the termination string must be flush with the left margin, even if the rest of the code block is indented.

Filter::Indent::HereDoc removes this restriction, and acts in a more DWIM kind of way - that if the terminator string is indented then that level of indent will apply to the whole document.

This module can be installed using sudo cpanm Filter::Indent::HereDoc

However, I am finding that if there is blank line inside the HereDoc, then it gives the error

  Can't find string terminator "EOT" anywhere before EOF at....

Which does not happen when I remove the blank line. Here is MWE

#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;

my $str = <<'EOT';
    some text
    some text
    EOT

print $str;

The above runs with no problem, even though space exists before EOT (It would have failed without using Filter::Indent::HereDoc)

>perl t4.pl 
some text
some text
>

Now I simply insert a blank line in between, like this

#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;

my $str = <<'EOT';
    some text

    some text
    EOT

print $str;

and now

>perl t4.pl 
Can't find string terminator "EOT" anywhere before EOF at t4.pl line 6.
>

If the space before EOT is now removed, then it works:

#!/usr/bin/perl
use strict;
use warnings;
use Filter::Indent::HereDoc;

my $str = <<'EOT';
     some text

     some text
EOT

print $str;

and now

>perl t4.pl 
     some text

     some text
>

But the whole idea of using this module is to allow one to indent the HereDoc.

Any idea what is going on?

Information:

>perl --version

This is perl 5, version 18, subversion 2 (v5.18.2) built 
for i686-linux-gnu-thread-multi-64int
1

There are 1 answers

3
simbabque On BEST ANSWER

The documentation says:

If there is no terminator string (so the here document stops at the first blank line), then enough whitespace will be stripped out so that the leftmost character of the document will be flush with the left margin, e.g.

print <<;
     Hello,
    World!

# This will print:
 Hello,
World!

This is the paragraph directly below the one you quoted in the question. The fact that it stops at blank lines is a feature.