in reply to Re: Here documents in blocks
in thread Here documents in blocks

prior of perl 5.26 you can use some trick to indent heredocs: substitution

That only works for variable assignment. If the same trick is attempted with a print statement, Perl complains that we Can't modify print in substitution (s///).

print <<"HEREDOC" =~ s/^\s+//gm; # <- Doesn't work your text goes here HEREDOC

I guess it could just be followed by print $var; but that continues the theme of inelegant solutions. I am sure there is a 'nice' solution to this...

Replies are listed 'Best First'.
Re^3: Here documents in blocks
by LanX (Saint) on Dec 19, 2020 at 19:15 UTC
    > If the same trick is attempted with a print statement, Perl complains that we Can't modify print in substitution (s///).

    That's why the /r flag was introduced. try s///r

    update

    print <<"HEREDOC" =~ s/^\s+//gmr; # <- works your text goes here HEREDOC __END__ your text goes here

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

Re^3: Here documents in blocks
by Discipulus (Canon) on Dec 19, 2020 at 17:07 UTC
    Hello Bod,

    this works

    use strict; use warnings; print $_=<<" EOT"; indented as intended EOT exit;

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      this works

      But this doesn't...

      use strict; if (1) { print<<" END"; Here is some text which needs to be flush to the left margin END }

      It works in the sense it compiles and runs but doesn't do as intended.

        This one?
        use strict; if (1) { $_=<<" END"; Here is some text which needs to be flush to the left margin END s/^\s+//gm; print; }

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.