我刚刚设置了一个新的开发环境,并从 Centos 7 (perl 5.16 v16) 转到 RHEL 8.5 (perl 5.26.3)。话虽如此,我无法弄清楚这个旧子程序中的定义语句的新语法。任何有关最后一行的帮助将不胜感激。我就是无法让它工作:
my $flags_loop = db()->retrieveSet(table => "flags",order=>["flag"],dir=>'DESC');
my $outflags;
foreach my $flag (@$flags_loop) {
my $flagsize = db()->count(
table => "songflag",
where => { flagid => $flag->{flagid} } );
$flag->{flagsize} = $flagsize;
push (@$outflags, $flag);
}
# in case no flags yet defined
# 5.26 incompatible
if (!defined(@$outflags)) { $outflags=[]; }
最佳答案
非标量事物上的定义
从来都不像人们想象的那样有效,或者至少人们在 DWIM 中定义了与定义
的原始意图不同的含义。
define
在 Perl 5.005 中已被弃用,但与许多弃用一样,它实际上并没有消失。最终,v5.16(您之前使用的版本)认真对待了它(不,这次是真的!)并添加了一个弃用警告,即使没有启用警告,您也会收到该警告。该警告告诉您想要执行的操作:
$ perl5.16.3 -e 'defined(@ARGV)'
defined(@array) is deprecated at -e line 1.
(Maybe you should just omit the defined()?)
没有新语法:只是您根本不使用的旧语法。 Perl 现在在多个版本的弃用警告方面非常擅长,因为开发人员非常认真地删除了几十年来已列出的所有内容。
使用diagnostics
给出了错误的详细解释,告诉您如果要检查数组是否为空该怎么做。
defined(@array) is deprecated at -e line 1 (#1)
(D deprecated) defined() is not usually useful on arrays because it
checks for an undefined scalar value. If you want to see if the
array is empty, just use if (@array) { # not empty } for example.
(Maybe you should just omit the defined()?)
即便如此,这个功能在 Perl 中一直存在到 v5.22。该警告告诉您该怎么做:
$ perl5.32.0 -e 'defined(@ARGV)'
Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at -e line 1.
关于Perl 5.32 定义语句语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70999905/