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.


In reply to Using caller to determine the namespace of an anonymous subroutine by rrwo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.