【问题标题】:Accessing hash of hashes in Perl在 Perl 中访问哈希值
【发布时间】:2018-08-06 16:50:21
【问题描述】:

我有这个代码:

my %options;

$options{"style"}{size} = "mini";
$options{"style"}{color} = "secondary";
diag("size=".$options{style}{size});
$self->applyStyle(%options);

然后:

sub applyStyle {
  my ($self, $options) = @_;
  diag("size=".$options->{style}{size});
}

但我明白了:

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

如何打印(和使用)这些值?

【问题讨论】:

    标签: perl hash


    【解决方案1】:

    您没有正确传递散列,因为子例程需要散列 ref,而您传递的散列将扩展为键和值列表。这就是为什么它尝试使用“style”作为 hashref,因为它是传递的第一个键。

    只需将调用更改为此,它应该可以工作。

    $self->applyStyle(\%options);
    

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2017-07-05
      • 2012-03-10
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多