Perl 语言编程
2
目录
第一章 Perl概述............................................................................................................................10
1.1 从头开始..........................................................................................................................10
1.2 自然语言与人工语言......................................................................................................11
1.2.1 变量语法...............................................................................................................12
1.2.2 单数变量...............................................................................................................13
1.2.3 复数变量...............................................................................................................14
1.2.4 复杂数据结构.......................................................................................................17
1.2.5 简单数据结构.......................................................................................................19
1.2.6 动词.......................................................................................................................21
1.3 一个平均值例子..............................................................................................................22
1.3.1 如何运行...............................................................................................................24
1.4 文件句柄..........................................................................................................................26
1.5 操作符..............................................................................................................................28
1.5.1 双目算术操作符...................................................................................................28
1.5.2 字符串操作符.......................................................................................................28
1.5.3 赋值操作符...........................................................................................................29
1.5.4 单目算术操作符...................................................................................................31
1.5.5 逻辑操作符...........................................................................................................32
1.5.6 比较操作符...........................................................................................................33
1.5.7 文件测试操作符...................................................................................................33
1.6 流程控制..........................................................................................................................34
1.6.1 什么是真...............................................................................................................34
1.6.2 If 和 unless 语句.................................................................................................35
1.6.3 循环.......................................................................................................................36
1.6.3.1 while 和 until 语句...................................................................................36
1.6.3.2 for 语句......................................................................................................38
1.6.3.3 foreach 语句...............................................................................................38
1.6.3.4 跳出控制结构: next 和 last .....................................................................39
1.7 正则表达式......................................................................................................................40
1.7.1 量词.......................................................................................................................43
1.7.2 最小匹配...............................................................................................................44
1.7.3 把钉子敲牢...........................................................................................................44
1.8 列表处理..........................................................................................................................46
1.9 你不知道但不伤害你的东西(很多) ...............................................................................47
第二章集腋成裘...........................................................................................................................48
2.1 原子..................................................................................................................................48
2.2 分子..................................................................................................................................49
2.3 内置的数据类型..............................................................................................................51
3
2.4 变量..................................................................................................................................52
2.5 名字..................................................................................................................................53
2.5.1 名字查找...............................................................................................................55
2.6 标量值..............................................................................................................................57
2.6.1 数字文本...............................................................................................................58
2.6.2 字串文本...............................................................................................................59
2.6.3 选择自己的引号...................................................................................................62
2.6.4 要么就完全不管引起...........................................................................................63
2.6.5 代换数组数值.......................................................................................................64
2.6.6“此处”文档.............................................................................................................65
2.6.7 V-字串文本............................................................................................................67
2.6.8 其他文本记号.......................................................................................................68
2.7 环境..................................................................................................................................69
2.7.1 标量和列表环境...................................................................................................69
2.7.2 布尔环境...............................................................................................................70
2.7.3 空(void)环境....................................................................................................71
2.7.4 代换环境...............................................................................................................71
2.8 列表值和数组..................................................................................................................72
2.8.1 列表赋值...............................................................................................................75
2.8.2 数组长度...............................................................................................................76
2.9 散列..................................................................................................................................77
2.10 型团(typeglob)和文件句柄......................................................................................79
2.11 输入操作符....................................................................................................................80
2.11.1 命令输入(反勾号)操作符.............................................................................80
2.11.2 行输入(尖角)操作符.....................................................................................81
2.11.3 文件名聚集操作符.............................................................................................84
第三章单目和双目操作符...........................................................................................................86
3.1 项和列表操作符(左向)..............................................................................................89
3.2 箭头操作符......................................................................................................................91
3.3 自增和自减操作符..........................................................................................................91
3.4 指数运算..........................................................................................................................92
3.5 表意单目操作符..............................................................................................................92
3.6 绑定操作符......................................................................................................................93
3.7 乘号操作符......................................................................................................................94
3.8 附加操作符......................................................................................................................95
3.9 移位操作符......................................................................................................................95
3.10 命名单目操作符和文件测试操作符............................................................................96
3.11 关系操作符..................................................................................................................100
3.12 相等操作符..................................................................................................................101
3.13 位操作符......................................................................................................................101
3.14 C 风格的逻辑(短路)操作符..................................................................................102
3.15 范围操作符..................................................................................................................103
3.16 条件操作符..................................................................................................................105
3.16 赋值操作符..................................................................................................................107
4
3.18 逗号操作符..................................................................................................................109
3.19 列表操作符(右向)..................................................................................................110
3.20 逻辑与,或,非和异或..............................................................................................110
3.21 Perl 里没有的 C 操作符............................................................................................ 111
第四章语句和声明..................................................................................................................... 111
4.1 简单语句.........................................................................................................................112
4.2 混合语句.........................................................................................................................113
4.2.1 if 和 else 语句...................................................................................................115
4.3 循环语句........................................................................................................................117
4.3.1 while 和 until 语句............................................................................................117
4.3.2 for循环.................................................................................................................118
4.3.3 foreach 循环........................................................................................................120
4.3.4 循环控制.............................................................................................................123
4.4 光块................................................................................................................................127
4.4.1 分支(case)结构..............................................................................................129
4.5 goto .................................................................................................................................132
4.6 全局声明........................................................................................................................132
4.7 范围声明........................................................................................................................134
4.7.1 范围变量声明.....................................................................................................135
4.7.2 词法范围的变量:my........................................................................................137
4.7.3 词法范围全局声明:our ...................................................................................138
4.7.4 动态范围变量:local .........................................................................................140
4.8 用法(pragmas) ..........................................................................................................142
4.8.1 控制警告..............................................................................................................142
4.8.2 控制全局变量的使用.........................................................................................143
第五章模式匹配.........................................................................................................................144
5.1 正则表达式箴言............................................................................................................145
5.2 模式匹配操作符............................................................................................................148
5.2.1 模式修饰词.........................................................................................................152
5.2.2 m// 操作符(匹配) ..........................................................................................155
5.2.3 s/// 操作符(替换)...........................................................................................158
5.2.3.1 顺便修改一下字串..................................................................................160
5.2.3.2 当全局替换不够“全局”地时候..............................................................162
5.2.4 tr/// 操作符(转换)..........................................................................................162
5.3.1 元字符表.............................................................................................................166
5.3.2 特定的字符.........................................................................................................170
5.3.3 通配元符号.........................................................................................................171
5.4 字符表............................................................................................................................172
5.4.1 客户化字符表.....................................................................................................172
5.4.2 典型 Perl 字符表缩写......................................................................................173
5.4.3 Unicode 属性......................................................................................................174
5.4.3.1 Perl 的 Unicode 属性.............................................................................175
5.4.3.2 标准的 Unicode 属性............................................................................176
第六章子过程.............................................................................................................................179
5
1.0 语法................................................................................................................................179
2.0 语意................................................................................................................................181
2.1 参数列表的技巧....................................................................................................182
2.2 错误指示................................................................................................................184
2.3 范围问题................................................................................................................185
3.0 传入引用........................................................................................................................187
4.0 函数原型........................................................................................................................189
4.1 内联常量函数........................................................................................................193
4.2 谨慎使用函数原型................................................................................................194
5.0 子过程属性....................................................................................................................196
5.1 Locked 和 method 属性.......................................................................................196
5.3 左值属性................................................................................................................197
第七章格式.................................................................................................................................199
7.1 格式变量.........................................................................................................................203
7.2 页脚................................................................................................................................206
7.2.1 访问格式的内部.................................................................................................206
第八章引用.................................................................................................................................208
8.1 什么是引用?................................................................................................................208
8.2 创建引用........................................................................................................................210
8.2.1 反斜杠操作符.....................................................................................................210
8.2.2 匿名数据.............................................................................................................210
8.2.2.1 匿名数组组合器......................................................................................211
8.2.2.2 匿名散列组合器......................................................................................211
8.2.2.3 匿名子过程组合器..................................................................................213
8.2.3 对象构造器.........................................................................................................213
8.2.4 句柄引用.............................................................................................................214
8.2.5 符号表引用.........................................................................................................215
8.2.6 引用的隐含创建.................................................................................................217
8.3 使用硬引用....................................................................................................................217
8.3.1 把一个变量当作变量名使用.............................................................................217
8.3.2 把一个 BLOCK 块当作变量名用...................................................................219
8.3.3 使用箭头操作符.................................................................................................219
8.3.4 使用对象方法.....................................................................................................222
8.3.5 伪散列.................................................................................................................222
8.3.6 硬引用可以用的其他技巧.................................................................................224
8.3.7 闭合(闭包).....................................................................................................226
8.3.7.1 用闭合做函数模板..................................................................................229
8.3.7.2 嵌套的子过程..........................................................................................230
8.4 符号引用........................................................................................................................231
8.5 花括弧,方括弧和引号................................................................................................232
8.5.1 引用不能当作散列键字用.................................................................................234
8.5.2 垃圾收集,循环引用和弱引用.........................................................................235
第九章数据结构.........................................................................................................................236
9.1 数组的数组....................................................................................................................237
6
9.1.1 创建和访问一个两维数组.................................................................................237
9.1.2 自行生长.............................................................................................................238
9.1.3 访问和打印.........................................................................................................240
9.1.4 片段.....................................................................................................................241
9.1.5 常见错误.............................................................................................................242
9.2 数组的散列....................................................................................................................245
9.2.1 数组的散列的组成.............................................................................................245
9.2.2 生成数组的散列.................................................................................................246
9.2.3 访问和打印数组的散列.....................................................................................247
9.3 散列的数组....................................................................................................................248
9.3.1 组成一个散列的数组.........................................................................................248
9.3.2 生成散列的数组.................................................................................................249
9.3.3 访问和打印散列的数组.....................................................................................250
9.4 散列的散列....................................................................................................................251
9.4.1 构成一个散列的散列.........................................................................................251
9.4.2 生成散列的散列.................................................................................................252
9.4.3 访问和打印散列的散列.....................................................................................255
9.5 函数的散列....................................................................................................................257
9.6 更灵活的记录................................................................................................................258
9.6.1 更灵活的记录的组合,访问和打印.................................................................258
9.6.2 甚至更灵活的记录的组合,访问和打印.........................................................259
9.6.3 复杂记录散列的生成.........................................................................................261
9.7 保存数据结构................................................................................................................263
第十章包.....................................................................................................................................264
10.1 符号表..........................................................................................................................268
10.2 自动装载......................................................................................................................273
第十一章模块.............................................................................................................................276
11.1 使用模块......................................................................................................................276
11.2 创建模块......................................................................................................................278
11.2.1 模块私有和输出器...........................................................................................279
11.2.1.1 不用 Exporter 的输入方法进行输出..................................................281
11.2.1.2 版本检查................................................................................................282
11.2.1.3 管理未知符号........................................................................................282
11.2.1.4 标签绑定工具函数................................................................................283
11.3 覆盖内建的函数..........................................................................................................283
第十二章对象(上).................................................................................................................285
12.1 简单复习一下面向对象的语言..................................................................................285
12.2 Perl 的对象系统..........................................................................................................286
12.3 方法调用......................................................................................................................287
12.3.1 使用箭头操作符的方法调用...........................................................................288
12.3.2 使用间接对象的方法调用...............................................................................289
12.3.3 间接对象的句法障碍.......................................................................................290
12.3.4 引用包的类.......................................................................................................292
12.4 构造对象......................................................................................................................293
7
12.4.1 可继承构造器...................................................................................................294
12.4.2 初始器...............................................................................................................296
12.5 类继承..........................................................................................................................298
第十二章对象(下).................................................................................................................300
12.5.1 通过 @ISA 继承.............................................................................................300
12.5.2 访问被覆盖的方法...........................................................................................301
12.5.3 UNIVERSAL:最终的祖先类.........................................................................303
12.5.4 方法自动装载...................................................................................................306
12.5.5 私有方法...........................................................................................................308
12.6 实例析构器..................................................................................................................309
12.6.1 用 DESTROY 方法进行垃圾收集.................................................................310
12.7 管理实例数据..............................................................................................................310
12.7.1 用 use fields 定义的域....................................................................................313
12.7.2 用 Class::Struct 生成类..................................................................................316
12.7.3 使用 Autoloading(自动装载)生成指示器..................................................318
12.7.4 用闭合域生成指示器.......................................................................................320
12.7.5 将闭合域用于私有对象...................................................................................321
12.7.6 新技巧...............................................................................................................324
12.8 管理类数据..................................................................................................................326
12.9 总结..............................................................................................................................329
第十三章重载.............................................................................................................................329
13.1 overload 用法...............................................................................................................330
13.3 可重载操作符..............................................................................................................333
13.4 拷贝构造器(=) .......................................................................................................341
13.5 当缺失重载句柄的时候(nomethod 和 fallback) .................................................342
13.6 重载常量......................................................................................................................343
13.7 公共重载函数..............................................................................................................345
13.8 继承和重载..................................................................................................................346
13.9 运行时重载..................................................................................................................346
13.10 重载诊断....................................................................................................................346
第十三章捆绑(tie)变量上.....................................................................................................347
14.1 捆绑标量......................................................................................................................349
14.1.1 标量捆绑方法...................................................................................................350
14.1.2 魔术计数变量...................................................................................................355
14.1.3 神奇地消除 $_.................................................................................................356
14.2 捆绑数组......................................................................................................................358
14.2.1 数组捆绑方法....................................................................................................360
14.2.2 大家方便...........................................................................................................365
第十四章捆绑(tie)变量下.....................................................................................................367
14.3 捆绑散列......................................................................................................................367
14.3.1 散列捆绑方法....................................................................................................368
14.4 捆绑文件句柄...............................................................................................................376
14.4.1 文件句柄捆绑方法...........................................................................................378
14.4.2 创建文件句柄....................................................................................................388
8
14.5 一个精细的松绑陷阱..................................................................................................395
14.6 CPAN 里的模块..........................................................................................................399
第十五章 UNICODE.................................................................................................................400
15.1 制作字符......................................................................................................................401
15.2 字符语意的效果..........................................................................................................403
第十六章,进程间通讯...............................................................................................................409
16.1 信号..............................................................................................................................409
16.1.1 给进程组发信号...............................................................................................412
16.1.2 收割僵死进程...................................................................................................413
16.1.3 给慢速操作调速...............................................................................................415
16.1.4 阻塞信号...........................................................................................................416
16.2 文件..............................................................................................................................416
16.2.1 文件锁定...........................................................................................................417
16.2.2 传递文件句柄...................................................................................................421
16.3 管道..............................................................................................................................425
16.3.1 匿名管道...........................................................................................................425
16.3.2 自言自语...........................................................................................................428
16.3.3 双向通讯...........................................................................................................431
16.3.4 命名管道...........................................................................................................434
16.4. System V IPC...............................................................................................................436
16.5. 套接字.........................................................................................................................442
16.5.1 网络客户端程序...............................................................................................443
16.5.2 网络服务器.......................................................................................................446
16.5.3 消息传递...........................................................................................................450
第十七章线程.............................................................................................................................453
17.1 进程模型......................................................................................................................453
17.2 线程模型......................................................................................................................454
17.2.1 线程模块...........................................................................................................456
17.2.1.1 创建线程................................................................................................456
17.2.1.2 线程删除................................................................................................457
17.2.1.3 捕获来自 join 的例外..........................................................................459
17.2.1.4 detach 方法............................................................................................459
17.2.1.5 标识线程................................................................................................460
17.2.1.6 列出当前线程........................................................................................460
17.2.1.7 交出处理器............................................................................................460
17.2.2 数据访问...........................................................................................................461
17.2.2.1 用 lock 进行访问同步.........................................................................461
17.2.2.2 死锁........................................................................................................464
17.2.2.3 锁定子过程............................................................................................464
17.2.2.4 locked 属性............................................................................................466
17.2.2.5. 锁定方法...............................................................................................467
17.2.2.6 条件变量................................................................................................467
17.2.3 其他线程模块...................................................................................................469
17.2.3.1 队列(queue) ......................................................................................469
9
17.2.3.2. 信号灯...................................................................................................471
17.2.3.3 其他标准线程模块................................................................................471
第十八章编译.............................................................................................................................472
18.1. Perl 程序的生命周期.................................................................................................472
18.2 编译你的代码..............................................................................................................474
18.3 执行你的代码..............................................................................................................479
18.4 编译器后端...................................................................................................................482
18.5 代码生成器..................................................................................................................483
18.5.1 字节码生成器...................................................................................................483
18.5.2. C 代码生成器..................................................................................................484
18.6 提前编译,回头解释...................................................................................................487
第十九章命令行接口.................................................................................................................491
19.1 命令行处理..................................................................................................................491 |