develooper Front page | perl.perl5.porters | Postings from March 2012

[perl #68312] perlop / list assignment documentation

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
March 28, 2012 18:40
Subject:
[perl #68312] perlop / list assignment documentation
Message ID:
rt-3.6.HEAD-4610-1332985243-993.68312-15-0@perl.org
There was a lot of back-and-forth and revision of patches in this RT
before discussion petered out about 2-1/2 years ago.

Could people evaluate the last patch submitted, below?

Thank you very much.
Jim Keenan

On Sun Aug 30 08:46:10 2009, iang wrote:
> 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<,>
> 




---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=68312

Thread Previous | Thread Next


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