Hi.
I've programmed in Perl for several years, but this feels like a
beginner question. I'm trying to understand how the built-in function
"caller" works. The documentation leads me to expect "caller" to do
something other than what it actually does. I'd like to figure out if
I'm simply misunderstanding what it does or if I'm using it in a way
it wasn't intended to be used.
>From the documentation, I expect the following code to return the
package name, filename, line number, and subroutine name that the
currently executing subroutine was called from:
($package, $filename, $line, $subroutine) = caller();
This does return the package name, filename, and line number as I
expect. However, it does not return the subroutine name where the
current subroutine was called, but instead the name of the current
subroutine.
Below is some simple code to illustrate this:
1: #!/usr/bin/perl
2:
3: use strict;
4: use warnings;
5:
6: sub c1 {
7: c2();
8: }
9: sub c2 {
10: c3();
11: }
12: sub c3 {
13: my $i = 0;
14: while (my ($package, $filename, $line, $subroutine) = caller ($i)) {
15: print "CALLER $i\n";
16: print "Package: $package.\n";
17: print "Filename: $filename.\n";
18: print "Line: $line.\n";
19: print "Subroutine: $subroutine.\n";
20: $i++;
21: }
22: }
23:
24: c1();
25: exit;
Part of the output looks like:
CALLER 0
Package: main.
Filename: caller.pl.
Line: 10.
Subroutine: main::c3.
.....
If my understanding of the documentation is correct, the subroutine
name should be main::c2 (which called c3 at line 10), not main::c3
(the name of the current subroutine). I'm having trouble
understanding why "caller" would be designed to return the name of the
current subroutine instead of the caller subroutine.
I'm interested in getting the name of the calling subroutine for error
handling reasons. Specifically, I'm trying to extend the Error perl
module to report more data about errors (such as the time an error
occurred and of course the subroutine in which the error occurred);
this information would be logged to a file. I currently am using code
similar to the following to extract the data I need:
report_error {
local $Error::Depth = $Error::Depth + 1;
.....
my ($package, $filename, $line) = caller($Error::Depth - 1);
my ($subroutine) = (caller($Error::Depth))[3];
.....
}
As you can see, I have to use two separate "caller" lines to get the
line number of the error and the subroutine name in which the error
occurred. This is a bit of an ugly nuisance, which makes me think I'm
doing something wrong.
So, I guess my questions are:
1) Does anyone else feel misled by the documentation on "caller"?
2) Does anyone else find the subroutine result of "caller" confusing?
3) Why does "caller" return the "wrong" subroutine name? Or perhaps,
why is the subroutine name returned by "caller" the right one to
return?
4) How is "caller" usually used by perl experts?
5) Is there something wrong with my use of "caller" to find the error
data I want?
6) Is there a better way of getting this data?
Any enlightenment you can provide would be appreciated.
+ Richard J. Barbalace
<[EMAIL PROTECTED]>