加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – “$scalar = @array [n]”的后果是什么?

发布时间:2020-12-16 18:26:36 所属栏目:安全 来源:网络整理
导读:use warnings;my @array = (0,1);my $scalar1 = $array[0];my $scalar2 = @array[0];if($scalar1 == $scalar2) { print "scalars are equaln";} 这是运行/usr/bin/perl5.10.1 test.pl时的输出: Scalar value @array[0] better written as $array[0] at tes
use warnings;
my @array = (0,1);
my $scalar1 = $array[0];
my $scalar2 = @array[0];
if($scalar1 == $scalar2) {
    print "scalars are equaln";
}

这是运行/usr/bin/perl5.10.1 test.pl时的输出:

Scalar value @array[0] better written as $array[0] at test.pl line 4.
scalars are equal

我很担心这个警告.

解决方法

您可以在 perldoc perldiag中查找所有警告消息,这解释了后果:

(W syntax) You’ve used an array slice (indicated by @) to select a
single element of an array. Generally
it’s better to ask for a scalar value
(indicated by $). The difference is
that $foo[&bar] always behaves like a
scalar,both when assigning to it and
when evaluating its argument,while
@foo[&bar] behaves like a list when
you assign to it,and provides a list
context to its subscript,which can do
weird things if you’re expecting only
one subscript.

On the other hand,if you were actually hoping to treat the array
element as a list,you need to look
into how references work,because Perl
will not magically convert between
scalars and lists for you. See
perlref.

同样,您可以use diagnostics;获取警告消息的详细说明.

第三种方法是使用splain实用程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读