Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Below I compare two scalars $temp3 which contains the number 27 and $user_id which contains the number 0. When I compare the two I get the HASH reference error. Displayed under the code below is the local $SIG{'__DIE__'} output where I dump values of variables used.The dump shows no undefined HASH reference. Infact where the script dies I am only testing numeric values.

1515 $ref = @{$no_call_requested_days->{$today_int}}[$i]; 1516 $temp2 = $ref->{'employee_id'}; 1517 1518 $temp3 = $user_id; 1519 if ($temp3 eq $user_id) {} 1520 if ($temp3 eq $temp2) {} 1521 1522 $temp3 = $temp2; 1523 1524 1525 if ($temp3 eq $temp2) {} 1526 1527 1528 if ($temp3 eq $user_id) # $temp3 = ref->{'employee_id'} 1529 { 1530 ...



THIS IS THE LOCAL $SIG{'__DIE__'} PRINTOUT
Can't use an undefined value as a HASH reference at /usr/local/www/cgi +-bin/kapc/md2/display_md_calendar.cgi line 1528. $user_id == 0 ref($user_id) == $temp2 == 27 ref($temp2) == $temp3 == 27 ref($temp3) == $ref == HASH(0x8390bf0) ref($ref) == HASH $ref->{'employee_id'} == 27 $ref->{'type'} == Routine --- $i == 0 $today_int == 1041483600 $no_call_requested_days == HASH(0x837ded0) \$no_call_requested_days->{$today_int} == ARRAY(0x8390c38) scalar(@{$no_call_requested_days->{$today_int}}) == 2 @{$no_call_requested_days->{$today_int}}[$i] == HASH(0x8390bf0) ${@{$no_call_requested_days->{$today_int}}[$i]}->{employee_id} == 27 %{@{$no_call_requested_days->{$today_int}}[$i]}->{type} == Routine ---

