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

Re: Why do here-docs have to end with a newline, not EOF?

by tachyon-II (Chaplain)
on May 12, 2008 at 00:24 UTC ( #686008=note: print w/replies, xml ) Need Help??

in reply to Why do here-docs have to end with a newline, not EOF?

This behaviour stems from the implementation of scan_heredoc() found in toke.c in the Perl 5 source

The basic logic in that function is to grok the token and add a newline to the end of it. Then scan the following lines until the token (complete with newline on the end) is found sitting on a line by itself, left justified (ie no leading whitespace). RFC 111 for perl 6 suggests making:

$var = <<STUFF; ... STUFF $var = <<STUFF; ... STUFF $var = <<STUFF; ... STUFF;

All as legal syntax, ie the requirement for no leading whitespace and the trailing newline will be removed. Ongoing discussion in RFC 162. Trailing whitespace on the closing token line also breaks heredocs as the search token "string\n" is not found. Commenting out the line *d++ = '\n' looks like it would change the trailing newline requirement, but my analysis was brief, suprficial and totally untested.

Replies are listed 'Best First'.
Re^2: Why do here-docs have to end with a newline, not EOF?
by GrandFather (Sage) on May 12, 2008 at 00:56 UTC

    An even briefer glance at the code suggests that commenting out the newline assignment (I presume following the token size test) would allow any terminal token starting with the heredoc start token string, but possibly including any amount of garbage following it, would match. The trailing new line is a little like an implicit \b.

    Perl is environmentally friendly - it saves trees

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://686008]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2020-08-09 12:28 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (54 votes). Check out past polls.