--- Timothy Kimball <[EMAIL PROTECTED]> wrote:
> 
> Randal L. Schwartz wrote:
> : ...
> : Second might mean something like:
> : 
> : $foo_length = SOME_LIST # although this can't happen
> :               ========= list
> :             =           assigned to
> : ===========             scalar
> : 
> : See the difference?  And the latter can't happen.
> 
> Sure it can. Well, the list itself doesn't get assigned to the scalar,
> but an assignment does get made: The last element of SOME_LIST to
> $foo_length. So
> 
> my $number_of_pets = ('dog','cat','iguana');
> 
> sets $number_of_pets to 'iguana'.
> 
> -- tdk

Actually, you're dealing with a well-documented, but poorly understood feature.  Try 
this:

my $number_of_pets = ('dog','cat','iguana');
print $number_of_pets;

my @pets = ('dog','cat','iguana');
$number_of_pets = @pets;
print $number_of_pets;

$number_of_pets = ('dog','cat',@pets);
print $number_of_pets;

@number_of_pets = ('dog','cat',@pets);
print scalar @number_of_pets;

The first print results in 'iguana' and the second and third print statements print 3. 
 The fourth
print display a 5.

What's going on here is subtle.  Putting parens around those scalars creates a "list 
literal". 
When a list literal is accessed in scalar context, it evaluates each item in scalar 
context and
returns the value of the final element.  Since, in the third example, the array is the 
third
element, it's evaluated in scalar context, as expected.

So why do the third and fourth examples have different outputs?  Because lists are not 
arrays and
do not behave as such.

Cheers,
Curtis Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to