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语言入门
204
10
尽管以逻辑操作符作为控制结构可能令人困惑,但有时候这却是大家都认可的写法。
比如下面就是打开文件的习惯写法:
open my $fh, '<', $filename
or die "Can't open '$filename': $!";
通过使用低优先级的短路
or
操作符,我们向
Perl
表达了“
open this file
……
or die
的意思。如果文件打开成功,就会返回真,此时
or
就不必执行了;但如果文件打开
失败,
or
就还得去执行右边的部分,也就是丢出信息并通过
die
终止程序。
所以,用这些操作符作为控制结构是
Perl
习惯用语的一部分,也就是
Perl
约定俗成
的表达方法。适当使用的话,程序会更具威力;否则,你的程序将会难以维护。请别
滥用它们。
习题
以下习题答案参见第
317
页上的
“第
10
章习题解答”一节:
1. [25]
编写程序,让用户不断猜测范围从
1
100
的秘密数字,直到猜中为止。
程序应该以神奇公式
int(1 + rand 100)
来随机产生秘密数字。当用户猜错时,
程序应该回应“
Too hight
”或“
Too low
”。若是好奇,可以参考
perlfunc
文档
中关于
int
rand
的介绍。如果用户键入
quit
exit
等字样,或是键入一个
空白行,程序就应该中止。当然,如果用户猜到了,程序也应该中止!
2. [10]
修改刚才的程序,打印额外的调试信息,例如程序选择的秘密数字。确保修
改的部分可以用开关控制,而且调试开关即使关上也不会产生警告信息。如果在 ...
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