Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to identify the package of a subroutine given only a reference to it

by drclaw (Acolyte)
on Jan 30, 2022 at 12:16 UTC ( #11140963=perlquestion: print w/replies, xml ) Need Help??

drclaw has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, Is it possible to find out the package a subroutine belongs to it you only have a reference to the subroutine? Any modules/documentation existing to do this? Thank you
  • Comment on How to identify the package of a subroutine given only a reference to it

Replies are listed 'Best First'.
Re: How to identify the package of a subroutine given only a reference to it (Sub::Identify)
by LanX (Sage) on Jan 30, 2022 at 12:36 UTC
    > find out the package a subroutine belongs to

    If you mean the package/stash were it was originally defined in: Yes.

    It's kept as special field inside the internal data.

    There is a way to inspect this w/o dependencies using B

    Otherwise there are various modules on CPAN doing that°.

    edit

    °) see stash_name in

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Thank you for the quick reply! I'll check out your suggestions.
Re: How to identify the package of a subroutine given only a reference to it
by Discipulus (Abbot) on Jan 31, 2022 at 08:16 UTC
    Hello drclaw and welcome to the monastery and to the wonderful world of Perl!

    As side note to the main ansewer given by LanX++, and in the case you are the author of the subroutine, they obviously already know the package they belong to:

    package XX; sub inxx{return __PACKAGE__} package main; my $ref = \&XX::inxx; print $ref->(); __END__ XX

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hi,

      Yes, it is a subroutine I generate through an eval, in a custom package

      I was toying with the idea that I could track the package with only the ref to the generated subroutine. However, I need to do package housekeeping when the sub is no longer required.

      So now have an object which contains the package name and the generated subroutine. The package name obtained using the __PACKAGE__ like you mentioned.

      Thanks for the welcome!

        But beware!

        #!/usr/bin/perl use warnings; use strict; { package Local; sub My::frob { print __PACKAGE__ } } My::frob(); # Local

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > So now have an object which contains the package name and the generated subroutine

        If you want to go that way you can also choose to bless the code ref directly.

        Objects don't need to be hashes in Perl.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-02-08 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?