FreeKB - Perl (Scripting) Hash (determine if hash is defined)
Perl (Scripting) - Hash (determine if hash is defined)

In Perl, there are 3 different kinds of hashes.

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


The following if statement will return "is defined" when the regular hash named %hash is defined.

if ( %hash ) {
  print "The regular hash named \%hash is defined\n";


If the regular 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 regular 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 = {};


Now the following if statement will return "is defined".

if ( %hash ) {
  print "\%hash is defined\n";


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


Web design by yours truely - me, myself, and I   |   |