The method who_called() below will more or less do what you want. Caveats: Its not guaranteed to work. Read perlfunc::caller(). Note that there are some funky issues that mean you _cant_ reliable replace the overload::StrVal calls with plain old reference stringification.
package NewObj;
sub new { return bless {},$_[0] };
package Foo;
our @ISA=qw(NewObj);
sub call_bar {
my ($self,$bar)=@_;
$bar->who_called;
}
package Bar;
use strict;
use warnings;
use overload; #VITAL!
our @ISA=qw(NewObj);
sub who_called {
my ($self)=@_;
package DB;
unless (my (undef,undef,undef,$sub)=caller(1)) {
print "Called from main\n";
} else {
print "Called from $sub\n";
if (ref($DB::args[0]) and overload::StrVal($DB::args[0])=~/=/) {
print "\tWhich is a method that was invoked on ".overload::Str
+Val($DB::args[0])."\n";
} else {
print "@DB::args";
}
}
}
package main;
my $obj1=Foo->new();
my $obj2=Bar->new();
print "OBJ1 (Foo) : ".overload::StrVal($obj1)."\n"
$obj1->call_bar($obj2);
HTH
--- demerphq
my friends call me, usually because I'm late....
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.