There I have three variables all called $foo. But none of them have any effect on the others. $foo::foo only refers to the first one as lexical variables are not "package globals" and so can't be accessed globally using a package name.package foo; use vars '$foo'; sub foo1 { my $prev= $foo; $foo= shift if @_; return $prev; } { my $foo; sub foo2 { my $prev= $foo; $foo= shift if @_; return $prev; } } my $foo; sub foo3 { my $prev= $foo; $foo= shift if @_; return $prev; }
Perl offers no way to prevent random code from creating random variables in any package (even in packages that don't exist yet). However, if you want to trap attempts to use a specific variable, then you can use tie:
- tye (but my friends call me "Tye")package foo; use vars '$foo'; sub TIESCALAR { return bless {}, foo::ThereIsNoSuchVariable; } tie $foo, 'foo';
In reply to (tye)Re: "my" variables going public?
by tye
in thread "my" variables going public?
by DeaconBlues
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |