Bootstrap FreeKB - Perl (Scripting) - Determine if a variable is defined
Perl (Scripting) - Determine if a variable is defined

Updated:   |  Perl (Scripting) articles

Let's say you want to determine if the $foo variable has been defined. In this example, the $foo variable has not been defined.

#!/usr/bin/perl

use strict;
use warnings;

if ($foo) { 
  print "the \$foo variable is defined\n"; 
} else {
  print "the \$foo variable is not defined\n"; 
}

 

Running this script will return the following.

Global symbol "$foo" requires explicit package name at example.pl line 6.
Execution of example.pl aborted due to comilation errors.

 

Likewise, the Data::Dumper module can be used to show the variable is undefined.

use Data::Dumper;
print Dumper $foo;

 

Which should return undef.

$VAR1 = undef;

 

This is expected since use strict and use warnings are being used. Let's create the $foo variable.

#!/usr/bin/perl

use strict;
use warnings;

my $foo;

if ($foo) { 
  print "the \$foo variable is defined\n"; 
} else {
  print "the \$foo variable is not defined\n"; 
}

 

Now the script return the following since we did not give $foo any value. $foo is still not defined.

the $foo variable is not defined

 

Let's give $foo a value of nothing.

#!/usr/bin/perl

use strict;
use warnings;

my $foo = "";

if ($foo) { 
  print "the \$foo variable is defined\n"; 
} else {
  print "the \$foo variable is not defined\n"; 
}

 

Now the scripts return the following.

the $foo variable is defined

 

Defined variables can be undefined.

#!/usr/bin/perl

use strict;
use warnings;

my $foo = "";
undef $foo;

if ($foo) { 
  print "the \$foo variable is defined\n"; 
} else {
  print "the \$foo variable is not defined\n"; 
}

 

Now the scripts return the following since we undefined $foo.

the $foo variable is not defined

 


Passing in a value to a subroutine

Let's say you are passing in a value to a subroutine. In this example, no value is passed into the fruit subroutine.

#!/usr/bin/perl

use strict;
use warnings;

fruit();

sub fruit{
  my ($foo) = @_;
  if ($foo) { 
    print "the \$foo variable is defined\n"; 
  } else {
    print "the \$foo variable is not defined\n"; 
  }
}

 

The scripts return the following since $foo was never updated to have a value.

the $foo variable is not defined

 

In this example, "apple" is passed into the subroutine.

#!/usr/bin/perl

use strict;
use warnings;

fruit("apple");

sub fruit{
  my ($foo) = @_;
  if ($foo) { 
    print "the \$foo variable is defined\n"; 
  } else {
    print "the \$foo variable is not defined\n"; 
  }
}

 

The scripts return the following since $foo contain a value of "apple".

the $foo variable is defined

 

In this example, "" is passed into the subroutine (an empty value).

#!/usr/bin/perl

use strict;
use warnings;

fruit("");

sub fruit{
  my ($foo) = @_;
  if ($foo) { 
    print "the \$foo variable is defined\n"; 
  } else {
    print "the \$foo variable is not defined\n"; 
  }
}

 

The scripts return the following since $foo contains an empty value.

the $foo variable is defined

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 331c6b in the box below so that we can be sure you are a human.