FreeKB - How to determine if a variable in defined in Perl
How to determine if a variable in defined in Perl

Home > Search > How-to


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 (defined $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.

 

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 (defined $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 (defined $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 (defined $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 (defined $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 (defined $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 (defined $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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments