Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

here document styling

by hatter (Pilgrim)
on Mar 06, 2002 at 18:30 UTC ( #149782=perlquestion: print w/replies, xml ) Need Help??

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

I came across something quite strange in a cow-orkers perl that I found both unusual, and slightly confused me as to why it works. Rather than using the familiar:
print <<EOTEXT; mumble mumble rhubarb EOTEXT
they write them like:
print <<EOTEXT mumble mumble rhubarb EOTEXT ;

So firstly, does anyone else use this style, or know where it starts ? A quick supersearch didn't show up anything pertinent, is there some set of tutorials that prefers the other method ?

And secondly, why does this form compile, it seems to accept EOTEXT (or whatever marker is used) as some sort of quoting, but I've not seen any explanation as to why it would.

the hatter

Edit by tye to reduce "\n"x5 (probably due to browser quirk)

Replies are listed 'Best First'.
Re: here document styling
by Juerd (Abbot) on Mar 06, 2002 at 18:37 UTC
    Quoting from perldata:

    Just don't forget that you have to put a semicolon on the end to finish the statement, as Perl doesn't know you're not going to try to do this:
    print <<ABC 179231 ABC + 20;

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq

Re: here document styling
by impossiblerobot (Deacon) on Mar 06, 2002 at 19:17 UTC
    Essentially what the perl compiler does with heredocs is to replace the heredoc definition with a string equivalent of the contents of the heredoc. By runtime, the heredoc itself is gone. If you do this yourself, mentally, then you will see that:
    my $text = <<"EOT" This is my text. EOT ;
    my $text = <<"EOT"; This is my text. EOT
    are identical, and equal to:
    my $text = "This\nis\nmy\ntext.\n";
    Update: BTW, this lets you do weird things like this:
    while ( <<"EOT" =~ m/(.*is.*)/g ) This is my text, isn't it? EOT { print $1,"\n"; }

    Impossible Robot
Re: here document styling
by strat (Canon) on Mar 06, 2002 at 23:15 UTC
    I just write
    print <<EOTEXT; mumble mumble rhubarb EOTEXT ;
    If I don't write the semicolon, emacs (my favourite editor) doesn't understand and doesn't insert the automatic spaces at the beginning when pressing TAB ;-)(i don't know the english expression, sorry, but I hope you will understand what I want to say).

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

    Comment to change:The word what I was looking for was: automatic indentation (thanks to Juerd)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-01-28 20:26 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (73 votes). Check out past polls.