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语言入门
Perl
模块
209
#
也是在你的
Perl
程序内部
use lib qw( /Users/fred/perl5 );
作为起步,上面这些应该够了。我们在《
Intermediate Perl
》中会有更深入的讨论,包
括教你编写自己的模块。其他相关信息不妨参阅
perlfaq8
文档。
使用简易模块
假设在程序里有个包含路径的长文件名,比如
/usr/local/bin/perl
,而你希望取出文件
的基名(
basename
)而不包括目录。这其实很简单,文件的基名就是最后一个斜线之
后的内容(此例中即为
perl
):
my $name = "/usr/local/bin/perl";
(my $basename = $name) =~ s#.*/##;
和你之前看到的一样,
Perl
会先在圆括号中进行赋值,然后再进行替换操作:将结尾
为斜线的字符串(也就是文件路径的目录部分)替换成空字符串,这样就只剩下基名
了。或者采用更为简洁的写法,在替换操作中使用
/r
开关:
use v5.14;
my $name = "/usr/local/bin/perl";
my $basename = $name =~ s#.*/##r;
如果这么写,看起来好像可以正常工作。好吧,这只是看起来,实际上这么写隐含着
三个问题。
首先,
Unix
上的文件或目录名称可能会包含换行符(这虽然不是常常发生,但
确实可能发生)。由于正则表达式的点号(
.
)无法匹配换行符,像
"/home/fred/
flintstone\n/brontosaurus" ...
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