in reply to Re^5: Stashing a Package Variable by Reference
in thread Stashing a Package Variable by Reference
Suppose we have a variable $x:
{ package Foo::Bar::Baz; our $x = 123; }
What is the simplest generic way to get a reference \$x from that package?
will not work.$ns = 'Foo::Bar::Baz::'; $ref = \${ $::{$ns}{x} };
You'd have to follow the chain, like this:
$ns = 'Foo::Bar::Baz'; my $stash = \%::; foreach(split '::', $ns) { $stash = $stash->{"$_\::"}; } $ref = \${ $stash->{x} };
Or you could use a module like Data::Diver.
All that, just to avoid the use of a symbolic reference:
{ no strict 'refs'; $ref = \${ "Foo::Bar::Baz::x" }; }
Really.
You're going across town through the sewers, just to avoid a few traffic lights.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Stashing a Package Variable by Reference
by almut (Canon) on Jun 01, 2010 at 09:42 UTC |