Friday, 19 August 2011

Difference between my $var and $main::var

use strict; 
use warnings; 
use diagnostics; package main; my $foo = "hello"; $main::foo = "world"; print "$foo $main::foo\n";
my $foo under the package main would manipulate the symbol table such that $foo were equal to $main::foo.
If $foo and $main::foo were the same variable, then the program would print world world,which it does not.
If you fully qualify your variables in package main, then you don't need to worry with declaring them with my, nor with our, nor with use vars, as can also be seen in the program above (see that $main::foo is not declared; yet, the compiler doesn't complain about it, even though the program is running under use strict).

No comments:

Post a Comment

Tweets by @sriramperumalla