On Wed, Feb 13, 2002 at 11:05:51AM -0500, Hanson, Robert wrote:
[snip] 
> In chapter 2 it mentions a rule in Perl: "any time that you need a variable
> in Perl, you can use an assignment instead.  First, Perl does the
> assignment.  Then it uses the variable in whatever way you requested".
> 
> Ok, that makes sense.  It allows you to do things like this:
> 
> chomp( $foo = <STDIN> );
> $x = $y = 10;
[snip]

> The value of a list assignment is a scalar context is the number of
> elements in the **source list** -- in this case, that's 0".
> 
> So this adds to the first rule (sort of).  Basically in scalar context the
> function takes the variable as it's argument, and in list context it takes
> the count of items in the source list.
> 
> ....Ok, that works for me so far, but then why would these work?
> 
> $x = () = 10; # $x is 1
> $x = () = (10, 20); # $x is 2

These match your list assignment rule.  Assigning to empty parens is
assigning to a list (that just happens to be empty).  The assignment to $x
imposes the scalar context.  So, the list assignment "() = (10, 20)"
evaluates to 2 in scalar context.

 
> >From this it sounds like the first rule does not exactly work as stated.  It
> seems that the real rule is that the "=" operator returns a value just like
> any other Perl operator.  It seems to me that given "x = y" that the "="
> operator returns the value from the right side of the operator (in the given
> context of x).

No, it returns the value from the left side.  Assignment is right
associative, which may be what's tripping you up; a string of assignments
are evaluated right to left, instead of left to right as in most algebraic
expressions.  So, in your examples, "() = (10, 20)" is evaluated first, then
the assignment to $x imposes the context.


> So "x = y = z" means that "y = z" returns the value of z, and then "x = "
> stores that value. ...Or at least that is how I am conceptualizing it.

No, in "x = y = z" the expression "y = z" evaluates to y, then x stores that
value.  Consider your chomp example:

    chomp($foo = <STDIN>);

You can't chomp <STDIN>, so the expression must be evaluating to $foo.  And
not just the contents of $foo, $foo itself; chomp requires a scalar it can
modify.


> ....And now the real question.  Why won't this work?
> 
> ($x) = () = 10;

It does work.  $x is getting the first value in the list ().  Consider:

    ($x) = ($y) = 10;

$x now equals 10.  The rightmost list assignment is evaluated in list
context, and the leftmost list is assigned to.  This is list assignment in
list context.

 
> Is there a need for an extra rule to cover this?  Or is there a single rule
> that covers the syntax from all of the above examples?

The assignment and list assignment rules that you've already read about
cover these cases.


Does that help to dispel your confusion?


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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

Reply via email to