Perl Variable Functions

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

Perl Variable Functions

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.
reset EXPR
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.)
Forces EXPR to be interpreted in a scalar context and returns the value of EXPR.
undef EXPR
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;

Tagged in:


You must LOGIN to add comments