Torsten Förtsch schrieb am 27.04.2010 um 11:25:33 (+0200): > On Tuesday 27 April 2010 10:18:17 Michael Ludwig wrote: > > > A lexical variable in Perl is any variable declared with "my", > > regardless of the scope, which may be file-level. Unlike globals, > > lexical variables aren't directly accessible from outside the > > package. > > Not quite correct. Consider this: > > $ perl -Mstrict -le ' > my $x=10; > our $y=20; > { > package hugo; > > print "x=$x"; # references $x outside of package hugo > print "y=$y"; # references $main::y
Thanks for catching this. Indeed, lexical variables aren't described in terms of packages, but in terms of lexical scope. > $x is file-level lexical. It is visible all over the file. The > embedded package hugo has no influence. Lexical variables are not > bound to a package but to a lexical scope. > > Same with our-variables. C<our> declares the visibility of a variable > in the current lexical scope. Variables declared with "our" are a funny hybrid between global variables, which are attached to a package, and lexical variables, which are attached to a scope. -- Michael Ludwig