Re: Identify the package a subroutine is being called from
by pc88mxer (Vicar) on Jun 06, 2008 at 17:04 UTC
|
You can get more information by calling caller in list context:
package Test:
sub package_name {
my @c = caller(0);
$c[3];
}
Although, the way you have set things up, this will return Test::package_name for both Calling->package_name and Calling2->package_name. Technically this is correct so you could add information about $_[0] in the result.
sub package_name {
my @c = caller(0);
"$c[3] (with self = $_[0])"
}
| [reply] [d/l] [select] |
|
$ perl -wle 'sub foo { my @l = caller(); return scalar @l }; print foo
+()'
3
$ perl -wle 'sub foo { my @l = caller(0); return scalar @l }; print fo
+o()'
10
| [reply] [d/l] [select] |
|
You're right. How may we invoke caller? Let us count the ways:
$package = caller;
@info = caller;
# -> ($package, $file, $line)
$package = caller($i);
@extra_info = caller($i);
# -> ($pack, $file, $line, $sub, $hasargs, ...)
| [reply] [d/l] [select] |
Re: Identify the package a subroutine is being called from
by moritz (Cardinal) on Jun 06, 2008 at 17:05 UTC
|
If you need that kind of information, you have to use the Package->subroutine() syntax (at least I think so). Then the package name is passed as the first argument, just like the object is in method calls. | [reply] [d/l] |
|
Well, I am adding code onto an existing module, and I cannot guarantee thats how they call the subroutine. It is driving me nuts because of it too! Otherwise that would have been the easy way to fix it.
| [reply] |
Re: Identify the package a subroutine is being called from
by kyle (Abbot) on Jun 06, 2008 at 17:31 UTC
|
Undefined subroutine &Calling::package_name called
It runs if I change Blah::package_name() to Blah->package_name(). The former will look for package_name only in the package you specify. The latter will look for it in the inheritance tree in the way you're talking about.
That being the case, moritz is correct. The package name you're looking for is the first thing in @_. If it's not there, then maybe you want __PACKAGE__.
If there's something I'm missing, please post some working code that shows the problem. | [reply] [d/l] [select] |
|
No, you are correct, I made a bad assumption evidently. You HAVE to call the subroutine with a package name( or blessed reference) or it will blow up(at least when you are using inheritance).
It was not the answer I expected, but it is an answer.
| [reply] |
Re: Identify the package a subroutine is being called from
by jettero (Monsignor) on Jun 06, 2008 at 17:10 UTC
|
Corollary, anyone have any idea how to get the calling object instance? I've always wanted that in perl. I don't think you can get it. It's called previous_object() in pike/lpc...
| [reply] |
|
use Devel::StackTrace;
sub calling_object {
my $t = Devel::StackTrace->new;
my $f;
$f = $t->next_frame for (1..4);
($f->args)[0]
}
sub e {
my $x = shift;
print "x = $x\n";
d('humma');
}
sub d {
print "I was called by object ", calling_object(), "\n";
}
e('a', 'b', 'c');
| [reply] [d/l] [select] |
|
Holy crap. That's exactly what I wanted. I should have asked years ago.
| [reply] |
|
package DB;
sub get_calling_object
{
my @foo = caller(2);
return $DB::args[0]
}
Update: This needs the package declaration. | [reply] [d/l] [select] |
|
I do not understand what $DB::args[0] is, but I suspect this is intended to return the package name of the caller, not the instance of the caller object.
| [reply] |
|
|
|