Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: question about || operator and list context

by converter (Priest)
on Jun 22, 2003 at 03:32 UTC ( #267908=note: print w/ replies, xml ) Need Help??


in reply to question about || operator and list context

Just because I find the subject of lists and the comma operator interesting (and I'm trying to avoid real work for a few minutes):

Compare the outputs of the following assignment expressions:

sub context { print defined wantarray ? wantarray ? "list" : "scalar" : "void" , "\n"; $_[0]; } ($a, $b) = (context('a'), context('b')); print "a=[$a],b=[$b]\n\n"; ($a, $b) = (context('a'), context('b')) || die "foo"; print "a=[$a],b=[$b]\n"; list list a=[a],b=[b] void scalar a=[b],b=[]

It appears to me that in the second case the comma is acting as a scalar comma expression and not a list. Granted, || is a binary, not a unary operator, but I find the following description of context supplied in comma expressions interesting:

from the perl 5.8 perlfunc scalar() section: Because "scalar" is unary operator, if you accidentally use for EXPR a parenthesized list, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context. This is seldom what you want.

Example:

$c = scalar(context('a'), context('b')); print "$c\n"; void scalar b
conv


Comment on Re: question about || operator and list context
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-07-31 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls