[LA.pm] list/scalar context

Peter Scott Peter at PSDT.com
Thu Jul 28 13:29:19 PDT 2005


At 12:14 PM 7/28/2005, cch2cch at netscape.net wrote:
>the following statement is similar to the one on P.49 of "Programming 
>Perl" 2nd Edition.

They're on the 3rd edition now.  Catch up :-)

>$cnt does prodcues a value 3. But it seems a bit counter intuitive to 
>me ( I think it should be a 2).

p. 75 of the 3rd edition says:

         List assignment in scalar context returns the number of elements
         produced by the expression on the *right* side of the assignment.

>$cnt=(($x,$y)=(1,2,3)); # p.49 "Programming Perl 2nd edition
>print "cnt=$cnt x=$x y=$y\n";
>
>Particularly when in this statement only two elements survive the assignment.
>
>@cnt=(($x,$y)=(1,2,3));
>print "array cnt=@cnt\n";

It depends on whose intuition is talking :-)  Usually, I care about 
this in some expression like

         if (my($x, $y) = /(...)(...)/)

and I certainly do want thaty to evaluate to the number of elements on 
the RHS, because I already know how many there are on the LHS.  But I 
want to do different things depending on whether or not any elements 
got assigned.

So I recommend you adjust your expectations :-)

-- 
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
http://www.perlmedic.com/



More information about the Losangeles-pm mailing list