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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.