Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Indented HEREDOC?

by Withigo (Friar)
on May 26, 2006 at 07:03 UTC ( #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!

Comment on Indented HEREDOC?
Download Code
Re: Indented HEREDOC?
by lidden (Deacon) 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 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 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 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
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? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-07-10 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls