in reply to Re^2: Changing quoted strings spanning more than one line (modifying constants)
in thread Changing quoted strings spanning more than one line

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}

Replies are listed 'Best First'.
Re^4: Changing quoted strings spanning more than one line (modifying constants)
by ikegami (Patriarch) on Sep 19, 2007 at 21:01 UTC

    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.