some puzzles

Rick J pisium at yahoo.com
Tue Mar 20 13:40:58 CST 2001


Hi, Michael,

Thanks for the explanation on scoping, and great site
http://perl.plover.com. I have read most of articles. 
I have a question about my scope as follow.

use strict;
my $count = 0;
callsub() while ($count++ < 3);
{
	my $num = 10;
	sub callsub {
		print ++$num, "\t";
	}
	print "$num\n";
}
It prints out 1 2 3 10.

Is my understanding right? The first time when
callsub() was called, the callsub was in the outer
block where my $num was defined. So local $num was
declared and defined, however, the process didn't
reach the statement 'my $num = 10', so it did not get
the defined value and printed as 1 2 3 respectively
instead of 11 12 13. However, after the while loop was
done, and this time, the program pass that statement,
and got the value and printed it as 10.

I am still wondering why in the sub, it did not print
11 12 13. My here, looks more like local, because in
that block, it stores 2 values for the my $num, one is
10, and the other is 1, 2, or 3. I am so clear and
sure.

Another question is about assignment. I know that in
assignment, if the right side is array, and left side
is list or array, it just assigns the values
accordingly. If the left side is scalar, it assigns
the total number of elements in that array to the
scalar variable. So the following cases indicate:
1. @arr = (10, 20, 30);
   $var = @arr; #$var is 3
2. $var = (10, 20, 30); #$var is 30
3. my ($v1, $v2, @arr);
   $v1 = (($v2, @arr) = (10, 20, 30, 40)); 
   #$v1 is 4, $v2 is 10 and @arr has 20 30 40
4. @arr = (10, 20, 30);
   $var = (@arr); #$var is 3  
5. $var = 10, 20, 30; #$var is 10
I don't understand 2 and 4 and 5.

2.and 5. Why the results are different because of the
parentheses? Isn't in case 2, the parentheses have to
be taken off before the assignment? If that's the
case, 2 and 5 should be same.

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).

Please advise, thanks.

Rick





__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/
=================================================
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