Skip to Content
Perl语言入门(第8版)
book

Perl语言入门(第8版)

by Randal L. Schwartz, brian d foy, Tom Phoenix
October 2022
Beginner to intermediate
390 pages
7h 5m
Chinese
Southeast University Press
Content preview from Perl语言入门(第8版)
其他控制结构
199
print "I can count to $_!\n";
}
这实际上就是一个
foreach
循环,但用的却是
for
关键字。除此以外,本书其他例
子都会写成
foreach
的形式。最终选用哪个,取决于你的个人偏好。
Perl
世界里,纯正的
foreach
循环几乎总是更好的选择。在上面的
foreach
环例子(表面上写成了
for
循环)里,我们可以一眼看出它是从
1
10
的循环。
但对于下面具有同样功能的代码,你看得出来问题在哪里吗?
for ($i = 1; $i < 10; $i++) { #
糟糕!这里有错!
print "I can count to $_!\n";
}
估计每个人这辈子都会犯这样的错。看出问题来了吗?虽然在比较部分写的数字没
错,但比较操作符选错了。数字
10
是不可能小于
10
的,所以这段代码实际只能计数
9
。这类错误称作单步偏差(
off by one
)错误。补上一个字符就能修复这个错误:
for ($i = 1; $i <= 10; $i++) { #
现在好了
print "I can count to $_!\n";
}
循环控制
现在你大概已经感觉到,
Perl
是一种所谓的“结构化”编程语言。特别是
Perl
程序的
任何块都只有一个入口,也就是代码块的顶端。不过相比前面介绍过的结构,有时候
需要更多样化的控制方式。比如有时你需要一个至少执行一次的
while
循环,或者需
要提早退出代码块。
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

Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Perl语言入门

Perl语言入门

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787576602036