http://qs321.pair.com?node_id=532407

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

I'm trying to figure out how to determine the package that an anonymous subroutine belongs to by using the caller function.

The difficulty I'm having is that when classes use something like Class::Accessor to create accessors for classes, the namespace of the methods is Class::Accessor (or whatever class created them) rather than the actual class it belongs to.

I've not found any clear documentation on this. From a bit of netsurfing I saw an article refer to the package that caller returned being the package that the subroutine was compiled in. Which sounds like an accurate description of what this is.

So my question is: is there a way for Perl to determine the actual package that called a subroutine, rather than the package where the call to the subroutine was compiled?

Some cample code to illustrate my question is below. The code prints "Creator::__ANON__" as the namespace, rather than "TracedCreatee::foo".

package Tracer; sub new { my $class = shift || __PACKAGE__; my $self = { }; bless $self, $class; } sub STORE { print STDERR "\n\x23 ", (caller(1))[3], "\n"; my $self = shift; my $key = shift; my $val = shift; $self->{$key} = $val; } sub FETCH { print STDERR "\n\x23 ", (caller(1))[3], "\n"; my $self = shift; my $key = shift; $self->{$key}; } 1; package Creator; use strict; use warnings; sub create { my $caller = (caller(0))[0]; # $caller =~ s/::(((?!::).)+)$//; my $field = shift; no strict 'refs'; *{$caller."::".$field} = sub { my $self = shift; if (@_) { return $self->STORE($field,@_); } else { return $self->FETCH($field); } }; } 1; package TracedCreatee; our @ISA = qw( Tracer ); Creator::create("foo"); 1; package main; use strict; use warnings; my $obj = TracedCreatee->new(); $obj->foo(1);

The purpose of this question is for improving Class::Tie::InsideOut, which is a proof-of-concept package for using tied hashes to implement inside-out objects. A downside in that is one cannot use Class::Accessor to create methods.