in reply to Here documents in blocks

Hello Bod,

if I understand your question, prior of perl 5.26 you can use some trick to indent heredocs: substitution (but the token in not indented):

($var = <<"HEREDOC") =~ s/^\s+//gm; your text goes here HEREDOC

But note you can also use "     HEREDOC" as token: see (and follow the white rabbit) here

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.

Replies are listed 'Best First'.
Re^2: Here documents in blocks
by ikegami (Patriarch) on Dec 20, 2020 at 16:58 UTC

    In addition to the missing /r (already mentioned), that will remove blank lines. Use \h instead of \s.

Re^2: Here documents in blocks
by Bod (Parson) on Dec 19, 2020 at 16:59 UTC
    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...

      > 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

      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.