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版)
70
3
如果在读取这些数据行时,能一次性去掉所有换行符岂不更好?可以直接把数组交
chomp
,它会自动去掉每个元素的换行符。例如:
@lines = <STDIN>; #
一次读取所有行的内容
chomp(@lines); #
然后去掉所有换行符
不过更常见的做法是按之前介绍的风格写成:
chomp(@lines = <STDIN>); #
一次读入所有行,换行符除外
你喜欢怎么写都行,但大部分
Perl
程序员都会倾向于第二种写法,因为够紧凑。
输入数据一经读取,就无法回头再读一次。这个设计是符合我们平时的使用习惯的。
一直读,读到文件结尾,读取结束。这样方便逐行处理,或者将数据统统拿到手后一
次性处理。
如果读入的一个
4 TB
大小的日志文件,会怎样?行输入操作符会读取所有行并占用
大量内存。
Perl
不会限制你做这种事,但系统的其他用户(当然还有系统管理员)很
可能会抗议。如果需要读取大量数据,就该选择合理方式,避免一次加载全部数据到
内存。
习题
以下习题答案见第
309
页上的“第
3
章习题解答”一节:
1. [6]
写一个程序,读入一些字符串,每行一个,直到文件结尾为止。然后,再
以相反顺序输出这个列表。假如输入来自键盘,你需要在
Unix
系统上键入
Control+D
或在
Windows
系统上键入
Control+Z
来表示输入的结束。
2. [12]
写一个程序,读入一些数字,每行一个,直到文件结尾为止。然后,根据每
一个数字输出如下名单中相应的人名(请将这份名单写到程序里,也就是说,你 ...
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