linux - Perl 模块错误 - defined(%hash) 已弃用

标签 linux perl hash crontab perl-module

背景:

  • 我正在努力将 Linux 服务器从 Ubuntu 10.04 迁移到更新的服务器 12.04
  • 该服务器负责通过 crontabs 执行多个 Perl 模块。
  • 这些 Perl 模块严重依赖 30-40 个 perl 扩展。
  • 我已经安装了所有 Perl 扩展,并且 crontab 能够成功处理,除了这些 Perl 扩展的较新版本导致的几个语法错误。
  • 我需要一些帮助来修改语法以使 Perl 脚本按预期进行处理。

错误:

defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 943.
        (Maybe you should just omit the defined()?)
defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 944.
        (Maybe you should just omit the defined()?)

代码:

###
    # Iterate the arrays deleting identical counts from each.
    # If we found a mismatch then die.
    # If either array is not empty when we are done then die
    $logger->info('Comparing ' . (scalar keys %cms_rows) . ' CMS symbols to ' . (scalar keys %stats_rows) . ' STATS symbols');

    foreach my $symbol ( keys %cms_rows ) {
    my %cms_row = delete $cms_rows{$symbol};
    my %stats_row = delete $stats_rows{$symbol};

##LINE 943##    die("Error: NULL CMS counts for symbol '$symbol'") unless defined %cms_row;
##LINE 944##    die("Error: NULL Stats counts for symbol '$symbol'") unless defined %stats_row;

    my $cms_json = encode_json(\%cms_row);
    my $stats_json = encode_json(\%stats_row);
    $logger->debug("Comparing counts for '$symbol': CMS($cms_json), Stats($stats_json)");

    die("Error: Up Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{1} && !defined $stats_row{1}) || $cms_row{1} == $stats_row{1};
    die("Error: Down Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{-1} && !defined $stats_row{-1}) || $cms_row{-1} == $stats_row{-1};
    }
    ###

希望有人可以提供帮助,我们将不胜感激。

最佳答案

您必须从非常旧的 Perl 版本升级。 Perl 5.6.1 release notes说:

defined(%hash) is deprecated

(D) defined() is not usually useful on hashes because it checks for an undefined scalar value. If you want to see if the hash is empty, just use if (%hash) { # not empty } for example.

这总是一件非常愚蠢的事情,Perl 现在会警告您您正在做一些愚蠢的事情。该警告非常清楚您应该如何解决此问题:

Maybe you should just omit the defined()?

所以你的台词会变成:

die("Error: NULL CMS counts for symbol '$symbol'") unless %cms_row;
die("Error: NULL Stats counts for symbol '$symbol'") unless %stats_row;

关于linux - Perl 模块错误 - defined(%hash) 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499393/

相关文章:

c - 从 C 程序中执行程序

mysql - 在 Windows-7-x64 上使用 DBI Perl 和 MySql 未定义 $DBI::errstr

python - 如何将数字更改为科学数字并获得最小值(科学数字格式)

java - 为什么在调整 HashTable 实现大小时会出现 OutOfMemoryError 错误?

从另一个进程关闭 XLib 应用程序

linux - ELF 程序头 : MemSiz vs. FileSiz

c++ - Linux C/C++ : Get ifconfig wlan0 up output

Perl6 : How could I make all warnings fatal?

python - 旧的 python 散列从左到右完成——为什么不好?

Javascript:对象数组的哈希(使用扑克牌示例)