0

I have a hash that has the following content, dumped via Data::Dumper:

$VAR1 = [
      0
    ];

What does that mean? I can´t seem to understand if that "0" is a key with nothing assigned to it or any other thing. Only clue is that I got this error below, which says a hash cannot be referenced by a zero string, is that right?

Can't use string ("0") as a HASH ref while "strict refs" in use at FOO.pm

The way this is assigned to an hash is obscure, since the code is restrict, but in some other cases, this same hash had something like

$VAR1 = [
          {
            'ExtraInfo->m_Fade_Notification_Timer' => 'RTPC',
            'ExtraInfo->m_FarEndNESlot' => '28 MHz',
            'ExtraInfo->m_Temperature_Ra2' => '',
            'ExtraInfo->m_Path' => '',
            'ExtraInfo->m_Radio_Terminal_Name' => 'ST01',

          }
        ];
dukibas
  • 35
  • 1
  • 4
  • 2
    That is an *array* reference, not a hash reference. See [`perldoc perlref`](http://perldoc.perl.org/perlref.html#Making-References) for more information. – Hunter McMillen Sep 11 '15 at 19:57
  • 2
    Please show the code that is creating your data structure before you dump it. – dgw Sep 11 '15 at 19:57
  • 1
    If Data::Dumper is dumping it that way, you have an array (square brackets) not a hash (curly braces). Note in your second example you have an array containing one element, which is a reference to the 5-element hash shown "inside" it. – Joe McMahon Sep 11 '15 at 19:58
  • It looks like you're dumping an array containing the hash of interest (in your second example). So, in your first example, your "hash reference" is just the scalar `0`. It doesn't *contain* a `0`— it *is* a `0`. Which isn't a hash ref, which is why you get that warning. As for why the variable that you expect to contain a hash ref actually contains a scalar, that depends on your code. – Wim Lewis Sep 11 '15 at 20:00
  • dgw, I don´t have acess to the data structure´s creator, the only thing I know is that the code tries to get the hash content (is this case, a hash ref) if a regex is valid to the array. Joe / Wim I understand it now. The "0" is supposed to be a hash, but because there is not matching with the previously mentioned regex, it get this 0. Will dig it more to see if I can handle this. Tks. – dukibas Sep 11 '15 at 20:10

2 Answers2

2

There are no hashes involved. $VAR1 = [ 0 ]; means the dumped variable is a reference to an array with a single element consisting of the number zero.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

You can check what type of reference it is with the ref() command. If it returns 'HASH' for that variable then you're safe to use it as a hash reference like you expect. If not then your incoming data isn't what you expect and you can raise an exception or handle it otherwise.

if (ref($data) eq 'HASH') {
    while (my ($key, $value) = each %{$data}) {
        ...
    }
} else {
    die "Unexpected data";
}

Or you could use ref() twice in the comparison, giving it the structure you want. That avoids coding specific string values:

if (ref($data) eq ref({})) {
    ...
Jeremy Jones
  • 4,561
  • 3
  • 16
  • 26
  • Thanks for that insight with `ref()`. With that I can remove/undef the array when the hash is 0. – dukibas Sep 14 '15 at 18:10