- Page 1: Perl 语 言 编 程
- Page 7 and 8: 12.4.1 可 继 承 构 造 器 ....
- Page 9 and 10: 17.2.3.2. 信 号 灯 .............
- Page 11 and 12: 不 管 你 喜 欢 把 Perl 称
- Page 13 and 14: 可 以 向 语 言 中 增 加 新
- Page 15 and 16: 数 组 和 散 列 也 互 不 相
- Page 17 and 18: 象 我 们 在 上 边 做 的 ,
- Page 19 and 20: $kids_of_wife{"Jacob"}{"Zilpah"}[1]
- Page 21 and 22: use Some::Cool::Module;然 后 你
- Page 23 and 24: 你 可 以 用 下 面 所 示 的
- Page 25 and 26: 来 。 在 这 些 系 统 中 ,
- Page 27 and 28: open(SESAME, "input-pipe-command |"
- Page 29 and 30: print $a . $b; # 打 印 123456同
- Page 31 and 32: 说 过 赋 值 类 似 一 个 过
- Page 33 and 34: 如 果 成 功 打 开 了 文 件
- Page 35 and 36: 在 Perl 中 , 真 总 是 在 标
- Page 37 and 38: $available = 10000 - $tickets_sold;
- Page 39 and 40: 不 同 于 if 和 while 语 句 ,f
- Page 41 and 42: 个 数 据 域 之 间 定 界 的
- Page 43 and 44: 甚 至 “a!”, 但 不 匹 配
- Page 45 and 46: 同 理 , 也 有 表 示 字 符
- Page 47: @chicks 包 含 (Wilma, Betty), 而
- Page 51 and 52: 以 合 并 到 程 序 里 面 去
- Page 53 and 54: 还 有 其 他 一 些 爱 好 者
- Page 55 and 56: 和 数 字 。Unicode 象 形 文
- Page 57 and 58: 这 些 搜 索 规 则 里 面 还
- Page 59 and 60: 数 字 文 本 是 用 任 意 常
- Page 61 and 62: 除 了 上 面 列 出 的 反 斜
- Page 63 and 64: $double =qq(can't we get some "good
- Page 65 and 66: 数 组 变 量 通 过 使 用 在
- Page 67 and 68: Here's a linero two.THISAnd here's
- Page 69 and 70: 符 ) 可 以 用 于 在 真 正
- Page 71 and 72: 境 里 用 于 真 / 假 测 试
- Page 73 and 74: 值 得 说 明 的 一 点 是 列
- Page 75 and 76: # 一 个 “ 反 转 的 逗 号
- Page 77 and 78: 截 断 一 个 数 组 并 不 回
- Page 79 and 80: 你 可 以 通 过 在 花 括 弧
- Page 81 and 82: $info = `finger $user`;一 个 用
- Page 83 and 84: 般 的 把 所 有 你 在 命 令
- Page 85 and 86: 你 可 能 会 问 : 如 果 我
- Page 87 and 88: 常 用 的 语 素 。 而 从 信
- Page 89 and 90: 尽 管 引 用 是 标 量 值 ,
- Page 91 and 92: 这 句 话 可 能 和 你 一 开
- Page 93 and 94: 串 。 这 些 规 则 的 一 个
- Page 95 and 96: print "\t" x ($tab/8), ' ' x ($tab%
- Page 97 and 98: 这 是 因 为 print 是 一 个
- Page 99 and 100:
请 注 意 -s/a/b/ 并 不 是 做
- Page 101 and 102:
表 3-5 关 系 操 作 符数 字
- Page 103 and 104:
符 , 因 为 它 们 是 通 过
- Page 105 and 106:
$hexdigit = (0 .. 9, 'a' .. 'f')[$n
- Page 107 and 108:
$year % 400 ? 0:1把 问 号 和
- Page 109 and 110:
$a += 2;$a *= 3;本 招 并 非 绝
- Page 111 and 112:
这 两 句 是 完 全 不 同 的
- Page 113 and 114:
while 和 until 修 饰 词 重 复
- Page 115 and 116:
LABEL BLOCKLABEL BLOCK continue BLO
- Page 117 and 118:
在 else 以 后 ,$color 变 量
- Page 119 and 120:
}}是 一 样 的 , 只 不 过 实
- Page 121 and 122:
print "$_\en"; sleep(1);}for $field
- Page 123 and 124:
# 这 里 是 last 把 我 带 到
- Page 125 and 126:
next LINE if /^#/;next LINE if /^$/
- Page 127 and 128:
不 过 , 你 会 觉 得 下 面
- Page 129 and 130:
}4.4.1 分 支 (case) 结 构和
- Page 131 and 132:
式 的 时 候 可 能 出 现 的
- Page 133 and 134:
全 局 声 明 可 以 放 在 任
- Page 135 and 136:
就 是 我 们 说 的 用 法 (pr
- Page 137 and 138:
最 常 见 的 声 明 形 式 是
- Page 139 and 140:
our @Current_Inventory;my $widget;f
- Page 141 and 142:
local $var = $newvalue;some_func();
- Page 143 and 144:
use warnings;# 在 这 里 打 开
- Page 145 and 146:
你 可 以 通 过 创 建 一 个
- Page 147 and 148:
挑 出 结 构 元 字 符 , 阅
- Page 149 and 150:
第 三 章 , 单 目 和 双 目
- Page 151 and 152:
print "No binary directories please
- Page 153 and 154:
s 和 /m 修 饰 词 并 不 涉
- Page 155 and 156:
\s+# 由 一 些 空 白 分 隔\l
- Page 157 and 158:
决 于 环 境 。 在 列 表 环
- Page 159 and 160:
所 有 的 标 量 变 量 都 在
- Page 161 and 162:
你 不 能 对 数 组 直 接 使
- Page 163 and 164:
LVALUE =~ tr/SEARCHLIST/REPLACEMENT
- Page 165 and 166:
$pathname =~ tr/a-zA-Z/_/cs;# 把
- Page 167 and 168:
? 否 匹 配 1 或 者 0 次 ( 最
- Page 169 and 170:
\t 是 匹 配 水 平 制 表 符
- Page 171 and 172:
\N{NAME}后 面 跟 随 的 字 符
- Page 173 and 174:
一 个 方 括 弧 中 的 一 个
- Page 175 and 176:
还 有 很 多 其 它 属 性 。
- Page 177 and 178:
IsPe ? 标 点 , 关 闭 否IsPf ?
- Page 179 and 180:
比 如 ,\p{IsSyLA} 将 匹 配 \N{
- Page 181 and 182:
1. &$subref(LIST) # 在 间 接 调
- Page 183 and 184:
eturn $max;}$bestday = max($mon, $t
- Page 185 and 186:
2.3 范 围 问 题因 为 每 次
- Page 187 and 188:
BEGIN 既 不 会 影 响 子 过
- Page 189 and 190:
($aref, $bref) = func(\@c, \@d);pri
- Page 191 and 192:
或 者 类 型 团 ( 用 小 写
- Page 193 and 194:
eturn @result;}一 些 读 者 希
- Page 195 and 196:
}这 将 得 到 一 个 单 目
- Page 197 and 198:
下 面 是 一 些 正 确 的 语
- Page 199 and 200:
(dat(3), data(3) = get_data(3,4);
- Page 201 and 202:
被 保 留 . 因 此 如 果 你
- Page 203 and 204:
$description.除 非 一 个 格
- Page 205 and 206:
"Some text line".> 域 长 度 标
- Page 207 and 208:
ENDprint "Wow, I just stored `$^A'
- Page 209 and 210:
那 么 我 们 说 的 就 是 硬
- Page 211 and 212:
在 我 们 刚 刚 显 示 的 例
- Page 213 and 214:
};和 方 括 弧 一 样 , 只 有
- Page 215 and 216:
my $fh = shift;return scalar ;}如
- Page 217 and 218:
上 面 对 splutter 的 两 个
- Page 219 and 220:
8.3.2 把 一 个 BLOCK 块 当 作
- Page 221 and 222:
假 设 现 在 $array[3] 是 未
- Page 223 and 224:
$john->[4];# 仍 然 打 印 "tall"
- Page 225 and 226:
@envrefs = \($ENV{HOME}, $ENV{TERM}
- Page 227 and 228:
{my $critter = "camel";$critterref
- Page 229 and 230:
sub get_method {my ($self, $methodn
- Page 231 and 232:
local *inner = sub { return $x * 19
- Page 233 and 234:
在 前 面 一 节 里 , 我 们
- Page 235 and 236:
);while ( my($keyref, $value) = eac
- Page 237 and 238:
1998), 或 者 CPAN—— 所 有
- Page 239 and 240:
当 然 , 你 不 必 命 名 那
- Page 241 and 242:
}就 象 你 看 到 的 那 样 ,
- Page 243 and 244:
for $i (1..10) {@array = somefunc($
- Page 245 and 246:
@{ $AoA[$i] } = @array;# 有 点
- Page 247 and 248:
$HoA{$group} = [ get_family($group)
- Page 249 and 250:
{husband => "george",wifeson=> "jan
- Page 251 and 252:
for $href ( @AoH ) {print "{ ";for
- Page 253 and 254:
husband=fred pal=barney wife=wilmap
- Page 255 and 256:
$HoH{flintstones}{$what} = $new_flo
- Page 257 and 258:
print "$role=$HoH{$family}{$role} "
- Page 259 and 260:
THATCODE => sub { $_[0] ** $_[1] },
- Page 261 and 262:
members => [{ name => "homer", role
- Page 263 and 264:
print " is on ", join (" and ", @{
- Page 265 and 266:
怎 么 乱 都 行 , 只 要 你
- Page 267 and 268:
老 的 包 分 隔 符 还 是 一
- Page 269 and 270:
第 一 种 形 式 更 高 效 是
- Page 271 and 272:
上 面 这 些 都 是 廉 价 传
- Page 273 and 274:
*foo{THING} 表 示 法 可 以 用
- Page 275 and 276:
sub AUTOLOAD {my $program = our $AU
- Page 277 and 278:
LIST, 那 么 所 有 你 的 符
- Page 279 and 280:
our @EXPORT_OK =qw($weight); # 按
- Page 281 and 282:
use Bestiary wq(:critters !:camelid
- Page 283 and 284:
Exporter 提 供 缺 省 的 export
- Page 285 and 286:
通 过 全 局 覆 盖 glob, 这
- Page 287 and 288:
的 话 , 我 们 将 永 远 用
- Page 289 and 290:
summon 和 speak 方 法 都 是
- Page 291 and 292:
变 成 :$sword->name($oldname || "
- Page 293 and 294:
owner => "Gandalf",domain => "fire"
- Page 295 and 296:
Spider 类 继 承 了 方 法 。
- Page 297 and 298:
回 一 个 数 据 库 句 柄 对
- Page 299 and 300:
你 现 在 应 该 可 以 在 原
- Page 301 and 302:
use base ("Horse", "donkey");# 声
- Page 303 and 304:
my $self = shift;print "The mule sp
- Page 305 and 306:
if ($invocant->can("copy")) {print
- Page 307 and 308:
没 能 找 到 匹 配 , 则 再
- Page 309 and 310:
}}这 样 就 可 以 让 该 文
- Page 311 and 312:
多 数 情 况 下 是 不 需 要
- Page 313 and 314:
}在 随 后 的 每 个 例 子
- Page 315 and 316:
$self->{race} = "unknown";$self->{a
- Page 317 and 318:
比 如 , 如 果 你 把 下 面
- Page 319 and 320:
sub new {my $invocant = shift;my $c
- Page 321 and 322:
*$field = sub {my $self = shift;$se
- Page 323 and 324:
# 生 成 方 法 名for my $field
- Page 325 and 326:
sub new {my $class = shift;my $self
- Page 327 and 328:
our $population = 0;sub pupulation
- Page 329 and 330:
我 们 暂 时 废 除 严 格 的
- Page 331 and 332:
对 于 单 目 操 作 符 ( 那
- Page 333 and 334:
my ($x, $y, $swap) = @_;my ($value)
- Page 335 and 336:
最 后 , 尽 管 咱 们 这 里
- Page 337 and 338:
package MagicDec;use overloadq(--)
- Page 339 and 340:
package LuckyDraw;use overload'' =>
- Page 341 and 342:
sub new {my $class = shift;return b
- Page 343 and 344:
如 果 你 在 一 个 对 象 上
- Page 345 and 346:
sub handler {my ($orig, $intergp, $
- Page 347 and 348:
如 果 你 现 在 觉 得 “ 重
- Page 349 and 350:
一 个 捆 绑 了 的 变 量 调
- Page 351 and 352:
捆 绑 是 对 变 量 进 行 的
- Page 353 and 354:
open $fh, ">", $filename){close $fh
- Page 355 and 356:
$string = "Here is the first line o
- Page 357 and 358:
然 后 所 有 把 $_ 当 作 一
- Page 359 and 360:
的 缺 省 实 现 只 是 简 单
- Page 361 and 362:
在 下 一 个 例 子 里 , 为
- Page 363 and 364:
my ($self, $count ) = @_;if ($count
- Page 365 and 366:
my $self = shift;if (@_ + $#{$self-
- Page 367 and 368:
第 十 四 章 捆 绑 (tie) 变
- Page 369 and 370:
数 据 域内 容USERHOME这 个
- Page 371 and 372:
这 个 方 法 实 现 的 是 从
- Page 373 and 374:
tie %daemon_dots, "Dotfiles", "daem
- Page 375 and 376:
SELF->FIRSTKEY当 用 户 开 始
- Page 377 and 378:
}sub PRINTF {my $self = shift;my $f
- Page 379 and 380:
open my $self, $form, @_ or croak "
- Page 381 and 382:
$self->CLOSE;open($self, $form, @_)
- Page 383 and 384:
当 用 read 或 者 sysread 对
- Page 385 and 386:
下 面 是 我 们 的 Shout 类
- Page 387 and 388:
my($self) = @_;getc($self);}并 且
- Page 389 and 390:
print CALC "$sum * $sum\n";$sum = ;
- Page 391 and 392:
};}for my $inmeth (qw(READ READLINE
- Page 393 and 394:
less \$fh, $class;}sub FETCH { rand
- Page 395 and 396:
3: 0.799749050426126 0.300184963960
- Page 397 and 398:
use Remember;my $fred;$x = tie $fre
- Page 399 and 400:
14.6 CPAN 里 的 模 块在 你
- Page 401 and 402:
荒 谬 的 是 , 我 们 让 Perl
- Page 403 and 404:
的 程 序 就 继 续 以 面 向
- Page 405 and 406:
们 建 议 你 把 你 的 程 序
- Page 407 and 408:
• pack/unpack 字 符 “c” 和
- Page 409 and 410:
第 十 六 章 , 进 程 间 通
- Page 411 and 412:
$SIG{QUIT} = \&catch_zap;# 把 另
- Page 413 and 414:
另 外 一 个 有 趣 的 信 号
- Page 415 and 416:
查 找 SV_INTERRUPT 或 者 SA_RES
- Page 417 and 418:
服 务 器 在 那 里 能 找 的
- Page 419 and 420:
filename may represend a different
- Page 421 and 422:
$DBNAME = "/path/to/database";$LCK
- Page 423 and 424:
如 果 你 真 的 希 望 新 程
- Page 425 and 426:
再 假 设 你 已 经 安 排 好
- Page 427 and 428:
or die "can't fork: $!";while () {n
- Page 429 and 430:
exit}这 个 方 法 的 一 个
- Page 431 and 432:
你 可 以 不 停 地 重 复 使
- Page 433 and 434:
的 ftp 这 样 的 交 互 式 程
- Page 435 and 436:
如 果 你 想 把 一 个 进 程
- Page 437 and 438:
尽 管 有 这 些 痛 苦 , 古
- Page 439 and 440:
}}下 面 是 ShMem ? 包 , 就 是
- Page 441 and 442:
$self->poke(@_);$self->unlock;}sub
- Page 443 and 444:
因 为 如 果 你 传 一 些 垃
- Page 445 and 446:
# 制 作 远 端 机 器 的 地
- Page 447 and 448:
# 制 作 自 己 的 套 接 字
- Page 449 and 450:
$| = 1; # 自 动 刷 新# 预 联
- Page 451 and 452:
# 等 待 1.5 秒 听 取 回 答us
- Page 453 and 454:
第 十 七 章 线 程并 行 编
- Page 455 and 456:
程 化 的 Perl 写 出 足 够
- Page 457 and 458:
你 可 以 用 两 种 方 法 之
- Page 459 and 460:
$retval = $t2->join();print "1st ki
- Page 461 and 462:
use strict;use Thread 'yield';yeild
- Page 463 and 464:
sub abump {if ($var == 0) {yield;$v
- Page 465 and 466:
my $done = 0;sub frob {my $arg = sh
- Page 467 and 468:
# 和 以 前 一 样}如 果 你
- Page 469 and 470:
$wait_var = 1;cond_wait $wait_var u
- Page 471 and 472:
Pulled Thread=SCALAR(0x8117200) fro
- Page 473 and 474:
你 可 以 把 一 个 Perl 程
- Page 475 and 476:
注 : 呵 呵 , 象 大 话 里 的
- Page 477 and 478:
伸 到 树 的 底 部 ( 象 图 1
- Page 479 and 480:
会 在 运 行 时 出 现 。)
- Page 481 and 482:
• 递 归 词 法 填 充 堆 栈
- Page 483 and 484:
有 些 后 端 已 经 有 调 用
- Page 485 and 486:
%perl -MO=Lint, all myprog目 前
- Page 487 and 488:
18.6 提 前 编 译 , 回 头 解
- Page 489 and 490:
1st BEGIN: still compiling2nd BEGIN
- Page 491 and 492:
要 做 的 只 是 把 一 个 CHE
- Page 493 and 494:
对 于 方 法 2 和 3, Perl 从
- Page 495 and 496:
告 诉 一 个 程 序 ( 比 如
- Page 497 and 498:
行 的 一 行 程 序 里 的 空
- Page 499 and 500:
** 开 关单 字 符 并 且 没
- Page 501 and 502:
位 | 字 母 | 含 义------------
- Page 503 and 504:
Howdy, world!对 于 csh 而 言 ,
- Page 505 and 506:
从 shell 上 , 你 说 :%perl -p -
- Page 507 and 508:
因 为 有 些 家 伙 喜 欢 使
- Page 509 and 510:
-M'module qw(foo bar)' 的 一 个
- Page 511 and 512:
隐 含 的 循 环 之 前 或 之
- Page 513 and 514:
的 确 , 这 个 东 西 很 难
- Page 515 and 516:
Config 模 块 获 取 配 置 信
- Page 517 and 518:
%setenv PATH '/bin:/usr/bin'而 在
- Page 519 and 520:
任 何 需 要 保 护 的 字 符