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语言入门
48
2
print "'$name' does not come after 'fred'.\n";
print "Maybe it's the same string, in fact.\n";
}
条件语句中的语句块周围一定要加上表示边界的花括号,这点和
C
语言不同(不管你
有没有学过
C
)。建议将语句块内的内容向里缩排,使其视觉上成为一个整体,方便
阅读。如果用的是专为编程设计的文本编辑器(我们之前在第
1
中提过),它一般
都会自动完成缩进。
布尔值
其实,任何标量值都可以成为
if
控制结构里的判断条件。如果把表达式返回的真假
值保存到变量中,那么在判断时可以直接检查该变量的值,可读性更强:
$is_bigger = $name gt 'fred';
if ($is_bigger) { ... }
Perl
是如何决断给定值的真假呢?和其他语言不同,
Perl
并没有专用的布尔
Boolean
)数据类型,它是靠一些简单的规则来判断的:
如果是数字,
0
为假;所有其他数字都为真。
如果是字符串,空字符串(
''
)以及字符串
'0'
都为假;所有其他字符串都为
真。
如果变量还未被赋值,那么返回假。
要取得相反的布尔值,可以用
!
这个单目取反操作符。若它后面的操作数为真,就返
回假;若后面的操作数为假,则返回真:
if (! $is_bigger) {
#
如果
$is_bigger
不为真,则执行这里的代码
}
这里还有一个小技巧,由于
!
会颠倒真假值,并且
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