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版)
134
7
注意,输入时如果键入的是大写的
F
则不会匹配,关于忽略大小写的匹配模式我们稍
后再说。另外需要注意,
Alfred
中的
fred
可以匹配,即便它是另一个单词中的一
部分,稍后我们会告诉你如何规避这类匹配。
每次尝试新模式都要修改程序代码,这可能会比较麻烦,而且很傻。既然我们知道模
式字符串可以用变量内插,不如改成从命令行接受参数,作为要测试的模式:
while( <STDIN> ) {
chomp;
if ( /$ARGV[0]/ ) { #
直接使用参数会有安全隐患
print "\tMatches\n";
}
else {
print "\tDoesn't match\n";
}
}
这么写实际上是有安全隐患的,因为输入的参数可以是任何东西,而
Perl
拿到之后直
接当正则表达式进行编译运算,可能会带来预期外的行为。我们这里只是拿来自己测
试,所以风险可控,但在正式运行的程序中可不能这么干。另外请注意,运行程序的
shell
或许会要求把模式字符串用引号引起来,因为某些模式匹配字符在
shell
中是有
特殊意义和用处的:
$
perl try_a_pattern "fred"
This will match fred
Matches
But not Barney
Doesn't match
现在可以不改程序代码就直接测试不同模式了:
$
perl try_a_pattern "barney"
This will match fred (not)
Doesn't match ...
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