Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: How can I find the calling object?

by strider corinth (Friar)
on Nov 18, 2002 at 17:57 UTC ( [id://213829]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://213829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found