I am surprised by sort

gabor on 2003-07-29T15:31:06

@a=qw(abc def ghi);
@b = map {chop $_} sort @a;
print "@a";

prints the following
# ab de gh

I though the sort already returns copy of the original values so the chop would affect only these copies and thus only @b. In @a I was expecting
# abc def ghi

 

This also means that the following code:
@b = map {chop $_} sort qw(ab cd ef);
print "OK\n"

sais:
Modification of a read-only value attempted at -e line 1.

instead of OK


Aliasing

dws on 2003-07-29T15:44:47

Read the description of grep carefully, and note what it says about aliasing. (Yeah, I know you're using map, and the POD for map doesn't say anything about aliasing (at least not in 5.8), but map and grep behave behave the same in this regard.)

Re:Aliasing

dws on 2003-07-29T16:40:41

Uh, never mind. The issue really is with sort, and the lack of info in perlfunc on sort's aliasing behavior.