some puzzles

Roxanne Reid-Bennett rox at tara-lu.com
Tue Mar 20 13:22:15 CST 2001


Rick J wrote:
[...]
> It prints out 1 2 3 10.
[...]
> I am still wondering why in the sub, it did not print
> 11 12 13. 

I haven't used sub-scoped subroutines much, so I don't have the internals for
perl down on that. It seems weird to me that you can even access "callsub"
outside the while loop.  Based on my understanding (from 30 years ago) of
scoping.

However, running this through the Activestate debugger, it does in fact run
through the callsub 3 times before executing the $num = 10.

I haven't played with the perl debugger on either platform (Linux or Winnt)
enough to know how to check, but I'll bet dollars to donuts that the $num inside
the callsub is the same as the one that is later assigned 10. [evaluating to a
memory address would verify - but I'm not going to take the time to figure out
how to do that at the moment]

> 2. $var = (10, 20, 30); #$var is 30
> 4. @arr = (10, 20, 30);
>    $var = (@arr); #$var is 3
> 5. $var = 10, 20, 30; #$var is 10

> 2.and 5. Why the results are different because of the
> parentheses? 

The parens turn the right side into an anonymous array.  The 10, 20, 30 is a
list of values, but not explicitly "arrayed".  As a result I would have assumed
you would get 3, not 30 on "2.".  5. made sense to me, you got the first value
off the list. However this brought up some other cases to me which didn't behave
as I expected, so I added a few cases to your example:

6. $var, $v2, $v3 = 10, 20, 30;  #$var is 10, $v2 is 10, $v3 is 10
7. $vx1, $vx2, $vx3 = (10, 20, 30);  #$vx1 is "" $vx2 is "" $vx3 is 30
8. ($v1, $v2, $v3) = (10, 20, 30);  #$v1 is 10 $v2 is 20 $v3 is 30

8. makes sense to me, but 6 and 7 were not what I expected to see.

> 4. Array in the parentheses, I think, should be
> flattened out before the assignment? Therefore, $var =
> (@arr) should be equal to $var = (10, 20, 30). But it
> seems different. Therefore, the result isn't same
> between if (@arr) and if (10, 20, 30).

but is the same as $var = @arr;

The (@arr) expands the @arr into the (), then takes the length.  The issue seems
to resolve around anonymous arrays versus named (?) arrays, even if partially
named or not.

Michael is more than welcome to respond, this just arroused my curiosity.

Rox
-- 
Roxanne Reid-Bennett                       rox at tara-lu.com
President, Tara-Lu Corporation     http://www.tara-lu.com/

Quality brings its own reward.
=================================================
Mailing list info:  If at any time you wish to (un|re)subscribe to
the list send the request to majordomo at hfb.pm.org.  All requests
should be in the body, and look like such
                  subscribe anchorage-pm-list
                  unsubscribe anchorage-pm-list



More information about the Anchorage-pm mailing list