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语言入门
78
4
然后
foreach
循环会遍历参数列表
@_
里剩余的元素。循环的控制变量默认为
$_
(别
忘了
@_
$_
没有任何关系,它们的名称相似纯属巧合)。循环第一次执行时,
$_
5
,而
if
进行比较时看到
$_
$max_so_far
还大,所以
$max_so_far
会被设
5
——新的高水位线。
第三次循环时,
$_
10
。这是新的最大值,所以它会被存入
$max_so_far
到第四次时,
$_
4
。这时
if
比较的结果为假,因为
$_
不比
$max_so_far
(即
10
大,所以会跳过
if
里的程序代码。
最后,
$_
6
,因此
if
里的程序代码又被跳过一次。这是最后一次执行循环,所以
整段循环就执行完了。
此时,
$max_so_far
就变成了我们的返回值。既然它是目前见过的最大值,并且我们
已经遍历过所有数字,那它一定是列表中最大的值:
10
空参数列表
现在,即使有超过两个的参数,改版后的
&max
算法也能给出正确结果。但假如没有
任何参数传入,又会发生什么呢?
乍听之下,这似乎有点杞人忧天。毕竟,怎么可能会有人调用
&max
却不传入任何参
数呢?不过,也许有人会写出下面这样的代码:
$maximum = &max(@numbers);
数组
@numbers
有时可能只是一个空列表。比如数组内容是通过程序从文件读入的,
但文件却是空的。所以,这种情况下
&max
会如何处理?
子程序的第一行会对参数数组
@_
(现在是空的)进行 ...
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