callerReturns the context of the current subroutine call. In scalar context,returns the caller's package name if there is a caller (that is, ifwe're in a subroutine or eval
or require
) and the undefined valueotherwise. In list context, returns
- # 0 1 2
- ($package, $filename, $line) = caller;
With EXPR, it returns some extra information that the debugger uses toprint a stack trace. The value of EXPR indicates how many call framesto go back before the current one.
- # 0 1 2 3 4
- ($package, $filename, $line, $subroutine, $hasargs,
- # 5 6 7 8 9 10
- $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
- = caller($i);
Here $subroutine may be (eval)
if the frame is not a subroutinecall, but an eval
. In such a case additional elements $evaltext and$is_require
are set: $is_require
is true if the frame is created by arequire
or use
statement, $evaltext contains the text of theeval EXPR
statement. In particular, for an eval BLOCK
statement,$subroutine is (eval)
, but $evaltext is undefined. (Note also thateach use
statement creates a require
frame inside an eval EXPR
frame.) $subroutine may also be (unknown)
if this particularsubroutine happens to have been deleted from the symbol table.$hasargs
is true if a new instance of @_
was set up for the frame.$hints
and $bitmask
contain pragmatic hints that the caller wascompiled with. The $hints
and $bitmask
values are subject to changebetween versions of Perl, and are not meant for external use.
$hinthash
is a reference to a hash containing the value of %^H
when thecaller was compiled, or undef
if %^H
was empty. Do not modify the valuesof this hash, as they are the actual values stored in the optree.
Furthermore, when called from within the DB package inlist context, and with an argument, caller returns moredetailed information: it sets the list variable @DB::args
to be thearguments with which the subroutine was invoked.
Be aware that the optimizer might have optimized call frames away beforecaller
had a chance to get the information. That means that caller(N)
might not return information about the call frame you expect it to, forN > 1
. In particular, @DB::args
might have information from theprevious time caller
was called.
Be aware that setting @DB::args
is best effort, intended fordebugging or generating backtraces, and should not be relied upon. Inparticular, as @_
contains aliases to the caller's arguments, Perl doesnot take a copy of @_
, so @DB::args
will contain modifications thesubroutine makes to @_
or its contents, not the original values at calltime. @DB::args
, like @_
, does not hold explicit references to itselements, so under certain cases its elements may have become freed andreallocated for other variables or temporary values. Finally, a side effectof the current implementation is that the effects of shift @_
cannormally be undone (but not pop @_
or other splicing, and not if areference to @_
has been taken, and subject to the caveat about reallocatedelements), so @DB::args
is actually a hybrid of the current state andinitial state of @_
. Buyer beware.