Yes it introduces a new namespace, but again lexical variables have nothing to do with the symbol table and package variables. Maybe this and a reading of Coping with Scoping will help.
use strict;
use warnings;
## Define a lexical variable $foo (which will be otherwise unrechable
+after
## the next our save by the closure)
my $foo = 'file lexical';
sub closure { print "closure says: $foo\n" }
print "before our: $foo\n";
## This masks the above lexical $foo for the rest of the outer file sc
+ope
our $foo = '$main::foo';
package One;
## And this masks the prior lexically visible $foo that pointed to $ma
+in::foo
our $foo = '$One::foo';
{
package Two;
## This $foo temporarily masks the prior $One::foo
our $foo = '$Two::foo';
print "inside package Two: $foo\n"
}
## Prior our $foo was scoped to the explicit block, so unadorned $foo
## once again means $One::foo . . .
print "back outside Two's block: $foo\n";
package main;
print "unadorned \$foo: $foo\n";
## But we can still explicitly refer to each of them by full package n
+ame
{
no strict 'refs';
print "$_: ${$_}\n" for qw( main::foo One::foo Two::foo );
}
## And now the sub closure is the only thing which has a handle on the
## original lexical $foo
closure();
exit 0;
__END__
before our: file lexical
inside package Two: $Two::foo
back outside Two's block: $One::foo
unadorned $foo: $One::foo
main::foo: $main::foo
One::foo: $One::foo
Two::foo: $Two::foo
closure says: file lexical
Update: Expanded example with more chattiness and a closure.
The cake is a lie.
The cake is a lie.
The cake is a lie.
|