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版)
进程管理
275
open STDOUT, '>', '/tmp/granite.out';
exec 'bedrock';
如果使用
system
而不是
exec
Perl
程序必须傻傻地等另一个程序运行完毕才能跟着
收工,这无疑是在浪费系统资源。
话虽如此,实际上我们很少用到
exec
,一般都是将它和
fork
一起使用,这稍后会介
绍。因此如果吃不准到底该用
system
还是
exec
,就总是用
system
好了,大多情况
下都是稳妥的。
一旦启动了要执行的程序,
Perl
便放手退出,无法再控制它,因此在
exec
调用之后
写的任何代码都无法运行,不过如果启动过程出现错误,那么后续的捕获语句还是可
以继续执行的:
exec 'date';
die "date couldn't run: $!";
用反引号捕获输出结果
无论用
system
还是
exec
,所执行命令的输出都会被重定向到
Perl
的标准输出。有时
候我们感兴趣的是将输出结果捕获成字符串,以便后续进一步处理。要提取该输出信
息,只要用反引号代替单引号或双引号就可以了:
my $now = `date`; #
捕获
date
命令的输出
print "The time is now $now"; #
换行符已经包含在捕获内容中
一般来说,
date
命令能输出长度约为
30
个字符的字符串,其中含有当前的日期与时
间,最后接一个换行符。当我们把
date
放在反引号里时,
Perl
会执行这个
date
命令
并将其标准输出结果以字符串形式捕获。在这个例子中,字符串会被赋值给 ...
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