in reply to Still don't undersand "Modification of a read-only value attempted" error

The for-loop aliases the variable (here $item) to the list elements. If the list element is a literal, s/// cannot modify it.

On the other hand @list = ("abcdef", "ghijkl", "Dumbo"); puts the elements into an array, and you can always change array elements.

Or phrased differently, it's the same as

"abcdef" = 3; # doesn't work $list[0] = 3; # works

Replies are listed 'Best First'.
Re^2: Still don't undersand "Modification of a read-only value attempted" error
by choroba (Cardinal) on Jun 20, 2012 at 22:04 UTC
    Also note that range operator behaves differently than a hardcoded list:
    s/[u3]/X/, print for 1 .. 10, "a" .. "z"; # ok s/[u3]/X/, print for 1, 2, 3, 4, "a", "b", "u", "z"; # error again

      I find this interesting — and surprising! Range Operators in perlop has:

      Binary ".." is the range operator.... In list context, it returns a list of values .... If the left value is greater than the right value then it returns the empty list. ... In the current implementation, no temporary array is created when the range operator is used as the expression in foreach loops...

      This certainly makes it seem as though the range operator (in list context) returns an ordinary list, so why the difference in behaviour?

      Is this documented anywhere? Or is it an undocumented feature?

      Athanasius <°(((><contra mundum