Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Indented HEREDOC?

by Withigo (Friar)
on May 26, 2006 at 07:03 UTC ( [id://551783]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
Several times I've found myself wishing I could indent the string that terminates a HEREDOC in order to align the entire HEREDOC with its containing sub/block(and also for nefarious-but-inspiring obfuscation & japh purposes).

i.e.,
if ($foo) { print <<STRING; foo is $foo. STRING }
Is this trivial enough to be capable of being added to perl5? Hopefully it'll be an option in Perl 6. But in the mean time, does anyone know if there's a source filter that accomplishes this?
Thanks!

Replies are listed 'Best First'.
Re: Indented HEREDOC?
by Ovid (Cardinal) on May 26, 2006 at 07:46 UTC

    I posted a snippet which will handle this at Unindent HERE docs. Also, if you want to indent the closing token, put the leading token in quotes.

    print <<' END'; This is text. END

    Cheers,
    Ovid

    New address of my CGI Course.

      sorry for the nit-pick, but given the OP's example showed a variable, the example really should have been.
      if ($foo) { print <<" STRING"; foo is $foo. STRING }
      With the difference being that I've used double quotes and not single quotes. I mention it becuase I missed this the first time I saw heredocs, and didn't realise it caused the entire contents of the heredoc to be either interpolated or not, dependant on whether you used double or single quotes, respectively.

      The output of the above would obviously be

      foo is whatever_is_in_foo

      as opposed to single quotes, which would give

      foo is $foo
      ---
      my name's not Keith, and I'm not reasonable.

        No need to apologize for the nitpick. It's a point well-taken.

        Cheers,
        Ovid

        New address of my CGI Course.

Re: Indented HEREDOC?
by kudra (Vicar) on May 26, 2006 at 07:10 UTC
    I think Filter::Indent::HereDoc does this. I haven't used it, however.
Re: Indented HEREDOC?
by LanX (Saint) on Mar 20, 2018 at 17:51 UTC
    Since this thread was my top-hit in google (YMMV):

    Intended Here Documents were introduced with 5.26.

    Indented Here-documents

    This adds a new modifier "~" to here-docs that tells the parser that it should look for /^\s*$DELIM\n/ as the closing delimiter.

    These syntaxes are all supported:

    <<~EOF; <<~\EOF; <<~'EOF'; <<~"EOF"; <<~`EOF`; <<~ 'EOF'; <<~ "EOF"; <<~ `EOF`;

    The "~" modifier will strip, from each line in the here-doc, the same whitespace that appears before the delimiter.

    Newlines will be copied as-is, and lines that don't include the proper beginning whitespace will cause perl to croak.

    For example:

    if (1) { print <<~EOF; Hello there EOF }

    prints "Hello there\n" with no leading whitespace.

    Probably my favourite improvement since // defined-or.

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

Re: Indented HEREDOC?
by lidden (Curate) on May 26, 2006 at 07:07 UTC
    It will be added to Perl 6, I think it will be the default.
Re: Indented HEREDOC?
by mantadin (Beadle) on May 26, 2006 at 09:24 UTC
    One workaround maybe:
    ($v = <<" END") =~ s/^\s+//gm; foo bar and so on END
Re: Indented HEREDOC?
by vek (Prior) on May 26, 2006 at 17:44 UTC
Re: Indented HEREDOC?
by YuckFoo (Abbot) on May 26, 2006 at 22:43 UTC
    Withigo

    I use the quote operators in place of heredocs whenever possible (perlop), because it looks better. This adds a leading newline and trailing whitespace to the string, but when that's not important, I like it.

    QqFoo

    if ($foo) { print qq { foo is $foo. }; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://551783]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found