简体   繁体   English

Perl中的奇数哈希表示

[英]Odd hash representation in perl

I have a hash that has the following content, dumped via Data::Dumper : 我有一个哈希,其中包含以下内容,通过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. 我似乎无法理解“ 0”是否是没有分配任何东西或其他任何东西的键。 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 在FOO.pm中使用“严格引用”时,不能将字符串(“ 0”)用作HASH引用

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',

          }
        ];

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. 表示转储的变量是对具有单个元素的数组的引用,该元素由数字零组成。

You can check what type of reference it is with the ref() command. 您可以使用ref()命令检查引用的类型。 If it returns 'HASH' for that variable then you're safe to use it as a hash reference like you expect. 如果该变量返回“ HASH”,则可以放心使用它作为哈希引用。 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. 或者,您可以在比较中使用两次ref() ,以提供所需的结构。 That avoids coding specific string values: 这样可以避免编码特定的字符串值:

if (ref($data) eq ref({})) {
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM