Using Here documents can cause you alot of grief if you are not careful.

You have to know what to expect. HERE docs originated in UNIX shell scripting. The shell, upon detecting a '<<' token, actually seeks ahead in the script file itself until it finds the end token (here, 'text'), and then it writes the intervening lines into a temp file and runs the command line containing with the '<<end_token' construct replaced by simple input redirection from that file.

This allows including input data in the script itself, rather than requiring separate files, which have the potential for getting out of sync.

Perl attempts to simulate the same effect, but in an improved way (e.g., the entire HERE doc is treated as though it were surrounded with whatever kind of quotes, if any, surround the end token). Perl does not necessarily use a temp file for each HERE doc, but it helps to think of them as though it did just that. The end token must be the first and only thing on its line or it will be treated as part of the content.

Within a HERE doc, line endings are treated just like any other character, much the same as if an external file were read using the sequence:

undef $/; # newlines are not record separators anymore $_ = <>; # slurp the whole file in as one big string

BTW, it is established convention to use all uppercase characters for the end token.

dmm

You can give a man a fish and feed him for a day ...
Or, you can
teach him to fish and feed him for a lifetime

In reply to Re(2): EOF in string by dmmiller2k
in thread EOF in string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.