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

Hi,
I have this line of code witch suppose to copy a file from machine_1 ro machine_2:
$machine_1->copy(<file_path> $machine_2->name:<dir>)
(It doesn't matter how "copy" or "name" methods are implemented.)
Is there a way for me to know inside the implementation of the method name that its object is an argument of the method copy?
Edited by Chady: code tags and some line breaks.

Replies are listed 'Best First'.
Re: finding the method name 2 levels above
by Corion (Patriarch) on Aug 16, 2006 at 08:41 UTC

    Have you tried caller ? It allows you to inspect the whole call stack. I'm not sure what you're trying to do, and usually, it's a bad and dangerous idea to try to solve a problem by inspecting who called you and how. So maybe this is a XY Problem and you should tell us what problem you are actually trying to solve.

      See also perldebguts which tells you about the magic in my @info = do { package DB; caller $some_level } that allows you to access the parameters your parents were called with in the @DB::args array.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: finding the method name 2 levels above
by bangers (Pilgrim) on Aug 16, 2006 at 09:50 UTC
    Not 100% sure I understand what you are trying to do, but 'ref' will reurn the name of the class if it is given an object.
Re: finding the method name 2 levels above
by ysth (Canon) on Aug 17, 2006 at 01:37 UTC
    Something like this should work, but you really shouldn't be doing this.
    sub name { { package For_name_internal_use; use overload '""' => sub { $_[0]->() }, fallback => 1; } return bless sub { my $in_copy; for (my $stack = 2; ! $in_copy and my @caller = caller +($stack); ++$stack) { $in_copy = 1 if $caller[3] && $caller[3] eq 'w +hatever::copy'; } if ($in_copy) { return 'some result to be used in whatever::co +py'; } else { return 'some result to be used outside of what +ever::copy'; } }, 'For_name_internal_use'; }
Re: finding the method name 2 levels above
by ysth (Canon) on Aug 17, 2006 at 01:04 UTC
    Not really; with a lot of work, you could do something like what the Want module does.

    But it doesn't make a lot of sense to make a method do something different when it's return value is being passed to a particular other method.