Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Validating a Code Reference

by chromatic (Archbishop)
on Aug 17, 2000 at 20:11 UTC ( #28317=note: print w/ replies, xml ) Need Help??


in reply to Validating a Code Reference

Use typeglobs. They're almost as good as caramel corn:

#!/usr/bin/perl -w use strict; sub here { } foreach my $poss ( qw( here not_here ) ) { no strict 'refs'; if (*{$poss}{CODE}) { print "$poss exists.\n"; } else { print "$poss does not.\n"; } }
What this does is look for a typeglob of a certain name. (That's the *{$poss} part.) Then it looks to see if the CODE slot is defined. I think you can take it from there.

Update: I just read the 'reference' part. If it's an anonymous reference, that's trouble. (I think you'd have to get access to Perl guts to find it. Yikes.) If it's not, you can check the symbol table for a match. There's probably a better way to do this with grep, but this is illustrative:

#!/usr/bin/perl -w use strict; sub findme { return 0; } sub another {} my $ref; if (rand(1) < 0.5) { $ref = \&findme; } else { $ref = \&another; } foreach (keys %main::) { no strict 'refs'; if (defined *{$_}{CODE}) { if (*{$_}{CODE} eq $ref) { print "\$ref points to $_!\n"; } } }
It's not beautiful, and you'll have to have some idea which package the sub might be in, but it's a little closer.


Comment on Re: Validating a Code Reference
Select or Download Code
RE: Re: Validating a Code Reference
by davorg (Chancellor) on Aug 17, 2000 at 20:15 UTC

    That was what I originally thought, but the problem is that I don't have the subroutine name, I only have a code ref. And as btrott points out, it might well be an anonymous code ref (which doesn't live in a typeglob).

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
RE (tilly) 2: Validating a Code Reference
by tilly (Archbishop) on Aug 17, 2000 at 20:26 UTC
    Very nice!

    OTOH I use anon subs a lot, and this approach will fail for those.

    Incidental note. After some testing it appears that if the sub exists when you define the sub reference, you get the first sub. Otherwise you get the first sub defined after that with that name. I tried various combinations of evals creating refs, defining subs, redefining subs, and that seemed to be what happened.

    Personally I would not worry about it. But I would like it if "strict 'subs'" disallowed making a reference to a sub that is not yet defined...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://28317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2014-10-20 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls