On Thu, July 21, 2005 3:34 pm, Dave Mitchell said: > On Thu, Jul 21, 2005 at 01:15:39PM -0700, Michael G Schwern wrote: >> On Thu, Jul 21, 2005 at 10:20:22AM -0400, Shiping Zhang wrote: >> > But shouldn't the alias be to the elements of the list returned by >> sort, >> > not to the elements of the original list? Should sort sort a copy of >> its >> > argument and return the sorted copy? >> >> That's what I'm thinking, that this is a sort/foreach optimization gone >> wrong. foreach $_ works on aliases of the array its looping over but >> sort >> does NOT sort in place. "foreach (sort @a)" should not loop over >> aliases >> to @a any more than "foreach (map {$_} @a)" should. > > I wouldn't expect map to maintain the alias, because map returns the > result of an expression; on the other hand, grep, like sort, returns the > original values, and like sort, it maintains the alias: > > $ perl587 -we '@a=qw(1 2 3); for (grep /\d/, @a) { $_++ } print > "@a\n"' > 2 3 4 > $ > > I don't think its a bug so much as undocumented behaviour. "fixing" it > would involve doing a whole bunch of unneccessary copying in sort and > grep.
And reverse and any XS sub that directly returns any of its SV arguments (eg List::Util::shuffle), not to mention any sub that returns a slice of @_ Graham.