Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to dereference a FORMAT reference?

by Jeffrey Kegler (Hermit)
on Jun 23, 2009 at 18:01 UTC ( #774128=perlquestion: print w/ replies, xml ) Need Help??
Jeffrey Kegler has asked for the wisdom of the Perl Monks concerning the following question:

For most practical purposes, this question is totally useless. But I have a CPAN module Test-Weaken, which detects leaks in arbitrary Perl data structures. It needs to traffic in references to as many Perl data objects as possible. For all the Perl data types except one, I can duplicate a reference, as follows:

\@{$array_ref} # ARRAY \%{$hash_ref} # HASH \${$scalar_ref} # SCALAR, REF, LVALUE, VSTRING, Regexp \*{$glob_ref) # GLOB, IO, FILEHANDLE \&{$code_ref} # CODE

My question: how do I dup a FORMAT reference?

You can get a format reference with this syntax: $formatref = *foo{FORMAT}. Formats themselves are deprecated and why you'd want a reference to one I don't know, but my purpose is to allow Test::Weaken to be able to deal with whatever data is thrown at it. That data may come from perverse and/or legacy code.

UPDATED: corrected typos.

Comment on How to dereference a FORMAT reference?
Select or Download Code
Replies are listed 'Best First'.
Re: How to dereference a FORMAT reference?
by morgon (Deacon) on Jun 23, 2009 at 23:17 UTC
    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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://774128]
Approved by Bloodnok
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2015-07-07 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls