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

Updated:   |  Perl (Scripting) articles

In Perl, there are 2 different kinds of hashes.

  • A hash, which is defined by the % and ( ) characters - %hash = ( );
  • A reference hash, which is defined with the $ and { } characters - $hash = { };

 

The following if statement will return "is defined" when the reference hash named $hash is defined.

if ( $hash ) {
  print "The reference hash named \$hash is defined\n";
}

 

If the reference hash named $hash has not been defined, something like this will be returned.

Global symbol "$hash" requires explicit package name

 

Let's say you create an empty reference hash.

my $hash = {};

 

Dumper can be used to display the structure of the hash.

use Data::Dumper;
print Dumper \%hash;

 

Which will return the following.

$VAR1 = {};

 

The following if statement will now return "is defined".

if ( $hash ) {
  print "The reference hash named \$hash is defined\n";
}

 




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 99bfa3 in the box below so that we can be sure you are a human.