Replies are listed 'Best First'.
Re: Can't use an undefined value as a HASH reference
by jarich (Curate) on Dec 20, 2002 at 04:54 UTC
    G'day Joe

    It'd be nice to have a little more data. I think your __DIE__ handler is a great idea, but it's obviously not showing up the error. I can't tell you why. It looks like your code is getting a warning for no reason, but I can't tell you why that is so.

    I have occasionally got HASH reference errors for nothing to do with hashes before and it's usually because I've forgotten to close some quotes or put in a semi-colon or whatever.

    Nevertheless, I'm curious what your code returns for the following (just drop this in place of the segment you cut'n'pasted for us above):

    use Data::Dumper; # Just grabbing the values of these so that # strict will cope with them. my $my_today_int = $today_int; my $my_no_call_requested_days = $no_call_requested_days; my $my_i = $i; my $my_user_id = $user_id; print STDERR Dumper($my_today_int, $my_i, $my_user_id); print STDERR Dumper($my_no_call_requested_days); { use strict; my @today = @{$my_no_call_requested_days->{$my_today_int}}; print STDERR "today:\n", Dumper(@today); my $ref = $today[$my_i]; print STDERR "ref:\n", Dumper($ref); my $employee_id = $ref->{'employee_id'}; print STDERR "employee_id: [$employee_id]\n"; if($employee_id eq $my_user_id) # should be "==" { print "They are the same!\n"; } }
    It's functionally the same. This block is strict compliant and all the interesting values are printed to STDERR so that we can see things as they go on.

    Hopefully rewriting to something like this will help identify the problem. Particularly if the bug is a missing close quote or } or similar above. :)

    Hope it helps.

    jarich

      jarich, thank you for your reply.

      i pasted your code in my $SIG{'__DIE__'} adn here is it.
      local $SIG{'__DIE__'} = sub { my $my_today_int = $today_int; my $my_no_call_requested_days = $no_call_requested_days; my $my_i = $i; my $my_user_id = $user_id; my @dump = (); # Just grabbing the values of these so that # strict will cope with them. push @dump, $_[0], ""; push @dump, Dumper($my_today_int, $my_i, $my_user_id), ""; push @dump, Dumper($my_no_call_requested_days), ""; { use strict; my @today = @{$my_no_call_requested_days->{$my_today_int}}; push @dump, "today: " . Dumper(@today), ""; my $ref = $today[$my_i]; push @dump, "ref: " . Dumper($ref), ""; my $employee_id = $ref->{'employee_id'}; push @dump, "employee_id: [$employee_id]", ""; if($employee_id eq $my_user_id) # should be "==" { push @dump, "They are the same!", ""; } } &debug(@dump); };


      Here is the output.
      Can't use an undefined value as a HASH reference at /usr/local/www/cgi +-bin/kapc/md2/display_md_calendar.cgi line 1534. $VAR1 = 1041483600; $VAR2 = 0; $VAR3 = '0'; $VAR1 = { '1050120000' => [ { 'employee_id' => '34', 'type' => 'Routine' } ], '1049173200' => [ { 'employee_id' => '28', 'type' => 'Routine' } ], '1043211600' => [ { 'employee_id' => '0', 'type' => 'Hospital' }, { 'employee_id' => '34', 'type' => 'Routine' } ], '1043989200' => [ { 'employee_id' => '16', 'type' => 'Routine' }, { 'employee_id' => '10', 'type' => 'Routine' } ], '1043125200' => [ { 'employee_id' => '27', 'type' => 'Routine' } ], '1051329600' => [ { 'employee_id' => '24', 'type' => 'Routine' }, { 'employee_id' => '16', 'type' => 'Routine' } ], '1051243200' => [ { 'employee_id' => '24', 'type' => 'Routine' }, { 'employee_id' => '16', 'type' => 'Routine' } ], '1041483600' => [ { 'employee_id' => '27', 'type' => 'Routine' }, { 'employee_id' => '27', 'type' => 'Routine' } ], '1058932800' => [ { 'employee_id' => '23', 'type' => 'Routine' } ], '1053144000' => [ { 'employee_id' => '20', 'type' => 'Routine' } ], '1043298000' => [ { 'employee_id' => '0', 'type' => 'Routine' } ], '1046149200' => [ { 'employee_id' => '23', 'type' => 'Routine' }, { 'employee_id' => '28', 'type' => 'Routine' } ], '1046322000' => [ { 'employee_id' => '28', 'type' => 'Routine' } ], '1046408400' => [ { 'employee_id' => '10', 'type' => 'Extension' } ], '1048222800' => [ { 'employee_id' => '28', 'type' => 'Routine' }, { 'employee_id' => '10', 'type' => 'Routine' } ], '1045198800' => [ { 'employee_id' => '28', 'type' => 'Routine' }, { 'employee_id' => '13', 'type' => 'Routine' }, { 'employee_id' => '13', 'type' => 'Routine' } ], '1048136400' => [ { 'employee_id' => '28', 'type' => 'Routine' } ], '1042693200' => [ { 'employee_id' => '0', 'type' => 'Routine' } ], '1045285200' => [ { 'employee_id' => '0', 'type' => 'Routine' } ], '104400' => [ { 'employee_id' => '16', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Routine' }, { 'employee_id' => '28', 'type' => '-Routine' }, { 'employee_id' => '13', 'type' => '-Routine' }, { 'employee_id' => '13', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Extension' }, { 'employee_id' => '28', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Routine' }, { 'employee_id' => '24', 'type' => '-Routine' }, { 'employee_id' => '16', 'type' => '-Routine' } ], '18000' => [ { 'employee_id' => '16', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Routine' }, { 'employee_id' => '28', 'type' => '-Routine' }, { 'employee_id' => '13', 'type' => '-Routine' }, { 'employee_id' => '13', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Extension' }, { 'employee_id' => '28', 'type' => '-Routine' }, { 'employee_id' => '10', 'type' => '-Routine' }, { 'employee_id' => '24', 'type' => '-Routine' }, { 'employee_id' => '16', 'type' => '-Routine' }, { 'employee_id' => '0', 'type' => '-Routine' }, { 'employee_id' => '34', 'type' => '-Routine' }, { 'employee_id' => '24', 'type' => '-Routine' }, { 'employee_id' => '16', 'type' => '-Routine' }, { 'employee_id' => '20', 'type' => '-Routine' } ], '-154800' => [ { 'employee_id' => '0', 'type' => '-Routine' }, { 'employee_id' => '34', 'type' => '-Routine' }, { 'employee_id' => '24', 'type' => '-Routine' }, { 'employee_id' => '16', 'type' => '-Routine' }, { 'employee_id' => '20', 'type' => '-Routine' } ], '1045717200' => [ { 'employee_id' => '23', 'type' => 'Routine' } ] }; today: $VAR1 = { 'employee_id' => '27', 'type' => 'Routine' }; $VAR2 = + { 'employee_id' => '27', 'type' => 'Routine' }; ref: $VAR1 = { 'employee_id' => '27', 'type' => 'Routine' }; employee_id: [27]


      Here is the line 1534 where the script dies.
      if (%{@{$no_call_requested_days->{$today_int}}[$i]}->{'employee_id'} = += $user_id)
        Now I'm checking the employeeid from a hash and the userid and setting $ok to 1 if the two values are equal. Then I check ok and when I do the script dies on that line with "Can't use an undefined value as a HASH reference at /usr/local/www/cgi-bin/kapc/md2/display_md_calendar.cgi line 1570."

        Here is the code.

        my $value1 = int(%{@{$no_call_requested_days->{$today_int}}[$i]}->{'em +ployee_id'}); my $value2 = int($user_id); my $ok == (length($value1) == length($value2)) ? (1) : (0); if ($ok == 1) { for ($j=0; $j<length($value1); $j++) { if (substr($value1, $j, 1) != substr($value2, $j, 1)) { $ok = 0; } } } if ($ok == 1) # SCRIPT DIES HERE {
Re: Can't use an undefined value as a HASH reference
by pfaut (Priest) on Dec 19, 2002 at 20:41 UTC

    ref->{'employee_id'} is missing the '$' on line 1528. Change 'ref' to '$ref'.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      I appreciate the response thank you. However that was just a comment. The script dies when I test $ref->{'employee_id'} with $user_id. The script also dies when I set a scalar to equal $ref->{'employee_id'} and then test that scalar with $user_id. That is why I added the senseless if clauses to see if the script would die. Thanks joe budd joebudd@iserv.net
Re: Can't use an undefined value as a HASH reference
by Enlil (Parson) on Dec 19, 2002 at 20:51 UTC
    For what it is worth 'numerical' comparisons are best done with == as opposed to eq which are best suited for 'string' comparisons.

    -enlil

      Thanks. Joe Budd joebudd@iserv.net