Skip to Content
Perl语言入门
book

Perl语言入门

by Randal L. Schwartz, brian d foy, Tom Phoenix
August 2018
Beginner to intermediate
382 pages
7h 14m
Chinese
Southeast University Press
Content preview from Perl语言入门
列表与数组
67
即使是普通的赋值运算(对标量或列表赋值),都可以有不同的上下文:
@list = @people; #
得到
3
个人的姓名列表
$n = @people; #
得到人数
3
但请不要立刻得出结论,认为在标量上下文中一定会得到元素个数而非元素列表。许
多能返回列表的表达式会有各种丰富有趣的行为。
其实“产生列表”的表达式和“产生标量”的表达式之间并无不同,任何表达式都可
以产生列表或标量,根据上下文而定。因此当我们说“产生列表的表达式”时,我们
指的是该表达式通常被用在列表上下文中。所以当它们不小心被用在标量上下文中时
(像
reverse
或是
@fred
),你可能会因此感到惊讶。
不光如此,从我们积累的经验来看,仅仅通过对表达式形式上的判断,是无法归纳出
一个通用法则来的。每一个表达式都可能有它自己的特定规则。所以,实际上能够概
括的规则也就是:哪种上下文更有意义,就应该是哪种上下文。但其实记住这条规则
并没什么用。
Perl
是一种会尽量帮你完成常见任务的语言,往往它的选择就是你想要
的结果。
在标量上下文中使用产生列表的表达式
有些经常被用来生成列表的表达式如果用在标量上下文中,结果会怎样?这要看表达
式的缔造者怎么说,这个人基本上就是
Larry
了,他会在说明文档里详细解释。其实
学习
Perl
的大部分时间都是在学习
Larry
的思维方式。因此,只要能以他的思考方式
出发,就能大致推猜
Perl
接下来会怎么做。但在初学阶段,请多翻多读说明文档。
某些表达式在标量上下文中不会返回任何值。比如, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
Perl语言入门(第8版)

Perl语言入门(第8版)

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787564177911