mlists一位大侠对上下文概念的阐述
http://bbs.chinaunix.net/viewthread.php?tid=907104
>@string_array = <STDIN>;
>
>@string_array = reverse(@string_array);
>print("\n@string_array\n");
>print("\n" . reverse(@string_array) . "\n");
>
>
>If this is run, the first print statement prints out the lines in the
>opposite order they were entered, however the second print statement
>reverses the entire line one at a time. I do not understand what is
>going on.
That is a very good question, and it goes right to the heart of a very important and unusual feature of the Perl language, so it'll take a little while to explain. Please bear with me through this explanation of one of Perl's central concepts.
Every Perl expression is in one of two `contexts', either `list context' or `scalar context', depending on whether it is expected to produce a list or a scalar. Many expressions have quite different behaviors in list context than they do in scalar context.
Here is a typical expression:
EXPRESSION
Here it is again, in a list context:
@array = EXPRESSION; # Expression is expected to produce a list
Here is how it looks in a scalar context:
$scalar = EXPRESSION; # Expression is expected to produce a scalar
The values that a particular expression has in these two contexts might be quite different. For example, consider this simple expression:
@q
In list context, this produces a list of the items in the array @q, but in scalar context it produces the number of items in the array @q.
@a = @q; # @q in list context produces a list of items from @q
$s = @q; # but in scalar context, it produces the number of items.
Similarly,
# Note two very different behaviors of // depending on context
# Put ('123.45', 'immediately') into @a
@a = /The amount was \$(\d+\.\d\d)\. You must pay ([\w\s]+)\./
# Put 1 into $s, put '123.45' into $1, put 'immediately' into $2
$s = /The amount was \$(\d+\.\d\d)\. You must pay ([\w\s]+)\./
The main point here: An expression's behavior can be drastically different in list context than it is in scalar context.
Now we'll return to your example.
> @string_array = reverse(@string_array);
Here, reverse is in list context, because it's being asked to produce a list to assign to @string_array. In list context, reverse reverses the order of its arguments, so that the lines come out in the reverse order.
Then you did
> print("\n" . reverse(@string_array) . "\n");
Now, here reverse is in scalar context, because it is being asked to produce a scalar value, a string, which will be concatenated with \n on the front and back. In a scalar context, reverse first concatenates all its arguments, to get a single string, and then reverses that string. As you know by now.
How can you get the behavior you really want? The reverse here needs to be in a list context. The argument list of a function like `print' is always a list context, so if you get rid of the concatenation operators that are providing scalar context to `reverse', your problem will go away:
print "\n", reverse(@string_array), "\n";
reverse is now expected to return a list, which will be inserted into the list which is passed to print. This is probably more efficient than what you were trying to do anyway, since print can print out the many strings without bothering to concatenate them first, as you were doing.
Again, the general rule is:
There is no general rule for deducing a function's behavior in scalar context from its behavior in list context.
This is the cause of a lot of common errors. One mistake you didn't make, but which we see a lot is something like this:
@a = (reverse("part"));
The programmer was expecting @a to get a list containing "trap", but instead they got a list containing "part" again. Because reverse was in a list context, it reversed the order of its arguments, instead of reversing the arguments themselves, and since it only had one argument, it ended up doing nothing.
Perl has a special operator for just such circumstances. It's called scalar, and it forces its argument to be evaluated in scalar context. The last example should have been written as:
@a = (scalar(reverse("part")));
# @a now contains ("trap")
I hope this clears things up for you.
分享到:
相关推荐
这些工具通常通过替换关键词、调整句子顺序、插入同义词等方式来改写已有文章,从而产生新的文本,使得在搜索引擎看来,这些文章具有较高的独特性和价值。 描述中提到的“优化的文章更被搜索引擎所青睐”意味着此类...
正如文章所言,“傻傻地坚持,才能看到牛逼的结果”。成功往往是留给那些愿意比别人多付出一份努力的人。有时候,成功可能就在下一次尝试之后,如果我们轻言放弃,就可能永远错过它。因此,不断努力,不畏艰难,是...
做一个好的程序员,困难而... 但在好的程序员看来,相 比构建一个让客户和各种各样的同事都满意的软件系统,(纯粹的)编程真的只是小孩子的 玩意。在这篇文章里,尝试尽可能简洁地总结那些作者21岁时,希望别人告诉自己的事。
从网上找到了这边文章: TortoiseSVN 客户端修改密码。 基本上按照步骤做了,但是启动时说mod_alias.so、mod_cgi.so有问题,原来是版本不对。我按照的VisualSVNServer是3.9版,里面用的Apache HTTPServer是2.4.35...
第二篇文章描述了一位父亲为了家庭的未来选择离别的场景,这让人不禁深思。父母之爱是无私的,他们愿意为了子女的未来做出牺牲。尽管离别带来深刻的痛苦和不舍,但这样的选择却也体现了父母的责任感和对子女未来...
同时,批评也是爱的一种表现,它提醒我们哪里做得不够好,需要改进。在父母的批评中,我们学会了自我反省,学会了在错误中寻找成长的机会。 家庭教育的重要性不言而喻。我们所接受的教育,很大一部分来源于父母。以...
《初中语文文摘生活晚年唯好静》这篇文章深刻地剖析了现代人对于宁静生活的向往,特别是晚年生活中对平静的渴望,以及这一追求与现实生活的喧嚣和忙碌之间的矛盾和对比。 首先,文章通过观察和描述老年人日常生活的...
为了确保高分,考生需要把握好文章的主旨和段落结构。 一篇优秀的短文应该具备明确的主旨句和主题句。主旨句是整篇文章的灵魂,而主题句则代表每个段落的核心思想。在提纲式的写作中,段落的构成通常包括主题句、...
这将需要额外的工作(除了听课和解决作业),但在我看来,这确实是值得的。 一般来说,你不会因为聪明而在编程或学术界取得好成绩,但是 . 我鼓励阅读的文章 故事 了解一些背景信息总是好的。 以下是我自己喜欢的...
在这本书里的许多想法都是作者在过去的文章或信函中表达过的。而且,如果由职业作家来写本书,语言以及文字一定能更加流畅、更加生动。但是,作者自己总有一种提笔写作的冲动,因为作者相信自己在青年一代中有一定的...
花时间看了一下相关资料(google真好,外果仁真屌),看来这部分也已经有比较完善的方法论+工具了。所以这篇文章记录一下自己从不懂到入门的经历~~我希望这篇文章不仅能提供具体的工具供大家使用,还提供足够的理论...
他认为,保持古寺的原始状态,以最朴素无华的方式呈现,才能够更好地传达其深远的文化意义,让后人领略到时间的沉淀和历史的厚重。 《古寺黄昏,朱以撒阅读附答案》是一篇集描绘、思考和感悟于一体的文章。朱以撒...
【标题】六年级阅读专项训练句子理解 【描述】该文档是针对六年级学生的阅读理解专项训练,重点在于理解...通过这些训练方法,六年级的学生能够提高阅读理解能力,更好地解读文章中的重要句子,从而提升整体阅读水平。
傻瓜函数编程 这篇文章是原文《》的...看来还是“自己动手,丰衣足食”,我最终还是开始自己翻译这篇文章。 这个版本在追求“信达雅”的同时,希望在不改变原意的前提下用中文语气/语境来进行本土化表达。唯一的例外
5. **对比手法**:“在山脚看来,其它的山峰好似高大的巨人,但在清凉山的山顶看来,只不过是一个个小小的土包”,这种对比手法强化了清凉山的高度和雄伟,也展示了作者的视角变化。 6. **结构与层次**:文章从山脚...
上一篇文章就介绍了一款免费软件MobaXterm,但菜单都是英文的,而且终端显示编码不支持GB-2312,导致中文乱码。今天就再推荐一款自由(free)软件 —— WindTerm ,说它是自由软件,是因为它在Github上开放源码了,...
一篇好的文章开头需要清晰地呈现文章的主题(Subject)以及作者的观点(Opinion),这样才能让读者明白你要讨论的问题和你的立场。 【什么是好的开头】 如材料中所述,一个好的托福独立写作开头应包含两个主要元素...
在他看来,书籍并非冰冷的存在,它们充满了生命的温度,以其独特的魅力吸引着每一位热爱知识的读者。他分享自己把书当作朋友,这不仅仅是一种比喻,更是一种情感的寄托。每翻开一本书,都像是在与一位智者对话,聆听...
但看到Phalcon框架如此优秀,在学习后就想和大家分享,但发现国内的人几乎没有使用的,故想翻译一下,一旦翻译才发现读懂和译出一篇好文章真的不太一样。 故前一期翻译的部分章节有点生硬,等有空的时候再回头重译吧...
大学英语期末考试写作必背范文 本文档提供了三篇大学英语期末考试写作必背范文,...这三篇文章提供了关于买房与租房、跳槽和在线社交网络的深入讨论和分析,帮助我们更好地理解这些主题,并提供了有价值的建议和观点。