| |
Miscellaneous Operations
-
- 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.
- defined(EXPR)
- defined EXPR
- Returns a boolean value saying whether
the lvalue EXPR has a real value or not. Many operations
return the undefined value under exceptional conditions,
such as end of file, uninitialized variable, system error
and such. This function allows you to distinguish between
an undefined null string and a defined null string with
operations that might return a real null string, in
particular referencing elements of an array. You may also
check to see if arrays or subroutines exist. Use on
predefined variables is not guaranteed to produce
intuitive results. Examples:
print if defined $switch{'D'};
print "$val\n" while defined($val = pop(@ary));
die "Can't readlink $sym: $!"
unless defined($value = readlink $sym);
eval '@foo = ()' if defined(@foo);
die "No XYZ package defined" unless defined %_XYZ;
sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
See also undef.
- dump LABEL
- This causes an immediate core dump.
Primarily this is so that you can use the undump program
to turn your core dump into an executable binary after
having initialized all your variables at the beginning of
the program. When the new binary is executed it will
begin by executing a "goto LABEL" (with all the
restrictions that goto suffers). Think of it as a goto
with an intervening core dump and reincarnation. If LABEL
is omitted, restarts the program from the top. WARNING:
any files opened at the time of the dump will NOT be open
any more when the program is reincarnated, with possible
resulting confusion on the part of perl. See also -u.
Example:
#!/usr/bin/perl
require 'getopt.pl';
require 'stat.pl';
%days = (
'Sun',1,
'Mon',2,
'Tue',3,
'Wed',4,
'Thu',5,
'Fri',6,
'Sat',7);
dump QUICKSTART if $ARGV[0] eq '-d';
QUICKSTART:
do Getopt('f');
- 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.
- require(EXPR)
- require EXPR
- require
- Includes the library file specified by
EXPR, or by $_ if EXPR is
not supplied. Has semantics similar to the following
subroutine:
sub require {
local($filename) = @_;
return 1 if $INC{$filename};
local($realfilename,$result);
ITER: {
foreach $prefix (@INC) {
$realfilename = "$prefix/$filename";
if (-f $realfilename) {
$result = do $realfilename;
last ITER;
}
}
die "Can't find $filename in \@INC";
}
die $@ if $@;
die "$filename did not return true value" unless $result;
$INC{$filename} = $realfilename;
$result;
}
Note that the file will not be
included twice under the same specified name. The file
must return true as the last statement to indicate
successful execution of any initialization code, so it's
customary to end such a file with "1;" unless
you're sure it'll return true otherwise.
- reset(EXPR)
- reset EXPR
- reset
- Generally used in a continue
block at the end of a loop to clear variables and reset
?? searches so that they work again. The expression is
interpreted as a list of single characters (hyphens
allowed for ranges). All variables and arrays beginning
with one of those letters are reset to their pristine
state. If the expression is omitted, one-match searches (?pattern?)
are reset to match again. Only resets variables or
searches in the current package. Always returns 1.
Examples:
reset 'X'; # reset all X variables
reset 'a-z'; # reset lower case variables
reset; # just reset ?? searches
Note: resetting "A-Z" is
not recommended since you'll wipe out your ARGV and ENV
arrays.
The use of reset on dbm associative
arrays does not change the dbm file. (It does, however,
flush any entries cached by perl, which may be useful if
you are sharing the dbm file. Then again, maybe not.)
- scalar(EXPR)
- Forces EXPR to be interpreted in a
scalar context and returns the value of EXPR.
- undef(EXPR)
- undef EXPR
- undef
- Undefines the value of EXPR, which
must be an lvalue. Use only on a scalar value, an entire
array, or a subroutine name (using &). (Undef will
probably not do what you expect on most predefined
variables or dbm array values.) Always returns the
undefined value. You can omit the EXPR, in which case
nothing is undefined, but you still get an undefined
value that you could, for instance, return from a
subroutine. Examples:
undef $foo;
undef $bar{'blurfl'};
undef @ary;
undef %assoc;
undef &mysub;
return (wantarray ? () : undef) if $they_blew_it;
- 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;
|
|
|