替换已弃用的“已定义(@array)";在三元运算中 [英] Replacing deprecated "defined(@array)" in a ternary operation

查看:29
本文介绍了替换已弃用的“已定义(@array)";在三元运算中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码需要更正,因为 defined(@array) 在最新的 Perl 中已被弃用.

I have the following code which needs to be corrected, as defined(@array) is deprecated in the latest Perl.

my @inputs = (
    ( defined @{$padSrc->{inouts}} ? @{$padSrc->{inouts}} : () ),
    ( defined @{$padSrc->{inputs}} ? @{$padSrc->{inputs}} : () )
);

我不想转向 if ( @{ $padSrc->{inouts} } ) 那种方法,因为这会增加我的行数.

I don't want to move to if ( @{ $padSrc->{inouts} } ) kind of approach, as that will increase my line count.

推荐答案

defined(@array) 从来没有像它看起来的那样做.它总是只返回 @array 是否为非空.

defined(@array) has never done what it looks like it does. It always just returns whether @array is non-empty.

perl5.10.1 -E 'say defined(@foo)'                # ""
perl5.10.1 -E '@foo=(); say defined(@foo)'       # ""
perl5.10.1 -E '@foo=(42); say defined(@foo)'     # "1"
perl5.10.1 -E '@foo=(undef); say defined(@foo)'  # "1"

在代码中测试 defined(@array) 的任何地方,都可以将其替换为@array != 0scalar(@array) 并且您的代码将完全相同(加上或减去一些弃用警告).

Anywhere that you test defined(@array) in your code, you can replace it with @array != 0 or scalar(@array) and your code will work exactly the same (plus or minus some deprecation warnings).

作为 if (condition) ... 还是 (condition) ?expr1 : expr2while (condition) 总是在标量上下文中评估 conditionscalar 在这些结构中是可选的,并且你可以用 if (@foo) 替换,比如 if (defined(@foo)).

As if (condition) ... or (condition) ? expr1 : expr2 or while (condition) always evaluate condition in scalar context, the scalar is optional in these constructions, and you can replace, say if (defined(@foo)) with if (@foo).

这篇关于替换已弃用的“已定义(@array)";在三元运算中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