Bob Showalter wrote:

> Paul Kraus wrote:
>> Maybe I am misunderstanding but that seems to be the exact same thing
>> my does.
> 
> No. my() creates a new variable. our() refers to a global variable.

not sure what you mean by that. 'our' does create new variable if that 
variable does NOT already exists in the current package's symble table. 
otherwise, it shadows it.

'my' creates a new variable but only in the local lex. scope. the largest 
lex. scope in Perl is file which means all variable tagged with 'my' can 
never be visialbe outside of its current package because 'my' does not put 
any variable in the current package's symble table.

the following shows just that:

[panda@dzhuo]$ perl
my $a_variable = 1234;
/a_variable/ and print "$_\n" for(keys %::);
^D
[panda@dzhuo]$

print nothing because $a_variable is not in the package's symble table

the following, however, does find the variable:

[panda@dzhuo]$ perl
our $a_variable = 1234;
/a_variable/ and print "$_\n" for(keys %::);
^D
a_variable
[panda@dzhuo]$

because 'our' creates (if it doesn't already exists) and puts it in the 
package's symble table.

> 
> Try this:
> 
>   use strict;
>   print "Global \$main::foo is at ", \$main::foo, "\n";
>   our $foo = 'One';

the above does create a new variable.

>   print "a: addr=", \$foo, ", val=$foo\n";
>   {
>       our $foo = 'Two';

the 'our' keyword here has no effect. $foo already exists so it simply 
shadows the outsider scope. it's the same as "$foo = 'Two'". Because 'our' 
doesn't create a new variable (because it already exists), nothing is 
destoried after the block exit so the value 'Two' is retain even after the 
block. this is another difference between 'our' and 'local' because 'local' 
never creates new variable but does make local copy of the variable so the 
old value can be retained after the block exit.

david

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

Reply via email to