Friday, March 12, 2010

Use of caller in Perl

Caller or Caller Expr
Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise. In list context, returns
#      0            1            2
($package, $filename, $line ) = caller;


With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to 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);

(caller 1)[3] will give the name of the calling subroutine.
($package, $filename, $line ) = caller; or

($package, $filename, $line ) = caller 1; # Both of these are equivalent

@caller = caller 0; #will return whole information about the calling subroutine

No comments:

Post a Comment