in reply to Packages, scope, and lexical variables
The output seems to indicate that a lexical variable (i.e. $var) is not associated with package level scope.
Correct. By definition, lexicals are block scoped and in no way related to packages. The file is considered a block for this purpose.
If you want $Test::var to work, change my to our, making $var a package variable instead of a lexical.
Furthermore, there doesn't appear to be any way to prevent an interloper from creating subroutines within the namespace of neighboring (or distant) packages.
That's true, but I haven't known this to be a problem. Any of the following three lines of code would create a sub named new_sub in the package Test, no matter which file contained the code.
{ package Test; sub new_sub { ... } } -or- sub Test::new_sub { ... } -or- *Test::new_sub = sub { ... };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Packages, scope, and lexical variables.
by ysth (Canon) on Mar 08, 2005 at 08:54 UTC | |
by ikegami (Patriarch) on Mar 08, 2005 at 15:03 UTC |