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

Why on earth are you reinventing symbolic references, only to be able to use strict everywhere??

There is a reason why symbolic references exist in Perl, and that is to avoid having to juggle with complex manipulations like yours. Symbolic references is the simple and thus the proper approach for this kind of application.

All you have to do is to locally disable use strict (or at the very least use strict 'refs') by making a small enclosing block, and put

no strict 'refs';
in there.

Even though using direct manipulation of the stashes bypasses strict, that does not imply that it's the better approach.

You just have to know when it is a proper time to break the rules.

Replies are listed 'Best First'.
Re^5: Stashing a Package Variable by Reference
by almut (Canon) on May 31, 2010 at 22:16 UTC
    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.

      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.