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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.