converter has asked for the wisdom of the Perl Monks concerning the following question:
Someone on DALnet #perl challenged the channel to come up with the shortest subroutine to return the last character of its argument(s). I thought about it for a few seconds, and replied with:
sub foo {chop(@_=@_)}
This works as expected when foo() is passed a single scalar argument, but when foo() is passed a list, rather than returning the last character of the last list element as one would expect after reading the documentation on chop()*, it returns the last character of the first element in the list.
@a = ('ab','cd','ef'); print chop @a; # f print foo(@a); # b # the list assignment has the same effect here: print chop(@a=@a); # b
Changing foo() to:
makes foo() work as expected.sub foo {chop(@_=reverse @_)}
Is this a documented behavior? I am probably missing some simple concept involved in list assignments, but I'd like to know why the list assignment has this effect.
*from perlfunc: If you chop a list, each element is the chopped. Only the value of the last `chop' is returned.
edit: chipmunk on 2001-03-05