tantalor has asked for the wisdom of the Perl Monks concerning the following question:
I'm curious about the behavior of the scalar value of a list assignment expression,
sub foo { my ($a) = qw(first second third); }; printf "in scalar context: %s\n", scalar foo(); printf "in list context: %s\n", join ',', foo();
This gives me,
in scalar context: 3 in list context: first
So it seems like the value of a list assignment in scalar context is the scalar-context value of the list on the right-hand side of the assignment, but the value of the list assignment in list context is the list-context value of list on the left-hand side of the assignment.
Is that correct? Is there a simpler way of putting that?
This is sort of a surprising result. I expected the list and scalar context values to "match", or come from the same place, but in this case it appears they come from different parts of the expression (left-hand/right-hand sides).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: scalar value of list assignment
by chromatic (Archbishop) on Jan 17, 2012 at 20:11 UTC | |
|
Re: scalar value of list assignment
by JavaFan (Canon) on Jan 17, 2012 at 20:14 UTC | |
by tantalor (Initiate) on Jan 17, 2012 at 20:22 UTC | |
by JavaFan (Canon) on Jan 17, 2012 at 20:40 UTC | |
|
Re: scalar value of list assignment
by ww (Archbishop) on Jan 17, 2012 at 20:21 UTC | |
|
Re: scalar value of list assignment
by tobyink (Canon) on Jan 17, 2012 at 19:58 UTC | |
|
Re: scalar value of list assignment
by ikegami (Patriarch) on Jan 18, 2012 at 07:13 UTC |