在标量上下文中是否存在诸如列表之类的东西? [英] Is there such a thing as a list in scalar context?

查看:102
本文介绍了在标量上下文中是否存在诸如列表之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my $mind = ( 'a', 'little', 'confused' );


这是因为 ikegami 坚持认为

那么,它是否为列表?

解决方案

列表文字实际上是用代码写出的列表,因此(1, 2, 3)是列表文字,而例如caller是一个函数可能会根据上下文返回列表或标量.

在这样的行中:

my $x = ...;

...看到标量上下文,因此,如果...是列表文字,那么您将在标量上下文中具有列表文字:

my $x = (1, 2, 3);

但是列表文字不会生成列表,因为其中包含的逗号运算符会看到标量上下文,然后导致标量上下文返回列表文字的最后一项,并在对它们进行求值后将其丢弃. >

就函数而言,函数本身可以看到从其调用的任何上下文,然后将其传播到该函数返回的任何行中.因此,您可以在标量,列表或void上下文中使用一个函数,并且如果该子句的最后一行恰好是列表文字,则该列表文字将看到这些上下文中的任何一个,并且行为适当.

因此,基本上这是术语上的区别,list literal指的是实际源代码*中逗号分隔的值列表,而list指的是放置在perl堆栈上的一系列值.

您可以编写具有返回值的子例程,这些子例程的行为与数组或上下文有关的列表文字一样.

sub returns_like_array  {my @x = 1..5; return @x}

sub returns_like_list   {my @x = 1..5; return @x[0 .. $#x]}

*或导致逗号分隔值列表的内容,例如qw()或粗逗号=>或哈希或数组切片.

您也可以在这里查看我的答案:perldoc perlfaq4 explains the line above as follows (emphasis added):

Since you're assigning to a scalar, the righthand side is in scalar context. The comma operator (yes, it's an operator!) in scalar context evaluates its lefthand side, throws away the result, and evaluates it's righthand side and returns the result. In effect, that list-lookalike assigns to $scalar it's rightmost value. Many people mess this up because they choose a list-lookalike whose last element is also the count they expect:

my $scalar = ( 1, 2, 3 );  # $scalar gets 3, accidentally

What I understand this to mean is that there is no such thing as a list in scalar context.

However, ikegami maintains that it "result[s] in a list operator, so it is a list literal."

So, is it a list or not?

解决方案

A list literal is something that is actually a list written out in code, so (1, 2, 3) is a list literal, whereas caller for example is a function that could return a list or a scalar depending on context.

In a line like:

my $x = ...;

the ... sees scalar context, so if ... was a list literal, then you would have a list literal in scalar context:

my $x = (1, 2, 3);

but the list literal does not result in a list, because the comma operator it contains sees scalar context, which then results in it returning the last item of the list literal, and throwing the remaining values away after evaluating them.

In terms of a function, the function itself sees whatever context it is called from, which then is propagated to any line in that function that returns. So you can have a function in scalar, list, or void context, and if the last line of that sub happens to be a list literal, that list literal will see any of those contexts and will behave appropriately.

So basically this is a terminology distinction, with list literal referring to a comma separated list of values in the actual source code*, and list referring to a sequence of values placed onto perl's stack.

You can write subroutines with return values that either behave like arrays or like list literals with regard to context.

sub returns_like_array  {my @x = 1..5; return @x}

sub returns_like_list   {my @x = 1..5; return @x[0 .. $#x]}

*or something that results in a list of comma separated values, like a qw() or a fat comma => or a hash or array slice.

You can also look at my answer here: How do I get the first item from a function that returns an array in Perl? which goes into a bit more detail about lists.

这篇关于在标量上下文中是否存在诸如列表之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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