Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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)

Comment on here document styling
Select or Download Code
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 ;
    and
    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-12-25 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls