in reply to Re^5: Stashing a Package Variable by Reference
in thread Stashing a Package Variable by Reference

If life was only that simple. Yours is only the simplest case.

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?

$ns = 'Foo::Bar::Baz::'; $ref = \${ $::{$ns}{x} };
will not work.

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
    You're going across town through the sewers, just to avoid a few traffic lights.

    No.  But you are reading things into my posts which I've never said.  In particular, I'm not sure where you got that notion from that I want to avoid symbolic references at any price...

    What you elaborate on here is more or less the same I already said yesterday, in the second half of that reply.

    Use whatever is appropriate in a certain context...  TIMTOWTDI.