Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Discipulus (Canon)
on Jan 31, 2022 at 08:16 UTC ( [id://11140985]=note: print w/replies, xml ) Need Help??


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

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.
  • Comment on Re: How to identify the package of a subroutine given only a reference to it
  • Download Code

Replies are listed 'Best First'.
Re^2: How to identify the package of a subroutine given only a reference to it
by drclaw (Acolyte) on Jan 31, 2022 at 22:56 UTC

    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

        I'm partial to array backed objects where applicable. But I've never actually tried a blessed code ref... until 5 minutes ago after reading your reply! It might be a good solution to my application.

        Out of interest, do you have insight on how perl knows to call the underlying code ref and not a method that may or may not be in the package? An extra lookup/check must be done when called?

        #eg $code_ref_obj->();#calls code ref directly $code_ref_obj->a_method(); #Call a method on the object
        Thanks again for you comments

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found