in reply to Scope, package, and 'my' variables
If you run your code through perl -MO=Xref mytest.pl, you'll get the following output...
----- unneeded portion of output not shown ----- Package aaa &do_a s14 Package bbb &do_b s24 Package ccc &do_c s25 Subroutine (main) Package (lexical) $var i10, i21, i34, 27 Package aaa &do_a &23 Package bbb &do_b &24 Package ccc &do_c &25 Subroutine aaa::do_a Package (lexical) $var 13 Subroutine bbb::do_b Package (lexical) $var 37 Subroutine ccc::do_c Package (lexical) $var 48 mytest.pl syntax OK
The important part of all that is:
Package (lexical) $var i10, i21, i34, 27
That's saying that $var is being declared in line 10, 21, and 34. ...and used in 27. This confirms what others have previously mentioned in this thread; that packages don't create their own lexical scopes. Files create lexical scope, and curly brackets do. So it's common to think of a package as its own lexical scope, since it's also common (though not a requirement) for packages to live alone in a file. But you can't rely on that logic because multiple packages in the same file reside in the same scope.
Update: On a side note, I do think that B::Xref's output is a little misleading by saying, "Package (lexical) $var .....". It seems that it ought to say something more akin to "File (lexical) $var" or "Block (lexical) $var ..."
I'm kind of interested in hearing the reason for why it does what it does.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scope, package, and 'my' variables
by duff (Parson) on Jan 04, 2005 at 18:34 UTC |