http://qs321.pair.com?node_id=686008


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 (Saint) 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