SPUG: Confusing behaivior with exported variables
Joshua ben Jore
twists at gmail.com
Tue Mar 24 12:57:40 PDT 2009
On Tue, Mar 24, 2009 at 12:25 PM, Mark Mertel <mark.mertel at yahoo.com> wrote:
> Using 'our' is only global within the package and its descendents. For the
> external scripts it would not be visible, hence, not global.
No, you're wrong.
my $Foo = 'bar';
{
our $Foo = 42;
}
print "$Foo\n"; # prints bar
print "$main::Foo\n"; # prints 42
our() declares a global. All globals are visible to all other code
though often other code must go to the effort of giving a fully
qualified name. Other code can avoid going to that trouble if it was
exported.
our() also has some additional magic in the parser but that doesn't
change that it's just a global.
my() is never a global. Other code that has an idea about where a
lexical lives can never access it. Rules-breaking code which inspects
perl internals can do it anyway but as stated - that is normally
against the rules.
If you put everything into a single file, or use do() or eval(), that
makes this more complicated but the above is still true.
Josh
More information about the spug-list
mailing list