【问题标题】:Perl Useless use of (/) in void contextPerl 在无效上下文中无用地使用 (/)
【发布时间】:2022-12-17 12:49:37
【问题描述】:

当我尝试这段代码时。

use strict;
use warnings;
print (44+66)/2;

它给了我。

在 p.pl 第 3 行的 void 上下文中无用地使用除法 (/)。.

我试着上网寻找线索,但无济于事。

但是经过一些尝试,它通过在表达式 like 周围添加额外的外括号来工作。

use strict;
use warnings;
print ((44+66)/2);

它确实有效!

所以请如果有人能解释为什么!?

提前致谢

发布脚本

为了增加错误,如果我尝试它也有效

use strict;
use warnings;
print (44+66)*2;

即,将除法改为乘法。

【问题讨论】:

  • 我认为 print (44+66)*2 被解释为 (print(44+66)) / 2 这就是为什么您会收到“在 void 上下文中无用地使用除法”错误

标签: perl


【解决方案1】:

使用 Deparse 将为您提供有关 perl 如何解析代码的更多信息:

perl -MO=Deparse p.pl
Useless use of division (/) in void context at p.pl line 3.
use warnings;
use strict;
print(110) / 2;
p.pl syntax OK

在这里您可以看到括号中的表达式按预期计算 (66+44 = 100)。但是,然后 perl 将 100 作为输入传递给 print,然后尝试将 print 的输出除以 2。

另一个有用的工具是diagnostics

perl -Mdiagnostics p.pl
Useless use of division (/) in void context at p.pl line 3 (#1)
    (W void) You did something without a side effect in a context that does
    nothing with the return value, such as a statement that doesn't return a
    value from a block, or the left side of a scalar comma operator.  Very
    often this points not to stupidity on your part, but a failure of Perl
    to parse your program the way you thought it would. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    相关资源
    最近更新 更多