Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: scalar in list context

by Crackers2 (Parson)
on Jan 26, 2010 at 21:38 UTC ( [id://819785]=note: print w/replies, xml ) Need Help??


in reply to scalar in list context

Maybe you mean the context of the assignment?

@array = ( "one", "two", "three" ); ($len) = @array; $len1 = @array; print "1 - ", $len, "\n"; print "2 - ", $len1, "\n";

Where the first will print "one" and the second will print "3", since (IIRC) assignment forces the context of the LHS onto the RHS.

Update: See ikegami's post below for the correct explanation of what I was trying to say.

Replies are listed 'Best First'.
Re^2: scalar in list context
by ikegami (Patriarch) on Jan 26, 2010 at 22:41 UTC

    Maybe you mean the context of the assignment?

    All three of your assignments are evaluated in void context, so I think you mean "the context imposed by the assignment" onto its operands.

    assignment forces the context of the LHS onto the RHS.

    So what determines the context of the LHS?

    The context in which both operands of an assignment are evaluated is determined by the type of assignment (scalar assignment or list assignment). Both types of assignments are represented by "=" in the code, so the type of assignment is determined by the literal code on the LHS.

    Expressions in parens, arrays, hashes, local() and my() cause the creation of a list assignment. Everything else causes the creation of a scalar assignment.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found