Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Easy Reference for Contexts

by ikegami (Patriarch)
on Apr 14, 2011 at 15:54 UTC ( [id://899468]=note: print w/replies, xml ) Need Help??


in reply to Easy Reference for Contexts

Honestly, the answers are usually obvious.

while( ) i.e. inside parenthesis is evaluated in scalar context?

It needs a single true/false value. What context do you think it imposes?

foreach( ) i.e. inside parenthesis is evaluated in list context?

It iterates over a list. What context do you think it imposes?

The are two areas of confusions.

  • Where prototypes are involved.

    sub plain { print $_[0]; } sub proto($) { print $_[0]; } my @a = 4; plain(@a); # 4 proto(@a); # 1

    Most builtins have prototypes, so passing an argument list from an array rarely works with them.

  • There are two different assignment operators. One imposes a scalar context on its LHS and RHS operand, while the other imposes list context on its LHS and RHS operands. It should be still be intuitive, but you might want to check out Mini-Tutorial: Scalar vs List Assignment Operator.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-23 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found