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.
edit: chipmunk on 2001-03-05
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ttchop()/tt and list assignments
by dvergin (Monsignor) on Mar 06, 2001 at 07:55 UTC | |
by converter (Priest) on Mar 06, 2001 at 08:06 UTC | |
by japhy (Canon) on Mar 06, 2001 at 08:49 UTC | |
by tye (Sage) on Mar 06, 2001 at 10:52 UTC | |
by tilly (Archbishop) on Mar 06, 2001 at 08:12 UTC | |
|
Re: chop() and list assignments
by chipmunk (Parson) on Mar 06, 2001 at 23:25 UTC | |
|
Re: chop() and list assignments
by InfiniteSilence (Curate) on Mar 06, 2001 at 22:50 UTC |