in reply to Stashing a Package Variable by Reference

${ *{${ns} . '::foo'} } = $cram;

You could also write

${ $::{$ns.'::'}{foo} } = $cram;

in which case you wouldn't need the no strict 'refs'.

#!/usr/bin/perl -l use strict; use warnings; my $ns = 'Teddy::'; ${ $::{$ns}{foo} } = 'cheese'; print '$Teddy::foo: ', $Teddy::foo; # or the other way round $Teddy::foo = 'bar'; print '${ $::{$ns}{foo} }: ', ${ $::{$ns}{foo} }; __END__ $Teddy::foo: cheese ${ $::{$ns}{foo} }: bar

The main stash %:: holds an entry 'Teddy::', whose value is the stash of that package (or more precisely, a glob whose hash part is the stash).  The latter holds an entry 'foo', whose value is a glob, the scalar slot of which you can select with ${ ... }

print grep /Teddy/, keys %::; # Teddy:: print $::{'Teddy::'}; # *main::Teddy:: print keys %{$::{'Teddy::'}}; # foo print $::{'Teddy::'}{foo}; # *Teddy::foo


P.S.: you could also put a direct reference to a string 'cheese' in the scalar slot of the glob, in which case you would get a kind of read-only variable:

$::{'Teddy::'}{foo} = \'cheese'; print $Teddy::foo; # cheese $Teddy::foo = 'bar'; # "Modification of a read-only value attempted a +t ..."

Replies are listed 'Best First'.
Re^2: Stashing a Package Variable by Reference
by Xiong (Hermit) on May 30, 2010 at 17:19 UTC

    Your code is, I admit, more clever than mine. 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 and I might write something like:

    package Stasher; sub stash { my $ns = caller; $ns = $ns . '::'; ${ $Stasher::{$ns}{foo} } = 'cheese'; };

    Before I hammered out my current approach, I considered a less clever one:

    package Stasher; our %stash_of; sub stash { my $ns = caller; $stash_of{$ns}{foo} = 'cheese'; };

    This resembles a flyweight object and suffers from a similar weakness, in that Stasher keeps %stash_of entries alive unless and until an explict DESTROY is called. That's okay for flyweights but in my case, how does Stasher know when caller's entire package has gone out of scope?

    My thought was to stash $foo in caller's symbol table because I can rely on it being deallocated in Perl's own good time.

    - the lyf so short, the craft so long to lerne -
      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"}

        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 -
        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.