On Wed, Jun 20, 2001 at 03:35:23PM -0400, Richard J. Barbalace wrote:
>       ($package, $filename, $line, $subroutine) = caller();

As a minor correction, this is incorrect.  The argument-less form of caller
returns only three elements: package, filename, and line.



> 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?

It's not so much the documentation is misleading, it's the name of the
function.  "caller" implies you're getting information about who called you,
when in fact you're getting information about the call stack.  The
information for the current subroutine, as in where it was called, what
package it belongs to, and what its name is, can be retrieved using
caller(0).  Similar information about the subroutine that called the current
subroutine can be found in caller(1), and so on.


[reordered]
> 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, taking the above into account, for the kind of information you're
looking for this is indeed the correct way to do it.  You should probably
take a look at the Carp module, part of standard Perl, as an example on how
to do this, and possible even a replacement.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to