in reply to Re: How can I find the calling object?
in thread How can I find the calling object?

Perfect. Here's some proof of concept code:
#!/usr/bin/perl package Caller; sub new { bless {}, 'Caller' } sub call { my( $self, $one ) = @_; $one->output; } sub output { "I am the Caller\n" } 1; package Called; use PadWalker 'peek_my'; sub new { bless {}, 'Called' } sub output { my $caller = peek_my( 1 )->{ '$self' }; #$h->{ '$self' }; print STDOUT $$caller->output(); } 1; package main; my $one = new Caller; my $two = new Called; $one->call( $two );
This prints:
I am the Caller.
--
Love justice; desire mercy.

Replies are listed 'Best First'.
Re^3: How can I find the calling object?
by Aristotle (Chancellor) on Nov 18, 2002 at 17:59 UTC
    Perfect, so long as it is indeed called $self.. :)

    Makeshifts last the longest.

      Hehe. Fair 'enough. But that can be ascertained without even looking at the original code, so I don't worry about it. ;) You're right, though, in that it can't be 100% generalized. I'd say this is still a Right Way To Do It.
      --
      Love justice; desire mercy.
        Only if that limitation shows up in the BUGS or CAVEATS section of your POD.

        Makeshifts last the longest.

        You might look at $_[0] instead of $self, but that doesn't work if it's been shifted. Maybe it's called $this.