Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: scalar value of list assignment

by JavaFan (Canon)
on Jan 17, 2012 at 20:14 UTC ( [id://948401]=note: print w/replies, xml ) Need Help??


in reply to scalar value of list assignment

The value of a list assignment in scalar context is the number of elements on the right hand side of the assignment.

This is documented in perlop:

Similarly, a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment.
This is not the same as "scalar-context value of the list on the right hand side of the assignment". First of all, there is no list in scalar context (duh! if the context is scalar, it ain't list!), and second, if you mean, "the value of the expression on the RHS of the assignment, but then in scalar context", it's wrong as well, as the value of qw(first second third) in scalar context is third, not 3.

Replies are listed 'Best First'.
Re^2: scalar value of list assignment
by tantalor (Initiate) on Jan 17, 2012 at 20:22 UTC
    You're absolutely right, I confused list with array. I was thinking of something more like this,
    sub foo { my @a = qw(first second third); my ($a) = @a; };
    In this case, "the number of elements produced by the expression on the right hand side of the assignment" is the the scalar-context value of the array, or 3.
      In that case, $a will be "first". Now, if you'd remove the parens in the second assignment, the value of $a will be 3.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://948401]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found