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语言入门
106
5
所以从
Perl 5.6
开始,你可以在
binmode
的第二个参数位置上指定过滤层。比如希望
STDOUT
输出
Unicode
编码的字符,就必须提前告之,以便
STDOUT
能够正确处理和
显示:
binmode STDOUT, ':encoding(UTF-8)';
若是不指明,
STDOUT
无法理解拿到的是什么,除非关闭警告功能,否则会提示以下信
息:
Wide character in print at test line 1.
binmode
同样可用于标准输入和标准输出。如果希望标准输入的数据按
UTF-8
编码方
式读取,以下面方式指明:
binmode STDIN, ':encoding(UTF-8)';
异常文件句柄的处理
和其他程序语言一样,
Perl
是无法自己说了算地直接打开操作系统中的文件的,只能
请求操作系统代为处理。如果没有权限读写或者提供的文件名错误,操作系统会拒绝
打开。
如果试着从没有正确打开的文件句柄(或已关闭连接的网络)读取数据,会立即读
到文件结尾(对于你将会在本章看到的各种
I/O
方法而言,这个文件结尾在标量上下
文中就是
undef
,在列表上下文中就是空列表)。如果试图将数据写入这样的文件句
柄,这些数据将被无声抛丢弃。
当然,我们可以避免这种情况。一种办法是用
-w
选项启动程序或者用
warnings
编译
指令,那么在读写异常文件句柄时,
Perl
会发出警告。另一种办法是检查
open
的返
回值,如果打开文件句柄失败会返回假,如果成功则返回真。所以,程序可以写成这 ...
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