13

How do I fix this error?

foreach (values %{$args{car_models}}) {
   push(@not_sorted_models, UnixDate($_->{'year'},"%o"));
}

Error: Can't use string ("1249998666") as a HASH ref while "strict refs" in use at /.../BMW.pm line 222.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Kys
  • 2,597
  • 5
  • 20
  • 16

3 Answers3

15

The Data::Dumper module is extremely useful in such situations -- to help you figure out why a complex data structure is not meeting your expectations. For example:

use Data::Dumper;
print Dumper(\%args);
FMc
  • 41,963
  • 13
  • 79
  • 132
13

Clearly, one of the values in %{ $args{car_models} } is not a hash reference. That is, the data structure does not contain what you think it does. So, you can either fix the data structure or change your code to match the data structure. Since you have not provided the data structure, I can't comment on that.

You could use ref to see if $_ contains a reference to a hash before trying to access a member.

if ( ref eq 'HASH' and exists $_->{year} ) {
    push(@not_sorted_models, UnixDate($_->{year},"%o")); 
}

Based on your comment, and my ESP powers, I am assuming those values are timestamps. So, I am guessing, you are trying to find the year from a timestamp value (number of seconds from an epoch). In that case, you probably want localtime or gmtime:

my $year = 1900 + (localtime)[5];
C:\Temp> perl -e "print 1900 + (localtime(1249998666))[5]"
2009

Without further, concrete information about what your data structure is supposed to contain, this is my best guess.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • My intent is to push these numbers into the @non_sorted_models array as numbers so that I can call sort on the array. How do I convert it into an integer – Kys Aug 12 '09 at 18:10
  • By the way, putting those checks in place still gave me the same errors. – Kys Aug 12 '09 at 18:12
  • 1
    @Kys I do not understand what you are saying. Are you saying `values %{ $args{car_models} }` are not supposed to be hash references? If that is the case, why are you dereferencing them as if they are supposed to be? **Show your data structure** Otherwise this is a case of the blind leading the blind. – Sinan Ünür Aug 12 '09 at 18:30
  • Here is how it looks like: { 'four-wheel' => 'true', 'description' => 'Lightning fast', 'producer' => { 'name' => {} }, 'year' => '2009-08-07T22:31:06Z', }; – Kys Aug 12 '09 at 19:02
  • And yes they are timestams that I want to convert to num secs from an epoch. I dunno if you read the stuff I posted on the other link, but it's strange that the erros disappear if I have my logger print out the values first. – Kys Aug 12 '09 at 19:03
2

Hi if you have a hash ref variable (like $hash_ref) then code will be

if ( ref($hash_ref) eq 'HASH' and exists $hash_ref->{year} ) {
    push(@not_sorted_models, UnixDate($hash_ref->{year},"%o")); 
}
#instead of below:
if ( ref eq 'HASH' and exists $_->{year} ) {
    push(@not_sorted_models, UnixDate($_->{year},"%o")); 
}
Manoj Shekhawat
  • 429
  • 1
  • 3
  • 11