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

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

概述我读过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 hap 我读过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 Listprint fileHANDLEprint List

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

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

my $line = <$fh>;

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

print <$fh>;

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

print scalar <$fh>;

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

print @List,scalar <$fh>,@List2;

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

总结

以上是内存溢出为你收集整理的请帮助我理解“Programming Perl”一书中的片段全部内容,希望文章能够帮你解决请帮助我理解“Programming Perl”一书中的片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1265178.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存