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

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 -

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

Re^5: Stashing a Package Variable by Reference
by ikegami (Patriarch) on May 31, 2010 at 20:38 UTC

    ... 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}) };