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语言入门
列表与数组
69
@betty = ( ); #
这才是清空数组的正确方法
强制指定标量上下文
有时在
Perl
列表上下文的地方我们需要强制引入标量上下文,可以使用伪函
scalar
,它不是真正的函数,只用于告诉
Perl
这里要切换到标量上下文:
@rocks = qw( talc quartz jade obsidian );
print "How many rocks do you have?\n";
print "I have ", @rocks, " rocks!\n"; #
错误,这会输出各种石头的名称
print "I have ", scalar @rocks, " rocks!\n"; #
正确,打印出来的是石头种数
说来也怪,
Perl
没有强行引入列表上下文的对应函数。原因很简单,因为你根本用不
到,相信我们。
列表上下文中的
<STDIN>
之前我们看到
<STDIN>
操作符在列表上下文中会返回不同的值。在标量上下文中,
<STDIN>
返回的是输入数据的下一行内容;在列表上下文中,返回的则是所有剩下行
的内容,一行一个元素,直到文件结尾。比如:
@lines = <STDIN>; #
在列表上下文中读取标准输入
当输入数据来自某个文件时,它会读取文件的剩余部分。但如果输入数据的来源是键
盘,应该如何发送文件结尾标记呢?对
Unix
或类似系统(包括
Linux
Mac OS X
来说,一般是键入
Ctrl+D
来告知系统,不会再有任何输入了。即使这个特殊字符会 ...
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