in reply to How to dereference a FORMAT reference?

I am not entirely sure but I think in all cases you can just get a new reference via your typeglob-syntax.

Consider this:

use strict; use Scalar::Util qw(weaken isweak); my $a = 1; my $r1 = *main::a{SCALAR}; my $r2 = *main::a{SCALAR}; weaken $r1; print "r1 is weak\n" if isweak $r1; print "r2 is weak\n" if isweak $r2;

Here I get two copies of the typeglob. Both are references to $a but they are different one as proved by weakening one and observing that the other one has not been weakened.

So I guess you don't need the special snytax for array-refs, hash-refs etc, you just generically get a new copy of whatever is in the corresponding slot of the typeglob.

Replies are listed 'Best First'.
Re^2: How to dereference a FORMAT reference?
by Jeffrey Kegler (Hermit) on Jun 24, 2009 at 00:52 UTC

    That's a very good idea, and one I hadn't thought of.

    But it's just half the problem. The solution requires that I know the typeglob slot. All I have is a reference and I don't know a general way of going from the reference to the typeglob slot.

    In fact, I can't see from the documentation any situation in which a format reference can be used for any purpose whatsover. (Unless you count taking its address and using that as a very low quality pseudo-random number generator as a "use".)

      All I have is a reference and I don't know a general way of going from the reference to the typeglob slot.

      What? I thought that the typeglob slots were chosen to match what ref returns (except you'd want Scalar::Util::reftype because of the unfortunate decision to overload ref to returning class names and you have to deal with just a few special cases like the unfortunate and silly decision to have ref(\\$x) return "REF" instead of "SCALAR" and the more reasonable features of returning "LVALUE" or "VSTRING" instead of "SCALAR").

      I don't see how having to deal with 3 special cases prevents this from being a "general" solution.

      Updated with minor wording changes.

      - tye        

      I think it is even simpler:

      Consider:

      use strict; use Scalar::Util qw(weaken isweak); my $a = []; my $r1 = $a; my $r2 = $a; weaken $r1; print "a is weak\n" if isweak $a; print "r1 is weak\n" if isweak $r1; print "r2 is weak\n" if isweak $r2;

      So here we have a array_ref $a of which we make two further copies for which we prove again that they are different.

      So to answer your initial question:

      Given a reference you simple assign it to a scalar to get another reference to the same referent - no need for special syntax or type-glob fiddling...

        You're 100% right. It's so simple and obvious that I never suspected. Thanks.