in reply to Using package variables dynamically

I'm a bit more care-free with the forbidden knowledge than nobull, so I'll give you an example. Note, this doesn't pass strict. That should be a big red flag, but here it is:

{ package a; our $foo = 1; } { package b; my $otherpkg = "a"; my $varname = "$otherpkg\::foo"; print ${ $varname }, "\n"; # can be simplified as: print $$varname, "\n"; # or the temp variable can be avoided: print ${ "$otherpkg\::foo" }, "\n"; }

Another solution would be to create an accessor sub in the package, and eliminate the package variable altogether.

use strict; { package a; my $foo = 1; sub foo { $foo } } { package b; my $otherpkg = "a"; print $otherpkg->foo, "\n"; }