【问题标题】:Error in perl: Using a hash as a reference is deprecatedperl 中的错误:不推荐使用散列作为参考
【发布时间】:2012-05-23 00:36:32
【问题描述】:
sub function{
my $storedata=shift;
my $storenameandaddress=$storedata->{$storeid}->{name}
."_".$storedata->{$storeid}->{location}->{address}
."_".$storedata->{$storeid}->{location}->{city}
."_".$storedata->{$storeid}->{location}->{state}
."_".$storedata->{$storeid}->{location}{country};}

我的代码如上所示。它给了我错误信息:

Using a hash as a reference is deprecated at main.pl line 141.

但是,该功能仍然可以运行。其余的似乎都很好。那么这个错误在说什么?我应该如何解决它?谢谢。

【问题讨论】:

  • 显示整个代码,尤其是变量如何填充数据结构的部分。
  • 您可以使用以下命令检查此错误:perldoc perldiag
  • 将“使用诊断”添加到您的代码中。

标签: perl hash reference deprecated


【解决方案1】:

您发布的代码没有给出该警告。表单代码

%foo->{bar}

发出警告。它给出了警告,因为它的功能是

$foo->{bar}

即使不应该这样做。


$ perl -wE'my %h = ( foo => 123 ); say %h->{foo};'
Using a hash as a reference is deprecated at -e line 1.
123

$ perl -Mdiagnostics -wE'my %h = ( foo => 123 ); say %h->{foo};'
Using a hash as a reference is deprecated at -e line 1 (#1)
    (D deprecated) You tried to use a hash as a reference, as in
    %foo->{"bar"} or %$ref->{"hello"}.  Versions of perl <= 5.6.1
    used to allow this syntax, but shouldn't have. It is now deprecated, and will
    be removed in a future version.

123

$ perl -wE'my %h = ( foo => 123 ); say $h->{foo};'
123

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2018-01-07
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多