This tutorial walks you through multiple inbuilt functions

  • defined function
  • undef function

How to check variable has a value

Sometimes, We need to check variable contains the value of a string or number.

The defined function checks variable has a string or numeric value.

It returns 1 if the variable holds some value, else returns empty.

Syntax:

defined variable

Here is a Perl Defined Function example

sub isVariableHasValue {
  my $value = $_[0];
  if (defined $value) {
    print "variable is initialized \n";
  } else {
    print "variable is not initialized \n";

  }
}
$name = "abc";

isVariableHasValue($name);
$name1;
isVariableHasValue($name1)