in reply to Unexpected results when removing a HERE-DOC "ending" newline, with map and/or s/// depending on context.

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.
  • Comment on Re: Unexpected results when removing a HERE-DOC "ending" newline, with map and/or s/// depending on context.
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Unexpected results when removing a HERE-DOC "ending" newline, with map and/or s/// depending on context.
by olivierp (Hermit) on Dec 09, 2004 at 08:51 UTC
    Doh !
    Thanks to fglock and Errto for pointing out the obivous.
    This at least "fixes" my $var3:
    my ($var3) = map { s/\r?\n\Z/(munched)/; $_ } <<EOT; Var also has an interpolated var: $interpolated And the last newline is also absent -> EOT
    However, $var4 still explodes on a read-only value, and I still haven't found, apart from eval'ing the whole thing, how to do the same for $href->{key3} = {...

    --
    Olivier

      Very interesting - that's because an interpolated string is actually an unnamed variable, while a simple string is just a constant.