此代码有效:

  my $href = shift @_;    # get reference to hash
  my %h = %{$href};       # dereference hash

这个没有:

  my %h = %{shift @_};

以及这个:

  my %h = ${$_[0]}

为什么?

=============================

再过一次精确的时间:

 1 #!/usr/bin/perl -w
  2 use strict;
  3 use warnings;
  4 
  5 my %h;
  6 sub a {
  7     
  8     # that works - result 1
  9     my $href = $_[0] || shift;
 10     %h = %{$href};
 11     
 12     # that does not work - result 0
 13     # my %h = %{$_[0]};
 14     
 15     # as well as that one - result 0
 16     # my %h = %{shift @_};
 17     $h{1}=2;
 18 }
 19 
 20 a(\%h);
 21 print scalar (keys %h) . "\n";

换句话说,第16行 - 没有。

有帮助吗?

解决方案

这将起作用。

  my %h = %{shift @_};

这不会。

  my %h = ${$_[0]} # not ${$_[0]}

那个塞吉尔应该是 %

  my %h = %{$_[0]}

还, use warnings;, , 和 use strict;


暗示: 您上面示例不起作用的原因,只是一个示例不声明词汇变量 %h = %{$href}; 不是 my %h = %{$href};

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top