in reply to Re: Changing quoted strings spanning more than one line
in thread Changing quoted strings spanning more than one line

I wonder how the $1 =~ tr/\n// in your solution doesn't die with "modification of a readonly value attempted"?

Have a peek at this:

local $, = ", "; local $\ = "\n"; for (1..2) { print map { ++$_ } 0..4; }
1, 2, 3, 4, 5 2, 3, 4, 5, 6

Replies are listed 'Best First'.
Re^3: Changing quoted strings spanning more than one line (modifying constants)
by shmem (Chancellor) on Sep 19, 2007 at 20:46 UTC
    That's cute, but doesn't help explaining why $1 =~ tr/\n// doesn't attempt to modify $1, which is read-only.

    Interesting, though.

    local $, = ", "; local $\ = "\n"; for ($foo,$bar) { print map { ++$_ } 0..4; }
    1, 2, 3, 4, 5 2, 3, 4, 5, 6

    The outer $_ isn't related to map's $_. But it seems like map's $_ isn't being reset each time through the loop. What gives? Bug or some (undocumented) feature?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      That's cute, but doesn't help explaining why $1 =~ tr/\n// doesn't attempt to modify $1, which is read-only

      I know. Someone already explained that tr/\n// doesn't modify the variable on which acts because it wasn't asked to.

      But it seems like map's $_ isn't being reset each time through the loop.

      Apparently, Perl expands 0..4 into the 5 item list only once, possibly as an optimisation since 0..4 is constant. However, it doesn't set the read-only flag on the members of the expanded list. The map I constructed modifies this list that should be constant, but isn't.

      Change 0..4 to 0,1,2,3,4 and you'll get a read-only violation.
      Change ++$_ to 1+$_ and you won't modify the list.