“$scalar = @array[n]”的后果是什么?
use warnings;
my @array = (0, 1);
my $scalar1 = $array[0];
my $scalar2 = @array[0];
if($scalar1 == $scalar2) {
print "scalars are equal\n";
}
这是我运行 /usr/bin/perl5.10.1 test.pl
时的输出:
Scalar value @array[0] better written as $array[0] at test.pl line 4.
scalars are equal
我担心这个警告。
use warnings;
my @array = (0, 1);
my $scalar1 = $array[0];
my $scalar2 = @array[0];
if($scalar1 == $scalar2) {
print "scalars are equal\n";
}
Here's the output when I run /usr/bin/perl5.10.1 test.pl
:
Scalar value @array[0] better written as $array[0] at test.pl line 4.
scalars are equal
I'm concerned about that warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 perldoc perldiag 中查找所有警告消息,其中解释了后果:
同样,您可以使用诊断来获取警告消息的详细解释。
第三种方法是使用 splain 实用程序。
You can look up all warning messages in perldoc perldiag, which explains the consequences:
Similarly, you can use diagnostics; to get this verbose explanation of the warning message.
A third way is to use the splain utility.
可以获取单个元素的数组切片:
但这通常意味着您犯了一个错误,Perl 会警告您您真正做的是什么
应该写的是:
It is possible to take an array slice of a single element:
but this usually means that you’ve made a mistake and Perl will warn you that what you really
should be writing is:
该用法不会产生任何后果。我认为目的是帮助你避免无法发出警告时产生的后果。
“=”的 LHS 上的切片导致“=”成为列表赋值运算符。
切片评估列表上下文中的索引。
There are no consequences for that usage. I think the purpose is to help you avoid the consequences when a warning can't be issued.
Slices on the LHS of "=" cause =" to be a list assignment operator.
Slices evaluate the index in list context.