Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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.

Replies are listed 'Best First'.
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).


    European Perl Conference - Sept 22/24 2000, ICA, London
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://28317]
[aitap]: usemodperl: maybe it's you who has changed
[marto]: demonstrably yes, since you claim to want a safe space, your definition for which seems to be a place where you can name call, make things up or otherwise post without being challenged
[usemodperl]: no that's not it at all, meant safe for perl people, to not be hounded by weirdos obsessed with things like... whitespace :-)
[choroba]: I'm definitely not obsessed with whitespace, I rate myself among Perl people, and I haven't noticed any hounding
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws
marto wishes tye was around, he's so much better at this sort of thing
usemodperl likes tye!

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-24 15:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.