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

having to juggle with complex manipulations like yours

What complex manipulations?  In essence, what I originally suggested as an alternative was

${ $::{$ns}{foo} } = ...

in place of

{ no strict 'refs'; ${ "${ns}::foo" } = ... }

I fail to see how that is any more complex.  Neither did I say anything about it being "better" in general.

Replies are listed 'Best First'.
Re^6: Stashing a Package Variable by Reference
by bart (Canon) on Jun 01, 2010 at 09:25 UTC
    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.

      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.