Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "Use of uninitialized value in hash element"?

Tags:

perl

..
use strict;
use warnings;
...

my (%customer);
%customer = ();
...
 17 sub _customer_id {
 18     my $customer_r = shift;
 19     unless(defined $customer{$customer_r}){
 20         $customer{$customer_r} = ++$customer_id;
 21     }
 22     $customer{$customer_r};
 23 }

I'm just checking whether some hash key exists.

But getting the Use of uninitialized value in hash element at line 19,20,22.

Why?

like image 256
asker Avatar asked Jul 22 '11 03:07

asker


2 Answers

This error means $customer_r is undef.

How are you calling this function?

like image 121
Nemo Avatar answered Nov 16 '22 21:11

Nemo


You may want to check this statement.
my $customer_r = shift;

If $customer_r gets some empty value, then you would get this error when you try to use this variable. Basically, whenever you try to use an empty variable(not initialised), you would encounter this error.

like image 1
cppcoder Avatar answered Nov 16 '22 23:11

cppcoder