develooper Front page | perl.perl5.porters | Postings from August 2009

Re: [perl #68312] perlbug AutoReply: perlop / list assignment documentation

From:
Ian Goodacre
Date:
August 30, 2009 08:45
Subject:
Re: [perl #68312] perlbug AutoReply: perlop / list assignment documentation
Message ID:
4A9A9E89.9010208@xtra.co.nz
Below is a revision of the patch to perlop.pod which incorporates
some detail of the handling of undef on LHS of a list assignment,
 From http://www.perlmonks.org/?node_id=791113.

diff --git a/pod/perlop.pod b/pod/perlop.pod
index 1df9fcb..7120a8c 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -729,9 +729,26 @@ X<assignment> X<operator, assignment> X<=> X<**=> 
X<+=> X<*
 X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=>
 X<%=> X<^=> X<x=>

-"=" is the ordinary assignment operator.
+"=" is the ordinary assignment operator.  There are two forms of this
+operator: list assignment and scalar assignment.  If the left operand
+is an array, a hash, an array or hash slice or enclosed in parentheses,
+then the assignment is a list assignment.  Otherwise it is a scalar 
assignment.

-Assignment operators work as in C.  That is,
+There are other assignment operators, as in C. The following are 
recognized:
+
+    **=    +=    *=    &=    <<=    &&=
+           -=    /=    |=    >>=    ||=
+           .=    %=    ^=           //=
+                 x=
+
+These other assignment operators are all scalar assignment operators.
+Although they are grouped by family, they all have the precedence
+of assignment.
+
+The scalar assignment operators provide scalar context to both their 
operands.
+The left operand must be an lvalue (meaning you can assign to it).
+
+The other assignment operators work as in C. That is,

     $a += 2;

@@ -740,18 +757,9 @@ is equivalent to
     $a = $a + 2;

 although without duplicating any side effects that dereferencing the lvalue
-might trigger, such as from tie().  Other assignment operators work 
similarly.
-The following are recognized:
-
-    **=    +=    *=    &=    <<=    &&=
-           -=    /=    |=    >>=    ||=
-           .=    %=    ^=           //=
-                 x=
-
-Although these are grouped by family, they all have the precedence
-of assignment.
+might trigger, such as from tie().

-Unlike in C, the scalar assignment operator produces a valid lvalue.
+Unlike in C, the scalar assignment operators produce a valid lvalue.
 Modifying an assignment is equivalent to doing the assignment and
 then modifying the variable that was assigned to.  This is useful
 for modifying a copy of something, like this:
@@ -767,10 +775,29 @@ is equivalent to
     $a += 2;
     $a *= 3;

-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.
+The list assignment operator provides list context to both of its operands.
+
+If the left operand of the list assignment operator is enclosed in 
parentheses,
+each element of the enclosed list must be an lvalue or C<undef>.
+
+In scalar context, the list assignment operator returns the number of
+elements produced by the expression on the right hand side of the
+assignment.
+
+In list context, the list assignment operator returns the list of
+lvalues assigned to. Where the value in the LHS list is C<undef>
+the corresponding value from the RHS is returned or, if there is
+any value in common between the LHS and RHS, a copy of the corresponding
+value from the RHS is returned. The difference is significant if
+the returned values are modified (e.g. in a for loop or map).
+
+    map {$_++} (($x, undef) = ($y, $z)) # increments $x and $z
+    map {$_++} (($x, undef) = ($y, $z, $x)) # increments $x and a copy 
of $z
+
+
+See L<List value constructors> in L<perldata> for examples of list 
assignment.
+
+

 =head2 Comma Operator
 X<comma> X<operator, comma> X<,>




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About