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

请帮助我理解“Programming Perl”一书中的片段

发布时间:2020-12-15 23:23:51 所属栏目:大数据 来源:网络整理
导读:我读过By Tom Christiansen,Brian d foy,Larry Wall,Jon Orwant的“Programming Perl”.有以下文字我无法理解(我不明白的确切地方标有粗体): What you really want to know is which operators supply which context to their operands. As it happens,you
我读过By Tom Christiansen,Brian d foy,Larry Wall,Jon Orwant的“Programming Perl”.有以下文字我无法理解(我不明白的确切地方标有粗体):

What you really want to know is which operators supply which context
to their operands. As it happens,you can easily tell which ones
supply list context because they all have LIST in their syntactic
descriptions
. Everything else supplies scalar context. Generally,
it’s quite intuitive. If necessary,you can force scalar context
onto an argument in the middle of a LIST
by using the scalar
pseudofunction. Perl provides no way to force list context in context,
because anywhere you would want list context it’s already provided
by the LIST
of some controlling function.

为方便起见,我想提出以下问题:

>片段中的LIST是什么意思?
>什么是句法描述? (似乎是某种文档)
>下一个文字是什么意思:

you can force scalar context
onto an argument in the middle of a LIST

解决方法

这很简单,就像文字说的那样.看一下perldoc -f print,例如:

print FILEHANDLE LIST
print FILEHANDLE
print LIST

就像它在那里说的那样,print接受LIST参数,这意味着在print之后发布的任何内容都在列表上下文中.对于任何将参数表示为LIST的函数,它都是相同的.

使用标量函数,您可以覆盖此列表上下文,以便不在列表上下文中评估您的参数.例如,文件句柄readline语句,例如:

my $line = <$fh>;

在标量上下文中进行评估,因为$line是标量.这意味着只读取一行并将其放入变量中.但是,如果你这样做:

print <$fh>;

readline位于列表上下文中,这意味着将读取文件中的所有剩余行.您可以通过将readline语句放在标量上下文中来覆盖它:

print scalar <$fh>;

然后你会读一行.更准确地说,您可以在列表中间强制执行标量上下文:

print @list,scalar <$fh>,@list2;

这可能是你的引用所指的内容.

(编辑:李大同)

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

    推荐文章
      热点阅读