Subroutine Functions

by Geethalakshmi 2010-09-17 12:39:21

Subroutine Functions


caller(EXPR)
caller
Returns the context of the current subroutine call:

($package,$filename,$line) = caller;

With EXPR, 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.
do SUBROUTINE (LIST)
Executes a SUBROUTINE declared by a sub declaration, and returns the value of the last expression evaluated in SUBROUTINE. If there is no subroutine by that name, produces a fatal error. (You may use the defined operator to determine if a subroutine exists.) If you pass arrays as part of LIST you may wish to pass the length of the array in front of each array. (See section Subroutines.) The parentheses are required to avoid confusion with the `do EXPR' form. SUBROUTINE may also be a single scalar variable, in which case the name of the subroutine to execute is take from the variable. As an alternate (and preferred) form, you may call a subroutine by prefixing the name with an ampersand: `&foo(@args)'. If you aren't passing any arguments, you don't have to use parentheses. If you omit the parentheses, no `@_' array is passed to the subroutine. The `&' form is also used to specify subroutines to the defined and undef operators.

if (defined &$var) { &$var($parm); undef &$var; }

local(LIST)
Declares the listed variables to be local to the enclosing block, subroutine, eval or do. All the listed elements must be legal lvalues. This operator works by saving the current values of those variables in LIST on a hidden stack and restoring them upon exiting the block, subroutine or eval. This means that called subroutines can also reference the local variable, but not the global one. The LIST may be assigned to if desired, which allows you to initialize your local variables. (If no initializer is given for a particular variable, it is created with an undefined value.) Commonly this is used to name the parameters to a subroutine. Examples:

sub RANGEVAL {
local($min, $max, $thunk) = @_;
local($result) = '';
local($i);

# Presumably $thunk makes reference to $i

for ($i = $min; $i < $max; $i++) {
$result .= eval $thunk;
}

$result;
}

if ($sw eq '-v') {
# init local array with global array
local(@ARGV) = @ARGV;
unshift(@ARGV,'echo');
system @ARGV;
}
# @ARGV restored

# temporarily add to digits associative array
if ($base12) {
# (NOTE: not claiming this is efficient!)
local(%digits) = (%digits,'t',10,'e',11);
do parse_num();
}

Note that local() is a run-time command, and so gets executed every time through a loop, using up more stack storage each time until it's all released at once when the loop is exited.
return LIST
Returns from a subroutine with the value specified. (Note that a subroutine can automatically return the value of the last expression evaluated. That's the preferred method--use of an explicit return is a bit slower.)
wantarray
Returns true if the context of the currently executing subroutine is looking for an array value. Returns false if the context is looking for a scalar.

return wantarray ? () : undef;

Tagged in:

639
like
0
dislike
0
mail
flag

You must LOGIN to add comments