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语言入门
256
14
这到底是在做什么?其实这里的意思是,只要这个替换运算返回真(表示成功),就
去执行循环主体。但是循环里的程序没有任何作用!对
Perl
来说,这没关系,这不过
是要让我们知道,该语句的主要目的是在执行条件表达式(替换运算),而不是这个
无用的循环主体。这么做时习惯上会使用数值
1
来占个位子,其实任何数值都可以使
用。为了改善可读性,改成以下等效写法:
'keep looping' while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
所以,现在我们知道替换运算是循环的真正目的。但此处的替换运算又做了什么事?
别忘了,这里的
$number
是个像
"12345678.90"
这样的字符串。上面的模式会匹配
字符串的第一个部分,但它不会越过小数点(你知道为什么吗?)。内存变量
$1
"12345"
,而
$2
会是
"678"
,所以这个替换运算会让
$number
变成
"12345,678.90"
(别忘了,它不会匹配小数点,所以字符串后面的部分并不会改变)。
你知道模式开头的减号是什么用处么?(提示:这个减号只能在字符串中的一个地方
出现),在本节结束时,万一你还没找到答案,可以看看我们的解答。
整个替换过程并非到此为止。既然替换运算成功了,这个无事可做的循环就会再来一
次。因为这次模式只能替换逗号之前的部分,所以
$number
会变成
"12,345,678.90"
于是在每次循环执行后,替换运算就会在数字中加一个逗号。
循环的工作还没完呢。因为上一次替换成功了,所以又会重新开始循环。但由于模式 ...
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