Nkuipers <[EMAIL PROTECTED]> wrote:
> Hello everyone,
> 
> The following is from page 75 in the Camel:
> 
> "List assignment in scalar context returns the number of elements 
> produced by the expression on the <i>right</i> side of the 
> assignment:
> 
> $x = ( ($a, $b) = (7,7,7) ); #set $x to 3, not 2
> 
> ...."
> 
> It goes on to explain how this is useful but from previous reading 
> in the same section I don't understand how or why this works.  When 
> I first saw this, I worked out the assignment where $a and $b each 
> get a 7 and the third 7 is discarded, then $x gets the last value 
> in the list of $a and $b because of the comma operator for list 
> literals in a scalar context...

The result of scalar assignment is the lvalue on the
left hand side:

  $x = $y = 2;   # $x = 2
  ($x = 1) = 2;  # $x = 2
  print $x = 2;  # prints 2

In list context, the result of list assignment is 
the list of values on left hand side:

  @x = () = 0..10;        # @x = ()
  @x = ($y) = 0..10;      # @x = (0)
  @x = ($y, $z) = 0..10;  # @x = (0,1)

These are consistent, and it's easy to infer the
simple, but incomplete, rule: 

  "assigment returns its left hand side"

Which can lead a person to believe that compound
assignments can always be broken down into two or
more steps:

  $x = $y = 1;  # or: $y = 1; $x = $y

The problem is that list assignment in scalar
context breaks the pattern.

  $x = ($y) = 0..10;  # $x = 11

There's no way to explain why "$x = ($y)" ought to
yield 11 here.  (In fact, it shouldn't.)

You just have to accept that list assigment in scalar
context *doesn't* return its LHS.  It returns the
number of elements on the RHS.


HTH
-- 
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to