空列表可以在标量上下文中吗? [英] Can the empty list be in scalar context?

查看:92
本文介绍了空列表可以在标量上下文中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个谎言是,标量上下文中的列表会产生列表的最后一个元素.这是一个谎言,因为(俗话说)您不能在标量上下文中有一个列表.在标量上下文中看起来像一个列表,实际上是标量上下文中的逗号运算符,并且在标量上下文中具有不同的行为.

There is a lie that a list in scalar context yields the last element of the list. This is a lie because (as the saying goes) you can't have a list in scalar context. What looks like a list in scalar context is really the comma operator in scalar context and it has different behavior in scalar context.

但是,这种逻辑似乎存在一个漏洞:空列表(有时称为空列表).字符()定义为perldoc perlglossary 的空列表.构造

However, there seems to be a loop hole in this logic: the null list (sometimes called the empty list). The characters () are defined to be the null list by perldoc perlglossary. The construct

my $s = ();

是有效的代码,并将undef返回到$s. perldoc中的任何地方似乎都没有对此进行记录(我没有检查过Camel),但是上面有很多代码,所以我认为它会保留下来.

is valid code and returns undef to $s. This does not appear to be documented anywhere in perldoc (I haven't checked the Camel), but lots of code counts on it, so I think it is here to stay.

现在序言已经完成了,这是一个问题:如果我们不能在标量上下文中有一个列表,那么在标量上下文中我们怎么称呼空列表以及不将其称为列表的合理性是什么(从那里开始)标量上下文中没有逗号)?

Now that the preamble is done, here is the question: if we cannot have a list in scalar context, then what do we call the empty list in scalar context and what is the rational for not calling it a list (since there are no commas to be in scalar context)?

如果您喜欢这个问题,则可能还喜欢讨论正在P5P中进行.

If you are enjoying this question, you may also like the discussion going on in P5P.

推荐答案

列表是一个非常通用的词.您可能引用的是列表运算符或列表值.

List is a very generic word. You could possibly be referring to the list operator or to a list value.

代码中没有逗号,因此没有列表运算符.

There is no comma in the code, so there is no list operator.

代码中没有列表上下文,因此没有列表值.

There is no list context in the code, so there is no list value.

因此,

my $s = ();

括号永远不会创建列表

(仅在赋值运算符的LHS上是间接的.)

(Only indirectly when on the LHS of an assignment operator.)

在标量上下文中我们怎么称呼空列表
what do we call the empty list in scalar context

Perl称它为存根"(如下所示),这确实是它的意思.它是代码中的占位符,实际上不允许放置任何内容.

Perl calls it a "stub" (as shown below), and that's truly what it is. It's a placeholder in the code where putting literally nothing would be disallowed.

存根由空括号"表示,因此它是另一个名称.

The stub is represented by "empty parentheses", so that's another name for it.

我称它为错误代码.如果要分配undef,请分配undef.

I call it bad code. If you want to assign undef, assign undef.

有一个谎言,一个标量上下文中的列表产生了列表的最后一个元素.
There is a lie that a list in scalar context yields the last element of the list.

不,是的.列表值不能在标量上下文中存在,因此可以使用列表运算符.

No, that's true. List values cannot exist in scalar context, so that leaves the list operator.

列表运算符又称为逗号运算符,返回标量上下文中列表的最后一个元素.

The list operator aka the comma operator returns the last element of the list in scalar context.

比较以下内容.没有提及清单:

Compare the following. No mention of list:

>perl -MO=Concise -e"my $s = ();"
6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
5     <2> sassign vKS/2 ->6
3        <0> stub sP ->4
4        <0> padsv[$s:1,2] sRM*/LVINTRO ->5
-e syntax OK

提到列表

>perl -MO=Concise -e"my @a = ();"
7  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
6     <2> aassign[t2] vKS ->7
-        <1> ex-list lK ->4
3           <0> pushmark s ->4
-           <0> stub lP ->-
-        <1> ex-list lK ->6
4           <0> pushmark s ->5
5           <0> padav[@a:1,2] lRM*/LVINTRO ->6
-e syntax OK

...与父母无关

>perl -MO=Concise -e"my @a = 's';"
8  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
7     <2> aassign[t2] vKS ->8
-        <1> ex-list lK ->5
3           <0> pushmark s ->4
4           <$> const[PV "s"] s ->5
-        <1> ex-list lK ->7
5           <0> pushmark s ->6
6           <0> padav[@a:1,2] lRM*/LVINTRO ->7
-e syntax OK

这篇关于空列表可以在标量上下文中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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