Jan 4, 2011

PERL defined Function

PERL defined Function:

Syntax:

defined EXPR
defined

Definition and Usage:

Returns true if EXPR has a value other than the undef value, or checks the value of $_ if EXPR is not specified. This can be used with many functions to detect a failure in operation, since they return undef if there was a problem. A simple Boolean test does not differentiate between false, zero, an empty string, or the string .0., which are all equally false.
If EXPR is a function or function reference, then it returns true if the function has been defined. When used with entire arrays and hashes, it will not always produce intuitive results. If a hash element is specified, it returns true if the corresponding value has been defined, but does not determine whether the specified key exists in the hash.

Return Value:

  • 0 if EXPR contains undef
  • 1 if EXPR contains a valid value or reference
$var1 = "This is defined";

if( defined($var1) ){
  print "$var1\n";
}
if( defined($var2) ){
  print "var2 is also defined\n";
}else{
  print "var2 is not defined\n";
}
Output:
This will produce following result
This is defined
var2 is not defined

No comments:

Post a Comment