什么是&QUOT的后果; $ =标@array [N] QUOT ;? [英] What are the consequences of "$scalar = @array[n]"?

查看:104
本文介绍了什么是&QUOT的后果; $ =标@array [N] QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

您可以查找的perldoc perldiag ,这也解释了后果:

You can look up all warning messages in perldoc perldiag, which explains the consequences:

(W语法)你已经使用数组片(由@表示)来选择
  阵列的单个元件。通常
  最好是要求标量值
  (由$表示)。所不同的是
  即$ foo的[&放大器;巴]总是表现得像一个
  标量,既当分配给它,并
  当评估它的参数,而
  @foo [&放大器;巴]就像一个列表时
  分配给它,并提供了一​​个列表
  上下文其下标,其可以做
  奇怪的事情,如果你只是希望
  了一个下标。

(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.

在另一方面,如果实际上希望治疗阵列
  元素作为一个列表,你需要看看
  到引用是如何工作的,因为Perl
  不会神奇地转换之间
  标量和名单为您服务。看到
  perlref。

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.

同样,您可以使用诊断; 得到警告信息这冗长的解释

Similarly, you can use diagnostics; to get this verbose explanation of the warning message.

第三种方法是使用 splain 工具。

A third way is to use the splain utility.

这篇关于什么是&QUOT的后果; $ =标@array [N] QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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