ibm1620 has asked for the wisdom of the Perl Monks concerning the following question:

I'm reading the Perl 5.26.1 perlop documentation about the indented here-doc. It says:
The here-doc modifier ~ allows you to indent your here-docs to make the code more readable:
if ($some_var) { print <<~EOF; This is a here-doc EOF }
This will print...
This is a here-doc
...with no leading whitespace.

The delimiter is used to determine the exact whitespace to remove from the beginning of each line.

It's not obvious to me what "the delimiter" is and how was it declared. To me, it's an odd use of the word "delimiter".

Am I correct in assuming that what's meant is: when you write "<<~EOF;", Perl will take the initial whitespace characters from the following line as an "indentation definition" and remove it from the beginning of every line in the here-doc?

Replies are listed 'Best First'.
Re: indented here-doc "delimiter"
by LanX (Saint) on Apr 14, 2018 at 00:33 UTC
    You should test it, I can't at the moment.

    My reading is that the whitespace before the delimiter EOF is striped from every line.

    The interesting question is rather what happens if the a line is more to the left than the delimiter. A warning?

    Btw: I love this feature! :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

      Turns out I can't test it either, since it was introduced in 5.26.

      However, brian d foy states here that the leading whitespace from the final delimiter is what's used to determine what gets stripped from every line (as you had supposed). And, that lines with insufficient indentation cause a compile-time error.

Re: indented here-doc "delimiter"
by trippledubs (Deacon) on Apr 14, 2018 at 01:52 UTC
    #!/usr/bin/env perl my $string = <<~THEEND; EOF THEEND print $string; # FAIL
    THEEND
    is the chosen delimiter
Re: indented here-doc "delimiter"
by Anonymous Monk on Apr 14, 2018 at 08:54 UTC
    Maybe EOF should better be renamed to DELIM ?
Re: indented here-doc "delimiter"
by Anonymous Monk on Apr 14, 2018 at 08:20 UTC
    Probably left out the word 'ending' delimiter because the starting delimiter doesnt have whitespace