in reply to "my" variables going public?

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; }
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.

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:

package foo; use vars '$foo'; sub TIESCALAR { return bless {}, foo::ThereIsNoSuchVariable; } tie $foo, 'foo';

        - tye (but my friends call me "Tye")