Couple of comments here:

  1. I admit that the discrepancy between what happens with $var3 and $var4 is bizarre. I can't figure out why one is allowed and one isn't.
  2. On the other hand, a here doc is kind of like a string literal. You're effectively trying to modify a string literal without assigning it to a variable first. This is dangerous even if it does sometimes work.
  3. Why are you applying map to a single string? map is meant to work on lists, and a here doc is only a single string, that is a scalar, that is a list of length one. Just assign it to a variable and then use s///. You've already shown how to do that in a single line of code (i.e. (my $foo = <<EOT) =~ s/foo/bar/).
  4. Update: You say in your comments that map returns the number of substitutions. That's not true. That's what s/// returns in scalar context. map in scalar context returns a count of the elements in the list, which in your case is always 1.

In reply to Re: Unexpected results when removing a HERE-DOC "ending" newline, with map and/or s/// depending on context. by Errto
in thread Unexpected results when removing a HERE-DOC "ending" newline, with map and/or s/// depending on context. by olivierp

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.