snaggs
snaggs

Reputation: 5713

Cannot fetch items from hash, get Global symbol requires explicit package name

I try to work with hashes in Perl.

I have following example:

my %ERROR_CODE_101 = ("id"  => 101,"desc" => "blablabla");

sub checkError
{   
   # some switch-case 
   #....
   return %ERROR_CODE_101;    
}

sub runCommand
{        
    my $code = checkError();
    if($code{id} > 0)    # error happens here line 216
    {
       #...
    }
}

Error:

Global symbol "%code" requires explicit package name at build_ios.pl line 216.

Upvotes: 0

Views: 64

Answers (1)

Quentin
Quentin

Reputation: 943615

my $code = checkError(); assigns the return value to the scalar $code.

$code{id} tries to read the scalar id value from the %code hash … but you have a scalar, not a hash.

You need to assign the return value to a hash in the first place:

my %code = checkError();

Upvotes: 4

Related Questions