`

网上看来的好文章

    博客分类:
  • Perl
阅读更多

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.

分享到:
评论

相关推荐

    免费伪原创工具,发文章软件,发博客软件

    这些工具通常通过替换关键词、调整句子顺序、插入同义词等方式来改写已有文章,从而产生新的文本,使得在搜索引擎看来,这些文章具有较高的独特性和价值。 描述中提到的“优化的文章更被搜索引擎所青睐”意味着此类...

    how-to-be-a-programmer-cn

    做一个好的程序员,困难而... 但在好的程序员看来,相 比构建一个让客户和各种各样的同事都满意的软件系统,(纯粹的)编程真的只是小孩子的 玩意。在这篇文章里,尝试尽可能简洁地总结那些作者21岁时,希望别人告诉自己的事。

    mod_cgi.so

    从网上找到了这边文章: TortoiseSVN 客户端修改密码。 基本上按照步骤做了,但是启动时说mod_alias.so、mod_cgi.so有问题,原来是版本不对。我按照的VisualSVNServer是3.9版,里面用的Apache HTTPServer是2.4.35...

    同等学力研究生英语考试短文写作技PPT课件.pptx

    为了确保高分,考生需要把握好文章的主旨和段落结构。 一篇优秀的短文应该具备明确的主旨句和主题句。主旨句是整篇文章的灵魂,而主题句则代表每个段落的核心思想。在提纲式的写作中,段落的构成通常包括主题句、...

    leetcode答案-man-computerscience:我发现的与计算机科学和编程相关的最佳文章和视频

    这将需要额外的工作(除了听课和解决作业),但在我看来,这确实是值得的。 一般来说,你不会因为聪明而在编程或学术界取得好成绩,但是 . 我鼓励阅读的文章 故事 了解一些背景信息总是好的。 以下是我自己喜欢的...

    李开复 做最好的自己(精彩节选)

    在这本书里的许多想法都是作者在过去的文章或信函中表达过的。而且,如果由职业作家来写本书,语言以及文字一定能更加流畅、更加生动。但是,作者自己总有一种提笔写作的冲动,因为作者相信自己在青年一代中有一定的...

    看我如何搞定Nodejs内存泄漏问题

    花时间看了一下相关资料(google真好,外果仁真屌),看来这部分也已经有比较完善的方法论+工具了。所以这篇文章记录一下自己从不懂到入门的经历~~我希望这篇文章不仅能提供具体的工具供大家使用,还提供足够的理论...

    初中语文文摘情感父母伤害了我但原谅他们能让我更好地生活

    尽管身体上的伤害在凌鑫看来并不是最严重的,但父母的怒目相向和拳脚相加使他形成了对水的恐惧,并且学会了自我压抑,不敢寻求帮助。父母的暴力行为强化了凌鑫的自我责备心态,他认为任何问题的发生都是自己的过错。...

    六年级阅读专项训练句子理解.docx

    【标题】六年级阅读专项训练句子理解 【描述】该文档是针对六年级学生的阅读理解专项训练,重点在于理解...通过这些训练方法,六年级的学生能够提高阅读理解能力,更好地解读文章中的重要句子,从而提升整体阅读水平。

    Functional-Programming-For-The-Rest-of-Us-Cn:文章《Functional Programming For The Rest of Us》的中文翻译

    傻瓜函数编程 这篇文章是原文《》的...看来还是“自己动手,丰衣足食”,我最终还是开始自己翻译这篇文章。 这个版本在追求“信达雅”的同时,希望在不改变原意的前提下用中文语气/语境来进行本土化表达。唯一的例外

    外婆家的清凉山作文.doc

    5. **对比手法**:“在山脚看来,其它的山峰好似高大的巨人,但在清凉山的山顶看来,只不过是一个个小小的土包”,这种对比手法强化了清凉山的高度和雄伟,也展示了作者的视角变化。 6. **结构与层次**:文章从山脚...

    【WindTerm-2.6.0】- 可以替代 secureCrt 和 xShell 的免费ssh工具

    上一篇文章就介绍了一款免费软件MobaXterm,但菜单都是英文的,而且终端显示编码不支持GB-2312,导致中文乱码。今天就再推荐一款自由(free)软件 —— WindTerm ,说它是自由软件,是因为它在Github上开放源码了,...

    IntroductionPart托福独立写作开头.ppt

    一篇好的文章开头需要清晰地呈现文章的主题(Subject)以及作者的观点(Opinion),这样才能让读者明白你要讨论的问题和你的立场。 【什么是好的开头】 如材料中所述,一个好的托福独立写作开头应包含两个主要元素...

    Phalcon 中文文档

    但看到Phalcon框架如此优秀,在学习后就想和大家分享,但发现国内的人几乎没有使用的,故想翻译一下,一旦翻译才发现读懂和译出一篇好文章真的不太一样。 故前一期翻译的部分章节有点生硬,等有空的时候再回头重译吧...

    聂震宁:“读书人”是最好的褒奖

    【聂震宁:“读书人”是最好的褒奖】这篇文章主要讲述了聂震宁的个人经历和他对阅读的热爱与贡献。聂震宁是一位多产的作家,同时也是一位出版业的重要人物和全民阅读的倡导者。他的阅读习惯源于童年时期,受到母亲的...

    大学英语期末考试写作必背范文.doc

    大学英语期末考试写作必背范文 本文档提供了三篇大学英语期末考试写作必背范文,...这三篇文章提供了关于买房与租房、跳槽和在线社交网络的深入讨论和分析,帮助我们更好地理解这些主题,并提供了有价值的建议和观点。

    SEO伪原创工具.rar

    这些工具通常运用各种算法,如词语替换、句子重组、段落调整等方法,使生成的文章在语法、语义上合理,同时在搜索引擎看来具有较高的新颖度。 以下是一些关于SEO伪原创工具的知识点: 1. **词语替换**:伪原创工具...

    13正反论证.ppt

    此外,还可以引入一些短语和句型来丰富论述,如"就我所知"(as far as I am concerned)、"在我看来"(from my perspective)、"一个做……的解决好办法"(a good solution to...)等,这些表达能帮助作者更好地表达...

    密码学文献

    当 Martin Hellman 和 I 在 1975 年提出公开密钥密码学 [496]时,我们的一种间接贡献是引入了一个看来不易解决的难题。现在一个有抱负的密码体制设计者能够提出被认为是很聪明的一些东西——这些东西比只是把有意义...

    英语写作篇章结构PPT课件.pptx

    - **第四部分**:得出结论,需先否定正面观点或肯定负面观点,再给出个人观点,如:"在我看来,尽管……,其优点远超缺点。因此,我主张……" 3. **观点分析类文章**: - 这类文章涉及对争议话题的多角度分析,...

Global site tag (gtag.js) - Google Analytics