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

But if I understand correctly, you're setting up a hash in the stasher's symbol table. Since my case isn't really a script, that's not main ...

Not really sure what you mean.  Of course, the approach isn't limited to main — the latter is just the top-level stash that every "package path" is rooted at.

Essentially, all I wanted to point out is that, in general

${ $::{'PackageA::'}{'PackageB::'}{'PackageC::'}{foo} }

functionally achieves the same as your symbolic reference

${ *{"PackageA::PackageB::PackageC::foo"} }

(which could be simplified to  ${"PackageA::PackageB::PackageC::foo"},  btw — or ${"$ns\::foo"} in your specific case)

But, as the former approach doesn't involve symbolic references, it's compatible with use strict, i.e. it doesn't need no strict 'refs'.


P.S.: in several places of the current version of your original node, you're missing the closing curly in the expression ${ *{"${ns}\::foo"}

Replies are listed 'Best First'.
Re^4: Stashing a Package Variable by Reference
by Xiong (Hermit) on May 31, 2010 at 20:27 UTC

    Yes, almut, I completely missed what you were doing!

    Study-up on the web is amazingly difficult on this topic. Source after source discusses "symbol tables" (plural) and forks every other source, contrasting package variables vs lexicals; our vs local vs doing nothing at all; and of course speaking briefly about symbolic references before telling how to use strict so that they can't be used at all. Some sources go into bloody detail about the structure of a typeglob; I even found an interesting explanation of the internal C representation of variables.

    There is really (for some value of "really") only one symbol table! Careful reading of Camel tells:

    As it happens, the main symbol table contains all other top-level symbol tables, including itself...
    <cite>Programming Perl 2nd ed. 5.1.1</cite>

    I've experimented a bit with this now. Your code works and it's a vast improvement over what I had. This works too, under strict and warnings:

    my $ns = 'Teddy::'; my $var = 'foo'; ${ $::{$ns}{$var} } = 'cheese';

    ... which isn't a symbolic reference??

    I need to rewrite this entire node. Meanwhile, I bow to almut's superior code-fu.

    - the lyf so short, the craft so long to lerne -
      There is really (for some value of "really") only one symbol table!

      Not really — or only if you add "per namespace/package". Somewhat simplified, you can think of it as a nested hash-of-hashes structure. For example, the package(s) Foo::Bar would be represented like

      %:: = ( 'Foo::' => { 'Bar::' => { varname => typeglob, ... }, ... }, ... );

      The main difference is that the values associated with the packagename keys like 'Foo::' aren't simple hashrefs, but rather typeglobs, whose hash slot points to the actual symbol table hash. But Perl has some DWIM magic in place for globs, i.e. if you treat it as a hashref, you'll get its hashref entry, which is why you can write $::{'Foo::'}{'Bar::'}{varname}, as if it were a HoHoH.

      I need to rewrite this entire node.

      Not necessarily  (all I would change is ${*{"${ns}\::foo"}} into the simpler ${"$ns\::foo"} or ${"${ns}::foo"} ).  Symbolic references are there for a reason.  While I would probably use the "non-symbolic" approach for simple static structures like your Teddy:: example, I would rather use symbolic references for more complex/arbitrary structures, (like you might get from caller), because creating a HoHo... access from something variable-depth like Foo::Bar::Baz::... is much too much work compared to locally saying no strict 'refs', and then simply using the string "Foo::Bar::Baz::..." as is...

      ... which isn't a symbolic reference??

      Yes, but it's not any safer than symbolic reference. Given that symbolic references are easier to use, you might as well use those instead of messing with %::.

      Compare

      my $r = do { no strict 'refs'; \${"${pkg}::$var"} };
      with
      my $r = do { $pkg_ref = $r->{$_."::"} for split(/::/, $pkg); \($pkg_ref->{$var}) };
Re^4: Stashing a Package Variable by Reference
by bart (Canon) on May 31, 2010 at 20:39 UTC
    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.

      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.