Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
Node Status?
node history
Node Type: perlquestion [id://149782]
Approved by root
[erix]: (culprits often are upstream of db of course)
[Corion]: Ah, how I missed it. After some years, I revisit slashdot on a click-bait link, and it provides the usual humor instantly: "I didn't know Drupal had rules for sex. It must be a plug-in"
[Corion]: erix: This is not for sybase, but for the input data files, resp. their contents.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-03-28 09:03 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.