perl:为什么引用匿名数组标量?

时间:2021-01-20 05:26:59

I haven't programmed in Perl in over 10 years so maybe this is something obvious to more experienced Perl programmers. I searched for an answer but didn't find anything.

我没有在Perl中编程超过10年,所以对于更有经验的Perl程序员来说这可能是显而易见的。我搜索了一个答案,但没有找到任何答案。

My question is: why are references to anonymous arrays scalar?

我的问题是:为什么引用匿名数组标量?

For example in the following code:

例如,在以下代码中:

#!/usr/bin/perl

use strict;
use feature qw(say);

my @array1 = ('one');
say 'array ref 1: ' . \@array1;
my @array2 = ('one', 'two');
say 'array ref 2: ' . \@array2;
say 'array ref 3: ' . \('one');
say 'array ref 4: ' . \('one', 'two');

exit 0;

The result is:

结果是:

array ref 1: ARRAY(0x1e1b1c0)
array ref 2: ARRAY(0x1e1b190)
array ref 3: SCALAR(0x1e1b280)
array ref 4: SCALAR(0x1e10c40)

Why are array ref 3 and array ref 4 scalar?

为什么数组引用3和数组引用4标量?

3 个解决方案

#1


14  

All references are scalars. When you stringify a reference, it includes the type of what it's referencing. That means you have a reference to a scalar.

所有引用都是标量。对字符串化引用时,它包括引用的类型。这意味着你有一个标量的引用。

[ ] is the operator that constructs an array. ( ) doesn't create any arrays.

[]是构造数组的运算符。 ()不会创建任何数组。

You want

你要

say 'array ref 3: ' . ['one'];
say 'array ref 4: ' . ['one', 'two'];

Normally, parens just change precedence. If that were true here,

通常情况下,parens只会改变优先权。如果这是真的,

\('one', 'two')

would be equivalent to

相当于

('one', \'two')

That's because a comma/list operator in scalar context normally returns that to which its last expression evaluates. But \(...) is special-cased to be equivalent to the following more useful construct:

这是因为标量上下文中的逗号/列表运算符通常会返回其最后一个表达式计算的值。但\(...)是特殊的,等同于以下更有用的结构:

(\'one', \'two')

Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

引用枚举列表与使用方括号不同 - 而是与创建引用列表相同!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c);      # same thing!

That means that

这意味着

say 'array ref 4: ' . \('one', 'two');

is equivalent to

相当于

say 'array ref 4: ' . (\'one', \'two');

which is equivalent to

这相当于

say 'array ref 4: ' . \'two';

#2


8  

From perlref:

来自perlref:

Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

引用枚举列表与使用方括号不同 - 而是与创建引用列表相同!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c);      # same thing!

You get a scalar reference because the item in the list that ends up with its reference passed to the LHS is a scalar.

您会得到一个标量引用,因为列表中以其引用传递给LHS的项目是标量。

#3


3  

It seems as if you expect that these are equivalent:

看起来好像你期望它们是等价的:

my @array1 = ('one', 'two');
my $array2 = \('one', 'two');

But that is not the correct syntax to get an anonymous array. You need square brackets to create an anonymous array.

但这不是获取匿名数组的正确语法。您需要使用方括号来创建匿名数组。

my $array3 = ['one', 'two'];  # a reference to an anonymous array
my $array4 = \['one', 'two']; # a reference to a reference to an anonymous array

#1


14  

All references are scalars. When you stringify a reference, it includes the type of what it's referencing. That means you have a reference to a scalar.

所有引用都是标量。对字符串化引用时,它包括引用的类型。这意味着你有一个标量的引用。

[ ] is the operator that constructs an array. ( ) doesn't create any arrays.

[]是构造数组的运算符。 ()不会创建任何数组。

You want

你要

say 'array ref 3: ' . ['one'];
say 'array ref 4: ' . ['one', 'two'];

Normally, parens just change precedence. If that were true here,

通常情况下,parens只会改变优先权。如果这是真的,

\('one', 'two')

would be equivalent to

相当于

('one', \'two')

That's because a comma/list operator in scalar context normally returns that to which its last expression evaluates. But \(...) is special-cased to be equivalent to the following more useful construct:

这是因为标量上下文中的逗号/列表运算符通常会返回其最后一个表达式计算的值。但\(...)是特殊的,等同于以下更有用的结构:

(\'one', \'two')

Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

引用枚举列表与使用方括号不同 - 而是与创建引用列表相同!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c);      # same thing!

That means that

这意味着

say 'array ref 4: ' . \('one', 'two');

is equivalent to

相当于

say 'array ref 4: ' . (\'one', \'two');

which is equivalent to

这相当于

say 'array ref 4: ' . \'two';

#2


8  

From perlref:

来自perlref:

Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

引用枚举列表与使用方括号不同 - 而是与创建引用列表相同!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c);      # same thing!

You get a scalar reference because the item in the list that ends up with its reference passed to the LHS is a scalar.

您会得到一个标量引用,因为列表中以其引用传递给LHS的项目是标量。

#3


3  

It seems as if you expect that these are equivalent:

看起来好像你期望它们是等价的:

my @array1 = ('one', 'two');
my $array2 = \('one', 'two');

But that is not the correct syntax to get an anonymous array. You need square brackets to create an anonymous array.

但这不是获取匿名数组的正确语法。您需要使用方括号来创建匿名数组。

my $array3 = ['one', 'two'];  # a reference to an anonymous array
my $array4 = \['one', 'two']; # a reference to a reference to an anonymous array