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