Академический Документы
Профессиональный Документы
Культура Документы
Third Edition
Perl
2002
Perl
.
.
.
.
. , .
.
.
.
., ., .
Perl. . . : , 2002.
1152 ., .
ISBN 5932860200
Perl,
, 1991 . Perl ,
, ,
Perl , .
Perl .
. , Perl ,
,
. ,
Perl, ,
.
Perl .
? .
Perl 5.6,
. ,
, ,
. , ,
, Unicode, , ,
pod Perl.
ISBN 5932860200
ISBN 0596000278 ()
, 2002
Authorized translation of the English edition 2000 OReilly & Associates Inc. This
translation is published and sold by permission of OReilly & Associates Inc., the own
er of all rights to publish and sell the same.
,
.
, , .
. 193148, , . , 4,
. (812) 3245353, edit@symbol.ru. N 000054 25.12.98.
00593, 2; 953000 .
28.02.2002. 701001/16 . .
72 . . 3000 . N
199034, , 9 , 12.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1. Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
, () . . . . . . . . . . . . . . . . . . . . . . 76
II. Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
() . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
. . . . . . . . . . . . . . . .128
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
. . . .133
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
C ( ) . . . . . . . . . . . . . .140
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146
() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
and, or, not xor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
C, Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148
4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
if unless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162
goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247
6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286
8. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
, . . . . . . . . . . . . . . . . . . 311
9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344
11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353
12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
. . . . . . . . . 355
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394
13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395
overload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .398
(=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405
(nomethod fallback) . . . . .406
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .407
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
System V IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .497
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .518
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
: . . . . . . . . . . . . . . . .532
19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .582
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .583
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Perl ( C Perl) . . . . . . . . . . . . . . . . . . . . . . . .584
Perl C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .596
23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .609
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .622
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629
10
24. . . . . . . . . . . . . . . . .639
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .648
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .662
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .672
V. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .709
28. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .711
, . . . . . . . . . . . . . . . . . . . 712
. . . . . . . . . . . . . . . . . .714
29. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .736
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .741
11
31. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .900
use attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .901
use autouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .902
use base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .903
use blib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .904
use bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .904
use charnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .905
use constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .906
use diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .908
use fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .910
use filetest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .912
use integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .913
use less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .914
use lib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .914
use locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .916
use open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .917
use overload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .917
use re . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .918
use sigtrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .919
use strict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
use subs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .925
use vars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .925
use warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .926
32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .930
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .931
Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .941
Carp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .943
CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .944
CGI::Carp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .944
Class::Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .945
Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .946
CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .947
Cwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .947
Data::Dumper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
DB_File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
Dumpvalue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .950
English . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .950
Errno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .951
Exporter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .951
Fatal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .952
Fcntl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .953
File::Basename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .953
File::Compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .954
File::Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .955
File::Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .955
12
File::Glob . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956
File::Spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .959
File::stat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .960
File::Temp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .960
FileHandle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .961
Getopt::Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .964
Getopt::Std . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .965
IO::Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .966
IPC::Open2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .967
IPC::Open3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .967
Math::BigInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .968
Math::Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .969
Math::Trig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .969
Net::hostent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .969
POSIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .970
Safe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .972
Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .973
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .974
Sys::Hostname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .975
Sys::Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .976
Term::Cap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .977
Text::Wrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .978
Time::Local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .978
Time::localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .979
User::grent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .980
User::pwent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .980
33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1045
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1084
Perl , .
, ,
.
,
, , .
,
.
(in
dustrialstrength languages),
.
Perl . , ,
, .
, ? ,
, . ,
, ,
, .
.
,
.
, . , ,
,
.
, , Perl.
Unix, Perl
.
Perl ,
, .
C C++,
#ifdef .
Java
.
14
, , , .
Visual Basic
.:)
Perl ,
.
: ,
Perl ,
open source .
Perl. Perl ,
, . Perl
, : , 1.
,
Perl .
Perl .
Unix
. Perl
Unix,
. , ,
: , Windows
Perl Unix
.
Perl
, , ;
Perl .
, Perl
,
, ,
, , ,
.
,
. Perl ,
.
Perl ,
, ,
,
. Perl ,
,
1
Give me your tired, your poor, Your huddled masses yearning to breathe free
, . . .
15
, .
Perl ,
,
Perl. ( ,
, , .)
. Perl open source
, . Perl
, . Perl ,
, .
Perl, . Perl
.
, , Perl ,
: . Perl
.
Perl
.
, Perl
. Perl,
. ,
; .
Perl, :
, ,
,
, , .
,
Obfuscated Perl Contest.
Perl ,
.
. Perl
. , , Perl
Practical Extraction and Report Language (
). , , Perl Pathologically
Eclectic Rubbish Lister (
). Perl
. .
( ).
. , , .
Perl is a simple language ( ).
,
Perl
. Perl . Perl
16
, ,
. Perl
,
, (,
C, awk, BASIC, Python, ).
Perl
, .
, Perl ,
. Perl
. Perl,
. , ,
. Perl
, ,
, ,
. Perl .
. Perl
, , .
, Perl ,
.
.
Perl,
, .
Perl ,
,
, ,
,
. Perl.
,
, ,
, ,
. Perl
,
Perl .
Perl
. . Perl
, ,
,
. Perl ,
Perl.
,
Perl.
Perl.
Perl .
, ,
17
, Perl
.
,
.
Perl ,
(
, C ).
,
,
.
Perl
Perl ,
. ,
,
, ,
. Perl , .
Perl .
, ,
Perl
,
, ,
. , Perl
,
.
, , Perl
, Perl, ,
. Perl ,
. , Perl
, . ,
,
. , .
, Perl,
,
,
. ,
,
.
: , (laziness, impa*
tience, hubris). , , ,
( ).
, ,
, Perl .
.
18
, .
, (
, ),
, . ,
,
.
.
, , ,
.
,
Perl.
, ,
. Perl ,
. , ,
, ,
. Perl ,
.
, ,
, ,
. :
1
. Perl
.
,
, .
,
.
2 Perl
, ,
. ,
,
. ( , ,
.)
3 Perl
Perl,
. ,
Perl ,
, ,
19
, , ,
C C++
API. Perl
, ,
, .
4 Perl
, ,
Perl , .
Perl
, .
,
,
.
5
,
. ,
, .
, , , ,
. ( , ,
.)
Perl .
Perl AIX,
BeOS, BSDI, Debian, DG/UX, DYNIX/ptx, FreeBSD, IRIX, LynxOS, Mac OS X,
OpenBSD, RedHat, SINIX, Slackware, Solaris, SuSE Tru64.
Perl CD
. ,
ActiveState,
, Microsoft.
Perl ,
, , Perl
. ,
, ,
. , Perl
, ,
,
<option>D</option>.
( .)
Perl,
www.perl.com,
20
, C
.
CPAN (Comprehensive Perl Archive
Network, Perl, 22 CPAN) http://
www.perl.com/CPAN http://www.cpan.org.
( ,
), CPAN .
URL ,
:
http://www.funet.fi/pub/languages/perl/CPAN
ftp://ftp.funet.fi/pub/languages/perl/CPAN/
ftp://ftp.cs.colorado.edu/pub/perl/CPAN/
ftp://ftp.cise.ufl.edu/pub/perl/CPAN/
ftp://ftp.perl.org/pub/perl/CPAN/
http://www.perl.com/CPAN*local
http://www.cpan.org/
http://www.perl.org/CPAN/
http://www.cs.uu.nl/mirror/CPAN/
http://CPAN.pacific.net.hk/
funet.fi
CPAN. CPAN
MIRRORED.BY, ,
. FTP,
HTTP ( ,
). http://www.perl.com/CPAN
.
.
,
README INSTALL, ,
Perl. INSTALL.platform, platform
.
Unix, ,
, , Perl,
. , ,
. ftp :
% ftp ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/latest.tar.gz
( CPAN. ,
, CPAN.)
ftp
:
% wget http://www.funet.fi/pub/languages/perl/CPAN/src/latest.tar.gz
21
, , :
%
%
%
%
C,
C , Perl .
CPAN ports
Perl ( , ),
. ,
, ,
C (
, ).
Perl
. (
.) ,
CPAN.
Perl,
Perl,
. (manpage)
,
Unix man. Perl
HTML, , Unix.
Perl ,
,
. perl, Unix
man perl .1 ,
, . , man perlre
Perl. perldoc
, man.
Macintosh Shuck.
Perl
HTML .
, , .
1
, , ,
4. , MANPATH ,
. ( perldoc perl,
, MANPATH perl V:man.dir.)
22
( Perl, 1987 )
perl 24
. ,
. ( egrep.)
.
, ,
,
1500 ,
. (
CPAN, , .)
:
perl ,
, . , , , .
,
. , ,
, ,
, . , ,
, , .
:
perl
Perl
perldata
perlsyn
perlop
perlre
perlvar
perlsub
perlfunc
perlmod
Perl
perlref
perlobj
perlipc
perlrun
Perl
perldebug
perldiag
, . ,
, perlop.
23
, perl*
var. , perl*
diag. .
Perl
(FAQ). :
perlfaq1
Perl
perlfaq2
Perl
perlfaq3
perlfaq4
perlfaq5
perlfaq6
perlfaq7
Perl
perlfaq8
perlfaq9
:
perlamiga
Amiga
perlcygwin
Cygwin
perldos
MSDOS
perlhpux
HPUX
perlmachten
Power MachTen
perlos2
OS/2
perlos390
OS/390
perlvms
DEC VMS
perlwin32
MSWindows
( . 25 Perl
CPAN ports, .)
1500 ,
. , (grep)
.1 ,
1
, .
24
, 5.6.1
.
,
Perl (. 5
) :
% perlop comma
% perlfunc split
% perlvar ARGV
% perldiag 'assigned to typeglob'
,
, . , FAQ
perlfaq (
):
% perlfaq round
perltoc ( )
:
% perltoc typeglob
perl5005delta: Undefined value assigned to typeglob
perldata: Typeglobs and Filehandles
perldiag: Undefined value assigned to typeglob
Perl,
, , perlhelp:
% perlhelp CORE::GLOBAL
. perldoc.
, Perl
, Perl, getiti*
mer(2), getitimer 2
Unix Programmer's Manual.1 ,
getitimer, , Unix,
,
. ,
1
2
. ( (system calls),
(syscalls),
system, .
,
, C,
, getitimer(2) 3.
25
, Unix, ,
.
+crypt(3)+manual AltaVista, .
Perl
1 man, (1)
. ,
perl.
, Perl, :
26
Perl.
Writing Apache Modules with Perl and C, by Lincoln Stein and Doug MacEac
hern, OReilly, 1999 ( Apache Perl C, . ).
,
, mod_perl
CGI Perl
Apache API.
Perl,
. (
.)
, Perl,
. Perl,
, .
. , 1 ; 2
; 3 ; 3 ,
, 2000.
27
,
. (,
, .)
Perl
Perl http://www.perl.com/.
, Perl, ,
, , .
Perl Mongers http://www.perl.org,
Perl , , ,
, ,
. PM
,
Perl,
.
( Usenet)
Perl , ,
Perl. comp.lang.perl.moderated
,
.
.
comp.lang.perl.misc
Perl, Perl Perl.
Perl, comp.lang.perl.misc ,
.1
1
, , (
FAQ.
, ,
, ?)
28
comp.lang.perl.tk , Perl
Tk. comp.lang.perl.modules
Perl,
. ,
comp.lang.perl., .
Usenet
, , "news:" ,
. ( ,
.) ,
Usenet , AltaVista Deja,
"*perl*" , .
, ,
, , CGI ,
comp.infosystems.www.authoring.cgi. , ,
Perl, Perl.
Perl, ,
mod_perl Apache; comp.in*
fosystems.www.servers.unix.
, Perl,
,
perl*
bug, Perl. http://
bugs.perl.org.
,
.
24
.
( ).
:
URL, , .
.
, , .
.
(""),
.
29
. , ,
w w.
,
.
,
.
,
.
, #!.
:
#!/usr/bin/perl
,
. % :
% perl e 'print "Hello, world.\n"'
Hello, world.
Unix,
.
. ,
MSDOS VMS ,
, .
,
, . :
(Todd Miller), (Sharon Hopkins Rauenzahn),
(Rich Rauenzahn), (Paul Marquess),
(Paul Grassie), (Nathan Torkington),
(Johan Vromans), (Jeff Haemer), (Gurusamy
Sarathy), (Gloria Wall), (Dan Sugalski)
(Abigail).
(Tim OReilly)
Associates ,
.
30
,
, ,
(
!). ,
:
OReilly & Associates, Inc.
101 Morris Street Sebastopol, CA 95472
18009989938 ( )
17078290515 (/)
17078290104 ()
.
,
info@oreilly.com.
,
bookquestions@oreilly.com.
,
, :
http://www.oreilly.com/catalog/pperl3
,
, , .
1
Perl
, Perl ,
, . ,
Perl , ,
, , .
,
, ,
. ,
, ,
. ,
, :
print "Howdy, world!\n";
.
Perl.
1, Perl,
"Howdy, world". (\n
.)
(script), (application),
(executable), (doohickey). .
34
1. Perl
. , ,
. , Perl
,
. , exit, *
,
. . Perl
, , .
, Perl ,
, .
, , Perl
. Perl
, , ,
. Perl
.
,
Perl .
. .
,
2 ,
. , ,
, ,
(Randal Schwartz) Learning
Perl ( O'Reilly & Associates).
.
Perl , ,
, ,
. Perl
, ,
( ). ,
: (. ). ,
Perl .
.
.1 (
) , Perl ,
, ,
. ,
.
,
,
1
, .
35
,
. ( , .)
,
.
,
, ,
, .
Perl ,
.
Perl . ,
, ,
, .
. (
, . Perl.)
,
Perl, .
,
: ,
.
.
.
,
, .
: , . .
dog () , ,
, .
, ,
, . If you dog a dog during
the dog days of summer, you'll be a dog tired dogcatcher.1
Perl .
, . , Perl ,
, . Perl
. ,
, Perl . (
Perl Perl , .)
.
,
.
,
, . ,
1
dog . . .
, , , .
, , Perl
!
36
1. Perl
. , ,
.
.
,
,
, . ,
, ,
. ,
.
,
, . Perl
,
. . (,
, local, my our
29 , ,
4 .)
, . , Perl
. ,
. (
, ,
,
.) *
, .
,
( ) :
$phrase = "Howdy, world!\n";
print $phrase;
# .
#
, ,
$phrase. $ Perl , phrase
, . .
. , @. (
, $ s, scalar,
@ a, array.)
Perl
, (hash), (handle)
(typeglob). ,
(funny characters
). ,
:
37
$cents
( )
@large
%interest
&
&how
Perl,
Typeglob
*struck
, struck
Perl. .
,
, (interpolation),
. , Perl
(, Perl!),
.
. ( , Perl
.)
: ,
.
! ( .)
,
= ,
.
: ,
, ,
.
, .
Unix1,
.
(double quotes) 2
(, \n
), .
1
, Unix, ,
Unix, BSD, Linux , , Unix.
,
Perl . ,
, .
( ),
(
).
38
1. Perl
( )
, ,
.
$answer = 42;
$pi = 3.14159265;
$avocados = 6.02e23;
$pet = "Camel";
$sign = "I love my $pet";
$cost = 'It costs $100';
$thence = $whence;
$salsa = $moles * $avocados;
$exit = system("vi $file");
$cwd = `pwd`;
#
#
#
#
#
#
#
#
#
#
""
, ,
,
.
$ary = \@myarray;
$hsh = \%myhash;
$sub = \&mysub;
#
#
#
$ary = [1,2,3,4,5];
#
$hsh = {Na => 19, Cl => 35}; #
$sub = sub { print $state }; #
$fido = new Camel "Amelia"; #
,
,
, .
: "" 0. ,
, true ()
false (), . ,
. Perl
, ,
.
, ,
. ( ,
.) Perl
, ,
. , , :
$camels = '123';
print $camels + 1, "\n";
$camels ,
, , ,
39
124. , "\n",
, ,
. , ,
: , . . '\n',
, n,
.
,
.
, . (
,
, ,
, .
. Perl
,
.)
. ,
:
$fido = new Camel "Amelia";
if (not $fido) { die "dead camel"; }
$fido>saddle();
Camel,
$fido. $fido
true (. .
, ); ,
new Camel Camel.
$fido , saddle()
, $fido Camel,
Perl saddle() Camel.
. , Perl ,
Perl , , ,
.
,
. Perl :
. :
, ,
. ,
,
.
.
, . ,
. .
40
1. Perl
. ( . ,
,
, .)
. ,
1 . ,
. (
() ().)
,
:
@home = ("", "", "", "");
, @home , ,
, ,
. , , ,
:
($potato, $lift, $tennis, $pipe) = @home;
.
, :
($alpha,$omega) = ($omega,$alpha);
C, (zerobased),
, ,
0 3.2
[ ],
$home[n], n
( ), .
. ,
$.
,
:
$home[0]
$home[1]
$home[2]
$home[3]
1
=
=
=
=
"";
"";
"";
"";
(, )
. , .
, , . .
. ,
, 0. .
( .)
41
,
, push pop. , ,
.
. Perl . (
Perl
, , .)
. ,
1 ,
.
. ,
,
. ,
, , .
( ,
,
.)
push pop : . .
, .
, , , Perl.
. 1.1 (
) .
. 1.1.
1
(, )
. , .
42
1. Perl
,
, .
, ,
.
,
%. ( %,
, .
.)
,
. :
%longday = ("", "", "", "", "", "",
"", "", "", "", "",
"", "", "");
, Perl
=> ( , ) ,
.
( ) ,
, :
%longday
""
""
""
""
""
""
""
);
= (
=> "",
=> "",
=> "",
=> "",
=> "",
=> "",
=> "",
, , ,
,
/, , .
. ,
, , keys.
,
sort.
.
,
( , 1).
, , ,
1
, ,
brace(s), curly brace(s)/curly brackets curlies (
, ). . .
43
, (), $longday{""}.
, ,
$, %,
.
,
, of
s. , :
$wife{""} = "";
, ,
. ,
.
, . Perl
, ,
. , Perl
, .
,
, ,
, .
.
, ,
. ,
. ( .) Perl,
, ,
. (
.) ,
:
$wife{""} = ("", "", "", ");
, , Perl
. (
,
.) Perl,
. ,
:
$wife{""} = ["", "", "", "];
# ok
$wife{""}. ,
. Perl
.
,
:
44
1. Perl
$wife{""}[0]
$wife{""}[1]
$wife{""}[2]
$wife{""}[3]
=
=
=
=
"";
"";
"";
"";
, ,
, .
, , ,
,
. .
. ( ,
, .
, , .)
$kids_of_wife{""} = {
""
=> ["", "", "", "", "", ""],
"" => ["", ""],
"" => ["", ""],
"" => ["", ""],
};
:
$kids_of_wife{""}{""}[0]
$kids_of_wife{""}{""}[1]
$kids_of_wife{""}{""}[2]
$kids_of_wife{""}{""}[3]
$kids_of_wife{""}{""}[4]
$kids_of_wife{""}{""}[5]
$kids_of_wife{""}{""}[0]
$kids_of_wife{""}{""}[1]
$kids_of_wife{""}{""}[0]
$kids_of_wife{""}{""}[1]
$kids_of_wife{""}{""}[0]
$kids_of_wife{""}{""}[1]
=
=
=
=
=
=
=
=
=
=
=
=
"";
"";
"";
"";
"";
"";
"";
";
"";
"";
"";
"";
,
. Perl
,
.
, Perl
.
Perl.
Camel :
$fido = new Camel "Amelia";
Camel, $fido.
Camel .
,
45
Camel (
Camel). Camel ,
Camel, (
"Amelia", "fido") ( ,
, , : ).
, .
,
.
, . *
(topicalization),
, ,
( , , , ).
.
,
, .
( indis
soluble asphyxiant) (
everlasting gobstopper1).
.
, ,
. ,
.
X. ,
, : , , ,
?
Perl .
(package declaration). ,
Perl . ,
Camel :
package Camel;
.
, , , Perl
Camel. ,
Camel:: .
:
package Camel;
$fido = &fetch();
1
. . .
46
1. Perl
&new Camel,
&Camel::new. :
$fido>saddle();
&Camel::saddle, $fido ,
Camel.
.
package Camel .
. Perl
use (),
,
. , :
use Camel;
, :
$fido = new Camel "Amelia";
Perl , Camel.
, , , ,
Camel, , Camel
. , Camel. ,
Perl Perl, CPAN (Comprehensive Perl
Archive Network) , ,
47
, , .
:
use Some::Cool::Module;
,
.
,
Perl ,
.
, Perl
. (prag*
mas). , strict:
use strict;
strict ,
,
Perl , ,
. .
Perl ,
strict Perl ,
. strict
, Perl
, .
.
, Perl :
Perl . ,
, Perl
. ,
,
. (
(procedures),
.)
print ( ):
print " $wife{''}.\n";
:
.
.
,
if.
48
1. Perl
,
() .
(functions)
,
.
:
$e = exp(1);
# 2.718281828459
Perl
. ,
. (
) (
).1 ,
.
, Perl
. Perl
.
, , ,
. Perl , (glue)
, , ,
.
Perl (plain old computer
language). .
, Perl ,
.
, .
,
. ( grades ),
:
1
Perl (&)
, ( $fido = &fetch();).
Perl 5 ,
, ,
, ($fido = fetch();).
, , ,
($fetcher = \&fetch;).
&fetch , to fetch,
do fetch. do fetch,
fetch. ,
Perl 5.
49
Nol 25
Ben 76
Clementine 49
Norm 66
Chris 92
Doug 42
Carol 25
Ben 12
Clementine 0
Norm 66
...
,
. ,
Carol. , , Carol,
, Carol (
Noel).
, ,
BASIC .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl
open(GRADES, "grades") or die " grades: $!\n";
while ($line = <GRADES>) {
($student, $grade) = split(" ", $line);
$grades{$student} .= $grade . " ";
}
foreach $student (sort keys %grades) {
$scores = 0;
$total = 0;
@grades = split(" ", $grades{$student});
foreach $grade (@grades) {
$total += $grade;
$scores++;
}
$average = $total / $scores;
print "$student: $grades{$student}\t: $average\n";
}
, ,
, ,
, .
,
. , ,
, .
,
, , , .
50
1. Perl
, , , , Perl.
,
Perl, , , perl.
: There's More Than One Way To Do It
.1
perl ( ,
) perl
.2 , e
( %
, ). Unix :
% perl e 'print "Hello, world!\n";'
. :
, Perl, 80 .3
( )
, , , gradati*
on, :
% perl gradation
Perl , ,
,
,
.
, (
)
.
.
,
1
Perl, ,
,
. TMTOWTDI, .
, . ,
TMTOWTDI.
, . ,
Mac, ,
BSD, Mac OS X.
(oneliners).
Perl, ,
.
Perl .
51
( perl /usr/bin,
#!.) :
% gradation
, ,
(. chmod(1))
PATH. PATH,
, ,
, :
% /home/sharon/bin/gradation
, , Unix,
#!,
32 (,
), :
#!/bin/sh # perl,
eval 'exec /usr/bin/perl S $0 ${1+"$@"}'
if 0;
, /bin/csh, DCL, COMMAND.COM
,
. .
#!/usr/bin/
perl , ,
.
: ,
test. Unix test, ,
, .
try.
: Perl,
, w, .
(
). w :
#!/usr/bin/perl w
52
1. Perl
, , Perl (
perl), .
,
. 3 4
GRADES,
Perl, (filehandle).
, , ,
,
, .
( (stre
ams) C++ / BASIC.)
. (glue language)
Perl ,
.
,
.1
open. open
: , . Perl
(
) . STDIN
, STDOUT . STDERR
,
, (
) .2
1
, Perl
: 8 , ,
. ,
. , ,
. (
). , ,
.
TMTOWTDI.
,
,
( ). Perl
,
. Unix ,
, .
/
, .
53
open
( , , ),
. ,
, .
open(SESAME,
open(SESAME,
open(SESAME,
open(SESAME,
open(SESAME,
open(SESAME,
"_")
#
"<_")
# ( , )
">_")
#
">>_")
#
"| __") #
"__ |") #
, .
SESAME
, ( ,
, close(SESAME))
open .1
,
, <>.
(angle operator).
(<SESAME>), .
<> ,
, STDIN, . (
.) STDIN
, , :
print STDOUT " : ";
#
$number = <STDIN>;
#
print STDOUT " $number.\n"; #
, ? STDOUT
print?
.
print, , ,
.
, STDOUT.
STDIN , STDOUT
. ( 18
,
.)
1
, ,
.
, . , ,
open($handle,$file), $handle . , $han
dle ,
. $handle , Perl
.
54
1. Perl
, ,
. ,
( , , "9\n"). ,
, Perl chop chomp.
chop ( ) ,
chomp ( "\n")
.
:
chop($number = <STDIN>);
:
$number = <STDIN>;
chop($number);
#
#
, Perl .
:
,
.
,
. ,
. Perl
, , ,
C, FORTRAN, sed(1) awk(1),
.
,
Perl .
Perl
, .
, (
) ( ).
,
, , , , .
,
.
, ,
.
, :
55
$a + $b
$a $b
$a * $b
$a $b
$a % $b
$a $b
$a ** $b
$a $b
, , ,
. , . (
3 .)
,
( ;
).
.
,
(. . ). ,
, , Perl
(.):
$a = 123;
$b = 456;
print $a + $b;
print $a . $b;
# 579
# 123456
, *
(repeat). , (x),
:
$a = 123;
$b = 3;
print $a * $b;
print $a x $b;
# 369
# 123123123
,
.
,
. , Perl
.
, .
, , ,
. . .
.
, ,
.
56
1. Perl
.
:
print $a . ' ' . $b . ".\n";
print $a, ' ', $b, ".\n";
print "$a $b.\n";
# ""
#
#
,
. ( , .)
x ,
, :
print "" x $scrwid, "\n";
, $scrwid
(screen width),
(screw identifier).
,
=.
, = , . (
==, ,
,
. == ,
, = ,
.)
, ,
; ,
.
, l*1 (lvalue),
.
. ,
, :
$a = $b;
$a = $b + 5;
$a = $a * 3;
,
: , .
, ,
( C). :
1
57
l =
, :
l = l
, l . (
, l .
, , ,
. .)
, :
$a *= 3;
$a 3.
Perl ,
, C :
$line .= "\n"; # $line.
$fill x= 80;
# $fill 80 .
$val ||= "2"; # $val 2, "true".
6 1 ,
. 14
+=.
, ,
.2 C,
, :
$a = $b = $c = 0;
,
while, 4 .
C, , Perl
, l,
. , :
($temp = 32) *= 5/9;
.
, :
chop($number = <STDIN>);
1
2
, , ?
, , Pascal,
. ,
, , Perl .
58
1. Perl
$number. ,
,
.
$variable += 1 , Perl
C .
( ) ( )
. ,
, :
++$a, $a++
1 $a
$a, $a
1 $a
, (
) , ,
. ,
()
, . :
$a = 5;
$b = ++$a;
$c = $a;
# $a 5
# $b $a, 6
# $c 6, $a 5
15
, ,
.
($scores++), ,
,
,
.
.1
,
(shortcircuit), ,
, if.
,
1
, . (
, , .)
59
() , ,
. .
,
, ,
, . Perl
: California or bust! ( ),
(, ).
Perl :
, C, ( )
, BASIC.
. C, , ,
, ,
, BASIC
, , ,
. ,
. (,
, and, or, not xor 3
.)
,
;
. . 1.1
.
1.1.
$a && $b
$a, $a , $b
$a || $b
$a, $a , $b
! $a
, $a
$a and $b
$a, $a , $b
$a or $b
$a, $a , $b
not $a
, $a
$a xor $b
, $a $b ,
,
Perl .
( 3 )
grades:
open(GRADES, "grades") or die " grades: $!\n";
,
. , ,
.
60
1. Perl
: grades !
, ,
, .
, . (
$! ,
. 28 .) ,
, if while.
, , ,
( ) .
: , . (
.)
, $a $b, :
==
eq
, $a $b
!=
ne
, $a $b
<
lt
, $a $b
>
gt
, $a $b
<=
le
, $a $b
>=
ge
, $a $b
<=>
cmp
0, , 1, $a ,
1, $b
(<=> cmp) .
sort (. 29
).1
,
. ,
, . , ,
,
1
,
, . Perl
, .
, Perl
. .
TMTOWTDI .
.
61
, .
:
e $a
(exists)
, $a
r $a
(readable) , $a
w $a
(writable) , $a
d $a
(directory)
, $a
f $a
(file)
, $a
T $a
(text file)
, $a
:
e "/usr/bin/perl" or warn "Perl \n";
f "/vmlinuz" and print ", \n";
, (regular) ,
(text file). /vmlinuz
, .
,
.
.
, . . (,
) true fal
se. , ,
, ; , ,
3 .
,
, :
.
(
) .
( CGI),
.
62
1. Perl
?
(truth)1 ,
true false.
, , .
Perl ,
, , ,
. ( , ,
.)
, Perl , .
, . Perl
, . ,
, .
Perl .
.
, :
1. , "" "0",
2. , 0, .
3. .
4. .
.
( 3) , ,
, , 0,
. ( 4)
0 .
2 1,
, . ,
,
, 0 "0"
. , "0",
. ,
:
0
1
1
# "0", false.
# "1", true.
, .
63
10 10
# 10 10 0, "0", false.
0.00
# 0, "0", false.
"0"
# "0", false.
""
# , false.
"0.00"
# "0.00", "", "0", true!
"0.00" + 0 # 0 ( +), false.
\$a
# $a, true, $a false.
undef()
# , , false.
, ,
. , Perl
. ,
, ,
.
, . . ,
. ,
, ,
. .
if unless
,
.
if. if (. .
) , :
if ($debug_level > 0) {
# . .
print ": , , !\n";
print ": '54', '42'.\n";
}
,
. if ,
. , C,
. C, ,
Perl .
.
, .
, if ,
, Perl
. if ,
else, , . (
.)
.
elsif
. (
elsif, . .)
64
1. Perl
if ($city eq "New York") {
print " , .\n";
}
elsif ($city eq "Chicago") {
print " , .\n";
}
elsif ($city eq "Miami") {
print " . !\n";
}
else {
print ", , $city.\n";
}
if elsif ,
else.
, ,
. , ,
. if else
, if ;
,
.
unless:
unless ($destination eq $home) {
print " .\n";
}
elsunless . ,
(feature).
()
Perl : while, until, for
foreach. Perl
.
while until
while until if unless,
. ,
. ,
. (. . true while
false until), .
while ($tickets_sold < 10000) {
$available = 10000 $tickets_sold;
print "$available . : ";
$purchase = <STDIN>;
chomp($purchase);
$tickets_sold += $purchase;
}
65
, ,
. , 10 000 ,
, :
print " , ,
.\n";
4 :
while ($line = <GRADES>) {
$line ,
, $line,
while $line. ,
Perl
. .
, .
,
"\n". , "\n"
. ,
.
, ,
,
. ,
. Perl eof,
,
.
,
() . ,
.
:
while (@ARGV) {
process(shift @ARGV);
}
shift
( ). ,
@ARGV, . . 0. 0 Perl
. .1
Perl. 0 0,
, .
,
while (@ARGV != 0). ,
. , .
66
1. Perl
for
for, ,
while, . ( C
.)
for ($sold = 0; $sold < 10000; $sold += $purchase) {
$available = 10000 $sold;
print "$available . : ";
$purchase = <STDIN>;
chomp($purchase);
}
for , :
,
.
. , .
, ,
, , ,
. ,
. (
,
.
, !)
foreach
Perl foreach,
, , , :
foreach $user (@users) {
if (f "$home{$user}/.nexrc") {
print "$user ... vi, perl!\n";
}
}
if while,
, foreach
.
( ,
).
,
. ,
, .
.
Perl
foreach, for, Perl
67
, foreach . ,
,
:
foreach $key (sort keys %hash) {
9 .
: next last
next last
. ,
. ,
Unix
, root lp. next
. last
,
. , ,
, , , .
foreach $user (@users) {
if ($user eq "root" or $user eq "lp") {
next;
}
if ($user eq "special") {
print " special.\n";
#
last;
}
}
,
. (
, )
( ,
):
LINE: while ($line = <ARTICLE>) {
last LINE if $line eq "\n"; #
next LINE if $line =~ /^#/; #
# your ad here
}
: , ^#
? .
. ,
( ).
. Perl ,
Perl.
68
1. Perl
(regular expressions) ( regexes,
regexps RE) ,
grep findstr, (textmunging) ,
sed awk, , vi emacs.
, ,
.1
( Perl5!),
,
Perl. Perl
. , , ,
, ,
. ,
/foo/, ,
(pattern*matching):
if (/Windows 95/) { print " ?\n" }
, , .
, s/foo/bar/, , Perl
, , bar foo. *
(substitution). true false
,
:
s/Windows/Linux/;
, ,
, , . split
, . ,
(separators),
. .
5 12
. ,
:
($good, $bad, $ugly) = split(/,/, "vi,emacs,teco");
( ,
,
,
.)
1
(Jeffrey Friedl) Mastering Regular Expressions
(O'Reilly & Associates).
69
.
.
, .
, , . ,
HTML, HTTP ( FTP).
, HTML
. , http:.
:
while ($line = <FILE>) {
if ($line =~ /http:/) {
print $line;
}
}
=~ ( ) Perl
http: $line.
, ,
( print).1
, =~, Perl
, , $line.
, : ! !
, ,
. , Perl , ,
, , .
$_.
; Perl $_,
Perl , ,
:
while (<FILE>) {
print if /http:/;
}
(, , .
.)
, ,
HTTP? , http:, ftp:,
mailto: . . , ,
?
1
70
1. Perl
while (<FILE>) {
print if /http:/;
print if /ftp:/;
print if /mailto:/;
# ?
}
,
, : ,
.
/[azAZ]+:/, (character
class). az AZ
(
). + ,
, . ,
(quantifier), ,
, . (
, , ,
. ,
.)
, ,
, Perl :
ASCII
[ \t\n\r\f]
\s
[azAZ_09]
\w
[09]
\d
, . \w
, .
( + ? \w+ .) Perl
,
, , \D , .
, \w [azAZ_09] ( \d
[09]). (locales)
, ASCII,
\w. Perl
Unicode, Unicode
. (Perl
\w.)
,
(.), .1 , /a./
, a,
1
, .
, (.) grep(1).
71
. , at am,
a!, a, a ,
. ,
oasis camel, sheba. caravan
a. a,
, .
, ,
. ,
\w+.
+, . ,
.
.
. ,
, \d{7,11}
, .
, ,
. . . (
{1}.)
, ,
. ,
, .
, \d{7} (
,
), \d{7,}
, (
).
. , , {0,5},
.
,
Perl . +,
, {1,}, .
*, {0,},
, ?, {0,1},
(. . ).
, .
, Perl .
, .
, /\d+/ 1234567890, .
, .,
. :
larry:JYHtPh0./NJTU:100:10:Larry Wall:/home/larry:/bin/tcsh
72
1. Perl
larry: /.+:/. ,
+ , ,
/home/larry:,
, .
, , . .
/[^:]+:/, ,
( ), .
.1
, ,
.
. ,
, ,
. ( ,
.) , ,
(s///) , . . $_,
x . :
$_ = "fred xxxxxxx barney";
s/x*//;
! , x* (
x) ,
, , ,
f fred .2
.
, /bam{2}/
bamm, bambam.
, . bambam
/(bam){2}/.
Perl ,
, . (
, .)
Perl ,
, .
/.*?:/.
.*? ,
, ,
.
, , .
Unix.
. .
73
, . (anchor)
, . , ,
, ,
. , , .
, ,
. ( ,
. ,
.)
\b ,
(\w) ,
(\W), . (,
, .) ,
/\bFred\b/
: LINE,
#.
, \d{7,11}
. ,
:
, /\d{7,11}/, ,
,
!
.
1
, ,
; ^ $ , ,
(lines), (strings).
5 (
).
74
1. Perl
, ,
, ,
.
, ,
, .
, /\d+/ /(\d+)/
,
, .
,
.
, . ,
,
, . ,
HTML, <B>Bold</B>,
/<(.*?)>.*?<\/\1>/. ,
, B .
, ,
, $ , . .
, .
, :
s/(\S+)\s+(\S+)/$2 $1/
(
)
, ,
. :
( ) , Perl
. , ,
. ,
, .
, .
, Perl
: ( )
( ).
.
( )
, .
:
@array = (1 + 2, 3 4, 5 * 6, 7 / 8);
75
,
, ,
.
Perl ,
, . ,
. ?
, , .
, ,
.
. , foreach.
print . .
,
, ,
LIST. , *
. LIST
, ,
. , sort,
:
sort LIST
, sort
.
, (. . Perl
)
, LIST,
LIST.
LIST ,
, .
, :
sort @dudes, @chicks, other();
76
1. Perl
LIST, sort, :
(Barney,Betty,Dino,Fred,Wilma)
(, keys),
(, print), (,
sort). ,
, , ,
,
. :
print reverse sort map {lc} keys %hash;
keys %hash
map, ,
lc, sort,
reverse,
print, .
, Perl , .
,
. ,
, .
,
, :
. ,
,
12:59:59 am. :
($hour, $min, $sec, $ampm) = /(\d+):(\d+):(\d+) *(\w+)/;
. :
@hmsa = /(\d+):(\d+):(\d+) *(\w+)/;
. , ,
Perl,
. , Perl,
, ,
. .
,
()
Perl
. , ,
, ,
, ()
77
,
, . Perl ,
, . Perl
.
Perl, ,
. , Perl
, ,
.
. ( , .)
Perl. , .
, , ,
. .
.
II
Perl
2
Perl.
,
. ,
, Perl
, ,
. ,
, .
, ,
. ( ,
.)
( *
, ), ,
.
, ,
, . (
.) , ,
, 29 .
,
Unix ,
, , .
, , , .
82
2.
,
, ,
Perl, .
: Perl
, ,
.
, , Perl, 7
ASCII. Perl
8 16 ,
. ,
(ASCII) , ,
ASCII .
. , 16
, Perl
, .
15 Unicode , Perl
Unicode.1 :
Unicode (,
), . Unicode
, . Perl
, Unicode
( 1),
. Perl
UTF8. (,
Unicode, U263A,
.)
, ,
. , ,
, (,
),
.
238,
U263A.
: , ,
: , .
: .
Perl,
1
Unicode,
ASCII,
Unicode.
83
. () use bytes
(. 31 ). , Perl
, 8 ,
.
. .
,
. ,
, .
84
2.
.
.
:
Perl ,
. , ,
. (
,
.)
#
.
. Perl
, .1
, =
, Perl ,
, =cut.
, pod (plain old documentation). Perl
, pod Perl
, , LaTeX,
HTML, , , XML. Perl, ,
Perl Perl pod.
.
, Perl, ,
.
26 ;
Perl.
.
#
. : .
, ,
, C,
* . ,
.
Perl, ,
. (statement):
,
.
, (braces),
, , braces .2
1
. Perl
#! (. 19 ).
,
(. Perl
24 ).
braces. . .
85
, , .
(subroutines),
(modules),
(programs). .
.
,
, . ,
.
. ,
, Perl . C,
char, short, int, long, long long, bool, wchar_t, size_t,
off_t, regex_t, uid_t, u_longlong_t, pthread_key_t, fp_exception_field_type . .
!
, , .
Perl :
. ( , Perl,
,
Perl .
12 .) Perl *
, ( *
). , *
. .
,
. ,
.
. ,
(subscript). Perl
0. ,
, Perl :
. (
, .)
, /,
()
(), .
. (
Perl ,
, , , , ,
, .)
86
2.
, ,
.
. .
,
.
. (
. , o
() , .)
, Perl
,
. , , Perl
, ,
, .
, :
$x = $y;
( ,
Perl ==). $y
$x. , $x ,
. ( $x .)
, $x l* ( left value ),
,
. , $y r* ( right
value ), .
, ,
, ,
Perl l r.
:
$x = $y + 1;
Perl $y 1,
,
$x. ,
, , , Perl
, .1
( )
, (
) , ,
,
1
,
(push) (),
(pop) (
).
87
.
: ,
, ( ,
). .
, , , 1
, ,
. , ,
.
.
, ,
.
(funny charac*
ter).1 $,
, .
the
. , :
$days
$days
$days[28]
29 @days
$days{'Feb'}
Feb %days
, $days,
@days %days, Perl .
, ,
, . :
${days}
, $days,
$Dog::days
$days, Dog
$#days
@days
$days>[28]
29 , $days
(
, ).
88
2.
()
$days[0][2]
$days{2000}{'Feb'}
$days{2000,'Feb'}
( )
@,
:
@days
@days[3, 4, 5]
@days[3..5]
@days{'Jan','Feb'}
, ($days{'Jan'},$days{'Feb'})
%:
%days
l,
, . ,
l
, :
@days = 1 .. 7;
,
( ) .
(namespaces).
Perl , *
(symbol tables) (lexical sco*
pes).1
,
. ,
1
Perl (packages)
(pads),
, .
.
89
, ,
(
). ,
,
, .
, . (
. ,
, ,
. .)
( )
,
. , ,
,
( , ,
). , $foo @foo .
, , $foo[1]
@foo, $foo.
, , .
&,
.
l, Perl
l
, .
, , foo,
.
*,
. *
(typeglobs),
. l.
typeglob Perl
. .
, Perl
, .
,
.
, .
( )
.
,
, .
, open(LOG, logfile), open(log, "logfile"),
Perl ,
log (, , ).
90
2.
1 ,
. ,
, ,
, ,
() .
,
,
.
,
FOO, Foo foo Perl .
,
( 1 251
) , ,
Unicode. Unicode
, , .
. 15 Unicode.
, ,
. ,
, $123. , ,
, , ()
(, $? $$) Perl
. , , $$
, $? .
5.6 Perl
. ${^NAME}
, Perl. ,
, .
28 .
,
, , ,
, . . , ,
.
, :::
$Santa::Helper::Reindeer::Rudolph::nose
:
/Santa/Helper/Reindeer/Rudolph/nose
, Perl,
,
1
Perl ,
. ,
.
91
. ,
Santa::Helper::Reindeer::Ru
dolph::, $nose (). (
, , .)
Perl (package),
.
, ,
, . ,
, ,
. , ,
$Dog::bert, $bert Dog::.
, $Cat::bert. . 10 .
, ,
,
"::". (
my.)
, : ? Perl ,
, $bert? . ,
Perl,
:
1. Perl
, my our (
29
4 ). my ,
(. . ).
,
.1
2. , Perl , ,
.
, ,
1
our my
,
,
my.
, our
my. ,
strict (. strict 31
). my,
.
92
2.
,
, , ,
1. Perl , 2,
.
3. Perl , ,
,
(
, eval
STRING). ,
, Perl
,
4. ,
. , Perl ,
,
eval STRING,
, ,
. , Perl
, , eval STRING
, 2,
eval STRING,
.
4. , , Perl
( my, our). Perl
,
. strict,
,
Perl
. ,
.
. Perl
, 1 3,
, .
, ,
,
.
5.
, Perl
, main,
. ,
, $bert , $::bert, , $main::bert.
( main
, $::main::bert, $main::main::bert, $::main::main::bert
. . .
10 .)
,
, .
93
,
, .
.
Perl , ,
.
, , .
. Perl
,
, , . .
.
, ,
,
,
( ).
. , ,
( ),
.
, ,
:
, ,
. ,
, ,
,
.
.
, Perl ,
, ,
.
; . Perl
:
, ,
. , :
$bert
:
${ some_expression() }
some_expression() $bert
( bert), , $bert.
, $ernie,
94
2.
.
( ) ,
8 .
, , ,
,
. ,
. ,
(undefined). ,
, :
,
, .1
Perl
. ,
, ,
, . Perl
, ,
.
. ,
(10/3 == 1/3*10) .
Perl
,
, Perl .
, Perl atof(3)
C. ,
, sprintf(3) "%.14g".
foo
0 ,
. ,
, 5 .
,
. ,
. ,
1
95
, .
,
, .
, , ,
.
. ,
,
, ,
.
.
,
. , Perl
. . 8 .
1
:
$x
$x
$x
$x
$x
$x
$x
=
=
=
=
=
=
=
12345;
12345.67;
6.02e23;
4_294_967_296;
0377;
0xffff;
0b1100_0000;
#
#
#
#
#
#
#
Perl ,
.
Perl .
,
, , , ,
. 0x
, 0b 0
.
: 2
oct,
0x 0b.
1
Unix. ,
!
, Perl
.
, Perl . ,
O'Reilly & Associates , .
02140. ,
02140 1120.
96
2.
. , Unix:
, ( \' \\,
, ).
, \n ( ),
. (
escape*,
.)
, ,
,
, , .
::. ,
$main'var $main::var , ,
, .
, ,
,
. . 2.1.
2.1. Escape*
\n
( LF)
\r
( CR)
\t
\f
\b
\a
()
\e
ESC
\033
ESC
\x7f
DEL
\cC
<Ctrl>+<C>
\x{263a}
Unicode ()
\N{NAME}
\N{NAME} charnames,
31 .
, , \N{GREEK SMALL LETTER SIGMA}, \N{greek:Sigma}
\N{sigma} . .
15 Unicode.
97
escape
, . 2.2.
2.2. Escape*
\u
( (tit
lecase) Unicode)
\l
\U
\L
\Q
,
\E
\U \L \Q
,
. . .
, ,
, , Perl
, ,
. ,
, Perl
, , , .
escape,
, , ,
.
,
.
.1
, ( ),
( ) .
. ,
, $ @,
( ), .
@,
, ,
(\@), .
, %,
, ,
$ @, .
1
, Perl
, .
.
98
2.
" $100.":
$Price = '$100';
print " $Price.\n";
#
#
,
, :
"How ${verb}able!". ,
. :
$days{'Feb'}
:
$days{Feb}
.
:
$days{'February 29th'}
$days{"February 29th"}
$days{ February 29th }
# Ok.
# ok. "" .
# , .
, , :
@days{'Jan','Feb'}
@days{"Jan","Feb"}
@days{ Jan, Feb }
# Ok.
# ok.
# ( use strict)
,
.
,
,
, .
, Perl
.
,
:
print "\n";
print \n ;
# Ok, .
# , .
, Perl
,
. Perl
,
.
2.3 , /
, . (
99
,
Perl .)
2.3.
''
q//
""
qq//
``
qx//
()
qw//
//
m//
s///
s///
y///
tr///
""
qr//
,
, , ,
.
,
,
.
, , ,
. (
.) :
$single = q! : " : ' .'"!;
$double = qq( "" $variable?);
$chunk_of_code = q {
if ($condition) {
print "!";
}
};
,
.
, s/// tr///,
,
.
, , s<foo>(bar)
tr(af)[AF].
,
:
tr (af)
[AF];
100
2.
,
#. q#foo# 'foo', q #foo#
q, .
.
, :
s {foo} # foo
{bar}; #
bar.
tr [af] #
[AF]; #
, ,
, , .
(barewords).1
, 2,
, ,
. ,
Perl . ,
@days = (Mon,Tue,Wed,Thu,Fri);
print STDOUT hello, ' ', world, "\n";
@days
hello world STDOUT.
, Perl
hello, .
,
.
, qw//
, ,
:
@days = qw(Mon Tue Wed Thu Fri);
print STDOUT "hello world\n";
, .
:
use strict 'subs';
1
, ,
,
( ).
, , .
, .
. . .
101
.
.
, :
no strict 'subs';
, :
"${verb}able"
$days{Feb}
, ,
.
, , main::
Dog::, . Perl
Camel:: Camel,
use strict .
,
, , ,
,
, $"1 (
). :
$temp = join( $", @ARGV );
print $temp;
print "@ARGV";
,
, : /$foo[bar]/
/${foo}[bar]/ ( [bar]
) /${foo[bar]}/ ( [bar]
@foo)? @foo , , , . @foo
, Perl , [bar],
.2 ,
, .
, .
1
2
102
2.
(here documents)
(here*documents) Unix.
,
, . ,
, . << ,
, ,
, , .
()
, .
, .
,
. ,
, ,
( ). <<
, ,
, . (
, ,
, , .
! .)
,
.
print <<EOF;
$Price.
EOF
# ,
print <<"EOF"; # , ,
$Price.
EOF
print <<'EOF';
#
All things (e.g. a camel's journey through
A needle's eye) are possible, it's true.
But picture how the camel feels, squeezed out
In one long bloody thread, from tail to snout.
C.S. Lewis
, , ,
( ).
?
EOF
print << x 10;
# 10
! ! !
103
print <<"" x 10; #
! ! !
print <<`EOC`;
#
echo , !
echo !
EOC
print <<" ", <<"camelid";
.
.
camelid
funkshun(<<"", 23, <<');
.
# , ,
, Perl
, :
print <<'odd'
2345
odd
+ 10000; # 12345
,
, :
($quote = <<'QUOTE') =~ s/^\s+//gm;
The Road goes ever on and on,
down from the door where it began.1
QUOTE
, :
@sauces = <<End_Lines =~ m/(\S.*\S)/g;
""
End_Lines
. . . . ,
(. . ). . . .
104
2.
v
, v,
, , ,
:
$crlf = v13.10;
# ASCII ,
v*,
,
v .
,
. , ,
v1.20.300.4000
, :
"\x{1}\x{14}\x{12c}\x{fa0}"
pack("U*", 1, 20, 300, 4000)
chr(1) . chr(20) . chr(300) . chr(4000)
( ),
v .
print v9786;
print v102.111.111;
print 102.111.111;
use 5.6.0;
# Perl ( )
$ipaddr = 204.148.40.9;
# IPv4 oreilly.com
v IP .
, ,
255, v
,
.
IP, v,
,
. , v
printf, "%vd", sprintf 29
. Unicode 15
Unicode, use bytes 31 ;
IPv4 . gethostbyaddr 29 .
,
, Perl
. __LINE__ __FILE__,
105
. ,
. , __PACKAGE__
, .
( package;), __PACKAGE__
. __END__ ( ControlD ControlZ)
. ,
DATA.
__DATA__ __END__,
DATA ,
, require,
DATA.
DATA 28 .
,
. ,
.
1, Perl,
,
. : . ,
:
$x
= funkshun(); #
$x[1]
= funkshun(); #
$x{"ray"} = funkshun(); #
,
:
@x
@x[1]
@x{"ray"}
%x
=
=
=
=
funkshun();
funkshun();
funkshun();
funkshun();
#
#
#
#
, . ,
, , ,
.
106
2.
, :
($x,$y,$z) = funkshun(); #
($x)
= funkshun(); #
my our, :
my
my
my
my
$x
@x
%x
($x)
=
=
=
=
funkshun();
funkshun();
funkshun();
funkshun();
#
#
#
#
,
, (,
funkshun(), ) ,
, , ,
, . (
, .)
,
.
,
.
, , ,
, , .
,
, ,
.
, ,
. , ,
, ,
LIST. .
.1
LIST scalar. Perl
,
, ,
LIST .
,
.
, .
1
, , , LIST
, ,
.
wantarray.
107
Perl
. ,
, , .
, , .
, .
()
*
. ,
.
true false, Perl
: ,
"" 0 ( , "0").
, , 0.
( undef) ,
"" 0, ,
. (
, !)
()
, , , ,
, .
,
, . ,
, ,
true/false . ,
, unlink,
:
unlink @files;
# , .
(
), , ,
, ,
. ,
, ,
:
while (@files) {
my $file = shift @files;
unlink $file or warn " $file: $!\n";
}
@files ,
while, Perl ,
.
, , ,
. ,
108
2.
.
( ) ,
. , ,
, .
defined @files.
, defined ,
undef, . .
. ,
, .
, .
, Perl
,
, ,
. , :
"Camel Lot";
:
Useless use of a constant in void context in myprog line 123;
[ myprog, 123]
,
,
( ,
)
.
qx//,
m//, s///
qr//.
,
.
, , , ,
,
. ( , .)
, ,
.
109
.
( ,
). ()
,
:
(LIST)
, LIST
, ,
,
,
.
.
Perl, , .
,
LIST, .
.
,
C, ,
.
, ,
, .
, , ,
, . .
, .
,
@stuff = ("", "", "");
@stuff,
$stuff = ("", "", "");
$stuff.
@files, ,
, .
, .
.
.
$stuff 3:
@stuff = ("", "", "");
$stuff = @stuff;
110
2.
"", , , ,
, Perl ,
, , , @stuff
. . @stuff
. .
, , ,
.
.
( ),
,
,
. ? ( ,
, .)
LIST ,
. ,
.
,
. ,
.
,
, LIST
. , LIST
,
LIST,
LIST. LIST.1
(@stuff,@nonsense,funkshun())
@stuff, @nonsense,
, &funkshun
. ,
() ,
,
. ().
,
,
. ((),(),()) ().
,
.
:
@releases = (
"",
1
, ,
. , ,
. . 8 .
111
"",
"",
);
:
qw (
).
. :
@froots = qw(
);
( , ,
.
. .)
,
.
().
, , :
(LIST)[LIST]
:
# stat .
$modification_time = (stat($file))[9];
# .
$modification_time = stat($file)[9]; # ,
# .
$hexdigit = ('a','b','c','d','e','f')[$digit10];
# " ".
return (pop(@foo),pop(@foo))[0];
# .
($day, $month, $year) = (localtime)[3,4,5];
,
:
($a, $b, $c) = (1, 2, 3);
($map{red}, $map{green}, $map{blue}) = (0xff0000, 0x00ff00, 0x0000ff);
112
2.
undef . ,
, :
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
:
($a, $b, @rest) = split;
my ($a, $b, %rest) = @arg_list;
,
, ,
, ,
. local
my, , .
:
() = funkshun();
. ,
,
,
.
,
:
$x = ( ($a, $b) = (7,7,7) );
$x = ( ($a, $b) = funk() );
funk()
$x = ( () = funk() );
funk()
# $x 3, 2
# $x ,
# $x ,
,
, 0, false.
while:
while (($login, $password) = getpwent) {
if (crypt($login, $password) eq $password) {
print "$login !\n";
}
}
@days , @days
, :
@days + 0;
scalar(@days)
# @days
# @days
113
, .
. ,
, ,
, C. Perl
, .
@days $#days.
,
, () 0 .
$#days .
.
, ,
. (
, .)
, ().
:
@whatever = ();
$#whatever = 1;
:
scalar(@whatever) == $#whatever + 1;
.
, undef(@whatever).
, , ,
.
, ,
,
. ,
(,
).
Perl , ,
,
/:
%map = ('red',0xff0000,'green',0x00ff00,'blue',0x0000ff);
:
%map = ();
#
$map{red} = 0xff0000;
$map{green} = 0x00ff00;
$map{blue} = 0x0000ff;
114
2.
, => /
. => ,
( ),
,
:
%map = (
red => 0xff0000,
green => 0x00ff00,
blue => 0x0000ff,
);
, :
$rec = {
NAME => 'John Smith',
RANK => 'Captain',
SERNO => '951413',
};
:
$field = radio_group(
NAME
VALUES
DEFAULT
LINEBREAK
LABELS
);
=>
=>
=>
=>
=>
'',
['', '', '', ''],
'',
'true',
\%animal_names,
. .
(%hash) ,
/ . ,
, ,
.
,
, ,
, ,
. , .
( / , ,
.)
keys 29 .
, ,
, true ,
/. / ,
,
, .
, , Perl (
115
) . ,
10 000 , %HASH
1/8, ,
. , 10 000 .
, .
, keys
: scalar(keys(%HASH)).
, .
,
$; ($SUBSCRIPT_SEPARATOR), chr(28).
.
:
$people{ $state, $county } = $census_results;
$people{ join $; => $state, $county } = $census_results;
a2p,
awk Perl.
(, ) ,
9 .
: , DBM (. DB_File 32
), .
.
, :
$hash{ $x, $y, $z }
@hash{ $x, $y, $z }
#
#
Perl typeglob,
. ( *foo
$foo, @foo, %foo, &foo foo.)
typeglob *, .
typeglob ( )
.
, :
$fh = *STDOUT;
:
$fh = \*STDOUT;
, :
116
2.
sub newopen {
my $path = shift;
local *FH;
# my(), our()
open(FH, $path) or return undef;
return *FH;
# \*FH!
}
$fh = newopen('/etc/passwd');
open.
typeglob
. . :
*foo = *bar;
foo ,
bar.
, :
*foo = \$bar;
, ,
. ,
, . (
print
29 .)
117
( )
, ,
(backticks), :
$info = `finger $user`;
, (,
, ), ,
.
, .
( Unix.)
, .
,
. ( $/ .)
.
$? (. 28
$?, , $CHILD_ERROR).
csh,
: . ,
Perl
. $ ,
. $user finger
Perl, . (
, ,
23 .)
qx// ( quoted executi
on ), ,
.
.
, ,
, :
$perl_info = qx(ps $$);
$shell_info = qx'ps $$';
# $$ Perl
# $$
( )
, read
line ( ).
(, STDIN)
. (
, Perl
,
, , ,
, .)
118
2.
, .
, while ,
, $_.
, . (
, ,
.) , :
while (defined($_ = <STDIN>)) { print $_; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print $_ while defined($_ = <STDIN>);
print while $_ = <STDIN>;
print while <STDIN>;
#
#
#
#
#
#
#
$_
while
$_
, while.
,
, :
while (<FH1> && <FH2>) { ... }
if (<STDIN>)
{ print; }
if ($_ = <STDIN>) { print; }
# :
# : $_
# :
}#
$_ , $_,
, while.
$_ :
while (local $_ = <STDIN>) { print; }
# $_
.
$_ , , , ,
, .
, :
while (my $line = <STDIN>) { print $line; } #
( while ,
, my local ,
.) STDIN, STDOUT STDERR
.
open sysopen.
. 29 .
while
, .
,
, ,
. ,
:
119
$one_line = <MYFILE>; # .
@all_lines = <MYFILE>; # .
while,
while (
).
:
Unix, sed awk.
<> ,
. ,
,
.
: <> @ARGV,
, $ARGV[0] ,
. @ARGV
. , :
while (<>) {
...
}
Perl :
@ARGV = ('') unless @ARGV;
# STDIN, ,
while (@ARGV) {
$ARGV = shift @ARGV;
# @ARGV
if (!open(ARGV, $ARGV)) {
warn " $ARGV: $!\n";
next;
}
while (<ARGV>) {
...
#
}
}
, ,
. @ARGV
$ARGV.
ARGV <>
<ARGV>, . (,
, , <ARGV>
.)
@ARGV <>,
, .
Perl open, ,
, ,
open (
120
2.
Getopt::* :
while (@ARGV and $ARGV[0] =~ /^/) {
$_ = shift;
last if /^ $/;
if (/^D(.*)/) { $debug = $1 }
if (/^v/)
{ $verbose++ }
...
#
}
while (<>) {
...
}
<> false .
, , @ARGV,
@ARGV , STDIN.
(,
<$foo>), ,
,
, . :
$fh = \*STDIN;
$line = <$fh>;
:
open($fh, "<data.txt");
$line = <$fh>;
, ,
. ,
.
(
),
,
121
(globbing).1
( , ),
. ,
,
. ,
:
@files = <*.xml>;
,
, <$foo>,
, ,
. Perl
:
<${foo}>.
, glob($foo), , , .
:
@files = glob("*.xml");
. .
glob
,
while, ,
$_. (
.) ,
C, :
while (glob "*.c") {
chmod 0644, $_;
}
:
while (<*.c>) {
chmod 0644, $_;
}
Fileglob ( )
typeglob ( ), , *
. *, ,
glob. typeglob
. fileglob
, .
122
2.
. ,
. File::Glob
32 ,
: (
) ,
, ,
. .
,
chmod , , ,
:
chmod 0644, <*.c>;
()
. ,
. ,
.
, ,
false, . , false
. , ,
, :
($file) = <blurch*>; #
:
$file = <blurch*>;
,
, false.
,
glob,
.
, :
@files = <$dir/*.[ch]>;
@files = glob("$dir/*.[ch]");
@files = glob $some_pattern;
# , .
# glob .
# glob .
, , glob
(), ;
, . glob
,
, 3.
, ,
.
3
,
, , ,
. , , .
.
,
. Perl
.
.
.
. ,
.
,
, .
, .
,
( ), ( ,
) *
(
, ).
124
3.
Perl: , *
. (
).1
,
, .
,
.
:
! $x
$x * $y
$x ? $y : $z
print $x, $y, $z
#
#
#
#
.
, .
,
, :
2 + 3 * 4
# 14, 20
.
, :
2 * 3 * 4
2 ** 3 ** 4
2 != 3 != 4
# (2 * 3) * 4,
# 2 ** (3 ** 4),
# ,
. 3.1 Perl,
.
3.1.
()
>
++
**
! ~ \ +
=~ !~
* / % x
+ .
, .
125
<< >>
0,1
== != <=> eq ne cmp
&
| ^
&&
||
.....
?:
= += = *= . .
, =>
0+
()
not
and
or xor
, ,
. . ,
. , ,
,
. , , ,
, .
: , C,
, C
. ( Perl
, C C++. , , Java.)
.
, . ,
.
,
,
Perl.
, ,
,
,
. , , Perl
. 13
.
126
3.
()
Perl .
, ,
, ,
.
, ,
, .
, 29 .
. ,
, ,
. (
, print) (,
chdir) (
),
, .
: ,
. , ,
, , ,
.
, || , chdir,
:
chdir $foo
chdir($foo)
chdir ($foo)
chdir +($foo)
||
||
||
||
die;
die;
die;
die;
#
#
#
#
(chdir
(chdir
(chdir
(chdir
$foo)
$foo)
$foo)
$foo)
||
||
||
||
die
die
die
die
. . * , chdir, :
chdir $foo * 20;
chdir($foo) * 20;
chdir ($foo) * 20;
chdir +($foo) * 20;
#
#
#
#
,
, rand:
rand 10 * 20;
rand(10) * 20;
rand (10) * 20;
rand +(10) * 20;
#
#
#
#
,
print, sort chmod, , ,
,
. ( , .) , :
@ary = (1, 3, sort 4, 2);
print @ary;
# 1324
127
sort sort,
. ,
, ,
.
:
# exit , print:
print($foo, exit); # , , .
print $foo, exit; # .
# print , exit:
(print $foo), exit; # , .
print($foo), exit; # .
print ($foo), exit; # .
, ,
:
print ($foo & 255) + 1, "\n";
, . ,
Useless use of addition in a void context (
), .
do {} eval {} ,
,
[] {}, sub {}.
, C C++, >
.
[...], {...} (...),
( )
, , . l (
), ,
, ,
(autovivification) .
(
) 8 .
$aref>[42]
$href>{"corned beef"}
$sref>(1,2,3)
#
#
#
.
( ,
128
3.
), (
) (. . ):
$yogi = Bear>new("Yogi");
$yogi>swipe($picnic);
#
#
, ,
SUPER::, ,
. . 12 .
++ , C. , ,
,
, , ,
,
. , $a++
$a,
. $b{(/(\w+)/)[0]} %b
,
($_) , .1
. ,
,
. , ,
,
/^[azAZ]*[09]*$/,
:
print
print
print
print
1
++($foo
++($foo
++($foo
++($foo
=
=
=
=
'99');
'a9');
'Az');
'zz');
#
#
#
#
'100'
'b0'
'Ba'
'aaa'
, .
. .
$_, \w+.
,
.
, (...)[0],
( ) .
, () .
, ,
, , .
129
Unicode,
.
,
.
** .
, , ,
2**4 (2**4), (2)**4.
C pow(3), () .
,
, ,
.
(.
),
,
.
. .
! , .
not.
, true, (1), ,
false, ( 0, "0",
), , false, (""), .
,
. ,
, ,
. , (
), , .
, bareword
"bareword". Tk.
~ , . . 1.
, ,
. , 32 ~123
4294967172, 64 18446744073709551492.
.
, , , , ~
, , ,
1.
, ,
130
3.
.
, ,
.
+
.
,
. (.
.) , +
.
\ , .
.
8 .
,
,
.
.
=~
, ( ).
,
, $_ ( ). ,
, ,
.
, , ,
.
,
, ,
,
. , $_ =~ $pat $_ =~ /$pat/.
, , ,
, .
qr// (
).
!~ =~, ,
.
:
$string !~ /pattern/
not $string =~ /pattern/
,
, .
131
, . (
.)
, .
:
,
, . ,
,
, .
:
if ( ($k,$v) = $string =~ m/(\w+)=(\w*)/ ) {
print " $k $v\n";
}
. =~ =,
=~. =~ $string
, ,
=. ,
. ,
, $k $v.
, 2
. 2 ,
. ,
0, .
. 5 .
Perl C * (), / () % (
). * / , ,
.
, integer.
% ,
. (
, 32
15 .) ,
$a $b. $b , $a % $b
$a $b, $a (
0 .. $b 1). $b
, $a % $b $a $b,
$a (
$b +1 .. 0).
use integer, %
,
C.
, .
132
3.
x .
.
, , ,
. (
, .)
print '' x 80;
print "\t" x ($tab/8), ' ' x ($tab%8);
#
#
, ,
x , .
:
@ones = (1) x 80;
@ones = (5) x @ones;
# 80
# 5
x
:
@keys = qw(perls before swine);
@hash{@keys} = ("") x @keys;
, , @keys
(
) .
%hash , :
$hash{perls} = "";
$hash{before} = "";
$hash{swine} = "";
, Perl + () (
).
.
, Perl . . :
$almost = "Fred" . "Flintstone";
# FredFlintstone
, Perl .
,
join, 29 .
, , ,
:
$fullname = "$firstname $lastname";
133
(<< >>)
, (<<) (>>) ,
. . :
1 << 4;
32 >> 4;
# 16
# 2
, , .
( )
,
.
, 29 ,
. . 3.2
.
3.2.
X (
)
gethostbyname
localtime
return
alarm
getnetbyname
lock
rmdir
caller
getpgrp
log
scalar
chdir
getprotobyname
lstat
sin
chroot
glob
my
sleep
cos
gmtime
oct
sqrt
defined
goto
ord
srand
delete
hex
quotemeta
stat
do
int
rand
uc
eval
lc
readlink
ucfirst
exists
lcfirst
ref
umask
exit
length
require
undef
,
. :
sleep 4 | 3;
134
3.
7 , 4, ,
sleep ( 0), 3,
:
(sleep 4) | 3;
:
print 4 | 3;
4 3 (7 ),
, :
print (4 | 3);
, print ,
. , ,
.
,
. : ,
, .
, ,
, $_,
. ,
, , , Perl
, .
Perl , . 3.3,
,
.
3.3.
*typeglob
//
<
<HANDLE>, <<END
.3333
?:
??
%assoc
&
&, &&
&subroutine
:
next if length < 80;
135
, , ,
.
, ,
, ,
. ,
$_, t, STDIN.
, 1 ""
,
.
. 3.4.
3.4.
r
(effecti
ve UID/GID)
w
(effecti
ve UID/GID)
x
(effective UID/GID)
o
(effective UID)
R
(real
UID/GID)
W
(real
UID/GID)
X
(real UID/GID)
O
(real UID)
e
z
s
( )
f
d
l
136
3.
3.4 ()
p
(FIFO)
S
b
c
t
u
setuid
g
setgid
k
(sticky bit)
T
B
( T)
M
,
,
A
,
,
C
,
,
, s/a/b/ .
exp($foo) , :
.
r, R, w, W, x X
. ,
, , ,
Andrew File System (AFS).1
, r, R, w W 1, x
X 1, .
, ,
stat, ,
.
, .
:
while (<>) {
chomp;
next unless f $_;
...
}
1
# ""
use
filetest. . 31 .
137
T B .
,
(
UTF8). ,
, . ,
,
ASCII NUL (\0). T B ,
, ( I/O
stdio). , T B,
, EOF ,
. Perl ,
T, ,
,
.
f, :
next unless f $file && T $file;
( stat lstat)
,
, stat
( stat), .
( t, , lstat l
stat , ,
. , l _
stat.)
:
print " .\n"
if r $a || w _ || x _;
stat($filename);
print " \n" if r _;
print " \n" if w _;
print " \n" if x _;
print " setuid\n"
if u _;
print " setgid\n"
if g _;
print "\n"
if T _;
print "\n"
if B _;
M, A C
( ) .
$^T ($BASETIME). ,
,
. ,
( , 86399 86400) ,
int
. :
next unless M $file > .5;
&newfile if M $file < 0;
# 12
# ,
138
3.
,
:
$^T = time;
Perl .
, ,
. 3.5.
3.5.
>
gt
>=
ge
<
lt
<=
le
1 "" .
, ,
$a < $b < $c.
ASCII/Unicode, ,
,
.
. ( ,
,
Unicode,
.)
, . 3.6,
.
3.6.
==
Eq
!=
Ne
<=>
Cmp
139
1
"" ( ,
). <=> cmp 1,
, 0 +1, .
,
, $a < $b <=> $c < $d
.
, ,
<=> (spaceship).
C, Perl ,
(XOR): &, |, ^. ,
, ,
,
.
. (
, Perl .)
( ),
.
32 ,
64 . , ,
.
(
),
.
,
. , , ,
.
, :
"123.45" & "234.56"
:
"020.44"
:
"123.45" & 234.56
, :
123.45 & 234.56
140
3.
:
123 & 234
106. ,
( "0"). ,
, , :
if ( "fred" & "\1\2\3\4" ) { ... }
:
if ( ("fred" & "\1\2\3\4") =~ /[^\0]/ ) { ... }
C
( )
C, Perl && ( ) || (
). ( &&
, ||), .
,
,
. , && ,
,
.
$a && $b
$a, $a , $b
$a || $b
$a, $a , $b
,
. , Perl
:
open(FILE, "_") || die " _: $!\n";
Perl open.
(. . _ ), die
. :
!
&& || C , 0
1, . ||
,
, .
:
$home = $ENV{HOME}
|| $ENV{LOGDIR}
141
|| (getpwuid($<))[7]
|| die " !\n";
,
, ||
:
@a = @b || @c;
@a = scalar(@b) || @c;
@a = @b ? @b : @c;
# , ,
# .
# .
.. , ,
.
.. . ,
,
() sed, awk
. ..
. , .
,
, ,
.
, .
,
( awk),
. ,
( sed),
(...). .., ...
,
, ,
.
,
( 1) .
. E0,
, ,
.
, , 1.
.. ,
$.,
. :
142
3.
.. ,
. for (1..10)
:
for (101 .. 200) { print; }
@foo = @foo[0 .. $#foo];
@foo = @foo[ 5 .. 1];
# 101102...199200
#
# 5
, . (
, ,
reverse.)
,
, .1
:
@alphabet = ('A' .. 'Z');
() , :
$hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
, :
@z2 = ('01' .. '31'); print $z2[$mday];
. :
@combos = ('aa' .. 'zz');
.
:
@bigcombos = ('aaaaaa' .. 'zzzzzz');
. ,
308 915 776 . ,
. , .
C, ?: .
, ifthen
1
,
, ,
.
143
else, , , , ,
.
, :
COND ? THEN : ELSE
COND ,
THEN, .
ELSE,
.
, . (
,
.)
$a = $ok ? $b : $c; #
@a = $ok ? @b : @c; #
$a = $ok ? @b : @c; #
printf,
,
.
printf "I have %d camel%s.\n",
$n,
$n == 1 ? "" : "s";
, ?: , , ,
, (== ),
. , ,
. , THEN
,
, if:
$leapyear =
$year % 4 == 0
? $year % 100 == 0
? $year % 400 == 0
? 1
: 0
: 1
: 0;
, ELSE
, :
$leapyear
$year
?
:
=
% 4
0
$year % 100
? 1
144
3.
: $year % 400
? 0
: 1;
, , COND THEN :
$leapyear
$year
$year
$year
=
% 4 ? 0 :
% 100 ? 1 :
% 400 ? 0 : 1;
:
printf ", %s !\n",
$i18n eq "french" ? "chameau"
:
$i18n eq "german" ? "Kamel"
:
$i18n eq "japanese" ? "\x{99F1}\x{99DD}" :
"camel"
,1
l (. .
), , (
Perl ,
):
($a_or_b ? $a : $b) = $c; # $a, $b $c
, ,
. (,
, $leapyear),
.
, ,
. , :
$a % 2 ? $a += 10 : $a += 2
:
(($a % 2) ? ($a += 10) : $a) += 2
Perl C,
. :
1
, .
(obfus
cated) Perl.
145
**=
+=
=
.=
*=
/=
%=
x=
&=
|=
^=
<<=
>>=
&&=
||=
l (
), , .
:
TARGET = EXPR
EXPR , TAR
GET. Perl :
TARGET OP= EXPR
:
TARGET = TARGET OP EXPR
,
. ,
, ,
OP . , TARGET
. , ,
:
$var[$a++] += $value;
$var[$a++] = $var[$a++] + $value;
# $a
# $a
C, l.
, .
, :
($tmp = $global) += $constant;
:
$tmp = $global + $constant;
:
($a += 2) *= 3;
:
$a += 2;
$a *= 3;
, , :
($new = $old) =~ s/foo/bar/g;
146
3.
.
,
:
$a = $b = $c = 0;
0 $c, ( 0)
$b, (* 0) $a.
=.
, .
,
, 2
.
, ,
( ), :
while (($key, $value) = each %gloss) { ... }
next unless ($dev, $ino, $mode) = stat $file;
, .
, ,
,
.
C. :
$a = (1, 3);
$a 3.
.
LIST.
.
, :
@a = (1, 3);
,
atan2(1, 3);
atan2 .
=> .
, . ,
,
, .
147
()
, ,
, , .
,
, ,
(, ), ,
(,
), ,
.
C, :
unlink("alpha", "beta", "gamma")
|| (gripe(), next LINE);
|| or. ,
$xyz = $x || $y || $z;
:
$xyz = $x or $y or $z;
! , or,
, ||, $x $xyz,
or. , ||,
:
$xyz = ( $x or $y or $z );
: ( )
,
.
148
3.
xor,
C Perl,
(^) . xor
,
. $a xor $b , , !$a != !$b.
, !$a ^ !$b $a ? !$b : !!$b. ,
$a $b true false ,
.
C, Perl
C Perl:
&
. \ Perl ( )
:
$ref_to_var = \$var;
Perl , C.
*
. Perl ,
. ,
Perl ,
: $, @, % &. ,
*, *
, typeglob,
.
(TYPE)
. , ,
.
4
Perl (declarations)
(statements). ,
,
. ,
,
.
.
, Perl ,
.
, ,
. ,
, (
) "" ( ),
.
,
use warnings (.
).
, ,
my our. ,
.
, , ,
. Perl
150
4.
, use strict
.
, . .
,
. ,
.
Perl , ,
.
, , ,
.
, eval {}, do {} sub {},
, .
, ,
.
,
.
, (
). :
if EXPR
unless EXPR
while EXPR
until EXPR
foreach LIST
if () unless ( )
:
$trash>take('out') if $you_love_me;
shutup() unless $you_want_me_to_leave;
foreach ( for)
LIST, $_
:
s/java/perl/ for @resumes;
print ": $_\n" foreach split /:/, $dataline;
151
do 29 .
, , ,
,
.
,
.
.
,
.
1 .
, ,
require, , .
, eval.
, ({}).
, .
, (BLOCK).
(BLOCK).
.
EXPR ,
. ,
, LIST.
BLOCK. ( LABEL .)
if
if
if
if
(EXPR)
(EXPR)
(EXPR)
(EXPR)
BLOCK
BLOCK else BLOCK
BLOCK elsif (EXPR) BLOCK ...
BLOCK elsif (EXPR) BLOCK ... else BLOCK
2
.
152
unless
unless
unless
unless
4.
(EXPR)
(EXPR)
(EXPR)
(EXPR)
BLOCK
BLOCK else BLOCK
BLOCK elsif (EXPR) BLOCK ...
BLOCK elsif (EXPR) BLOCK ... else BLOCK
, , C Java,
, . ,
.
.
:
unless (open(FOO, $foo))
if (!open(FOO, $foo))
.
, , or die.
|| ,
or .
, ,
,
, . ,
,
.1 or die,
,
:
chdir $dir
open FOO, $file
1
( .)
153
if unless
@lines = <FOO>
close FOO
if unless
if . ,
, if
else elsif, .
if/elsif/else ,
. ,
else, . else elsif
, .
unless if,
. , :
unless ($x == 1) ...
:
if ($x != 1) ...
:
if (!($x == 1)) ...
, ,
,
elsif else, ,
:
if ((my $color = <STDIN>) =~ /red/i) {
$value = 0xff0000;
}
elsif ($color =~ /green/i) {
$value = 0x00ff00;
}
elsif ($color =~ /blue/i) {
$value = 0x0000ff;
}
else {
warn " RGB `$color, \n";
$value = 0x000000;
}
else $color .
, .
154
4.
Loop
LABEL. (
, .)
, .
,
. Perl , ,
, if open, ,
.
while until
while , EXPR .
while until,
, . . , EXPR .
.
while until
: continue, ,
next (
, ).
continue , , ,
for .
foreach, ,
while .
,
. ,
( ) 2
, while
$_, ,
, $_ .
my, .
, while until,
,
. .
:
while (my $line = <STDIN>) {
$line = lc $line;
}
continue {
print $line; #
}
# $line
$line
,
Loop
155
continue, . ,
.
for
for ,
. , ,
, .
( ) ; ,
, . ,
for while.
:
LABEL:
for (my $i = 1; $i <= 10; $i++) {
...
}
:
{
my $i = 1;
LABEL:
while ($i <= 10) {
...
}
continue {
$i++;
}
}
, ,
, my.
, :
for ($i = 0, $bit = 0; $i < 32; $i++, $bit <<= 1) {
print " $i \n" if $mask & $bit;
}
# $i $bit
,
for:
for (my ($i, $bit) = (0, 1); $i < 32; $i++, $bit <<= 1) {
print " $i \n" if $mask & $bit;
}
# $i $bit
, for
.
156
4.
. , ,
.
$on_a_tty = t STDIN && t STDOUT;
sub prompt { print "? " if $on_a_tty }
for ( prompt(); <STDIN>; prompt() ) {
#
}
for, ,
, ,
. ,
:
for (;;) {
...
}
,
while (1) {
...
}
, ,
, ,
, last. ,
,
.
.1
foreach
foreach ,
(VAR) :
foreach VAR (LIST) {
...
}
foreach for,
for foreach
,
. VAR , $_. (
, Perl for (@ARGV) for ($i=0; $i<$#ARGV; $i++),
.) :
157
Loop
$sum = 0; foreach $value (@array) { $sum += $value }
for $count (10,9,8,7,6,5,4,3,2,1,'!') { #
print "$count\n"; sleep(1);
}
for (reverse '!', 1 .. 10) {
print "$_\n"; sleep(1);
}
# LIST
.
keys sort 29 .
foreach, , .
,
,
for, . ,
for .
LIST , (. .
, ),
VAR . ,
foreach
.
, ,
:
foreach $pay (@salaries) {
$pay *= 1.08;
}
# 8%
#
#
#
,
158
4.
my.
,
, .
,
( ) ;
, ,
. ,
, .
( )
. ,
;
, :
for my $i
(1 .. 10) { ... }
for our $Tick (1 .. 10) { ... }
# $i
# $Tick
,
for, foreach,
.
C Java
Perl:
for ($i = 0; $i < @ary1; $i++) {
for ($j = 0; $j < @ary2; $j++) {
if ($ary1[$i] > $ary2[$j]) {
last;
# . :(
}
$ary1[$i] += $ary2[$j];
}
# last
}
Perl:
WID: foreach $this (@ary1) {
JET: foreach $that (@ary2) {
next WID if $this > $that;
$this += $that;
}
}
, Perl? ,
. , . ,
,
, next (
) , .
, Perl foreach ,
for, . .
, .
159
Loop
, . TMTOWTDI.
while, foreach continue.
,
next.
, , : next .
,
.
, next, last redo. ,
. , ,
go to .
,
, ,
.
,
.
, ,
. ,
LINE:,
:
next LINE if /^#/;
:
last LABEL
next LABEL
redo LABEL
LABEL ; ,
.
, ,
, .
, , ,
.
.
eval , Perl
( ).
return,
.
.
,
.
160
4.
,
, .
.
.
( ),
.
,
, .
last
. continue, , .
:
LINE: while (<STDIN>) {
last LINE if /^$/;
...
}
next ()
. continue,
, , ,
for. continue
,
next:
LINE: while (<STDIN>) {
next LINE if /^#/;
next LINE if /^$/;
...
} continue {
$count++;
}
#
#
redo , .
continue, , .
, , .
, ,
, .
redo:
while (<>) {
chomp;
if (s/\\$//) {
$_ .= <>;
redo unless eof;
}
# $_
}
# eof
161
Loop
Perl ( ) :
LINE: while (defined($line = <ARGV>)) {
chomp($line);
if ($line =~ s/\\$//) {
$line .= <ARGV>;
redo LINE unless eof(ARGV);
}
# $line
}
,
.
, Perl Getopts::*,
:
ARG: while (@ARGV && $ARGV[0] =~ s/^(?=.)//) {
OPT: for (shift @ARGV) {
m/^$/
&& do {
m/^$/
&& do {
s/^d//
&& do { $Debug_Level++;
s/^l//
&& do { $Generate_Listing++;
s/^i(.*)// && do { $In_Place = $1 || ".bak";
say_usage(" : $_");
}
}
next
last
redo
redo
next
ARG;
ARG;
OPT;
OPT;
ARG;
};
};
};
};
};
.
, (statements).
, , ,
, .
,
. ,
.
, .
:
open FILE, $file
or warn " $file: $!\n", next FILE;
# okay
162
4.
, :
unless (open FILE, $file) {
warn " $file: $!\n";
next FILE;
}
, ,
. last
redo .1 ,
eval {}, sub {} , , do {}.
,
(BLOCKs);
, .
,
.
, return (, eval).
if unless,
.
, , :
if (/pattern/) {{
last if /alpha/;
last if /beta/;
last if /gamma/;
# , if()
}}
do {}. next
redo do, :
do {{
next if $x == $y;
#
}} until $x++ > $z;
last :
{
do {
last if $x = $y ** 2;
1
, , , ( , )
, next
. : next continue,
last .
163
#
} while $x++ <= $z;
}
, ,
:
DO_LAST: {
do {
DO_NEXT:
{
next DO_NEXT if $x == $y;
last DO_LAST if $x = $y ** 2;
#
}
} while $x++ <= $z;
}
, ( ),
last :
for (;;) {
next if $x == $y;
last if $x = $y ** 2;
#
last unless $x++ <= $z;
}
case
, Perl
switch case. , Perl
, . . ,
. case (
) . :
SWITCH: {
if (/^abc/) { $abc = 1; last SWITCH; }
if (/^def/) { $def = 1; last SWITCH; }
if (/^xyz/) { $xyz = 1; last SWITCH; }
$nothing = 1;
}
:
SWITCH: {
/^abc/
/^def/
/^xyz/
$nothing =
}
164
4.
, :
SWITCH: {
/^abc/
&& do {
$abc = 1;
last SWITCH;
/^def/
};
&& do {
$def = 1;
last SWITCH;
/^xyz/
};
&& do {
$xyz = 1;
last SWITCH;
};
$nothing = 1;
}
(, !):
if
(/^abc/) { $abc = 1
elsif (/^def/) { $def = 1
elsif (/^xyz/) { $xyz = 1
else
{ $nothing
}
}
}
= 1 }
, last
do {}, ,
for:
for ($very_nasty_long_name[$i++][$j++]>method()) {
/this pattern/
and do { push @flags, 'e'; last; };
/that one/
and do { push @flags, 'h'; last; };
/something else/
and do {
last; };
die " : `$_'";
}
,
, .
for/foreach
, , $_.
, ,
.
. ,
,
switch case.
?:
. for
, :
165
goto
for ($user_color_preference) {
$value = /red/
? 0xFF0000
/green/
? 0x00FF00
/blue/
? 0x0000FF
0x000000
}
:
:
:
;
# ,
, ,
.
,
, ,
. , ,
. :
%color_map = (
azure
chartreuse
lavender
magenta
turquoise
);
=>
=>
=>
=>
=>
0xF0FFFF,
0x7FFF00,
0xE6E6FA,
0xFF00FF,
0x40E0D0,
:
$value = $color_map{ lc $user_color_preference } || 0x000000;
(
)
. . ,
, 9
.
goto
( ) Perl
goto. : goto LABEL, goto EXPR goto &NAME.
goto LABEL LABEL
.
, , ,
foreach. ,
(. 18 ).
(. . ,
). goto
, . Perl
goto ( , Perl,
C ).
166
4.
# , 0 <= i < 3
, next, last redo,
goto.
eval die.
goto &NAME
goto, ,
, goto.
, .
AUTOLOAD,
, ,
, . goto
caller ,
. autouse, AutoLoader SelfLoader
, ,
, ,
.
.
, ,
( , ,
, , ).
, ,
: .
,
,
if, . .
. (
use no) , .
.
167
(. ) ,
,
.
,
. (definition)
(declaration).
, , ,
, . (
, .)
, (
):
sub count (@);
my $x;
$x = count(3,2,1);
sub count (@) { @_ }
#
#
#
#
, count().
.
.
, count().
,
,
( ,
, ),
.
,
, ,
. ( count,
.) , ,
, :
sub myname;
$me = myname $0
,
, or, ||. ||
,
,
, ,
.
($), :
sub myname ($);
$me = myname $0
, ,
or.
. 6 .
168
4.
,
, ,
.
.
require. Perl 4 ,
. ,
( ) ,
. , require ,
, , require.
, , , .
use, , , require
( use BEGIN)
.
use ,
(
) , .
10 ,
; 11
; 18
BEGIN CHECK, INIT END,
,
.
,
.
,
, ,
(, eval
). : ,
, , ,
(). ,
(lexically scoped),
.
Perl .
(dynamic scope)
,
,
. ,
,
.
169
, , ,
, .
, use
, use
. , use
,
, .
, use strict 'vars',
, .
. .
, ,
, , .
.
, .1
, ,
, . Perl.
. , .
,
, , .
, Perl ,
.
.
,
( ),
. , $Some::stuff2 ,
$stuff Some. . 10 .
, ,
Perl
. , Perl
( my),
(
our)
( local):
1
,
.
$Some'stuff,
Perl .
170
4.
my $nose;
our $House;
local $TV_channel;
,
. my our
, . local
:
typeglob
:
my ($nose, @eyes, %teeth);
our ($House, @Autos, %Kids);
local (*Spouse, $phone{HOME});
. , , our
, local
, my , .
,
, ,
.
, ( )
: my local ,
, , undef (),
our
.
my, our local (
) l.
l, ,
l . ,
, ,
. :
my ($foo) = <STDIN>;
my @array = <STDIN>;
, :
my $foo = <STDIN>;
.
(
), .
, , ,
, .
my $foo, $bar = 1;
171
:
my $foo;
$bar = 1;
w W , , use
warnings, ,
.
,
, . ,
,
, , .
.
sub check_warehouse {
for my $widget (our @Current_Inventory) {
print " $widget.\n";
}
}
my,
,
.
our,
, my,
,
. , ,
.
, ,
local, ,
local,
.
: my
,
, Perl
, (lexicals).
, .
, ,
, ,
. ,
my ().
.
172
4.
, .
,
:
,
:
my $name = "fred";
my @stuff = ("car", "house", "club");
my ($vehicle, $home, $tool) = @stuff;
.
, local (.
), ,
.
, :
.
,
; , , , .
do, require use,
,
.
( )
. ,
,
.
, C :
{
my $state = 0;
sub on
{ $state = 1 }
sub off
{ $state = 0 }
sub toggle { $state = !$state }
}
eval STRING ,
eval
(
eval).
; , (clo*
sures).1 : eval
1
enclosing scope (
) closure (). (
,
.)
173
,
eval,
, eval . . 8
.
( local)
, , .
:
my $x = $x;
$x
$x , $x
. ( ,
.)
.
, , ,
, , ,
$PackageName::varname.
: our
,
use strict, our.
,
. ,
my local, our
. ,
, , ,
, .
our , my.
our ,
,
. ,
, :
sub check_warehouse {
our @Current_Inventory;
my $widget;
foreach $widget (@Current_Inventory) {
print " $widget.\n";
}
}
174
4.
,
,
, . ,
,
, use strict, ,
.
our .
my , local
. , our,
.
our,
. , our ;
, :
our $PROGRAM_NAME = "waiter";
{
our $PROGRAM_NAME = "server";
# "server".
...
}
# "server".
, my local,
:
my $i = 10;
{
my $i = 99;
...
}
# .
local $PROGRAM_NAME = "waiter";
{
local $PROGRAM_NAME = "server";
# , , "server".
...
}
# , , "waiter".
our,
, , , ,
our local:
{
local our @Current_Inventory = qw(bananas);
check_warehouse(); # , :)
}
175
:
local
local
local,
.
,
.
, ,
. ,
,
,
, . ,
, , ,
.1
local, :
{
local $var = $newvalue;
some_func();
...
}
:
{
$oldvalue = $var;
$var = $newvalue;
some_func();
...
}
continue {
$var = $oldvalue;
}
, local
, ,
.
,
, .
: .
1
:
.
, C C++.
, .
176
4.
my, local
. ,
( ,
, , ,
),
. , :
# :
.
local $Some_Global = $Some_Global;
, our
, local,
. ,
,
.
use strict 'vars', .
my local ,
my, local. local
,
, , 28 .
,
. local
,
10 . ,
local .
, ,
local
.
local.
local 29 .
.
Perl (
) use. :
use
use
use
use
use
warnings;
strict;
integer;
bytes;
constant pi => ( 4 * atan2(1,1) );
177
Perl 31 ,
, ,
.
,
,
,
, eval (
, ).
no, use, .
, , warnings,
Perl,
:
use warnings;
# .
...
{
no warnings;
# .
...
}
# .
, Perl
, , ,
,
,
, ,
( ), ,
33 .
use warnings
.
w
$^W:
{
local $^W = 0;
...
}
178
4.
,
,
, ,
use warnings 31 .
use strict,
,
. Perl
( )
.
.
,
. ,
:
use strict 'vars';
,
, ,
.
:
Perl,
(. 28 ).
(. 10
).
(. 11 ).
our. ( , Perl
our.)
, :
,
no strict 'vars'
.
:
use strict;
.
use strict 31 .
5
Perl
. Perl
,
,
, (
), ,
,
, .
, ,
, 90% , , 90% .
, Perl, ;
Perl: Practical Extraction and Re
port Language
. Perl
.
(regular expression
regex). Perl (Engine)
, ( )
. , ,
,
,
. Perl ,
180
5.
256. (
. 15 Unicode.)
, ,
, Perl .
,
, ,
,
. , Perl,
, ,
, ,
. , Perl,
match( $, $ );
subst( $, $, $ );
Perl
, : m/PATTERN/ (//)
s/PATTERN/REPLACEMENT/ (///), m// s///.
,
, ; ,
, .
. ,
, . ,
. , s///
,
REPLACEMENT.
.
Perl ,
.
, ,
. ,
,
. , C
Java Perl ,
.
, , ,
, , , ,
. .
, , .
.
181
, ,
, . ,
:
/Frodo/
, ,
"Frodo". (
.) ,
.
,
. . (
, ,
.)
:
\ | ( ) [ { ^ $ * + ? .
; .
, ,
( ). ,
,
: \\.
, ,
. ,
,
.
,
, , ;
( )
:
.
:
\b \D \t \3 \s
, ,
. , \b
, \t . ,
,
, .
\b (zero*width assertion).
, \t \b ,
. , ,
.
182
5.
,
, . ,
,
, ,
. (, \t)
, (,
\b) .
. ,
, , . ( ,
,
, ,
.)
,
, ( ,
Perl,
).
,
,
.
,
.1
,
(alternation):
/Frodo|Pippin|Merry|Sam/
, ;
.
,
:
/(Frodo|Drogo|Bilbo) Baggins/
:
/(Frod|Drog|Bilb)o Baggins/
, , ,
, .
:
* + ? *? {3} {2,5}
, , , ,
.
183
.
, , . . ,
.1
; ,
.
"bar" , "bar"
, :
/(bar){3}/
"barbarbar". /bar{3}/,
"barrr". .
.
, ,
, , , .
,
,
. (
,
.)
, , Perl
,
. ,
, Perl
. ,
,
, . ,
,
,
. .
, m//
s/// ( )
.
,
, (
)
1
,
4 ,
.
while
, .
.
184
5.
, . (.
.) ,
. (
Perl, .)
, ,
.
, , $,
, ,
,
, . , :
$foo = "bar";
/$foo$/;
,
$ . $foo,
:
/bar$/;
,
Perl ,
. , (
), .
,
(
,
). Perl , ,
.
,
, ,
, ,
( ). .
.
1 tr///
, ! ( ,
, , .)
m// s///:
=~ !~.
=~ !~, 3 ,
1
transliteration operator ,
, . .
. . .
185
, : m//
, s/// ,
, tr/// ( y///)
() . ( m// //,
m, .)
=~ !~ ,
m//,
(.
),
:
print "" if $somestring =~ $somepattern;
, :
print "" if $somestring =~ m/$somepattern/;
=~ !~ ,
(matches) , (
).
m// s/// Perl
. split
, ,
. .
split 29 . qr// (quote regex
)
, ( m//,
).
, .
.
m//, s/// tr///
=~ (, ,
, ,
). :
$haystack =~ m/needle/
$haystack =~ /needle/
#
#
$rotate13 =~ tr/azAZ/nzamNZAM/
# ( )
#
$_:
/new life/ and
/new civilizations/
# $_ ( )
# $_
186
5.
s/sugar/aspartame/
# $_
tr/ATCG/TAGC/
# $_
s/// tr/// , ,
:
"onshore" =~ s/on/off/;
# :
m// :
if ((lc $magic_hat>fetch_contents>as_string) =~ /rabbit/) {
print ", ?\n";
}
else {
print " !\n";
}
, =~ !~
:
.1 !~
=~, :
if ($song !~ /words/) {
print qq/, "$song" .\n/;
}
s/// tr/// ,
(, ,
), ,
:
s(egg)<larva>;
s{larva}{pupa};
s[pupa]/imago/;
lc
, _
.
187
:
s (egg) <larva>;
s {larva} {pupa};
s [pupa] /imago/;
, (
), $`, $& $'
,
. :
"hot cross buns" =~ /cross/;
print ": <$`> $& <$'>\n";
print ":
<$`>\n";
print ": <$&>\n";
print ":
<$'>\n";
#
#
#
#
: <hot
:
:
:
,
, .
, *
, .
; ,
,
$1, $2, $3 . .:1
$_ = " 22 ";
/(.*) (.*)/;
print ": $1\n";
print ": $2\n";
$0, .
188
5.
, . Perl
$& , .
, , : *
.
m//, s///, qr// tr///
,
.
/o ,
( ). (
, ; .)
,
. ,
, . m//, s///
qr//1
:
/i
/s
.
$*
/m
^ $ \n
/x
( )
/o
/i
( title Unicode). , /perl/i
"PROPERLY" "Perlaceous" ( ).
use locale . (
, Unicode.)
/s /m .
, Perl ,
. \n
, , Perl ,
(/s) (/m), . .
,
, , .
1
tr/// ,
.
189
"." ,
,
(line). /s "."
,
Perl ,
(string) . ( /s
Perl $*, ,
, .) , /m
^ $,
,
. . .
/o .
(m'PATTERN', s'PAT
TERN'REPLACEMENT' qr'PATTERN'),
( )
.
, /o.
.
/o ,
, . , Perl
.
qr//. .
.
/x :
.
, /x ( #):
,
, , ,
, ( ).
, /x ,
, Perl.
#,
, ,
(line) (string) .1
( #),
,
. (
\s* \s+, .)
1
, , . .,
, Perl ,
.
190
5.
.
TMTOWTDI
. :
m/\w+:(\s+\w+)\s*\d+/;
# , , , , , .
#
#
#
#
#
#
.
.
.
( )
.
}x;
. (
, ,
/x, .)
,
Perl. .
/x /i, /g, .
# , , .
# /x , /i `is'
# "Is is this ok?", /g .
$/ = "";
# "paragrep"
while (<>) {
while ( m{
\b
#
(\w\S+)
# ,
(
\s+
# ,
\1
# .
) +
# , ,
\b
#
}xig
)
{
print " '$1' $.\n";
}
}
, :
'THE' 100
, .
191
m// ()
EXPR =~ m/PATTERN/cgimosx
EXPR =~ /PATTERN/cgimosx
EXPR =~ ?PATTERN?cgimosx
m/PATTERN/cgimosx
/PATTERN/cgimosx
?PATTERN?cgimosx
{ ... } # $_
{ ... } # $_
m// ,
(. . $1, $2, $3 . .),
, .
,
. ,
. ,
( /g), (1).
, m//
,
:
if (($key,$value) = /(\w+): (.*)/) { ... }
m// ( )
. 5.1.
192
5.
5.1. m//
/i
/m
^ $ \n
/s
.
$*
/x
( )
/o
/g
/cg
/g
.
. /g , . .
. .
m//g .
, , , "perl", "Perl", "PERL" . .:
if (@perls = $paragraph =~ /perl/gi) {
printf "Perl %d .\n", scalar @perls;
}
/g ,
. , .
:
$string = "password=xyzzy verbose=9 score=0";
:
%hash = (password => "xyzzy", verbose => 9, score => 0);
, , , .
, m//g
/, :
%hash = $string =~ /(\w+)=(\w+)/g;
(\w+)
. . .
/g ,
Perl ,
, .
\G (. \G
). /g
193
/c (continue), /g ,
.
?, ?PATTERN?,
, /PATTERN/,
reset.
,
, .
, , , ,
,
reset. Perl .
?? ,
, :
open DICT, "/usr/dict/words" or die " words: $!\n";
while (<DICT>) {
$first = $1 if ?(^neur.*)?;
$last = $1 if /(^neur.*)/;
}
print $first,"\n";
# "neurad"
print $last,"\n";
# "neurypnology"
reset ??,
, reset. m??
??.
s/// ()
LVALUE =~ s/PATTERN/REPLACEMENT/egimosx
s/PATTERN/REPLACEMENT/egimosx
PATTERN , ,
REPLACEMENT. ( .)
$lotr = $hobbit;
#
$lotr =~ s/Bilbo/Frodo/g; # .
, s/// (
), (
/g, ).
, ,
(""), 0.
if ($lotr =~ s/Bilbo/Frodo/) { print " ." }
$change_count = $lotr =~ s/Bilbo/Frodo/g;
, ,
.
($`, $&, $', $1, $2 . .)
194
5.
, . , ,
"revision", "version", "release"
\u
:
s/revision|version|release/\u$&/g; # | ""
, . , %Names,
;
, $Names{"3.0"} "Isengard".
s///
:
s/version ([09.]+)/the $Names{$1} release/g;
$1 , ( )
. ( \1, ,
. ,
, \1 <Ctrl>+<A>.)
PATTERN ,
.
, PATTERN REPLACEMENT,
PATTERN , s///
, REPLACEMENT , . (PAT
TERN ,
/g.)
, . 5.2
; , m// qr//.
.
5.2. s///
/i
( )
/m
^ $ \n
/s
.
$*
/x
( )
/o
/g
, . .
/e
/g /// ,
, PATTERN REPLACEMENT. s///g
, ,
195
m//g, , m//g
. ( s///g ,
m//g.)
/e REPLACEMENT Perl,
.
. , s/([09]+)/sprintf("%#x", $1)/ge
, , , 2581 0xb23.
, ,
,
. /x, :
s{
version
\s+
(
[09.]+
)
}{
$Names{$1}
? "the $Names{$1} release"
: $&
}xge;
s///e (, , )
,
.
,
. /e (, /ee, /eee
. .) eval STRING
, /e.
,
$@.
.
en passant
, ,
, . :
$lotr = $hobbit;
$lotr =~ s/Bilbo/Frodo/g;
.
, ,
=~ .
($lotr = $hobbit) =~ s/Bilbo/Frodo/g;
$hobbit, $lotr
,
.
196
5.
s/// .
. for/foreach
$_
Perl
:
for (@chapters) { s/Bilbo/Frodo/g } #
s/Bilbo/Frodo/g for @chapters;
# .
,
, :
@oldhues = ('bluebird', 'bluegrass', 'bluefish', 'the blues');
for (@newhues = @oldhues) { s/blue/red/ }
print "@newhues\n";
# : redbird redgrass redfish the reds
, .
:
for ($string) {
s/^\s+//;
s/\s+$//;
s/\s+/ /g;
}
#
#
#
, :
$string = join(" ", split " ", $string);
,
:
for ($newshow = $oldshow) {
s/Fred/Homer/g;
s/Wilma/Marge/g;
s/Pebbles/Lisa/g;
s/Dino/Bart/g;
}
/g ,
, . .
, , $`
. , s///.
, , s///
, ,
.
1, , ,
197
.
, :
#
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/;
# 8
1 while s/\t+/' ' x (length($&)*8 length($`)%8)/e;
# ( ( ( )))
1 while s/\([^()]*\)//g;
# , ( ( ...))
1 while s/\b(\w+) \1\b/$1/gi;
, :
Paris in THE THE THE THE spring.
:
Paris in THE THE spring.
, ,
, , . . the
.1 , , .
tr/// ()
LVALUE =~ tr/SEARCHLIST/REPLACEMENTLIST/cds
tr/SEARCHLIST/REPLACEMENTLIST/cds
sed y///
tr///. y,
q m. y/// tr///,
.
, .
, SEARCH
LIST ( ), REPLACE
MENTLIST ( ). m// s///,
=~ !~,
. ( qr// split ,
,
. .)
.
=~ !~,
1
Spring ; ; Paris ; , . . .
198
5.
# rot13 .
, AZ
ASCII. ,
, , ,
.
,
(ae, AE) (04).
. ,
: ABCDE.
SEARCHLIST REPLACEMENTLIST ,
, ; , ,
,
, \n \015.
. 5.3 , tr///.
, m//, s/// qr//,
.
5.3. tr///
/c
SEARCHLIST
/d
/s
/c,
SEARCHLIST; . . , *
SEARCHLIST. Unicode
, ,
, , .
/d tr/// :
, SEARCHLIST, REPLACE
MENTLIST, . ( ,
tr(1), , SEARCHLIST.)
/s, ,
, .
/d REPLACEMENTLIST
, . , REPLACEMENTLIST
SEARCHLIST, .
REPLACEMENTLIST ,
SEARCHLIST, ,
, . /s.
199
tr/aeiou/!/;
tr{/\\\r\n\b\f. }{_};
# !
#
# ASCII
tr/@$%*//d;
tr#AZaz09+/##cd;
#
# base64
# en passant
($HOST = $host) =~ tr/az/AZ/;
$pathname =~ tr/azAZ/_/cs; # (ASCII)
tr [\200\377]
[\000\177];
# 8
SEARCHLIST ,
,
tr/AAA/XYZ/
A X ( $_).
tr///,
eval EXPR:
$count = eval "tr/$oldlist/$newlist/";
die if $@; #
eval
: tr///
.
\U \L , (
uc lc),
Unicode, tr/az/AZ/ . , Unicode
\u ucfirst
(titlecase),
.
, ,
, ,
, .
,
Perl,
.
, Perl. (
.)
200
5.
Perl
12 , ,
:
\ | ( ) [ { ^ $ * + ? .
,
,
. ,
, , *
(metasymbols, symbols).
, ( Perl)
. .
( . ^ $) .
.
, ,
, \. ,
, , , *, + ?.
, |, ,
. ,
, ,
, (...) [...]. ,
| *, +, ?
.
,
. (... .) ,
.
,
, .
,
. \.
, \$ , \\
. . (escaping,
quoting backslashing). (, ,
,
.)
,
.
, , . ,
.
. 5.7.
,
(
201
, ).
(...) . ,
, , ...,
.
. 5.4 .
, ,
. . ,
, (
); ^ $ ,
, , ,
, .
5.4.
\...
( )
...|...
( )
(...)
( )
[...]
( )
(, ,
)
(,
)
(, ,
)
, , ,
(. . )
. . 5.5.
5.5.
*
0 ()
1 ()
1 0 ()
{COUNT}
COUNT
{MIN,}
MIN ()
{MIN, MAX}
MIN , MAX (
)
*?
0 ()
202
5.
5.5 ()
+?
1 ()
??
0 1 ()
{MIN,}?
MIN ()
{MIN, MAX}?
MIN, MAX (
)
, .
, .+ ,
, .
, /() ?/
.
, .
.
, ,
.
(?KEY...); . . (),
, KEY
. KEY
. . 5.6.
, ,
.
,
().
5.6.
(?#...)
(?:...)
(?imsximsx)
(?imsx
imsx:...)
(?=...)
True,
(?!...)
True,
(?<=...)
True,
(?<!...)
True,
(?>...)
(?{...})
Perl
(??{...})
Perl
(?(...)...|...)
(?(...)...)
203
ifthenelse
ifthen
, . 5.7
. (, ,
,
.)
5.7. *
)
\0
\NNN
, , \377
\n
n ()
\a
(BEL)
\A
True
\b
(BS)
\b
True
\B
True
\cX
\C
(C char) utf8 ()
\d
\D
\e
\E
\f
(FF)
\G
True m//g
\l
\L
\E
\n
( NL, CR
Mac)
\N{NAME}
(\N{greek:Sigma})
\p{PROP}
\P{PROP}
204
5.
5.7 ()
)
\Q
() \E
\r
( CR, NL
Mac)
\s
\S
\t
(HT)
\u
\U
( ) \E
\w
(
_)
\W
\x{abcd}
\X
Unicode
\z
True
\Z
True
\p \P ,
. \x
, .
\N.
( )
, ...
. ,
,
. ,
. , \b
: (back
space), .
,
, ,
.
, ,
. ,
, ,
.
, ,
205
.
,
, .
, ,
,
.
( `...`, qq(...), qx(...)
(here documents).
,
qr// ( ).
, escape
(\U )
,
.
, escape
. , escape
(\U . .)
, m'...' qr'...'.
escape
,
,
.
,
,
, tr///. (
,
).
, , ,
. , /a/ a, /=/ = . .
,
, ;
. Perl
:
Escape)
\0
Null (ASCII NUL)
\a
(BEL)
\e
Escape (ESC)
\f
(FF)
\n
(NL, CR Mac)
\r
(CR, NL Mac)
\t
(HT)
206
5.
, , Perl
:
\cX
, , \cC <Ctrl>+<C>, \cZ
<Ctrl>+<Z>, \c[ <Esc> \c? <Del>.
\NNN
,
. 0 , , 010 (8
), ( )
,
. n ,
n ( n
);
.
\x{LONGHEX}
\xHEX
,
([09afAF]), \x1B.
,
. ,
, Unicode.
, \x{262f} Unicode .
\N{NAME}
, \N{GREEK SMALL LETTER EPSILON}, \N{gre
ek:epsilon} \N{epsilon}. use charnames,
31 ,
, (":long", ":full", ":short"
, ).
Unicode
Unicode __PERLLIB/unicode/Names.txt.
,
(
). ("."), \C \X.
. ,
() ,
.
, .
\C \X ,
Unicode,
, .
207
,
. ( /s .)
,
.
, ,
:
if ($pathname =~ /\.(.)\z/s) {
print " $1\n";
}
, ,
, .
\z ,
, /s ,
. (,
, ,
.)
. .*
, .*?
.
: /(..):(..):(..)/ ,
.
, use utf8,
,
Unicode. (
use utf8, . ,
, , ,
.)
use utf8;
use charnames qw/:full/;
$BWV[887] = "G\N{MUSIC SHARP SIGN} minor";
($note, $black, $mode) = $BWV[887] =~ /^([AG])(.)\s+(\S+)/;
print "That's lookin' sharp!\n" if $black eq chr(9839);
\X . ,
, Unicode,
combining character sequence
. ,
( ,
(dieresis)),
. \X (?:\PM\pM*).
,
. /\X/
, .
( , .)
208
5.
Unicode ,
, \C.
( char C),
Unicode. .
15.
, (
) .
.
:
Perl, Perl Unicode POSIX.
.
,
, \d+ . ( ,
\w . \w+.)
, ,
.
, [aeiouy] , .
( "w", "r".)
.
az. , , [09afAF]
.
, ,
,
(, ,
).
^ ( , , )
,
, . (
, , , ,
.) , [^aeiouy] ,
.
, . ,
, ,
( ) ,
, ,
. , ,
209
Perl
Perl
. . 5.8.
,
.
, :
.
,
Unicode, . (
, use bytes.
1
Cirth (, ) Tengwar () . ..
() (). Klin
gon ()
Star Trek. (Mark Okrand)
( Paramount).
. .
210
5.
utf8 Unicode .
utf8 .)
5.8.
utf8
\d
[09]
\p{IsDigit}
\D
[^09]
\P{IsDigit}
\s
[ \t\n\r\f]
\p{IsSpace}
\S
[^ \t\n\r\f]
\P{IsSpace}
\w
[azAZ09_]
\p{IsWord}
\W
( )
[^azAZ09_] \P{IsWord}
( ,
; \w .
Perl, .)
, :
if ($var =~ /\D/)
{ warn " " }
if ($var =~ /[^\w\s.]/) { warn " (, , )" }
Unicode
Unicode \p{PROP}
\P{PROP}. ,
, , \pN
( ,
).
:
if ($var =~ /^\p{IsAlpha}+$/)
{ print " " }
if ($var =~ s/[\p{Zl}\p{Zp}]/\n/g) { print " " }
Unicode,
, Perl
. Zl Zp Unicode,
, IsAlpha Perl
, Ll, Lu, Lt Lo (. .
, ).
Perl 5.6.0 use utf8, .
.
. , ,
. , Unicode
, .
.
211
Unicode \p{IsPROP}. Is
, ,
.
Unicode Perl
, . 5.9 Perl.
,
POSIX .
5.9. Unicode
IsASCII
IsAlnum
IsAlpha
IsCntrl
IsDigit
IsGraph
IsLower
IsPrint
IsPunct
IsSpace
IsUpper
IsWord
IsXDigit
[\x00\x7f]
[\p{IsLl}\p{IsLu}\p{IsLt}\p{IsLo}\p{IsNd}]
[\p{IsLl}\p{IsLu}\p{IsLt}\p{IsLo}]
\p{IsC}
\p{Nd}
[^\pC\p{IsSpace}]
\p{IsLl}
\P{IsC}
\p{IsP}
[\t\n\f\r\p{IsZ}]
[\p{IsLu}\p{IsLt}]
[_\p{IsLl}\p{IsLu}\p{IsLt}\p{IsLo}\p{IsNd}]
[09afAF]
Perl
Unicode (. ):
IsC
IsL
IsM
IsN
IsP
IsS
IsZ
. .
(? ?)
212
5.
Unicode
. 5.10 Unicode,
.
. ,
. Unicode
,
.
5.10. Unicode
IsCc
Normative
IsCf
IsCn
IsCo
IsCs
IsLl
IsLm
IsLo
IsLt
IsLu
IsMc
IsMe
IsMn
IsNd
IsNl
IsNo
IsPc
IsPd
IsPe
IsPf
IsPi
IsPo
IsPs
IsSc
IsSk
IsSm
IsSo
IsZl
IsZp
IsZs
213
,
( )
.
.
, , ,
, .
IsDecoCanon
IsDecoCompat
( )
IsDCcircle
IsDCfinal
()
IsDCfont
IsDCfraction
IsDCinitial
()
IsDCisolated
()
IsDCmedial
()
IsDCnarrow
(narrow)
IsDCnoBreak
( )
IsDCsmall
IsDCsquare
(, , ),
IsDCsub
IsDCsuper
IsDCvertical
( )
IsDCwide
(wide)
IsDCcompat
(identity),
, ,
:
IsBidiL
(, )
IsBidiLRE
IsBidiLRO
IsBidiR
IsBidiAL
IsBidiRLE
IsBidiRLO
214
5.
()
IsBidiPDF
IsBidiEN
IsBidiES
IsBidiET
IsBidiAN
IsBidiCS
IsBidiNSM
(nonspacing mark)
IsBidiBN
(boundary neutral)
IsBidiB
IsBidiS
IsBidiWS
IsBidiON
Other Neutrals
IsMirrored
:
IsSylA
IsSylAA
IsSylAAI
IsSylAI
IsSylC
IsSylE
IsSylEE
IsSylI
IsSylII
IsSylN
IsSylO
IsSylOO
IsSylU
IsSylV
IsSylWA
IsSylWAA
IsSylWC
IsSylWE
IsSylWEE
IsSylWI
IsSylWII
IsSylWO
IsSylWOO
IsSylWU
IsSylWV
Unicode
Unicode \p{InSCRIPT}. (
Is In.) In
SCRIPT.
, ,
215
,
. \P{InSCRIPT},
, ; ,
\P{InDingbats} , .
:
InArabic
InArmenian
InArrows
InBasicLatin
InBengali
InBopomofo
InBoxDrawing
InCherokee
InCyrillic
InDevanagari
InDingbats
InEthiopic
InGeorgian
InGreek
InGujarati
InGurmukhi
InHangulJamo
InHebrew
InHiragana
InKanbun
InKannada
InKatakana
InKhmer
InLao
InMalayalam
InMongolian
InMyanmar
InOgham
InOriya
InRunic
InSinhala
InSpecials
InSyriac
InTamil
InTelugu
InThaana
InThai
InTibetan
InYiRadicals
InYiSyllables
:
InAlphabeticPresentationForms
InArabicPresentationFormsA
InArabicPresentationFormsB
InBlockElements
InBopomofoExtended
InBraillePatterns
InCJKCompatibility
InCJKCompatibilityForms
InCJKCompatibilityIdeographs
InCJKRadicalsSupplement
InCJKSymbolsandPunctuation
InCJKUnifiedIdeographs
InCJKUnifiedIdeographsExtensionA
InCombiningDiacriticalMarks
InCombiningHalfMarks
InCombiningMarksforSymbols
InControlPictures
InCurrencySymbols
InEnclosedAlphanumerics
InEnclosedCJKLettersandMonths
InGeneralPunctuation
InGeometricShapes
InGreekExtended
InHalfwidthandFullwidthForms
InHangulCompatibilityJamo
InHangulSyllables
InHighPrivateUseSurrogates
InHighSurrogates
InIdeographicDescriptionCharacters
InIPAExtensions
InKangxiRadicals
InLatin1Supplement
InLatinExtendedA
InLatinExtendedB
InLatinExtendedAdditional
InLetterlikeSymbols
InLowSurrogates
InMathematicalOperators
InMiscellaneousSymbols
InMiscellaneousTechnical
InNumberForms
InOpticalCharacterRecognition
InPrivateUse
InSuperscriptsandSubscripts
InSmallFormVariants
InSpacingModifierLetters
:
InUnifiedCanadianAboriginalSyllabics
,
__PERLLIB/unicode/In/*.pl. , In
, ,
. , ,
Is, ,
, . ,
216
5.
. ,
Latin1.
; , ,
. .
,
, (. 6
). ,
(. 10 ), ,
,
(. 11 ),
, (. 12 ).
,
, __PERLLIB/unicode/Is. ,
, (line). ,
. , ,
,
, (
). , :
sub InKana {
return <<'END';
3040
309F
30A0
30FF
END
}
,
:
sub InKana {
return <<'END';
+utf8::InHiragana
+utf8::InKatakana
END
}
.
, ,
. :
sub IsKana {
return <<'END';
+utf8::InHiragana
+utf8::InKatakana
217
utf8::IsCn
END
}
,
!:
sub IsNotKana {
return <<'END';
!utf8::InHiragana
utf8::InKatakana
+utf8::IsCn
END
}
Perl
( \w),
( [.\w\s]). ,
, , ,
Perl 64
, ,
. ( 64 (swatches),
utf8.)
, ,
, .
POSIX
Perl,
POSIX, [:CLASS:],
,
. /[.,[:alpha:][:digit:]]/
, (
) , ,
.
POSIX, Perl 5.6, . 5.11.
5.11. POSIX
alnum
, . . alpha digit
alpha
. ( , ,
Unicode, .)
ascii
cntrl
0 127
. ,
, , ,
, ,
. 32
218
5.
5.11 ()
digit
, , , 0 9. ( Uni
code .) \d
graph
lower
print
punct
,
space
. ,
, ( Uni
code). \s
( )
upper
word
xdigit
,
.
( [09afAF]),
POSIX,
^ [:. ( Perl.) :
POSIX
[:^digit:]
\D
[:^space:]
\S
[:^word:]
\W
219
, POSIX .
, :, i, t, g d.
Perl , : .
:
42 =~ /^[[:digit:]]+$/
,
. , /nop/,
, . ,
panoply xenophobia, , ,
.
, xenophobia, Snoopy,
/nop/ ,
o n p, Snoopy .
: ,
, .
. ,
,
. ,
({5}).
, ,
({2,4}). , ,
, , ({2,}).
. 5.12 , Perl .
5.12.
{MIN, MAX}
{MIN, MAX}?
MIN , MAX
{MIN,}
{MIN,}?
MIN
{COUNT}
{COUNT}?
COUNT
*?
0 ( , {0,})
+?
1 ( , {1,})
??
0 1 ( , {0,1})
220
5.
, * ?, . ,
0 . +
,
.
.
, .
, $n =~ /\d{3}/ :
? , $n ,
. 101 Morris Street ,
95472 18005551212.
, , .
. ,
( /^\d{3}$/).
,
. ,
: , ,
, ,
,
.
, , ,
: , ,
, .
, , .
.
Perl :
? ,
. ,
,
, ,
,
. ,
, , , . (
.)
, :
"exasperate" =~ /e(.*)e/
# $1 "xasperat"
.* "xasperat", . (
$1,
.) ,
.
, .
:
"exasperate" =~ /e(.*?)e/
# $1 "xasp"
221
, .*?. ? *
*? : ,
, .
*? ,
, ,
. , $1 "",
"e", . .
.
, /e(.*?)e/
Perl $1 "rat" . "rat" "e"
"xasp". Perl
,
. ,
,
, ,
.
, .
,
.
, . Perl
; *
.
.1
. ,
( .*),
.
,
:
"exasperate" =~ /.*e(.*?)e/
# $1 "rat"
,
.
, .
.
,
, . Perl . ,
( ).
,
/() ?/.
222
5.
, ,
.
,
. (asser
tions). ( (anchors),
.)
. substr
, ,
.
, , .
, .
, ,
.
: \ A ^
\A , .
(string)
(line) ^. , /m1
, ^
:
/\Abar/
/^bar/
/^bar/m
# "bar" "barstool"
# "bar" "barstool"
# "bar" "barstool" and "sand\nbar"
/g /m
^ :
s/^\s+//gm;
#
$total++ while /^./mg; #
: \z, \Z $
\z .
\Z ,
, , . $
, \Z. /m
, $
:
1
$* 1 $*
/s.
223
/bot\z/
/bot\Z/
/bot$/
/bot$/m
#
#
#
#
"robot"
"robot" "abbot\n"
"robot" "abbot\n"
"robot" "abbot\n" "robot\nrules"
/^robot$/
/^robot$/m
/\Arobot\Z/
/\Arobot\z/
#
#
#
#
"robot" "robot\n"
"robot" "robot\n" "this\nrobot\n"
"robot" "robot\n"
"robot" eq?
^, /m $
, /g. (
, $_ ; ,
$/ "".)
s/\s*$//gm;
while (/^([^:]+):\s*(.*)/gm ) { #
$headers{$1} = $2;
}
, : $foo
bc, /a$foo/ /abc/. $
. $ ,
,
.
: \b \B
\b ,
\w \W, . \W\w
, \w\W . (
\W.) \B
, , . .
\w\w \W\W.
/\bis\b/
/\Bis\B/
/\bis\B/
/\Bis\b/
#
#
#
#
\W ( ),
\b , isn't, booktech@oreil
ly.com, M.I.T. key/value.
([\b]) \b ,
.
224
5.
/g pos
,
:
$burglar = "Bilbo Baggins";
while ($burglar =~ /b/gi) {
printf " B %d\n", pos($burglar)1;
}
( , , pos
.)
:
B 0
B 3
B 6
. /c ( continue),
/g ,
. ,
.
$burglar = "Bilbo Baggins";
while ($burglar =~ /b/gci) {
# ADD /c
printf " B %d\n", pos($burglar)1;
}
while ($burglar =~ /i/gi) {
printf " I %d\n", pos($burglar)1;
}
B, , Perl i,
10. /c
i 1.
: \G
pos
substr, .
, , .
, , , \G.
\G , pos
.
/g ( pos
), \G
.
,
225
pos. ,
:
($recipe = <<'DISH') =~ s/^\s+//gm;
Preheat oven to 451 deg. fahrenheit.
Mix 1 ml. dilithium with 3 oz. NaCl and
stir in 4 anchovies. Glaze with 1 g.
mercury. Heat for 4 hours and let cool
for 3 seconds. Serves 10 aliens.
DISH
$recipe
$recipe
$recipe
$recipe
$recipe
$recipe
=~
=~
=~
=~
=~
=~
/\d+ /g;
/\G(\w+)/;
/\d+ /g;
/\G(\w+)/;
/\d+ /g;
/\G(\w+)/;
# $1 "deg"
# $1 "ml"
# $1 "oz"
\G ,
.
, . ,
, :
pos($recipe) = 0;
# \G 0
$recipe =~ /(\d+) /g ) {
my $amount = $1;
if ($recipe =~ / \G (\w{0,3}) \. \s+ (\w+) /x) { # . +
print "$amount $1 of $2\n";
} else {
$recipe =~ / \G (\w+) /x;
#
print "$amount $1\n";
}
}
:
451 deg of fahrenheit
1 ml of dilithium
3 oz of NaCl
4 anchovies
1 g of mercury
4 hours
3 seconds
10 aliens
, . *
(clustering), (capturing).
226
5.
,
.
$1, $2 . .
; Perl
, .
:
/(\d)(\d)/ # , $1 $2
/(\d+)/
# , $1
/(\d)+/
# , $1
.
, .
. ,
, , .
(back*
references), .
. ,
, , ,
, .
\1, \2 . .1 ,
, "the the" "had had", :
/\b(\w+) \1\b/i
$1,
. ,
( ), :
From: gnat@perl.com
To: camelot@oreilly.com
Date: Mon, 17 Jul 2000 09:00:00 1000
Subject: Eye of the needle
,
.
(, ), :
1
$1 ,
.
\1.
,
. , Perl \11 $11,
11 .
\011, . . .
227
while (<>) {
/^(.*?): (.*)$/;
$fields{$1} = $2;
}
# $1, $2
. Pri
mula Brandybuck
/^((\w+) (\w+))$/
. 5.1.
, . . ,
:
($first, $last)
= /^(\w+) (\w+)$/;
($full, $first, $last) = /^((\w+) (\w+))$/;
/g
.
, , ,
(, $_). ,
, :
%fields = /^(.*?): (.*)$/gm;
,
. /gm
228
5.
, %fields
/,
.
, ,
. $& ,
$` , , $' , $+
.
$_ = "Speak, <EM>friend</EM>, and enter.";
m[ (<.*?>) (.*?) (</.*?>) ]x;
# , ,
print "prematch: $`\n";
# Speak,
print "match: $&\n";
# <EM>friend</EM>
print "postmatch: $'\n";
# , and enter.
print "lastmatch: $+\n";
# </EM>
(
) 28 .
@ (@LAST_MATCH_START)
, @+ (@LAST_MATCH_END) :
#!/usr/bin/perl
$alphabet = "abcdefghijklmnopqrstuvwxyz";
$alphabet =~ /(hi).*(stu)/;
print "
$[0] $+[0]\n";
print " $[1] $+[1]\n";
print " $[2] $+[2]\n";
, ,
:
/\(e.g., .*?\)/
(. . ).
,
e g (. .
).
, .
. ,
.
: (?:PATTERN) .
,
:
229
1. .
2. ;
, /^cat|cow|dog$/ /^(?:cat|cow|dog)$/ ,
(cat) ^.
3.
, , , /foo(?i:Case_Matters)bar/i. (.
.)
, , ,
.
.
.
,
Perl.
.
, ,
. (?:PATTERN) ,
. :
@fields = split(/\b(?:a|b|c)\b/)
:
@fields = split(/\b(a|b|c)\b/)
. ( split m//g
, , .
split , .
split . 29.)
(cloister) /i, /m, /s /x,
, ( )
? :, . :
/Harry (?i:s) Truman/
230
5.
, /i /x.
:
/Harry (?xi: [AZ] \.? \s )?Truman/i
,
, ,
/i, , .
PATTERN,
, . ,
,
, :
/(?i)foo/
/foo((?i)bar)/i
/foo((?xi) bar)/
# /foo/i
# "bar"
# /x /i "bar"
,
. , (?i:bar)
(?xi: bar), .
,
, .
, . /s
.
|
, . :
/Gandalf|Saruman|Radagast/
,
$1, .
231
, . .
, :
/(Sam|Samwise)/
$1 Samwise,
,
Sam. ,
.
.
,
Sam:
"'Sam I am,' said Samwise" =~ /(Samwise|Sam)/;
# $1 eq "Sam"
,
,
:
"'Sam I am,' said Samwise" =~ /.*(Samwise|Sam)/; # $1 eq "Samwise"
( )
, ,
\G, ^ $. :
"'Sam I am,' said Samwise" =~ /(Samwise|Sam)$/; # $1 eq "Samwise"
$ (
, ),
,
. ,
__DATA__ __END__:
#!/usr/bin/perl
while (<>) {
print if /^__DATA__|^__END__/;
}
. ,
( | )
, .
:
/^cat|dog|cow$/
:
/^(cat|dog|cow)$/
232
5.
"cat" "dog" ,
"cow" . ,
"cat" "dog", "cow". $1,
, :
/^cat$|^dog$|^cow$/
.
.
/com(pound|)/;
# "compound" "com"
/com(pound(s|)|)/; # "compounds", "compound" "com"
?, 0
1 :
/com(pound)?/;
# "compound" "com"
/com(pound(s?))?/; # "compounds", "compound" "com"
/com(pounds?)?/;
# , $2
. ? ,
,
, .
, ,
.
,
. , ,
, . ,
:
; .
, ,
.
Perl. , Perl
,
Perl .
Perl
, , .
Perl
. ,
,
233
. , ,
, .
, .
, ,
Perl , ,
. ,
. ,
. .
:
/Gandalf|Saruman|Radagast/
, , , ,
,
.
,
, , ,
.
Perl :
// || // || //
. ,
. , .
, ()
.
,
. , ,
.
||.
. ,
. ,
. , ,
. ,
,
:
while (<CONF>) {
next if /^#/;
next if /^\s*(#|$)/;
chomp;
munchabunch($_);
}
,
Perl
234
5.
, ,
( ) ,
. :
warn
warn
warn
warn
warn
warn
warn
" "
if
/\D/;
" " unless /^\d+$/;
# 3
" "
unless /^?\d+$/;
# +3
" "
unless /^[+]?\d+$/;
" " unless /^?\d+\.?\d*$/;
# rejects .2
" " unless /^?(?:\d+(?:\.\d*)?|\.\d+)$/;
" C"
unless /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+]?\d+))?$/;
, , ,
.
Perl .
, .
(, , .)
Perl,
,
. ,
; , .
,
.
, ,
, ,
. ( .)
,
. , ,
:
if ($num =~ /^[+]?\d+\.?\d*$/) { ... }
, :
$sign = '[+]?';
$digits = '\d+';
$decimal = '\.?';
$more_digits = '\d*';
$number = "$sign$digits$decimal$more_digits";
...
if ($num =~ /^$number$/o) { ... }
. ,
235
/o ,
, $number
.
,
:
chomp($answer = <STDIN>);
if
("SEND" =~ /^\Q$answer/i)
elsif ("STOP" =~ /^\Q$answer/i)
elsif ("ABORT" =~ /^\Q$answer/i)
elsif ("LIST" =~ /^\Q$answer/i)
elsif ("EDIT" =~ /^\Q$answer/i)
{
{
{
{
{
print
print
print
print
print
"Action
"Action
"Action
"Action
"Action
is
is
is
is
is
send\n"
stop\n"
abort\n"
list\n"
edit\n"
}
}
}
}
}
send, S,
SE, SEN SEND ( ).
stop , , ST ( St, sT, st).
,
. ,
, ,
,
, (
). , Perl
. ( ,
, \b,
,
. , ,
.)
,
. ,
/x:
($col1, $col2) = /(.*?) \t+ (.*?)/x;
Perl \t
, \t ,
/x.
Perl .
. ,
:
$colsep = "\t+";
# ( )
($col1, $col2) = /(.*?) $colsep (.*?)/x;
, \t
,
236
5.
, , /(.*?)+(.*?)/,
. , /x
. , qr//. (.
.)
escape ,
,
: \U, \u, \L, \l, \Q \E. ,
Perl,
escape, \t , \n
. . escape
. ( . 5.7
, .)
,
, .
?
. m'...',
qr'...' s'...'...'
escape,
, . m'\ufrodo',
frodo ( ) . ,
, m'\t\d'
, .
.
:
$var = '\U';
/${var}frodo/;
frodo . Perl
,
, , , .
, , :
$hobbit = 'Frodo';
$var = '$hobbit';
/$var/;
# ( )
# m'$hobbit', m'Frodo'.
, ,
,
. , grep,
Perl:1
1
, grep, .
grep : , grep
. ( ,
Perl .)
237
#!/usr/bin/perl
$pattern = shift;
while (<>) {
print if /$pattern/o;
}
pgrep :
% pgrep '\t\d' *.c
, C,
.
, Perl , \t .
Perl
, ; , .
.
grep i,
.
pgrep; .
/i:
% pgrep '(?i)ring' LotR*.pod
qr//
, ,
, . , Perl
, ; ,
.
, ,
/o,
:
print if /$pattern/o;
pgrep,
. ,
, , :
foreach $item (@data) {
foreach $pat (@patterns) {
if ($item =~ /$pat/) { ... }
}
}
238
5.
/$pat/o, $pat
.
qr/PATTERN/imosx,
PATTERN. PATTERN , m/PATTERN/.
',
( escape )
. Perl,
. ,
$regex = qr/my.STRING/is;
s/$regex/something else/;
:
s/my.STRING/something else/is;
:
@regexes = ();
foreach $pat (@patterns) {
push @regexes, qr/$pat/;
}
Perl map:
@regexes = map { qr/$_/ } @patterns;
,
:
foreach $item (@data) {
foreach $re (@regexes) {
if ($item =~ /$re/) { ... }
}
}
Perl
if, ,
.
qr// ,
:
$regex = qr/$pattern/;
$string =~ /foo${regex}bar/;
Perl ,
qr//.
239
, qr// ,
,
13 . ,
:
$re = qr/my.STRING/is;
print $re;
# (?sixm:my.STRING)
/s /i ,
qr//. /x /m, , ,
.
, ,
,
,
:
$re = qr/$pat/is;
#
$re = eval { qr/$pat/is } || warn ... #
eval . 29.
,
.
,
.
,
, ,
(. ).
.
. . ( , , ,
, ,
. .)
,
, , . use re
"debug", ,
. ( ,
Dr, , Perl
DDEBUGGING.)
#!/usr/bin/perl
use re "debug";
"Smeagol" =~ /^Sm(.*)g[aeiou]l$/;
240
5.
. , Perl
: BOL
(^), REG_ANY . .:
Compiling REx `^Sm(.*)g[aeiou]l$'
size 24 first at 2
rarest char l at 0
rarest char S at 0
1: BOL(2)
2: EXACT <Sm>(4)
4: OPEN1(6)
6: STAR(8)
7:
REG_ANY(0)
8: CLOSE1(10)
10: EXACT <g>(12)
12: ANYOF[aeiou](21)
21: EXACT <l>(23)
23: EOL(24)
24: END(0)
anchored `Sm' at 0 floating `l'$ at 4..2147483647
(checking anchored) anchored(BOL) minlen 5
Omitting $` $& $' support.
,
. , "Sm",
. ,
"l",
. ,
, .
, ,
. (. study 29.)
:
EXECUTING...
Guessing start of match, REx `^Sm(.*)g[aeiou]l$' against `Smeagol'...
Guessed: match at offset 0
Matching REx `^Sm(.*)g[aeiou]l$' against `Smeagol'
Setting an EVAL scope, savestack=3
0 <> <Smeagol>
| 1: BOL
0 <> <Smeagol>
| 2: EXACT <Sm>
2 <Sm> <eagol>
| 4: OPEN1
2 <Sm> <eagol>
| 6: STAR
REG_ANY can match 5 times out of 32767...
Setting an EVAL scope, savestack=3
7 <Smeagol> <>
| 8:
CLOSE1
7 <Smeagol> <>
| 10:
EXACT <g>
failed...
6 <Smeago> <l>
| 8:
CLOSE1
6 <Smeago> <l>
| 10:
EXACT <g>
241
5 <Smeag> <ol>
5 <Smeag> <ol>
failed...
| 8:
CLOSE1
| 10:
EXACT <g>
failed...
| 8:
CLOSE1
| 10:
EXACT <g>
| 12:
ANYOF[aeiou]
| 21:
EXACT <l>
| 23:
EOL
| 24:
END
4 <Smea> <gol>
4 <Smea> <gol>
5 <Smeag> <ol>
6 <Smeago> <l>
7 <Smeagol> <>
7 <Smeagol> <>
Match successful!
Freeing REx: `^Sm(.*)g[aeiou]l$'
Smeagol, ,
, .*
, ,
.
.
, /() ?/
, : , . ,
. , .
,
Perl .
.
, , ,
. ,
,
. ,
, , ,
.
, .
,
, , .
(). ,
, , , ,
. (back*
tracking).
, ,
. ,
. ,
,
.
242
5.
, , , Perl
.
, ,
, .
.
:1
1
,
2.
.
, ,
. ,
;
,
.
,
,
, ,
. .
.
,
, .
n n + 1
. ,
. ,
/x*/,
"x". "fox", "x".
"f" .
x, /x+/.
5.
, ,
,
(
).
,
,
.
.
243
2
(
|) , ,
,
, .
,
3.
, ,
, 1,
4 6, .
, 2.
, , ,
2 . ( ,
, ,
.)
3
,
, ,
4 5 (,
).
( 4) *
( 5). ,
,
.
,
2.
, ,
:
,
. /^foo/,
, .
, 4,
,
, , 5.
, :
/x*y*/
x* , y*
. , x*
y*
, . .
, .
244
5.
4
,
3
.
, . Perl
,
. ,
(?=...) (?!...)
,
, , ..., (
) ,
.1
5
,
, . (
6.)
,
.
, . .
.
, (. . /x/
, /x{1}/).
,
3
.
*, +, ?, *?, +?, ??
. {COUNT}, ,
.
,
, .
, .
,
.
( )
; , ,
.
, .
,
1
.
2, ,
, ,
. ( , ,
.)
245
,
. :
, ()
,
. (
,
.) ( )
, ,
.
,
.
, , /.*foo/,
( )
, "foo"; "foo"
( ,
),
, "foo". "foo"
, ,
.
.*, ,
.* (,
, "foo" ).
,
,
. ,
/.*?foo/, .*? 0 , 1
, 2 . ., "foo".
, , ,
, "foo", .
6
,
. (
),
5 .
:
, (...),
, , (...)
2. ,
. ,
,
(backreference).
, (?:...),
: $1, $2 . .
246
5.
( ) .
.
, , ,
.
( )
, .
. 5.7.
.
, , .
,
. ,
, ,
. ,
. ,
.
,
. ,
, , ,
,
.
, ,
:
$a = 'nobody';
$b = 'bodysnatcher';
if ("$a $b" =~ /^(\w+)(\w+) \2(\w+)$/) {
print "$2 $1$2$3\n";
}
:
body nobodysnatcher
, $1 "no
body". . ,
$2,
+. , $1
$2.
, \2,
y. y, b.
247
, , $1 body
$2. Habeas corpus,1 .
, ,
, , , "rococo" "cocoon".
, , $2, "co", "co
co". "rocococoon"; "rococoon". ,
.
$1,
/^(\w+?)(\w+) \2(\w+)$/, ,
.
(Jeffrey Friedl) Mastering regular expressions.
Perl
, Perl,
, .
.
,
(lookaround assertions),
,
. ,
( ) ,
. ,
,
( ).
, (lookahead
assertion).
(lookbehind assertion).
,
, ,
.
( , ),
1
Habeas Corpus, ,
1679 .,
, ,
. . .
248
5.
,
.
(?=PATTERN) ( )
(?=PATTERN),
, PATTERN. ,
, :
$_ = "Paris in THE THE THE THE spring.";
, ( ( ...))
1 while s/\b(\w+) \1\b/$1/gi;
,
. (, .) ,
, , ,
, :
s/ \b(\w+) \s (?= \1\b ) //gxi;
, ,
The clothes you DON DON't fit.
(?!PATTERN) ( )
(?=PATTERN),
, PATTERN .
,
:
s/ \b(\w+) \s (?= \1\b (?! '\w))//xgi;
\w ,
. ,
that that particular
.
, that ( ,
):
s/ \b(\w+) \s (?= \1\b (?! '\w | \s particular))//gix;
, 1863
. that that nation .
. .
249
.
, $" |:
@thatthat = qw(particular nation);
local $" = '|';
s/ \b(\w+) \s (?= \1\b (?! '\w | \s (?: @thatthat )))//xig;
(?<=PATTERN) ( )
(?<=PATTERN),
, PATTERN .
.
, that that nation,
Paris, in the the nation of France.
,
@thatthat that that.
s/ \b(\w+) \s (?= \1\b (?! '\w | (?<= that) \s (?: @thatthat )))//ixg;
, ,
, .
, ,
qr// . ,
, .
(?<!PATTERN) ( )
(?<!=PATTERN), ,
PATTERN .
.
: I E, C. Perl
:
s/(?<!c)ei/ie/g
,
.
, /() ?/,
.
*
(nonbacktracking subpattern).
(?>PATTERN) , (?:PATTERN),
PATTERN ,
. (,
250
5.
,
.)
.
, .
: ; ,
. ,
. .
.
, ,
.
Prolog SNOBOL
(scoped cut or fence operator).
, "aaab" =~ /(?:a*)ab/ a* a,
, a .
, ,
. (
, ,
.) , "aaab" =~ /(?>a*)ab/
, ,
.
(?>PATTERN) ,
,
, ,
( ).
, ,
.
:
$_ = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
/a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*[b]/;
. .
b, , ,
, . .
.1 ,
, ,
b,
( ), , /[b]/
1
.
, . .
, , ,
,
.
251
/b/. ,
, , :
/(?>a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*)[b]/;
() .
, ,
, .
Makefile Perl,
:
# Files to be built with variable substitution before miniperl
# is available.
sh = Makefile.SH cflags.SH config_h.SH makeaperl.SH makedepend.SH \
makedir.SH myconfig.SH writemain.SH
:
#!/usr/bin/perl 00p
while ( /( (.+) ( (?<=\\) \n .* )+ ) /gx) {
print "GOT $.: $1\n\n";
}
, . ,
,
, $1. .
, . :
(.+(?:(?<=\\)\n.*)+)
, .
. :
((?>.+)(?:(?<=\\)\n.*)+)
, ,
, .
(?>...) ,
(?:...) (...). ,
, .
Perl (
) ,
: , ,
, , , .
(
, , , , ,
)
252
5.
(, , ).
.
, .
,
, .
(, Prolog)
, , ,
. Perl make(1)
yacc(1),
, , , ,
.
Perl ,
,
, .
, , ,
,
, .
Perl
s/// /e.
.
(?{CODE }) ,
,
,
.
, s///ee (??{CODE })
:
,
.
,1 , ,
. Mastering re
gular expressions ,
,
, RFC 822, . .
.
,
, .
Perl ;
1
253
, , ,
,
, .
.
, $number (.
).
,
.
,
; , audio eerie
VVCVV ()1. , ,
, ,
. VVCVV ,
:
^[aeiouy][aeiouy][cbdfghjklmnpqrstvwxzy][aeiouy][aeiouy]$
VVCVV
.
, , , audio, ,
VVCVV,
. ,
, .
cvmap, :
#!/usr/bin/perl
$vowels = 'aeiouy';
$cons = 'cbdfghjklmnpqrstvwxzy';
%map = (C => $cons, V => $vowels); # C () V ()
#
#
#
#
#
$re = qr/^${pat}$/i;
print "REGEX is $re\n";
@ARGV = ('/usr/dict/words')
#
#
#
if t && !@ARGV;
while (<>) {
print if /$re/;
}
# ,
#
254
5.
%map.
, . C
V , VVCVV, audio,
eerie. , ,
. qr//, (
) . ,
fortuitously (,
):
% cvmap fortuitously /usr/dict/words
REGEX is (?ixsm:^[cbdfghjklmnpqrstvwxzy][aeiouy][cbdfghjklmnpqrstvwxzy][cbd
fghjklmnpqrstvwxzy][aeiouy][aeiouy][cbdfghjklmnpqrstvwxzy][aeiouy][aeiouy][c
bdfghjklmnpqrstvwxzy][cbdfghjklmnpqrstvwxzy][aeiouycbdfghjklmnpqrstvwxzy]$)
carriageable
circuitously
fortuitously
languorously
marriageable
milquetoasts
sesquiquarta
sesquiquinta
villainously
REGEX, ,
,
.
/e (e expression evaluation
) s/PATTERN/CODE/e,
, Perl.
do { CODE }.
, , ,
,
.
/e
, .
:
s/(\d+)/$1 * 2/;
s/(\d+)/$1 * 2/e;
:
$_ = " 233C.\n";
s/\b(\d+\.?\d*)C\b/int($1 * 1.8 + 32) . "F"/e;
# 451F
255
. ,
( ),
100 , (
, ,
):
% perl pi e 's/^(\d+)(?=:)/100 + $1/e' filename
,
. ,
,
. /e
, , eval.
, :
s/PATTERN/CODE/ee
s/PATTERN/eval(CODE)/e
:
s/(\$\w+)/$1/eeg;
eval, /ee
.
(
) :
$_ = "I have 4 + 19 dollars and 8/2 cents.\n";
s{ (
\d+ \s*
#
[+*/]
#
\s* \d+
#
)
}{ $1 }eegx;
# $1
print;
# "I have 23 dollars and 4 cents."
eval STRING,
(, )
(, ). , ,
, $@ ($EVAL_ERROR).
, ,
. if
while ,
. s///e
, , ,
.
256
5.
, ,
.
,
(?{CODE }). ,
. ,
.
, , .
"glyph" =~ /.+ (?{ print "hi" }) ./x; # "hi" .
glyph
, .+ .
hi. , ,
.+ .
, ,
hi, h .
CODE ,
Perl, . ,
my, .
local, , .
(?{CODE }) ,
,
, .
,
.
.
.1
$i 0,
, .
1
, ,
,
, .
: , (
!).
,
, ,
.
. ,
. (
, .)
257
.* . *
, , .
.
$_ = 'lothlorien';
m/ (?{ $i = 0 })
(.
(?{ $i++ })
lori
/x;
)*
# $i 0
# $i,
#
, $i
0 .* 10 .
lori, .*.
, $i 10.
$i ,
.*, :
$_ = 'lothlorien';
m/ (?{ $i = 0 })
(. (?{ local $i = $i + 1; }) )* # $i, .
lori
(?{ $result = $i })
# .
/x;
local $i ,
.*, . $i ,
, (?{ $result =
= $i }) $result.
$^R ( 28)
(?{CODE }), .
(?{CODE }) COND
(?(COND) IFTRUE|IFFALSE). $^R ,
:
"glyph" =~ /.+(?(?{ $foo{bar} gt "symbol" }).|signet)./;
, $foo{bar}, symbol. ,
. , , signet. ,
:
"glyph" =~ m{
.+
(?(?{
$foo{bar} gt "symbol"
})
.
|
signet
#
#
#
#
#
# signet
258
5.
)
.
}x;
use re 'eval',
(?{CODE }), :
/(.*?) (?{length($1) < 3 && warn}) $suffix/; # use re 'eval'
, .
, $suffix,
,
, ,
, .
, use re 'eval'
.
use re 'taint'
, (
, m// ) .
,
,
. .
23 .
( qr//)
:
/foo${pat}bar/
, $pat
, $pat (?{CODE }).
use re 'debug'.
(?{CODE }) , :
"abcdef" =~ / .+ (?{print " : $&\n"}) bcdef $/x;
:
:
:
:
:
:
abcdef
abcde
abcd
abc
ab
a
, .+ ,
.
259
, . (??{CODE })
,
. , /$pattern/,
, $pattern ,
, . :
/\w (??{ if ($threshold > 1) { "red" } else { "blue" } }) \d/x;
:
$text =~ /( \(+ ) (.*?) (??{ '\)' x length $1 })/x;
#
#
, ,
. , .
260
5.
:
$funpat = qr/\w+$np/;
'myfunfun(1,(2*(3+4)),5)' =~ /^$funpat$/;
# !
(?(COND) IFTRUE|IFFALSE)
?: Perl. COND , IFTRUE;
IFFALSE. COND
( \ $),
. (.
.)
COND ,
. :
#!/usr/bin/perl
$x = 'Perl .';
$y = 'ManagerWare $99.95.';
foreach ($x, $y) {
/^(\w+) (?:|()) (?(2)(\$\d+)|\w+)/; # (\$\d+), \w+
if ($3) {
print "$1 .\n";
# ManagerWare .
} else {
print "$1 .\n"; # Perl
.
}
}
COND (2), ,
. , (\$\d+) (
$3); \w+.
COND
, , , IFTRUE IFFALSE,
:
/[ATGC]+(?(?<=AA)G|C)$/;
COND ,
, AAG
C.
|IFFALSE . IFTRUE
, , COND,
,
.
261
Perl, ,
,
, . Perl
,
,
.
,
, Perl . ,
\tag , ,
(?:<.*?>), HTML
XML. , \w ,
.
Tagger,
. , :
use Tagger;
$_ = '<I></I>';
print " " if /\tag\w+\tag/;
Tagger ,
, , :
$re = '\tag\w+\tag';
print if /$re/;
# \t,
# , "a"...
,
convert :
$re = '\tag\w+\tag';
$re = Tagger::convert $re;
print if /$re/;
# \t,
# \tag \w
# $re <.*?>[AZaz]+<.*?>
, sub Tagger,
, .
, Perl
, .1
,
do, require use
eval.
, 10
.
, ,
, ,
.
, ,
(closures), 8
.
, ,
:
1
, Perl
. ,
, , .
263
sub
sub
sub
sub
NAME
NAME PROTO
NAME
ATTRS
NAME PROTO ATTRS
,
BLOCK:
sub
sub
sub
sub
NAME
BLOCK
NAME PROTO
BLOCK
NAME
ATTRS BLOCK
NAME PROTO ATTRS BLOCK
NAME:
sub
sub
sub
sub
BLOCK
BLOCK
ATTRS BLOCK
PROTO ATTRS BLOCK
PROTO
PROTO ATTRS ;
. PROTO ATTRS
: NAME BLOCK,
.
NAME
. ,
sub
, ,
:
$subref = sub BLOCK;
,
, :
use MODULE qw(NAME1 NAME2 NAME3...);
:
NAME(LIST)
NAME LIST
&NAME
# & , .
# , sub /
.
# @_
# ( ).
( )
:
&$subref(LIST)
$subref>(LIST)
&$subref
# &
# ( ).
# @_.
264
6.
&.
,
, , .
& ,
, defined
undef, ,
$subref = \&name. & ,
&$subref() &{$subref}().
$subref>() .
. 8 .
Perl
. ,
, Perl (BEGIN,
CHECK, INIT, END, AUTOLOAD, DESTROY , 14
), , ,
, . ( ,
, .
. )
, ,
:
sub razzle {
print "Ok, you've been razzled.\n";
}
:
razzle();
()
( ). Perl
:
; ,
. , ,
, ,
, ,
.
,
(
). Perl
(variadic) (,
265
), C, ,
printf(3).
,
, ,
. ,
Perl, @_.
,
: $_[0] $_[1]. @_
, ,
.1 @_ ,
. (
.)
, @_. (
, Perl ,
.)
, ( ),
, .
return,
. ,
,
.
Perl ,
@_ my,
. ( ,
,
, ,
.) :
sub maysetenv {
my ($key, $value) = @_;
$ENV{$key} = $value unless $ENV{$key};
}
,
@_. ,
@_:
sub max {
my $max = shift(@_);
for my $item (@_) {
1
, Perl ,
.
266
6.
$max = $item if $max < $item;
}
return $max;
}
$bestday = max($mon,$tue,$wed,$thu,$fri);
:
sub configuration {
my %options = @_;
print " .\n" if $options{VERBOSE} == 9;
}
configuration(PASSWORD => "xyzzy", VERBOSE => 9, SCORE => 0);
, , , ,
:
upcase_in($v1, $v2); # $v1 $v2
sub upcase_in {
for (@_) { tr/az/AZ/ }
}
, .
, ,
, , $1,
, Perl (
, ; , ). ,
:
upcase_in("");
upcase_in ,
, :
($v3, $v4) = upcase($v1, $v2);
sub upcase {
my @parms = @_;
for (@parms) { tr/az/AZ/ }
# , .
return wantarray ? @parms : $parms[0];
}
, ( ) ,
: . Perl ,
, @_. ,
Perl. upcase
upcase,
:
@newlist = upcase(@list1, @list2);
@newlist = upcase( split /:/, $var );
267
, , :
(@a, @b) = upcase(@list1, @list2);
? , @_,
.
@a @b, .
, . .
,
, ,
Perl return .
undef,
.
.
,
. ,
. .
wantarray undef,
, , :
if ($something_went_awry) {
return if defined wantarray; # , .
die " ?? , !!!\n";
}
,
,
. &,
. &,
, :
@_ . ,
, , .
&foo(1,2,3);
foo(1,2,3);
#
#
foo();
&foo();
#
#
&foo;
foo;
# foo() , foo(@_), !
# , foo(), sub foo,
"foo"
268
6.
& ,
.
, ,
, , . .
.
, ,
, :
Perl .
2 , ,
( ) , .
my
.
, bumpx
$x , ,
my ,
:
#
my $x = 10;
#
sub bumpx { $x++ } #
C C++ , , $x
. ,
, my.
C, Perl ,
,
static. Perl ,
,
.
Perl static, Perl
,
.
.
Perl ,
, static.
,
;
, ,
. ,
,
, my
. ,
, :
{
my $counter = 0;
269
,
. ,
( ) ,
$counter, ,
.
require use,
. , ,
my
.
, BEGIN INIT,
:
BEGIN {
my @scale = ('A' .. 'G');
my $note = 1;
sub next_pitch { return $scale[ ($note += 1) %= @scale ] };
}
BEGIN
,
. ,
.
my our, , 29 . BEGIN INIT
8 .
,
,
.
, 8 .
.
...
. , ,
. ,
, :
$total = sum ( \@a );
sub sum {
my ($aref) = @_;
my ($total) = 0;
foreach (@$aref) { $total += $_ }
270
6.
return $total;
, , :
@tailings = popmany ( \@a, \@b, \@c, \@d );
sub popmany {
my @retlist = ();
for my $aref (@_) {
push @retlist, pop @$aref;
}
return @retlist;
}
, ,
, :
@common = inter( \%foo, \%bar, \%joe );
sub inter {
my %seen;
for my $href (@_) {
while (my $k = each %$href ) {
$seen{$k}++;
}
}
return grep { $seen{$_} == @_ } keys %seen;
}
.
, ?
, ,
, .
, :
(@a, @b) = func(@c, @d);
:
(%a, %b) = func(%c, %d);
. @a %a @b
%b. ,
: , ,
@_.
, ,
, . ,
,
:
271
,
, .
8.
Perl ,
Perl. push(@array, $item),
, @array,
, @array, .
,
, . .
.
, , ,
, , C
Java. Perl
scalar ,
.
, :
sub mypush (\@@);
mypush , push.
,
.
, & . ,
, .
, .
& .
, , ,
\&foo &{$subref}
$subref>().
. ,
, Perl
.
272
6.
,
, .
,
:
myreverse $a,$b,$c
myjoin ":",$a,$b,$c
mypop @array
mysplice @array,@array,0,@pushme
mykeys %{$hashref}
myopen HANDLE
myopen HANDLE, $name
myopen HANDLE, "|", @cmd
myrand 42
sub mytime ()
mytime
(
) (
),
. keys %,
mykeys %.
. (
@ % , . . .)
.
@ %
. (
LIST .) , $,
. &
.
* ,
: , ,
, typeglob typeglob.
(
273
) typeglob.
typeglob, Symbol::qualify_to_ref
:
use Symbol 'qualify_to_ref';
sub foo (*) {
my $fh = qualify_to_ref(shift, caller);
...
}
,
. mygrep
, myrand
, rand, mytime
, time.
, , :
mytime +2;
mytime() + 2, mytime(2), ,
.
mygrep , & ,
. &
\&foo sub{}. , , sub
( ). &
,
, & :
sub try (&$) {
my ($try, $catch) = @_;
eval { &$try };
if ($@) {
local $_ = $@;
&$catch;
}
}
sub catch (&) { $_[0] }
try {
die "phooey";
}
# !
catch {
/phooey/ and print "unphooey\n";
};
unphooey. try
: {die "phooey";} ,
catch,
. try
274
6.
, eval,
, . ,
$_,
.1 ,
die eval 29,
8. , , ,
Error CPAN,
try, catch, except, otherwise finally.
grep ( , ,
):
sub mygrep (&@) {
my $coderef = shift;
my @result;
foreach $_ (@_) {
push(@result, $_) if &$coderef;
}
return @result;
}
,
.
. ( .)
,
, ,
, .
, (), , ,
,
time. ,
(inlining). ,
,
,
. &NAME
, ,
. (. 31
use constant, , .)
1
, , @_.
. @_
,
Perl,
.
275
:
sub pi ()
sub PI ()
{ 3.14159 }
{ 4 * atan2(1, 1) }
# ,
# ,
,
Perl :
sub FLAG_FOO ()
sub FLAG_BAR ()
sub FLAG_MASK ()
{ 1 << 8 }
{ 1 << 9 }
{ FLAG_FOO | FLAG_BAR }
sub OPT_GLARCH ()
sub GLARCH_VAL ()
if (OPT_GLARCH)
else
}
sub N () { int(GLARCH_VAL) / 3 }
BEGIN {
#
my $prod = 1;
#
for (1 .. N) { $prod *= $_ }
sub NFACT () { $prod }
}
NFACT ,
,
, ,
.
NFACT , ,
BEGIN.
, ,
. (
,
.) ,
, . .
.
, ,
, () ( ,
)
, :
sub not_inlined () {
return 23 if $$;
}
,
, . 18.
276
6.
, ,
. , ANSI C,
,
. , , ,
, :
sub func ($) {
my $n = shift;
print " $n\n";
}
( rand)
.
.
,
, , ,
:
func @foo;
func split /:/;
func "a", "b", "c";
func("a", "b", "c");
#
#
#
#
@foo
"a", "b" "c"
!
,
. @foo,
, . func 1 ( @foo).
split, ,
@_. , . . func
, a; , func,
, . .
c.
,
.
,
,
, , :
sub func (\$) {
my $nref = shift;
print " $$nref\n";
}
, $:
func @foo;
func split/:/;
func $s;
# , @, $
# , , $
# ok $
277
func
func
func
func
$a[3];
$h{stuff}[1];
2+5;
${ \(2+5) };
#
#
#
#
,
ok,
,
. ,
. , , , ,
.
. ,
, ,
use attributes.
use attributes 31.
: locked, method lvalue.
locked method
# .
sub afunc : locked { ... }
# .
sub afunc : locked method { ... }
locked ,
.
, , Perl
.
, (. .
method), Perl
(), .
, ,
lock .
17 .
method :
sub afunc : method { ... }
,
Ambiguous
call resolved as CORE::%s ( CORE::%s). (
, .)
,
.
278
6.
( , _).
,
.
( ):
sub fnord (&\%) : switch(10,foo(7,3)) : expensive;
sub plugh () : Ugly('\(") :Bad;
sub xyzzy : _5x5 { ... }
:
sub
sub
sub
sub
sub
fnord
snoid
xyzzy
plugh
snurt
:
:
:
:
:
switch(10,foo();
Ugly('(');
5x5;
Y2::north;
foo + bar;
#
#
#
#
#
"5x5"
"Y2::north"
"+"
,
. ,
( ), .
attri*
butes(3).
lvalue
,
, lvalue:
my $val;
sub canmod : lvalue {
$val;
}
sub nomod {
$val;
}
canmod() = 5;
nomod() = 5;
# $val.
# ERROR
, l,
:
canmod $x
canmod 42
canmod($x)
canmod(42)
=
=
=
=
5;
5;
5;
5;
#
#
#
#
$x 5!
ok
,
. ($),
279
.
,
, . (
.)
, (. .
()), , , :
canmod = 5;
, =.
, l,
:
$obj>canmod = 5;
, Perl.
, (wrap)
( , , ,
).
, l
, ,
. :
data(2,3) = get_data(3,4);
,
:
(data(2,3)) = get_data(3,4);
:
(data(2),data(3)) = get_data(3,4);
.
lvalue ,
.
Perl ,
. Perl
, .
, ,
, . .
FORTRAN: format write
; . 29 . ,
PRINT USING
BASIC. , nroff(1) . (
, nroff.)
, , , ,
. (
.)
, Perl. ,
Foo, , Foo.
,
, .
STDOUT "STDOUT",
TEMP "TEMP". ,
.
:
format NAME =
FORMLIST
.
281
NAME , STDOUT. FORMLIST
,
:
, # .
(picture line),
.
, ,
.
, ,
, .1
@ (at) ^ ().
. @ (
@) ;
, ^,
. <, > |
, , ,
.
, .
# ( @ ^)
. # . ()
. , ,
,
. , , @*
;
.
,
. , , .
,
. ,
. (
.)
.
,
(. . , #,
1
,
. , ,
. , ,
WYSIWYG, ,
.
.
282
7.
.), , ,
LC_NUMERIC. , ,
, .
perllocale.
\n, \t \f
. ,
:
$value =~ tr/\n\t\f/ /;
, \r, ,
.
, ^, @,
. # , .
^ .
,
, . Perl ,
, ,
. (,
, write
.
.) ,
. ...,
,
, . ,
( ) , ,
$: ( English, $FORMAT_LINE_BREAK_CHARACTERS)
.
^ .
,
^ .
.
~ (). (
.) ,
, . (
, ^ .
@ , ,
! shift
, .)
,
_TOP. .
. write 29.
283
:
# /etc/passwd
format STDOUT_TOP =
Passwd
Uid Gid
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
$name,
$login, $office,$uid,$gid, $home
.
#
format STDOUT_TOP =
@<<<<<<<<<<<<<<<<<<<<<<<
@|||
@>>>>>>>>>>>>>>>>>>>>>>>
$system,
$%,
$date
.
format STDOUT =
: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$subject
: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<
$index,
$description
: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<
$priority,
$date, $description
: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$from,
$description
: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<
$programmer,
$description
~
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~
^<<<<<<<<<<<<<<<<<<<<<<<...
$description
.
,
.
print write ,
$
($FORMAT_LINES_LEFT, English).
284
7.
$~ ($FORMAT_NAME),
$^ ($FORMAT_TOP_NAME).
$% ($FORMAT_PAGE_NUMBER),
$= ($FORMAT_LINES_PER_PAGE).
$| ($OUT
PUT_AUTOFLUSH). ,
( ), $^L ($FORMAT_FORMFEED).
,
select,
:
select((select(OUTF),
$~ = "My_Other_Format",
$^ = "My_Top_Format"
)[0]);
, ? ,
, .
:
$ofh = select(OUTF);
$~ = "My_Other_Format";
$^ = "My_Top_Format";
select($ofh);
,
, ,
.
English :
use English;
$ofh = select(OUTF);
$FORMAT_NAME
= "My_Other_Format";
$FORMAT_TOP_NAME = "My_Top_Format";
select($ofh);
select .
, FileHandle, Perl.
,
:
use FileHandle;
OUTF>format_name("My_Other_Format");
OUTF>format_top_name("My_Top_Format");
285
, ,
( @, ^),
, , sprintf
. , :
format Ident =
@<<<<<<<<<<<<<<<
commify($n)
.
@, ~ ^, :
format Ident =
@ .
"@"
.
:
format Ident =
@||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
" "
.
>
, , .
: ,
. ,
. , ,
, ,
eval:
$format = "format STDOUT = \n"
. '^' . '<' x $cols . "\n"
. '$entry' . "\n"
. "\t^" . "<" x ($cols8) . "~~\n"
. '$entry' . "\n"
. ".\n";
print $format if $Debugging;
eval $format;
die $@ if $@;
, , print.
print :
format STDOUT =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$entry
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$entry
.
286
7.
, Unix fmt(1):
format =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
$_
.
$/ = "";
while (<>) {
s/\s*\n\s*/ /g;
write;
}
$^ ($FORMAT_TOP_NAME),
. ,
, . TODO.1
:
, ,
$ ($FORMAT_LINES_LEFT) write,
.
: open(MESELF, "|") (.
open 29) write MESELF STDOUT.
STDIN
, . , .
formline $^A ( $ACCUMULATOR)
. (, ,
formline.) :
$str = formline <<'END', 1,2,3;
@<<< @||| @>>>
END
print ", `$^A' !\n";
, , , .
, , WWW, Unicode, XML, XSLT ,
.
287
use Carp;
sub swrite {
croak ": swrite PICTURE ARGS" unless @_;
my $format = shift;
$^A = "";
formline($format, @_);
return $^A;
}
$string = swrite(<<'END', 1, 2, 3);
Check me out
@<<< @||| @>>>
END
print $string;
FileHandle, formline
72 :
use FileHandle;
STDOUT>formline("^" . ("<" x 72) . "~~\n", $long_text);
8
C
, Perl
.
, .
, ( )
, .
, :
@john = (47, "", 186);
@mary = (23, "", 128);
@bill = (35, "", 157);
,
:
@vitals = ('john', 'mary', 'bill');
,
,
@john, "john".
(indirection),
. C
,
. Perl
(reference).
289
?
$vitals[0] "john". ,
, (
) . , ,
(symbolic), Perl
@john . (
.)
.
(hard) ,
Perl
. ,
. ,
, . ,
.
, , ,
. . 8.1 $bar,
$foo, bot.
. 8.1.
,
( ),
, .
, ,
(referent). , ,
,
@array. , ,
, @array .
,
.
,
. , Perl
,
( ) .
, ,
290
8. C
, .
.
,
( ).1
,
. ,
.
.
. ,
, , , , ,
, .
;
.
, (to reference) ,
. (
.)
,
. (dereference)
. ,
;
Perl . ,
.
, .
, ,
, . .
6. , ,
,
,
. , ,
, bless lock,
,
, .
, , Perl
.
.
, ,
, , ,
. . 9 .
, , ,
Perl . ,
(, ).
1
Devel::Peek, Perl.
291
,
, , ,
.
Perl,
, ,
, ,
.
,
, , ()
.
. (
, , 7 "camel",
.) & () C,
, .
:
$scalarref
$constref
$arrayref
$hashref
$coderef
$globref
=
=
=
=
=
=
\$foo;
\186_282.42;
\@ARGV;
\%ENV;
\&handler;
\*STDOUT;
,
. ,
. .
.
, ,
. 186_282.42 ,
. ,
.
. ,
, .
292
8. C
:
$arrayref = [1, 2, ['a', 'b', 'c', 'd']];
,
( . 8.2). (
, . ,
$arrayref>[2][1] 'b'.)
. 8.2. ,
, :
$table = [ [ "john", 47, "", 186],
[ "mary", 23, "", 128],
[ "bill", 35, "", 157] ];
,
Perl .
$array[6],
. ,
, ,
. (
,
"VAL=$array[6]\n". , ,
,
, ,
. ,
, .)
:
$hashref = {
'' => '',
293
'' => $bonnie,
'' => '' . '',
};
( )
, ,
.
,
:
$table = {
"john" => [ 47, "", 186 ],
"mary" => [ 23, "", 128 ],
"bill" => [ 35, "", 157 ],
};
.
, 9.
:
$table = {
"john" => { age
eyes
weight
},
"mary" => { age
eyes
weight
},
"bill" => { age
eyes
weight
},
=> 47,
=> "",
=> 186,
=> 23,
=> "",
=> 128,
=> 35,
=> "",
=> 157,
};
,
, Perl .
$hash{key}
.
.
,
.
( ),
, +
return, Perl , .
, ,
:
294
8. C
sub hashem {
{ @_ } }
sub hashem {
+{ @_ } }
sub hashem { return { @_ } }
# , @_.
# Ok.
# Ok.
sub :
$coderef = sub { print "Boink!\n" }; # &$coderef "Boink!"
,
. (
sub NAME {}
.) sub {} ,
{} eval {} ,
.
, $coderef.
, $coderef
.1
. ,
,
. , ,
, .
, ,
, . ,
bless, , blessed reference (
, , ).
. , ,
bless ,
, .
new C++, Perl
.
:
$objref
$objref
$objref
$objref
1
=
=
=
=
#1
#2
#3
#4
,
,
.
.
295
.
new, Doggie.
, , :
, Doggie. (
.)
, new
require use Doggie,
. ,
4. ( Doggie.)
Perl . 12 .
typeglob :
splutter(\*STDOUT);
sub splutter {
my $fh = shift;
print $fh "her um well a hmmm\n";
}
$rec = get_rec(\*STDIN);
sub get_rec {
my $fh = shift;
return scalar <$fh>;
}
,
() typeglob: \*STDOUT \*STDIN
*STDOUT *STDIN.
typeglob typeglob
, . typeglob
bless, typeglob
typeglob.
:
for $file (@names) {
local *FH;
open(*FH, $file) || next;
$handle{$file} = *FH;
}
,
typeglob:
296
8. C
, typeglob,
typeglob /
. ,
.
, typeglob typeglob.
,
.
, .
,
*foo{THING}. *foo{THING} THING
*foo,1 ,
$foo, @foo, %foo .
$scalarref
$arrayref
$hashref
$coderef
$globref
$ioref
=
=
=
=
=
=
*foo{SCALAR};
*ARGV{ARRAY};
*ENV{HASH};
*handler{CODE};
*foo{GLOB};
*STDIN{IO};
#
#
#
#
#
#
,
,
,
,
,
...
\$foo
\@ARGV
\%ENV
\&handler
\*foo
, *STDIN{IO}.
IO::Handle, ty
peglob, typeglob,
/.
Perl *foo{FILEHANDLE}
*foo{IO}.
, *HANDLE{IO} , *HANDLE
\*HANDLE, ,
. (
, .)
/, typeglob,
, typeglob (
, typeglob,
1
foo
(http://www.jargon.org/). . .
297
).
.1
splutter(*STDOUT);
splutter(*STDOUT{IO});
sub splutter {
my $fh = shift;
print $fh "her um well a hmmm\n";
}
, ,
.
l, .
, .
, ,
.
,
, (dereference).
: Perl
.2 ,
. ,
, :
.
, , $foo,
foo. , foo,
1
298
8. C
$ ,
. , , ,
, ,
$foo. ,
,
, , foo
$foo. ,
$$foo , $foo, @$bar
, $bar, %$glarch
, $glarch, . . :
:
$foo
= "";
$scalarref = \$foo;
# $scalarref $foo
$camel_model = $$scalarref; # $camel_model ""
:
$bar = $$scalarref;
push(@$arrayref, $filename);
$$arrayref[0] = "January";
# @$arrayref
@$arrayref[4..6] = qw/May June July/; # @$arrayref
%$hashref = (KEY => "", BIRD => ""); #
$$hashref{KEY} = "";
# /
@$hashref{"1","2"} = ("1","2"); #
&$coderef(1,2,3);
print $handleref "\n";
( ). ,
( ) .
, , :
$$arrayref[0] ${$arrayref}[0]
, $arrayref. ,
${$arrayref[0]}, @ar
rayref, , . , $$hashref{KEY}
, ${$hashref}{KEY}, ${$hash
ref{KEY}}, %hashref,
, . , .
,
.
"howdy":
$refrefref = \\\"howdy";
print $$$$refrefref;
299
, .
.
, , ,
.
,
. ,
,
, . ,
:
$bar = ${$scalarref};
push(@{$arrayref}, $filename);
${$arrayref}[0] = "January";
@{$arrayref}[4..6] = qw/May June July/;
${$hashref}{"KEY"} = "";
@{$hashref}{"1","2"} = ("1","2");
&{$coderef}(1,2,3);
:
$refrefref = \\\"howdy";
print ${${${$refrefref}}};
, ,
,
. , $dispatch{$in
dex} (
coderef ).
.
&{ $dispatch{$index} }(1, 2, 3);
.
Perl $dispatch, $dispatch{$index}.
,
>.
, .
, . . ,
.
,
, ,
300
8. C
. ,
,
, .
,
, . (
.)
$ $arrayref [2] = "Dorian";
${ $arrayref }[2] = "Dorian";
$arrayref>[2] = "Dorian";
#1
#2
#3
#1
#2
#3
#1
#2
#3
,
. Perl ,
,
. , ,
> ,
, :
print $array[3]>{"English"}>[0];
, @array
, "English"
.
, $array[3] $array>[3] .
@array,
(, ),
$array.
, $array[3] .
:
$array[3]>{"English"}>[0] = "January";
, (
) l (. .
). $array[3] ,
,
$array[3]>{"English"}. $array[3]>{"English"}
,
. , r
: print $array[3]>{"English"}>[0]
301
, ,
C:
$answer[$x][$y][$z] += 42;
, , , C. , C
, Perl . ,
, ,
. Perl :
$listref>[2][2] = "hello";
$$listref[2][2] = "hello";
#
#
C,
*a[i] , i
a. Perl ($ @ * % &)
, .1 $$listref,
$listref[2] . C,
${$listref[2]} $listref[2]
$ >:
$listref[2]>[$greeting] = "hello";
, ,
,
, ,
( ). ,
, ,
, Perl .
1
. Perl
. Perl
, ,
.
302
8. C
Perl . .
.
.
, . , .
,
.
( ), (
). :
$john = [ {age => 1, eyes => 2, weight => 3}, 47, "", 186 ];
# $john
# $john
;
: .
, , ,
, :
$john>[0]{height} = 4;
$john>{height} = "tall";
# height 4
# $john>[4] = "tall"
Perl ,
, . Perl
,
:
delete $john>[0]{height}; #
$john>{height};
#
$john>[4];
# "tall"
, ,
. ,
,
.
.
,
fields::phash, use fields:
303
use fields;
$ph = fields::phash(age => 47, eyes => "", weight => 186);
print $ph>{age};
.
exists, ,
.
. :
use fields;
$ph= fields::phash([qw(age eyes brown)], [47]);
$ph>{eyes} = undef;
print exists $ph>{age};
# True, 'age' .
print exists $ph>{weight}; # False, 'weight' .
print exists $ph>{eyes};
# True, 'eyes'.
exists ,
. ,
:
print exists $ph>[0]{age}; # True, 'age'
print exists $ph>[0]{name}; # False, 'name'
, , delete
,
, . ,
.
:
print
print
print
print
print
delete $ph>{age};
exists $ph>{age};
exists $ph>[0]{age};
delete $ph>[0]{age};
$ph>{age};
#
#
#
#
#
$ph>[1], 47
false
True, 'age'
'age'
, , ,
.
,
, ;
.
Perl.
,
( ) ,
.
, ,
(
),
. . use fields 12
31 .
304
8. C
,
. ,
fields phash new.
,
, .
.
, ,
.
@reflist = (\$s, \@a, \%h, \&f);
@reflist = \($s, @a %h, &f);
#
#
,
:
@reflist = \(@x);
@reflist = map { \$_ } @x;
# ,
#
:
@reflist = \(@x, (@y));
@reflist = (\@x, map { \$_ } @y);
,
( ) (
).
,
. :
@envrefs = \@ENV{'HOME', 'TERM'};
#
@envrefs = \( $ENV{HOME}, $ENV{TERM} ); #
@envrefs = ( \$ENV{HOME}, \$ENV{TERM} ); #
,
. ,
, ,
, :
@reflist = \fx();
@reflist = map { \$_ } fx();
#
#
305
,
.
, ,
:
@reflist = \localtime();
$lastref = \localtime();
# 9
#
Perl, print, reverse sort,
,
. ,
scalar, :
$dateref = \scalar localtime();
, ,
ref. ref typeof, ,
, .
.
SCALAR, ARRAY, HASH, CODE, GLOB, REF, LVALUE, IO, IO::Handle Re
gexp. :
sub sum {
my $arrayref = shift;
warn " " if ref($arrayref) ne "ARRAY";
return eval join("+", @$arrayref);
}
, ,
, : SCALAR(0x1fc0e). (
,
, ,
,
.)
bless
, .
ref , .
,
, : MyType=HASH(0x20d10)
IO::Handle=IO(0x186904). . 12
.
, , ,
, typeglob ,
, , typeglob
. ${*main::foo} ${\$main::foo}
, .
306
8. C
, ,
:
print " @{[ mysub(1,2,3) ]}.\n";
. , @{...}
, ,
. ,
, , .
mysub(1,2,3) ,
, .
@{...},
,
.
, :
print " @{ [$n + 5] } !\n";
:
. ,
mysub . ,
scalar :
print "mysub @{ [scalar mysub(1,2,3)] }.\n";
sub {}.
, ,
.
, .
:
{
my $critter = "camel";
$critterref = \$critter;
}
307
(closure),
LISP Scheme.1 ,
,
,
. ( ,
.)
,
,
. ,
, , .
eval.
,
, .
(callbacks)
,
, , . .
, ,
,
. ( , ,
, .
,
, .)
, . .
, .
, :
sub make_saying {
my $salute = shift;
my $newfunc = sub {
my $target = shift;
print "$salute, $target!\n";
};
return $newfunc;
#
}
$f = make_saying("Howdy");
#
$g = make_saying("Greetings"); #
# ...
$f>("world");
$g>("earthlings");
1
308
8. C
:
Howdy, world!
Greetings, earthlings!
, $salute
, make_saying, my $salute
.
. $f $g ,
$salute,
. $f, $salute
. (Perl ,
.)
Perl ( 12
),
. , ,
, ,
.
, :
sub get_method_ref {
my ($self, $methodname) = @_;
my $methref = sub {
# @_ , !
return $self>$methodname(@_);
};
return $methref;
}
my $dog = new Doggie::
Name => "Lucky",
Legs => 3,
Tail => "clipped";
#
#
#
#
,
.
(Lucky) (wag) ,
, $dog
Lucky : $wagger
wag, .
, .
, ,
HTML:
print " ", red(" "), " ", green(""), "!!!";
309
red green .
, , . .
. ,
. ,
typeglob , . .
10 .
, :
@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs';
#
*$name = *{uc $name} = sub { "<FONT COLOR='$name'>;@_</FONT>" };
}
. ,
,
, . . .
, BEGIN,
. ( , ,
use .)
.
( )
, ,
, Perl
.
, .1 ,
1
, . ,
.
( my sub),
, .
310
8. C
. :
sub outer {
my $x = $_[0] + 35;
local *inner = sub { return $x * 19 };
return $x + inner();
}
inner outer,
. ,
$x outer.
,
, Perl, , .
local
, ,
outer, inner.
, :
sub outer {
my $x = $_[0] + 35;
my $inner = sub { return $x * 19 };
return $x + $inner>();
}
, ,
?
(symbolic reference). ,
, .
:
$name = "bam";
$$name = 1;
$name>[0] = 4;
$name>{X} = "Y";
@$name = ();
keys %$name;
&$name;
#
#
#
#
#
#
$bam
@bam
X %bam Y
@bam
%bam
&bam
,
( ) ,
. ,
:
use strict 'refs';
.
no strict 'refs';
311
:
${identifier};
# , $identifier.
${"identifier"}; # $identifier, .
,
, use strict 'refs',
. strict 'refs'
, .
, .
( ).
,
. ,
. :
our $value = "global";
{
my $value = "private";
print "Inside, mine is ${value}, ";
print "but ours is ${'value'}.\n";
}
print "Outside, ${value} is again ${'value'}.\n";
:
Inside, mine is private,
Outside, global is again
[ ,
,
, ${identifier}
. ,
, : .
push ,
"pop on over":
$push = "pop on ";
print "${push}over";
,
, Unix
, ,
, .
312
8. C
,
Perl. Perl
,
, , . , :
print ${push} . 'over';
( ):
print ${ push } . 'over';
"pop on over", ,
.
, . :
$hash{ "aaa" }{ "bbb" }{ "ccc" }
:
$hash{ aaa }{ bbb }{ ccc }
:
$hash{aaa}{bbb}{ccc}
,
. :
$hash{ shift }
$hash{"shift"}.
,
:
$hash{ shift() }
$hash{ +shift }
$hash{ shift @_ }
.1
, :
$x{ \$a } = $a;
($key, $value) = each %x;
print $$key;
,
. , $key, ,
1
, , DBM.
, DBM , ( ) .
313
, . .,
, SCALAR(0x1fc0e) ,
. :
$r = \@a;
$x{ $r } = $r;
, , ,
, , .
, (
) ,
, , . .
.
.
.
,
. (magic)1 Tie::RefHash,
Perl, , , , ,
:
use Tie::RefHash;
tie my %h, 'Tie::RefHash';
%h = (
["this", "here"] => "at home",
["that", "there"] => "elsewhere",
);
while ( my($keyref, $value) = each %h ) {
print "@$keyref is $value\n";
}
, (tie)
, , , ,
, . , ! ...
. 14 .
, .
(garbage collec*
tion). Perl
, .
, , ,
mg.c Perl.
314
8. C
, ,
Perl . ,
. :
{
# $a $b
my ($a, $b);
$a = \$b;
$b = \$a;
:
{
# $a
my $a;
$a = \$a;
$a ,
.
( , . ) , ,
, ( )
. (
,
(markandsweep).) ,
DESTROY; .
DESTROY 12 .
. ,
. , ,
.
Perl ,
, ,
. , ,
.
(weak references) ,
,
, . . , .
, ,
.
WeakRef CPAN,
.
.
.
9
Perl ,
. ,
, Perl
. push pop ( shift unshift), ;
push shift ( unshift pop), .
,
. , , Perl
,
, ,
, ,
.
,
. Perl
.
, , ,
, , , . . Mas
tering Algorithms with Perl, O'Reilly 1999 ( Perl
); Perl Cookbook, O'Reilly 1998 (Perl: ,
, . , 2000) CPAN .
,
, .
316
9.
.
, . (
:
, . . .)
, , ,
,
.
:
#
@AoA = (
[
[
[
);
.
"fred", "barney" ],
"george", "jane", "elroy" ],
"homer", "marge", "bart" ],
print $AoA[2][1];
# "marge"
, ,
, .
, :
# .
$ref_to_AoA = [
[ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "elroy", "judy", ],
];
print $ref_to_AoA>[2][3];
# "judy"
,
>. :
$AoA[2][3]
$ref_to_AoA>[2][3]
:
$AoA[2]>[3]
$ref_to_AoA>[2]>[3]
, ,
>, $ref_to_AoA > .
,
, :
317
$AoA[0][2]
, ,
?
. ,
,
, .
:1
while (<>) {
@tmp = split;
push @AoA, [ @tmp ];
}
# .
# @AoA .
, ,
:
while (<>) {
push @AoA, [ split ];
}
, :
while (<>) {
push @$ref_to_AoA, [ split ];
}
.
?
:2
for $x (0 .. 9) {
for $y (0 .. 9) {
$AoA[$x][$y] = func($x, $y);
}
}
# ...
# ...
# ...
for $x ( 0..9 ) {
$ref_to_AoA>[$x][3] = func2($x);
}
# ...
# ...
, , ( )
my. my @tmp = split.
,
: , ,
, , for my $x.
318
9.
, ,
@AoA : Perl
, , ,
. (Perl , ,
$ref_to_AoA.)
:
# .
push @{ $AoA[0] }, "wilma", "betty";
, :
push $AoA[0], "wilma", "betty"; # !
, push
, .
@. , @,
.
. ,
:
print $AoA[3][2];
, :
print @AoA;
, ,
. Perl ,
. ,
@AoA print:
for $row ( @AoA ) {
print "@$row\n";
}
, :
for $i ( 0 .. $#AoA ) {
print " $i: @{$AoA[$i]}\n";
}
( ):
for $i ( 0 .. $#AoA ) {
for $j ( 0 .. $#{$AoA[$i]} ) {
print " $i $j : $AoA[$i][$j]\n";
}
}
319
, .
:
for $i ( 0 .. $#AoA ) {
$row = $AoA[$i];
for $j ( 0 .. $#{$row} ) {
print " $i $j : $row>[$j]\n";
}
}
( ) ,
.
,
.
:
@part = ();
for ($y = 7; $y < 13; $y++) {
push @part, $AoA[4][$y];
}
:
@part = @{ $AoA[4] } [ 7..12 ];
, , $x 4..8 $y
7..12, :
@newAoA = ();
for ($startx = $x = 4; $x <= 8; $x++) {
for ($starty = $y = 7; $y <= 12; $y++) {
$newAoA[$x $startx][$y $starty] = $AoA[$x][$y];
}
}
@newAoA ,
@AoA. ,
@AoA,
@newAoA.
@newAoA ( , ),
@newAoA.
:
for ($x = 4; $x <= 8; $x++) {
push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
}
320
9.
, , , ,
, , , extract_rectangle.
, , ,
PDL (Perl Data Language),
CPAN.
, Perl .
Perl ,
,
.
, , . ,
:
@AoA = ( [2, 3], [4, 5, 7], [0] );
print "@AoA";
:
ARRAY(0x83c38) ARRAY(0x8b194) ARRAY(0x8b1d0)
, 7:
print $AoA[1][2];
, ,
, :
for $i (1..10) {
@array = somefunc($i);
$AoA[$i] = @array;
}
# !
@array
, $AoA[$i].
, .
, , ,
:
for $i (1..10) {
@array = somefunc($i);
$AoA[$i] = \@array;
}
# !
, , ,
@array. ,
, , $AoA
321
10 ,
. @{$AoA[1]} ,
@{$AoA[2]}.
:
for $i (1..10) {
@array = somefunc($i);
$AoA[$i] = [ @array ];
}
# !
@array
, @array.
.
( ):
for $i (1..10) {
@array = somefunc($i);
@{$AoA[$i]} = @array;
}
$AoA[$i] ,
. @ ,
@array ( ) ,
$AoA[$i]. , ,
.
, .
, @AoA , . .
:
$AoA[3] = \@original_array;
, @original_array (. .
$AoA) , @array.
:
@{$AoA[3]} = @array;
, ,
.
@original_array.
, :
for $i (1..10) {
my @array = somefunc($i);
$AoA[$i] = \@array;
}
,
my @array . ,
322
9.
, ,
, . ,
,
. (
,
.) ,
( ),
, ,
:
for $i (1..10) {
$AoA[$i] = [ somefunc($i) ];
}
:
$AoA[$i] = [ @array ]; # ,
$AoA[$i] = \@array;
# , , my
@{ $AoA[$i] } = @array; #
, ,
. , C
Pascal, Perl ,
. . ,
,
,
,
12 .
Perl :
, ,
, /, ,
. Perl
,
. ,
,
.
, .
.
, ,
( ), (
),
.
, .
, .
, ,
.
323
,
, .
, 0
, 1 . ., ,
.
,
, sort
.
:
# , .
%HoA = (
flintstones
=> [ "fred", "barney" ],
jetsons
=> [ "george", "jane", "elroy" ],
simpsons
=> [ "homer", "marge", "bart" ],
);
, :
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laalaa", "po" ];
,
. :
flintstones: fred barney wilma dino
jetsons:
george jane elroy
simpsons:
homer marge bart
:
while ( <> ) {
next unless s/^(.*?):\s*//;
$HoA{$1} = [ split ];
}
while ( $line = <> ) {
($who, $rest) = split /:\s*/, $line, 2;
@fields = split ' ', $rest;
$HoA{$who} = [ @fields ];
}
get_family, ,
%HoA
:
324
9.
:
push @{ $HoA{flintstones} }, "wilma", "pebbles";
:
$HoA{flintstones}[0] = "Fred";
:
$HoA{simpsons}[1] =~ s/(\w)/\u$1/;
, :
for $family ( keys %HoA ) {
print "$family: @{ $HoA{$family} }\n";
}
,
:
for $family ( keys %HoA ) {
print "$family: ";
for $i ( 0 .. $#{ $HoA{$family} } ) {
print " $i = $HoA{$family}[$i]";
}
print "\n";
}
:
for $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) {
print "$family: @{ $HoA{$family} }\n"
}
,
ASCII (, , utf8):
# , .
for $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) {
325
print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";
}
, ,
, /.
, ,
.
:
@AoH = (
{
husband
wife
son
},
{
husband
wife
son
},
husband
wife
son
},
);
=> "barney",
=> "betty",
=> "bamm bamm",
=> "george",
=> "jane",
=> "elroy",
=> "homer",
=> "marge",
=> "bart",
:
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
,
. :
husband=fred friend=barney
:
while ( <> ) {
$rec = {};
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec>{$key} = $value;
}
326
9.
push @AoH, $rec;
}
while ( <> ) {
push @AoH, { split /[\s=]+/ };
}
get_next_pair, /
, @AoH
:
while ( @fields = get_next_pair() ) {
push @AoH, { @fields };
}
while (<>) {
push @AoH, { get_next_pair($_) };
}
:
$AoH[0]{pet} = "dino";
$AoH[2]{pet} = "santa's little helper";
/ :
$AoH[0]{husband} = "fred";
husband
, :
$AoH[1]{husband} =~ s/(\w)/\u$1/;
:
for $href ( @AoH ) {
print "{ ";
for $role ( keys %$href ) {
print "$role=$href>{$role} ";
}
print "}\n";
}
:
for $i ( 0 .. $#AoH ) {
print "$i is { ";
for $role ( keys %{ $AoH[$i] } ) {
print "$role=$AoH[$i]{$role} ";
}
print "}\n";
}
327
Perl.
, , , .
(
). , , /
.
sort.
:
%HoH = (
flintstones => {
husband =>
pal
=>
},
jetsons => {
husband =>
wife
=>
"his boy" =>
},
simpsons => {
husband =>
wife
=>
kid
=>
},
);
"fred",
"barney",
"george",
"jane",
"elroy", # .
"homer",
"marge",
"bart",
%HoH , :
$HoH{ mash }
captain
major
corporal
};
= {
=> "pierce",
=> "burns",
=> "radar",
, .
:
flintstones: husband=fred pal=barney wife=wilma pet=dino
:
while ( <> ) {
next unless s/^(.*?):\s*//;
$who = $1;
for $field ( split ) {
328
9.
($key, $value) = split /=/, $field;
$HoH{$who}{$key} = $value;
}
}
while ( <> ) {
next unless s/^(.*?):\s*//;
$who = $1;
$rec = {};
$HoH{$who} = $rec;
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec>{$key} = $value;
}
}
get_family,
/, %HoH
:
for $group ( "simpsons", "jetsons", "flintstones" ) {
$HoH{$group} = { get_family($group) };
}
for $group ( "simpsons", "jetsons", "flintstones" ) {
@members = get_family($group);
$HoH{$group} = { @members };
}
sub hash_families {
my @ret;
for $group ( @_ ) {
push @ret, $group, { get_family($group) };
}
return @ret;
}
%HoH = hash_families( "simpsons", "jetsons", "flintstones" );
:
%new_folks = (
wife => "wilma",
pet => "dino";
);
for $what (keys %new_folks) {
$HoH{flintstones}{$what} = $new_folks{$what};
}
/ :
$HoH{flintstones}{wife} = "wilma";
329
/
:
$HoH{jetsons}{'his boy'} =~ s/(\w)/\u$1/;
, :
for $family ( keys %HoH ) {
print "$family: ";
for $role ( keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "\n";
}
each (
):
while ( ($family, $roles) = each %HoH ) {
print "$family: ";
while ( ($role, $person) = each %$roles ) {
print "$role=$person ";
}
print "\n";
}
( , ,
, .)
, :
for $family ( sort keys %HoH ) {
print "$family: ";
for $role ( sort keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "\n";
}
( ASCII ( utf8)),
keys :
for $family ( sort { keys %{$HoH{$a}} <=> keys %{$HoH{$b}} } keys %HoH ) {
print "$family: ";
for $role ( sort keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "\n";
}
:
330
9.
$i = 0;
for ( qw(husband wife son daughter pal pet) ) { $rank{$_} = ++$i }
for $family ( sort { keys %{$HoH{$a}} <=> keys %{$HoH{$b}} } keys %HoH ) {
print "$family: ";
for $role ( sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "\n";
}
Perl ,
.
:
if
($cmd =~ /^exit$/i)
{ exit }
elsif ($cmd =~ /^help$/i)
{ show_help() }
elsif ($cmd =~ /^watch$/i)
{ $watch = 1 }
elsif ($cmd =~ /^mail$/i)
{ mail_msg($msg) }
elsif ($cmd =~ /^edit$/i)
{ $edited++; editmsg($msg); }
elsif ($cmd =~ /^delete$/i) { confirm_kill() }
else {
warn " : `$cmd'; `help'\n";
}
,
:
%HoF = (
exit
help
watch
mail
edit
delete
);
=>
=>
=>
=>
=>
=>
#
sub { exit },
\&show_help,
sub { $watch = 1 },
sub { mail_msg($msg) },
sub { $edited++; editmsg($msg); },
\&confirm_kill,
,
( ) , %HoF. ,
,
.
&{ $HoF{lc $cmd} }() , Perl 5.6,
$HoF{lc $cmd}().
331
, ,
:
, . ,
, .
, . . ,
. , ,
. ,
, .
, , ,
, .
,
,
. , ,
, ,
.
, :
$rec = {
TEXT
SEQUENCE
LOOKUP
THATCODE
THISCODE
HANDLE
};
=>
=>
=>
=>
=>
=>
$string,
[ @old_values ],
{ %some_table },
\&some_function,
sub { $_[0] ** $_[1] },
\*STDOUT,
TEXT , :
print $rec>{TEXT};
SEQUENCE LOOKUP :
print $rec>{SEQUENCE}[0];
$last = pop @{ $rec>{SEQUENCE} };
print $rec>{LOOKUP}{"key"};
($first_k, $first_v) = each %{ $rec>{LOOKUP} };
THATCODE , THISCODE
, :
$that_answer = $rec>{THATCODE}>($arg1, $arg2);
$this_answer = $rec>{THISCODE}>($arg1, $arg2);
332
9.
$rec>{HANDLE} :
print { $rec>{HANDLE} } "\n";
FileHandle
:
use FileHandle;
$rec>{HANDLE}>autoflush(1);
$rec>{HANDLE}>print("\n");
,
,
:
%TV = (
flintstones => {
series => "flintstones",
nights => [ "monday", "thursday", "friday" ],
members => [
{ name => "fred",
role => "husband", age
{ name => "wilma", role => "wife",
age
{ name => "pebbles", role => "kid",
age
],
},
series => "jetsons",
nights => [ "wednesday", "saturday" ],
members => [
{ name => "george", role => "husband", age
{ name => "jane",
role => "wife",
age
{ name => "elroy", role => "kid",
age
],
},
simpsons
=> {
series => "simpsons",
nights => [ "monday" ],
members => [
{ name => "homer", role => "husband", age =>
{ name => "marge", role => "wife",
age =>
{ name => "bart", role => "kid",
age =>
],
},
);
=> 36, },
=> 31, },
=> 4, },
=> 41, },
=> 39, },
=> 9, },
34, },
37, },
11, },
Perl ,
Perl,
333
do require.
CPAN (
, XML::Parser) ,
(, XML).
:
$rec = {};
$rec>{series} = "flintstones";
$rec>{nights} = [ find_days() ];
(
=):
@members = ();
while (<>) {
%fields = split /[\s=]+/;
push @members, { %fields };
}
$rec>{members} = [ @members ];
,
:
$TV{ $rec>{series} } = $rec;
. ,
"kids", ,
, .
, ,
:
for $family (keys %TV) {
my $rec = $TV{$family}; #
@kids = ();
for $person ( @{$rec>{members}} ) {
if ($person>{role} =~ /kid|son|daughter/) {
push @kids, $person;
}
}
# $rec $TV{$family} !
$rec>{kids} = [ @kids ];
}
$rec>{kids} = [ @kids ] ,
. ,
:
$TV{simpsons}{kids}[0]{age}++;
# 12
334
9.
, $TV{simpsons}{kids}[0] $TV{simpsons}{members}[2]
, :
print $TV{simpsons}{members}[2]{age};
# 12
%TV:
for $family ( keys %TV ) {
print "the $family";
print " is on ", join (" and ", @{ $TV{$family}{nights} }), "\n";
print "its members are:\n";
for $who ( @{ $TV{$family}{members} } ) {
print " $who>{name} ($who>{role}), age $who>{age}\n";
}
print "children: ";
print join (", ", map { $_>{name} } @{ $TV{$family}{kids} } );
print "\n\n";
}
,
. Perl Data::Dumper,
(, )
, ,
eval do.
use Data::Dumper;
$Data::Dumper::Purity = 1;
# %TV
open (FILE, "> tvinfo.perldata") or die " tvinfo: $!";
print FILE Data::Dumper>Dump([\%TV], ['*TV']);
close FILE
or die " tvinfo: $!";
( ) :
open (FILE, "< tvinfo.perldata") or die " tvinfo: $!";
undef $/;
#
eval <FILE>;
# %TV
die " tv tvinfo.perldata: $@" if $@;
close FILE
or die " tvinfo: $!";
print $TV{simpsons}{members}[2]{age};
:
do "tvinfo.perldata"
or die " tvinfo: $! $@";
print $TV{simpsons}{members}[2]{age};
,
( ) XML ( ).
CPAN!
10
,
.
,
,
.
(cutandpaste),
,
.1 ,
, ,
.2
, .
, , ,
,
.3
,
,
. ,
? (
1
2
3
.
.
: .
, , .
336
10.
, ,
.)
: . ,
,
, .
,
, , ,
. Perl
(package). ,
, .
, .
. ,
, ,
( ),
( ).
,
. Perl ,
, ,
.pm, perl mo
dule. Perl
.
use,
.
use .
Perl (Comprehensive Perl Archive Network, CPAN) ,
, ,
. Perl
. , CPAN
,
. . CPAN 22 www.cpan.org.
25
, . ,
,
. , ,
, . Perl, ,
, , ,
, .1
,
, . .
Perl
1
Perl , . .
23 .
337
:
, , ,
. .
,
. Perl , ,
, .
, ,
. .
.
,
.
:
. , ,
, .
8 ,
Perl , ,
. , ,
, , .
, . ,
.
,
.
, ,
. Perl , ,
.
main,
package. ,
, ,
/ .
, my,
$_ %SIG.
, , Perl .
( , _ SIG,
, main, .)
(, eval
) ,
. ( .)
( our,
my )
, . (
, my, ;
338
10.
,
.)
,
require use. , ,
. ,
. ,
.
; ,
. (
, .)
1 ,
: $::
( ). ,
main. , $::sail $main::sail.2
,
Perl $main'sail $some
pack'horse.
, , ,
emacs.
C++
,
Ada.
, , "This is $ow
ner's house", $owner::s; . . $s
owner, , , .
, , "This is ${owner}'s house".
: $Red::Blue::var. $var,
Red::Blue. Red::Blue
Red Blue. ,
Red::Blue Red Blue ,
, Perl. (
, Red::Blue
Red. Perl .)
1
,
, ,
, , .
, ,
,
. .
, $main::sail
main sail, main::sail.
,
.
339
package
.
, ( )
.
(,
) .
main, , $!, $?
$_. , STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC
SIG, ,
main, ,
. m, s, y, tr, q, qq, qr, qw qx,
. ,
, ,
.
, ,
main, ,
, . (
, ,
,
, .)
%SIG ( ; . 16
) .
,
main, .
,
, ,
typeglob :
$SIG{QUIT}
$SIG{QUIT}
$SIG{QUIT}
$SIG{QUIT}
$SIG{QUIT}
=
=
=
=
=
"Pkg::quit_catcher"; #
"quit_catcher";
# "main::quit_catcher"
*quit_catcher;
# sub
\&quit_catcher;
# sub
sub { print " SIGQUIT\n" }; # sub
,
.
,
,
eval. , eval
, Perl , eval,
. (,
eval , eval
, , do, require use.)
340
10.
, eval , ,
__PACKAGE__,
. ,
. , ,
our,
.
(symbol tab*
le). , ,
. , main
%main::. main , Perl
%:: %main::.
, Red::Blue %Red::Blue::.
main ,
, %Red::Blue:: %ma
in::Red::Blue::.
, ,
, .
main , ,
%main:: , %main::main::, %main::main::main::,
. . .
, .
/
.
,
typeglob. *NAME typeglob ,
, ,
.
() :
*sym = *main::variable;
*sym = $main::{"variable"};
, main
. typeg
lob , , .
,
. typeglob,
. , :
foreach $symname (sort keys %main::) {
local *sym = $main::{$symname};
print "\$$symname \n" if defined $sym;
341
print "\@$symname \n" if
print "\%$symname \n" if
@sym;
%sym;
( ) main,
Perl, .
Perl,
V. , ,
my, ,
, our. . 20 .
, ,
main. :
, Perl
, :
$!@#$%
${'!@#$%'}
= 0;
= 1;
${'main::!@#$%'} = 2;
print ${ $main::{'!@#$%'} }
# , .
# Ok, .
# .
# Ok, 2!
typeglob , . .
*dick = *richard;
, , , ,
richard,
dick.
, :
*dick = \$richard;
# \%newhash typeglob
# 70; !
342
10.
my %newhash = (km => 10, kg => 70);
return \%newhash;
:
%units = (miles => 6, stones
fillerup( \%units );
print $units{quarts};
sub fillerup {
local *hashsym = shift;
$hashsym{quarts} = 4;
}
=> 11);
#
# 4
# \%units typeglob
# %units; !
,
. ,
; ,
%units my.
:
*PI = \3.14159265358979;
$PI , , , . ,
, .
,
, ;
6 .
use constant (. 31 ):
use constant PI => 3.14159;
*PI,
, . ( )
:
*PI = sub () { 3.14159 };
, : sub {}
typeglob
.
typeglob typeglob (*sym = \*oldvar)
, typeglob, Perl
typeglob. typeglob
, typeglob , Perl
.
,
, :
343
*sym
*sym
*sym
*sym
= *oldvar;
= \*oldvar;
= *{"oldvar"};
= "oldvar";
#
#
#
,
typeglob:
*sym
*sym
*sym
*sym
=
=
=
=
\$frodo;
\@sam;
\%merry;
\&pippin;
, typeglob
, .
, typeglob
, , . . :
*pkg::sym{SCALAR}
*pkg::sym{ARRAY}
*pkg::sym{HASH}
*pkg::sym{CODE}
*pkg::sym{GLOB}
*pkg::sym{IO}
*pkg::sym{NAME}
*pkg::sym{PACKAGE}
#
#
#
#
#
#
, \$pkg::sym
, \@pkg::sym
, \%pkg::sym
, \&pkg::sym
, \*pkg::sym
/,
# "sym" ( )
# "pkg" ( )
*foo{PACKAGE} *foo{NAME} ,
*foo.
, typeglob :
sub identify_typeglob {
my $glob = shift;
print ' ', *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, "\n";
}
identify_typeglob(*foo);
identify_typeglob(*bar::glarch);
:
main::foo
bar::glarch
*foo{THING}
*foo. .
8 .
, ,
,
. ( *foo{FILEHANDLE}
*foo{IO}, ,
.) ,
344
10.
, . ,
,
Perl.
, .
AUTOLOAD ( ,
), AUTOLOAD
, .
AUTOLOAD ,
,
, , .
$AUTOLOAD ,
AUTOLOAD. ,
,
:
sub AUTOLOAD {
our $AUTOLOAD;
warn " $AUTOLOAD .\n";
}
blarg(10);
# $AUTOLOAD main::blarg
print " !\n";
:
sub AUTOLOAD {
our $AUTOLOAD;
return " $AUTOLOAD(@_)\n";
}
print blarg(20);
# : main::blarg(20)
AUTOLOAD
eval require,
glob,
goto,
AUTOLOAD.
, glob:
sub AUTOLOAD {
my $name = our $AUTOLOAD;
*$AUTOLOAD = sub { print " $name(@_)\n" };
goto &$AUTOLOAD;
# .
}
blarg(30);
# : main::blarg(30)
345
glarb(40);
blarg(50);
# : main::glarb(40)
# : main::blarg(50)
AutoSplit
(
.al ),
. auto/
Perl,
AutoLoader.
SelfLoader,
, DATA
,
. Perl AutoLoader SelfLoader
C Dyna
Loader, ,
, ,
C C++. (
Perl AutoSplit, AutoLoader,
SelfLoader DynaLoader. , , ,
.)
, AUTOLOAD,
. , , ,
system
. :
sub AUTOLOAD {
my $program = our $AUTOLOAD;
$program =~ s/.*:://; #
system($program, @_);
}
(, Shell,
Perl.) ( Unix) :
date();
who('am', 'i');
ls('l');
echo("Abadugabudabuda...");
, ,
, ,
, :
sub date (;$$);
#
sub who (;$$$$);
#
sub ls;
#
sub echo ($@);
#
date;
who "am", "i";
ls "l";
echo "That's all, folks!";
.
.
.
.
11
Perl
. ,
( .pm ). ,
.
Perl ,
lib Perl. 32
31 .
,
(, !) , .
perldoc, man .
Perl Comprehensive Perl Archive Network (CPAN)
, Perl,
22 CPAN. . http://www.cpan.org.
: .
,
.
,
, 12
. .
347
Perl :
use MODULE LIST;
:
use MODULE;
MODULE ,
. ( ;
use 29 .)
use MODULE
,
. LIST
, ,
@EXPORT , Exporter,
Exporter . (
LIST , @EXPORT
@EXPORT_OK, .)
Exporter,
:
use Fred;
flintstone();
Perl .pm. ( )
use require, "MODULE.pm".
.pl .ph, Perl. MODULE
,
.
,
, Red::Blue::Green, Perl
Red/Blue/Green.pm.
Perl , @INC.
use ,
@INC .
lib, 32
BEGIN. %INC
/. ( Red/
Blue/Green.pm), ,
C:/perl/site/lib/Red/Blue/Green.pm ,
Windows.
,
.
( ),
348
11.
( )
.
use ,
require. ,
,
:
require MODULE;
, , use, require,
,
, .
:
require MODULE;
require "MODULE.pm";
. require
, use.
,
.
, require ,
, MODULE (,
$ob = purge MODULE), , . (
,
purge.)
use no
BEGIN, (
),
.
,
,
. , use re
quire. require
,
. ( , .)
Perl .pm, , ,
, ,
C C++
Perl.
. (
) .pm.
POSIX ,
, :
use POSIX;
349
,
:
. ;
.
( ,
. . , Perl
, .)
Bestiary, Bestiary.pm,
:
package
require
our
our
our
our
Bestiary;
Exporter;
@ISA
@EXPORT
@EXPORT_OK
$VERSION
=
=
=
=
qw(Exporter);
qw(camel);
# ,
qw($weight); # ,
1.00;
#
###
sub camel { print " " }
$weight = 1024;
1;
use Bestiary,
camel ( $weight), use Bestiary qw(camel
$weight), , .
, ,
C. . 21 .
Exporter
Perl
, C++, Java Ada, Perl .
Perl , ,
, , .
,
, . ,
,
.
( ) .
, , RedefineTheWorld,
, ,
.
350
11.
Exporter. ,
.
use,
, , ,
. (,
, )
(polluting) .
Exporter; :
require Exporter;
our @ISA = ("Exporter");
Exporter.
, ,
Bestiary
:
our @EXPORT
= qw($camel %wolf ram);
#
our @EXPORT_OK = qw(leopard @llama $emu);
#
our %EXPORT_TAGS = (
#
camelids => [qw($camel @llama)],
critters => [qw(ram $camel %wolf)],
);
@EXPORT
, : ,
, use Bestiary. @EXPORT_OK
,
use. , / %EXPORT_TAGS
, @EXPORT @EX
PORT_OK.
, use ,
, , %EXPORT_TAGS,
, ,
@EXPORT.
Bestiary
:
use
use
use
use
use
use
use
use
use
Bestiary;
# @EXPORT
Bestiary ();
#
Bestiary qw(ram @llama);
# ram @llama
Bestiary qw(:camelids);
# $camel @llama
Bestiary qw(:DEFAULT);
# @EXPORT
Bestiary qw(/am/);
# $camel, @llama ram
Bestiary qw(/^\$/);
#
Bestiary qw(:critters !ram); # critters, ram
Bestiary qw(:critters !:camelids);
# critters, camelids
351
(
)
, .
, ,
, %Bestiary::gecko. (
, : .
12.)
,
, BEGIN { $Exporter::Verbose=1 }.
Exporter Perl, , ,
, typeglob
. Exporter
import, ,
, .
use Bestiary LIST
BEGIN {
require Bestiary;
import Bestiary LIST;
}
, Exporter.
, , . . use
import ,
, .
import Exporter
Exporter export_to_level,
, ( )
import Exporter. export_to_level :
MODULE>export_to_level($where_to_export, @what_to_export);
$where_to_export , ,
,
@what_to_export ,
( @_ ).
, , Bestiary import:
package Bestiary;
@ISA = qw(Exporter);
@EXPORT_OK = qw ($zoo);
sub import {
$Bestiary::zoo = "";
}
352
11.
import import
Exporter. import Bestiary,
$Bestiary::zoo, , import Exporter,
:
sub import {
$Bestiary::zoo = "";
Bestiary>export_to_level(1, @_);
}
,
. . , Bestiary.
$VERSION, ,
, ,
. :
use Bestiary 3.14; # Bestiary 3.14
use Bestiary v1.0.4; # Bestiary 1.0.4
Bestiary>require_version,
.
. ,
, .
Exporter , @EXPORT_FAIL.
, Exporter
,
. ,
export_fail, ( ,
Carp):
sub export_fail {
my $class = shift;
carp " , : @_";
return @_;
}
Exporter export_fail, ,
use
, .
export_fail , , ,
.
353
, %EXPORT_TAGS,
@EXPORT @EXPORT_OK, Exporter ,
:
%EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
Exporter::export_tags('foo');
# aa, bb cc @EXPORT
Exporter::export_ok_tags('bar'); # aa, cc dd @EXPORT_OK
, , .
(overridden), (
, )
.
,
, Unix. (
(overloading),
, .
overload 13 .)
.
, typeglob,
, , *open = \&myopen. ,
;
typeglob.
,
, subs
,
:
use subs qw(chdir chroot chmod chown);
chdir $somewhere;
sub chdir { ... }
, ,
open chdir, @EXPORT, . .
.
@EXPORT_OK,
, .
CORE. CORE::chdir ,
Perl, chdir .
354
11.
, .
, .
,
,
.
CORE::GLOBAL. , glob
, . (,
, Perl glob,
,
.
Perl ,
.
glob ,
File::Glob, Perl.) ,
:
*CORE::GLOBAL::glob = sub {
my $pat = shift;
my @got;
local *D;
if (opendir D, '.') {
@got = grep /$pat/, readdir D;
closedir D;
}
return @got;
}
package Whatever;
print <^[az_]+\.pm\$>;
glob
( ) glob
,
. ,
. , .
: ,
.
12
, ; . 10
11 .
; . 8 9 .
,
(),
( ).
(behaviors). ,
,
. , ,
, , .
,
(instance) . :
, . ,
, ,
, , , ,
. : Perl
, .
356
12.
,
, , ,
(
).
( )
,
( , ,
). ,
() , ( ,
).
( ),
( ).
(inherit) (parent
classes), (base classes) *
(superclasses).
(derived class) (subclass). ( ,
. .)
, , ,
,
. , , Perl
. ,
attack .
, Perl, ,
. , ,
attack .
attack,
, . ,
, , ,
.
, at
tack, . ,
, . .
,
. ,
, , ,
: , .
, ,
. :
, ,
.
Perl ,
. ,
Perl
357
. (
, ) (,
). Perl
.
: ,
.
Perl ,
, ,
. Perl
, , Perl ,
, .
, Perl
, C++. , Perl
, ,
( ).
, .
, ,
,
. .
.
Perl
Perl ,
.
.1
,
:
... , .
, ,
.
, , , .
Perl , ,
,
, .2
1
2
, !
.
.
358
12.
.
.
.
.
,
, .
: , .
,
, ,
, , .
,
.
, ,
.
, ,
.
, .
,
, ,
.
Perl .
, Perl,
. , ,
, .
,
. , .
, (invocant)1 .
.
, .
, , .
(agent) (actor).
,
. , ,
1
. in , vocare , . . .
359
, !.
,
, , , ,
.
. ( .)
, ,
,
. ,
, , Perl
. , 13
, 14 .
,
, . . ( )
Perl , .
,
.1 ,
, . (
,
, .)
, ,
: ,
, .
: , ,
, , , ,
, , .
.
Perl,
, ,
. , ,
.
, .
.
, . :
INVOCANT>METHOD (LIST)
INVOCANT>METHOD
1
, typeglob,
. typeglob
AUTOLOAD
. , , typeglob
.
360
12.
. ( > =>, ,
.) , .
,
INVOCANT METHOD, ,
INVOCANT .
INVOCANT , , METHOD
(), INVOCANT ,
, METHOD .
, ,
, .
, , .
, ,
.
, summon
speak, :
$mage = Wizard>summon("Gandalf"); #
$mage>speak("friend");
#
, .
:
$method = "summon";
$mage = Wizard>$method("Gandalf"); # Wizard>summon
$travel = $companion eq "Shadowfax" ? "ide" : "walk";
$mage>$travel("seven leagues");
# $mage>ride $mage>walk
,
use strict 'refs',
.
$travel ,
.
, .
can UNIVERSAL:
. ,
, . 8 .
361
:
METHOD INVOCANT (LIST)
METHOD INVOCANT LIST
METHOD INVOCANT
LIST ; ,
.
:
$mage = summon Wizard "Gandalf";
$nemesis = summon Balrog home => "Moria", weapon => "whip";
move $nemesis "bridge";
speak $mage "You cannot pass";
break $staff;
# : break $staff ();
; ,
print printf:
print STDERR "help!!!\n";
, .
,
.
, LIST
1
, ( Gollum), .
. .
362
12.
, .
:
enchant $sword ($pips + 2) * $cost;
, ,
. :
($sword>enchant($pips + 2)) * $cost;
, , : enchant
$pips + 2, , ,
$cost. ,
&& || and or.
,
name $sword $oldname || "Glamdring";
# "or"!
:
$sword>name($oldname || "Glamdring");
:
speak $mage "friend" && enter();
# "and"!
:
$mage>speak("friend" && enter());
, :
enter() if $mage>speak("friend");
$mage>speak("friend") && enter();
speak $mage "friend" and enter();
, INVOCANT ,
.1 INVOCANT
, ,
LIST. , :
move $party>{LEADER};
move $riders[$i];
# , !
# , !
:
$party>move>{LEADER};
$riders>move([$i]);
1
,
,
, , @ary, @$aryref @{$aryref}.
363
, , , :
$party>{LEADER}>move;
$riders[$i]>move;
,
, , .
,
, , .
. :
Throw your cat out the window a toy mouse to play with.
,
, ( , ).1
Perl,
: Throw your cat the mouse Throw the mouse to your cat.
,
. Perl, ,
.
,
,
,
, . ,
,
:
, (packagequote), ,
:
$obj = method CLASS::;
# "CLASS">method
, :
$obj = new CLASS;
, ,
new CLASS.
, ,
. ,
, CLASS:: Perl
.
, :
1
:
, . ,
. , ,
, . . .
364
12.
# new("ElvenRing")
# new(ElvenRing())
# could be ElvenRing()>new()
# "ElvenRing">new()
# "ElvenRing">new()
:
$obj = new ElvenRing::
name
=>
owner =>
domain =>
stone =>
"Narya",
"Gandalf",
"fire",
"ruby";
: !, ,
, ,
. , ,
. ( ,
, new, , ,
ElvenRing, , .)
, :
use ElvenRing;
require ElvenRing;
Perl , ElvenRing
, new
ElvenRing ,
new .
,
, Perl ,
. ,
ModuleNames, .
, .
,
, Perl ,
. , ,
, , . . ,
bless blessing ().
, ,
, .
bless .
, , .
, .
365
$obj = { };
bless($obj);
bless($obj, "Critter");
# .
# .
# Critter.
,
.
. , bless
, , Perl,
, , typeglob.
bless , .
( .) Perl
.
,
ref ,
, HASH. ,
reftype attributes. . use attributes 31
.
. ,
. , ,
. ,
, bless ,
, .
bless ,
:
package Critter;
sub spawn { bless {}; }
, :
package Critter;
sub spawn {
my
$self = {};
#
bless $self, "Critter"; # Critter
return $self;
# Critter
}
, Critter:
$pet = Critter>spawn;
, ,
. ,
, .
.
, , .
366
12.
, ,
.
,
. , Spider,
Critter ( ). ,
Spider spawn.
:
Critter>spawn()
Critter::spawn("Critter")
Spider>spawn()
Critter::spawn("Spider")
,
. , spawn,
, , ,
Spider Critter.
bless (
):
sub spawn {
my $class = shift;
my $self = { };
bless($self, $class);
return $self;
}
#
#
:
$vermin = Critter>spawn;
$shelob = Spider>spawn;
.
:
$type = "Spider";
$shelob = $type>spawn;
# , "Spider">spawn
, ,
, .
$type , ,
, bless
.
.
,
, :
sub spawn {
my $invocant = shift;
367
my $class
= ref($invocant) || $invocant; #
my $self
= { };
bless($self, $class);
return $self;
}
,
.
, . ,
,
/.
, , ,
*, (pro*
perties, attributes, accessors, member data, instance data, instance variables).
.
Horse () name () co
lor ():
$steed = Horse>new(name => "Shadowfax", color => "white");
, /
,
:
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = { @_ };
#
bless($self, $class);
#
return $self;
}
new, C++
. Perl new, ;
. ,
, de facto. ,
. , Tk ,
. DBI
connect ,
, prepare,
.
, new .
, ,
368
12.
(. .
), .
/ ,
:
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "",
legs => 4,
owner => undef,
@_,
#
};
return bless $self, $class;
}
$ed
= Horse>new;
# 4
$stallion = Horse>new(color => ""); # 4
Horse
.
, ,
,
:
$steed = Horse>new(color => "");
$foal = $steed>clone(owner => "EquuGen Guild, Ltd.");
sub clone {
my $model = shift;
my $self = $model>new(%$model, @_);
return $self;
# >new
}
( new,
.)
, clone
Horse . , , ,
new. $model>new
Horse>new,
. , ,
.
, , : ,
,
. ,
,
. Perl .
, Perl (
369
) ,
,
, .
clone, ,
,
.
Perl,
. ,
Perl , @ISA1
. Perl :
@ISA ,
. , Horse
Critter. ( @ISA our,
, ,
my.)
package Horse;
our @ISA = "Critter";
Horse
, Critter.
, , Critter
, .
, $steed Horse,
move:
$steed>move(10);
, is a.
370
12.
@ISA .
,
.
, .
methname classname Perl
, , :
1. Perl
classname::methname. ,
, 2.
2. Perl , ,
parent, @classname::ISA,
parent::methname. ,
.
, ,
. .
3. , Perl UNIVERSAL::methname.
4. Perl methname AUTOLOAD.
classname::AUTOLOAD.
5. , Perl parent,
@classname::ISA, parent::AUTOLOAD.
, .
6. , Perl UNIVERSAL::AUTOLOAD.
Perl
. ,
( ):
Can't locate object method "methname" via package "classname"
( "methname" "classname")
Perl
DDEBUGGING, Perl Do
.
.
@ISA
@ISA ,
. ,
, Mule (),
:
package Mule;
our @ISA = ("Horse", "Donkey");
371
:
package Mule;
BEGIN {
our @ISA = ("Horse", "Donkey");
require Horse;
require Donkey;
}
372
12.
,
. , Mule
( Horse Donkey)
breed () .
breed, Mule ,
Mule breed. ,
:
$stallion = Horse>new(gender => "male");
$molly = Mule>new(gender => "female");
$colt = $molly>breed($stallion);
,
,
Mule::breed.
, :
$colt = Horse::breed($molly, $stallion);
,
. , Horse::breed
, Horse Donkey
Equine. , ,
Perl, ,
, :
$colt = $molly>Horse::breed($stallion);
,
.
,
.
,
.
,
, .
SUPER.
, ,
.1 @ISA
, :
1
, 11
Perl,
.
CORE, SUPER.
373
package Mule;
our @ISA = qw(Horse Donkey);
sub kick {
my $self = shift;
print " !\n";
$self>SUPER::kick(@_);
}
SUPER
. , , SUPER , ,
.
SUPER , ,
. , @ISA
: , , .
speak , Horse Donkey,
Donkey, :
sub speak {
my $self = shift;
print " !\n";
$self>Donkey::speak(@_);
}
UNIVERSAL::can,
. Class::Mul
timethods CPAN, ,
.
Perl , , . .
package. SUPER @ISA
, SUPER.
, .
,
:
package Bird;
use Dragonfly;
sub Dragonfly::divebomb { shift>SUPER::divebomb(@_) }
, Bird, Dragonfly.
, , ,
SUPER :
package Bird;
use Dragonfly;
{
package Dragonfly;
sub divebomb { shift>SUPER::divebomb(@_) }
}
374
12.
, ,
.
, ,
, ,
. .
, . . ,
. (,
,
?)
UNIVERSAL:
, ,
,
UNIVERSAL.
@ISA, , @ISA
. UNIVERSAL ,
.
, UNIVERSAL,
. ,
.
INVOCANT>isa(CLASS)
isa true, INVOCANT CLASS
, CLASS. CLASS
, , , "HASH" "ARRAY".
(
. ,
.)
use FileHandle;
if (FileHandle>isa("Exporter")) {
print "FileHandle Exporter.\n";
}
$fh = FileHandle>new();
if ($fh>isa("IO::Handle")) {
print "\$fh IO.\n";
} if ($fh>isa("GLOB")) {
print "\$fh GLOB.\n";
}
INVOCANT>can(METHOD)
can ,
METHOD INVOCANT. ,
undef.
375
if ($invocant>can("copy")) {
print " .\n";
}
,
:
$obj>snarl if $obj>can("snarl");
, :
sub snarl {
my $self = shift;
print " (snarling): @_\n";
my %seen;
for my $parent (@ISA) {
if (my $code = $parent>can("snarl")) {
$self>$code(@_) unless $seen{$code}++;
}
}
}
%seen, ,
,
. ,
.
, AUTOLOAD (
), , ,
( ) ,
.
INVOCANT>VERSION(NEED)
VERSION INVO
CANT, $VERSION.
NEED, , ,
NEED, , .
use , .
use Thread 1.0; # Thread>VERSION(1.0)
print " Thread ", Thread>VERSION, ".\n";
VERSION
UNIVERSAL. , ,
.
, ,
, UNIVERSAL.
UNIVERSAL
Perl, ,
, , UNIVERSAL::isa($formobj, "HASH"). ( ,
376
12.
, ,
can.)
UNIVERSAL . (,
: ,
.) copy,
,
. ( ,
, .)
use Data::Dumper;
use Carp;
sub UNIVERSAL::copy {
my $self = shift;
if (ref $self) {
return eval Dumper($self); # CODE
} else {
confess "UNIVERSAL::copy $self";
}
}
Data::Dumper ,
, .
, .
,
AUTOLOAD,
AUTOLOAD (.
10 ). .
( , , , UNIVERSAL)
, ,
AUTOLOAD. ,
, $AUTOLOAD
,
AUTOLOAD.
.
, AUTOLOAD ,
DESTROY,
DESTROY, Perl ,
.
sub AUTOLOAD {
return if our $AUTOLOAD =~ /::DESTROY$/;
...
}
377
, AUTOLOAD,
UNIVERSAL::can , ,
. AUTOLOAD :
if ($obj>can("methname") || $obj>can("AUTOLOAD")) {
$obj>methname();
}
,
, AUTOLOAD,
, . . Perl , AUTOLOAD.
, ,
AUTOLOAD .
:
package Goblin;
sub kick;
sub bite;
sub scratch;
use subs, ,
:
package Goblin;
use subs qw(kick bite scratch);
, ,
, .
UNIVERSAL::can , , 2 ,
3, 4.
, , AUTOLOAD,
, , . 2
, Perl . , ,
, AUTOLOAD,
, ,
( UNIVERSAL). Perl
AUTOLOAD AUTOLOAD,
.
, Perl
.
, ,
. UNIVERSAL>can
, .
,
. ,
378
12.
.
:
#
my $secret_door = sub {
my $self = shift;
...
};
,
. ,
. ,
.
sub knock {
my $self = shift;
if ($self>{knocked}++ > 5) {
$self>$secret_door();
}
}
( )
,
.
Perl,
,
.
, , DESTROY
. ,
,
.
Perl,
.
, ;
, .
package MailNotify;
sub DESTROY {
my $self = shift;
my $fh = $self>{mailhandle};
my $id = $self>{name};
print $fh "\n$id " . localtime() . "\n";
close $fh; #
}
379
Perl ,
, Perl DESTROY ,
. , Perl
.
, DESTROY DESTROY
:
sub DESTROY {
my $self = shift;
# ...
$self>SUPER::DESTROY if $self>can("SUPER::DESTROY");
#
}
; , *
, ,
.
, (
hasa )
, ( isa ).
,
, , .
,
.
DESTROY , .
,
.
DESTROY
,
8 , , (
, ),
( ).
,
WeakRef CPAN.
,
.
DESTROY , ,
, .
13 Perl:
. . (Perl Cookbook)
13.13 .
, ,
Perl.
, .
380
12.
, DESTROY
. ( ,
,
(exit)
. ,
Perl .)
, , ,
(
), .
Perl , ,
, .
,
. , Perl,
.
,
, ,
. , ,
$city elevation, $ci
ty>{elevation}. .
.
, Person. ,
name, , ,
name , .
, .
,
.
, :
sub get_name {
my $self = shift;
return $self>{name};
}
sub set_name {
my $self
= shift;
$self>{name} = shift;
}
:
$him = Person>new();
$him>set_name("Frodo");
$him>set_name( ucfirst($him>get_name) );
381
:
sub name {
my $self = shift;
if (@_) { $self>{name} = shift }
return $self>{name};
}
:
$him = Person>new();
$him>name("Frodo");
$him>name( ucfirst($him>name) );
( Person name , age ,
height . .) , .
,
. ,
, ,
. ,
,
.
, ,
.
, ,
. ( ,
,
, , .)
,
. ,
, .
.
.
,
? ,
.
,
.
, Struct::Class
.
:
sub name {
my $self = shift;
my $field = __PACKAGE__ . "::name";
382
12.
if (@_) { $self>{$field} = shift }
return $self>{$field};
Person
name, race aliases, ,
. , ,
, .
. , .
use fields
.
. ,
:
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
return bless [], $class;
}
:
sub name {
my $self = shift;
if (@_) { $self>[0] = shift }
return $self>[0];
}
sub race {
my $self = shift;
if (@_) { $self>[1] = shift }
return $self>[1];
}
sub aliases {
my $self = shift;
if (@_) { $self>[2] = shift }
return $self>[2];
}
, ,
, .
( ,
), ,
. .
use fields:
package Person;
use fields qw(name race aliases);
383
,
( ),
. (
, .)
, ,
.
use fields, ,
Person; Perl.
( my Person $self
), ,
, .
, ,
( )
. (. 8 ).
:
package Person;
use fields qw(name race aliases);
sub new {
my $type = shift;
my Person $self = fields::new(ref $type || $type);
$self>{name} = "unnamed";
$self>{race} = "unknown";
$self>{aliases} = [];
return $self;
}
sub name {
my Person $self = shift;
$self>{name} = shift if @_;
return $self>{name};
}
sub race {
my Person $self = shift;
$self>{race} = shift if @_;
return $self>{race};
}
sub aliases {
my Person $self = shift;
$self>{aliases} = shift if @_;
return $self>{aliases};
}
1;
,
, .
, $self (
), , ,
Person.
(, $self>{mane}),
384
12.
.
,
. ,
,
.
, ,
, ,
. ,
use base use fields.
,
package Wizard;
use base "Person";
use fields qw(staff color sphere);
Wizard Person
Person.pm. ,
Person. , :
my Wizard $mage = fields::new("Wizard");
:
$mage>name("Gandalf");
$mage>color("Grey");
,
,
, :
$mage>{name} = "Gandalf";
$mage>{color} = "Grey";
, ,
.
use base ,
. . use base use fields 31
.
Class::Struct
Class::Struct struct.
.
new,
( ), .
385
, Person.pm:
package Person;
use Class::Struct;
struct Person => {
name
=> '$',
race
=> '$',
aliases => '@',
};
1;
# "Person"
#
name
#
race
#
aliases
:
use Person;
my $mage = Person>new();
$mage>name("Gandalf");
$mage>race("Istar");
$mage>aliases( ["Mithrandir", "Olorin", "Incanus"] );
Class::Struct.
, ,
, , .
,
Person::name, name.
struct Perl.
,
, struct,
, , ,
. , new
,
.
Class::Struct 32
.
Class::Struct , Us
er::pwent Net::hostent. .
, Perl
AUTOLOAD ,
.
. AUTOLOAD
$AUTOLOAD. :
386
12.
use Person;
$him = Person>new;
$him>name("Aragorn");
$him>race("Man");
$him>aliases( ["Strider", "Estel", "Elessar"] );
printf "%s is of the race of %s.\n", $him>name, $him>race;
print "His aliases: ", join(", ", @{$him>aliases}), ".\n";
, Person
: name, race aliases:
package Person;
use Carp;
my %Fields = (
"Person::name" => "unnamed",
"Person::race" => "unknown",
"Person::aliases" => [],
);
# .
use subs qw(name race aliases);
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = { %Fields, @_ };
# , Class::Struct
bless $self, $class;
return $self;
}
sub AUTOLOAD {
my $self = shift;
# ,
croak "$self " unless ref($invocant);
my $name = our $AUTOLOAD;
return if $name =~ /::DESTROY$/;
unless (exists $self>{$name}) {
croak " `$name' $self";
}
if (@_) { return $self>{$name} = shift }
else
{ return $self>{$name} }
}
387
, , :
.
Perl
.
, ,
, .
: typeglob
.
package Person;
sub new {
my $invocant = shift;
my $self = bless({}, ref $invocant || $invocant);
$self>init();
return $self;
}
sub init {
my $self = shift;
$self>name("unnamed");
$self>race("unknown");
$self>aliases([]);
}
for my $field (qw(name race aliases)) {
my $slot = __PACKAGE__ . "::$field";
no strict "refs";
# typeglob.
my $self = shift;
$self>{$field} = shift if @_;
return $self>{$field};
};
}
. ,
.
( ),
,
: ,
, init.
.
,
.
388
12.
, , ,
. Perl ,
, :
, ,
, , ,
, .
, Perl
. Perl ,
,
, , ,
.
, .
,
, .
,
,
.
. ,
.
,
. . .
: , ,
.
.
, :
package Person;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $data = {
NAME
=> "unnamed",
RACE
=> "unknown",
ALIASES => [],
};
my $self = sub {
my $field = shift;
##############################
### ###
##############################
if (@_) { $data>{$field} = shift }
return
$data>{$field};
389
};
bless($self, $class);
return $self;
}
#
for my $field (qw(name race aliases)) {
no strict "refs"; #
my $self = shift;
return $self>(uc $field, @_);
};
}
*$field = sub {
, new, ,
.
, ,
$data.
$data (, , ) .
$him>name("Bombadil") ,
$self, , bless
. ,
; $self>(uc $field, @_).
: ,
. name,
, .1
, $data
.
.
,
. ,
, for, ,
, .
,
. ,
:
use Carp;
local $Carp::CarpLevel = 1; # croak
my ($cpack, $cfile) = caller();
.
:
croak " '$field' "
unless exists $data>{$field};
1
, ,
, ?
390
12.
:
carp " "
unless $cfile eq __FILE__;
:
carp " ${cpack}::"
unless $cpack eq __PACKAGE__;
, :
carp " ${cpack}::"
unless $cpack>isa(__PACKAGE__);
.
, ,
, . Perl
, . ,
.
. ,
.
, , CPAN Tie::Se
cureHash (Damian Conway).
, ,
.
, .
Class::Contract,
Perl
. ,
,
,1 ,
(designby
contract condition) Perl
, ,
, ,
. !
Perl 5.6, , l
. lvalue (
).
, :
1
, . ,
Object Oriented Perl (Manning Publications, 1999).
391
package Critter;
sub new {
my $class = shift;
my $self = { pups => 0, @_ };
bless $self, $class;
}
sub pups : lvalue {
my $self = shift;
$self>{pups};
}
# .
# pups() .
package main;
$varmint = Critter>new(pups => 4);
$varmint>pups *= 2;
# $varmint>pups!
$varmint>pups =~ s/(.)/$1$1/;
# $varmint>pups !
print $varmint>pups;
# 88 pups.
$varmint>pups ,
. . lvalue 6
.
Perl, , ,
,
locked method:
sub pups : locked method {
...
}
pups, Perl
, . .
locked method 6 .
.
, ,
.
, , ,
() . (
C++ , .)
,
:
.
, .
,
.
392
12.
, ,
.
,
.
,
.
.
,
. Perl
, . Perl
, ,
. ,
, .
, ,
CPAN. , TMTOWTDI.
, ,
,
. , ,
,
, , $SomeClass::Debug = 1.
,
, ,
.
, Crit
ter. ,
population,
.
Critter>population()
$gollum>population()
#
#
Perl ,
.
. population
, $Population. (
.)
package Critter;
our $Population = 0;
sub population { return $Population; }
sub DESTROY { $Population }
sub spawn {
393
my $invocant = shift;
my $class = ref($invocant) || $invocant;
$Population++;
return bless { name => shift || "anon" }, $class;
}
sub name {
my $self = shift;
$self>{name} = shift if @_;
return $self>{name};
}
, :
our $Debugging = 0;
#
sub debug {
shift;
#
$Debugging = shift if @_;
return $Debugging;
}
.
, $Debugging .
our my, .
:
.
:
{
my $Debugging = 0;
#
sub debug {
shift;
#
$Debugging = shift if @_;
}
}
, . .
, , .
,
,
our my. .
,
, , .
, .
, Warg Critter.
, Warg population Critter,
394
12.
, ,
$Critter::Population.
,
. ,
,
:
sub debug {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $varname = $class . "::Debugging";
no strict "refs";
#
$$varname = shift if @_;
return $$varname;
}
,
. : ,
, ,
.
,
( ).
, ,
, .
,
. .
;
, . ,
,
, .
, , ,
. ,
, .
perltootc.
, , .
.
13
, , , .
,
. : ,
: ,
( )
Perl. , .
, :
print $object>as_string;
$new_object = $subject>add($object);
, , , . .
, ,
. .
(overloading), ,
, :
print $object;
$new_object = $subject + $object;
Perl ,
, .
Perl, Math::BigInt,
Math::BigInt,
, .
396
13.
+, /, <=>,
print.
, (overloading)
, (autoloading),
. ,
, (overriding),
. ;
.
overload
use overload .
/,
:
package MyClass;
use overload
, MyClass,
myadd.
MyClass <,
Perl ,
, .
less_than My
Class MyClass , my
add .
abs .
, *
(handlers).
(. . , abs)
,
.
, + <, ,
, .
:
$object + 6
:
6 + $object
397
. (
.) :
$animal + $vegetable
$animal () $vegetable () ,
,
$animal. ( , .)
Perl () , ?:,
. .
.
. ,
undef.
,
. ,
, ,
, , .1 :
$object 6
:
6 $object
,
true. false,
:
( +=,
+ ), false,
undef. .
,
. + ,
, 0
255:
package ClipByte;
use overload '+' => \&clip_add,
1
, ,
, .
, , +
, , .
Perl .
398
13.
'' => \&clip_sub;
sub new {
my $class = shift;
my $value = shift;
return bless \$value => $class;
}
sub clip_add {
my ($x, $y) = @_;
my ($value) = ref($x) ? $$x : $x;
$value
+= ref($y) ? $$y : $y;
$value = 255 if $value > 255;
$value = 0 if $value < 0;
return bless \$value => ref($x);
}
sub clip_sub {
my ($x, $y, $swap) = @_;
my ($value) = (ref $x) ? $$x : $x;
$value
= (ref $y) ? $$y : $y;
if ($swap) { $value = $value }
$value = 255 if $value > 255;
$value = 0 if $value < 0;
return bless \$value => ref($x);
}
package main;
$byte1 = ClipByte>new(200);
$byte2 = ClipByte>new(100);
$byte3 = $byte1 + $byte2;
$byte4 = $byte1 $byte2;
$byte5 = 150 $byte2;
# 255
# 100
# 50
,
, bless
, ;
, . ,
$y , .
, ref($y) $y>isa("Clip
Byte") ( ).
,
. 13.1. %overload::ops,
use overload,
.
399
13.1.
"" 0+ bool
+ * / % ** x . neg
<>
400
13.
. ,
SCALAR(0xba5fe0) .
, (numification),
, , ,
.. .
, boolification,
(, if,
unless, while, for, and, or, &&, ||, ?: grep)
.
*
, ( ,
). . ,
, , ,
.
"", as_string
. :
package Person;
use overload q("") => \&as_string;
sub new {
my $class = shift;
return bless { @_ } => $class;
}
sub as_string {
my $self = shift;
my ($key, $value, $result);
while (($key, $value) = each %$self) {
$result .= "$key => $value\n";
}
return $result;
}
$obj = Person>new(height => 72, weight => 165, eyes => "");
print $obj;
Person=HASH(0xba1350) ( ,
):
weight => 165
height => 72
eyes =>
( ,
.)
: +, , *, /, %, **, x, ., neg
, neg,
: 123.
401
neg
, .
, neg,
, Perl neg.
(autogeneration);
( ,
, .)
( 123 0 123), Perl
neg, . (, ,
,
.)
.
( "" ).
: !
! ,
bool, "" 0+. !,
not . (
?)
,
,
.
,
.
, ?: .
: &, |, ~, ^, <<, >>
~ , .
>> chop:
package ShiftString;
use overload
'>>' => \&right_shift,
'""' => sub { ${ $_[0] } };
sub new {
my $class = shift;
my $value = shift;
return bless \$value => $class;
}
sub right_shift {
my ($x, $y) = @_;
my $value = $$x;
substr($value, $y) = "";
return bless \$value => ref($x);
}
402
13.
$camel = ShiftString>new("Camel");
$ram = $camel >> 2;
print $ram;
# Cam
</para>
: +=, =, *=, /=, %=, **=, x=,.=, <<=, >>=, ++,
. ,
.
+=,
+/. , ,
,
(nomethod fallback), Perl
+, += .
(.=)
, .
++ + ( += =).
, ++ (
). ,
, :
package MagicDec;
use overload
q( ) => \&decrement,
q("") => sub { ${ $_[0] } };
sub new {
my $class = shift;
my $value = shift;
bless \$value => $class;
}
sub decrement {
my @string = reverse split(//, ${ $_[0] } );
my $i;
for ($i = 0; $i < @string; $i++ ) {
last unless $string[$i] =~ /a/i;
$string[$i] = chr( ord($string[$i]) + 25 );
}
$string[$i] = chr( ord($string[$i]) 1 );
my $result = join('', reverse @string);
$_[0] = bless \$result => ref($_[0]);
}
package main;
for $normal (qw/perl NZ Pa/) {
$magic = MagicDec>new($normal);
$magic ;
print "$normal $magic\n";
}
403
:
perl perk
NZ NY
Pa Oz
Perl
.
++$a $a += 1
$a = $a + 1, $a $a = 1 $a = $a 1.
,
++. .
.
: ==, <, <=, >, >=, !=, <=>, lt, le, gt, ge, eq, ne, cmp
<=> ,
<, <=, >, >=, == !=. ,
cmp, lt,
le, gt, ge, eq ne.
, cmp ,
, ,
, . ,
"" .
: atan2, cos, sin, exp, abs, log, sqrt
abs , < <=>
.
abs,
. (, , abs ,
, Perl .)
: <>
<> readline (
, while (<FH>)) glob (
, @files = <*.*>).
package LuckyDraw;
use overload
'<>' => sub {
my $self = shift;
return splice @$self, rand @$self, 1;
};
sub new {
my $class = shift;
return bless [@_] => $class;
}
404
13.
package main;
$lotto = new LuckyDraw 1 .. 51;
for (qw(1 2 3 4 5 6)) {
$lucky_number = <$lotto>;
print "$_ : $lucky_number.\n";
}
$lucky_number = <$lotto>;
print "\n : $lucky_number.\n";
1 :
1 : 18
2 : 11
3 : 40
5 : 51
6 : 33
: 5
# prints 365
. 14 ,
, .
1
CALIFORNIA SUPERLOTTO. . . .
405
(=)
. :
use overload '+' => sub { bless [ \$_[0], \$_[1] ] };
, $animal += $vegetab
le, $animal ,
$animal. *
(circular reference), ,
$animal ,
( ). . ,
8 .
(=)
, =
.
Perl. ,
, .
= , (mutator) (
, ++, )
, . =
, .
.
$copy = $original;
++$copy;
#
#
. , $original
. ++$copy
$copy, $original, $copy $copy
. ,
++$copy, $copy $original
. , ++,
.
, ++
+=, nomethod, .
+, :
$copy = $original;
$copy = $copy + 1;
, + ,
.
, = ,
406
13.
, ,
.
, , :
$copy = $original;
...
++$copy;
:
$copy = $original;
...
$copy = $copy>clone(undef, "");
$copy>incr(undef, "");
, $original
, ++ \&incr, = \&clone.
$copy = $original++,
$copy = $original; ++$original.
(nomethod fallback)
, Perl
. , Perl
nomethod, , .
, AUTOLOAD
: , , .
nomethod
, ( ,
). ,
; , ,
. , $AUTOLOAD
AUTOLOAD.
Perl nomethod, ,
.
,
,
fallback ( ).
:
undef
fallback undef,
:
, , ,
407
nomethod. ,
.
false
fallback , false (
, 0), . Perl
nomethod, ,
.
true
, undef,
. Perl
, use overload .
, Perl ,
overload::constant, import .
( overload::remove_constant
unimport , ,
.)
overload::constant, overload::remove_constant
/. integer, float, binary,
q qr, ,
, .
sub import { overload::constant ( integer
float
binary
q
qr
=>
=>
=>
=>
=>
\&integer_handler,
\&float_handler,
\&base_handler,
\&string_handler,
\®ex_handler ) }
, integer float, ,
Perl .
use constant;
$year = cube(12) + 1;
$pi = 3.14159265358979;
#
#
, .
binary ,
. q (
, q) ,
qq qx (here documents).
, qr ,
5 .
408
13.
.
, Perl.
, Perl ;
, 123_456 123456.
,
q qr, qq, q, s tr ,
. qq ,
, , ,
(backticks), m// s///. q ,
, s ,
s///, tr
tr/// y///.
,
.
, :
package DigitDoubler;
use overload;
# , DigitDoubler.pm
,
, . , :
use DigitDoubler;
$trouble = 123;
$jeopardy = 3.21;
# trouble 246
# jeopardy 6.42
.
,
("."),
"ab$cd!!" 'ab' . $cd .
'!!'. ,
, neg,
. (
, ,
.)
, overload::constant
eval ,
409
(bug feature)
.
Perl 5.6 use overload
.
overload::StrVal(OBJ)
, OBJ
("").
overload::Overloaded(OBJ)
true, OBJ
, false .
overload::Method(OBJ, OPERATOR)
, OPERA
TOR, OBJ, undef,
.
. ,
, ,
.
.
,
, ,
. ,
.
.
,
.
, Alpha Beta Gamma
Beta + \&Beta::plus_sub, Gamma
+ "plus_meth", + Alpha
Beta::plus_sub.
fallback ,
.
fallback ,
( ,
).
410
13.
use ,
:
eval " use overload '+' => \&my_add ";
:
eval " no overload '+', ' ', '<=' ";
Perl DDEBUGGING,
Do
. , ,
m Perl.
, , ,
.
14
.
,
. , ,
, ,
, ,
. , :
.
Perl tie ( ) :
, , ,
Perl
. , .
, $, @, % *
Perl :
.
tie, ,
. , Perl,
(tie) , ,
Perl
, Perl
.
. ,
Perl. ( , ,
412
14.
Perl, magic ()
,
, %ENV %SIG.
.)
Perl dbmopen dbmclose,
,
, Perl tie. tie
. Perl dbmopen dbmclose
tie.
, , ( typeg
lob) ,
,
. tie:
, ,
, , Perl
, , .
tied:
tie VARIABLE, CLASSNAME, LIST; # VARIABLE CLASSNAME
$object = tied VARIABLE;
:
$object = tie VARIABLE, CLASSNAME, LIST;
,
;
.
, :
untie VARIABLE;
tie bless,
, , .
, tie ,
, ,
,
: TIESCALAR, TIEARRAY, TIEHASH TIEHANDLE.1
CLASSNAME
, LIST. ( VARIABLE
.)
1
, ,
. , ,
, ,
.
413
.
, , , tie,
.
tie, ,
. ,
. (
, ;
, .
, ,
.)
bless
, .
, ;
bless,
. ,
TIEARRAY ,
.
tie use
require: , , ,
tie. ( , dbmopen
use
.
use ,
dbmopen.
AnyDBM_File.)
, ,
, FETCH STORE,
(. . ) Perl.
,
. (
, , BEGIN, CHECK, INIT,
END, DESTROY AUTOLOAD, UNIVERSAL>VERSION.
Perl
: STDIN, SUPER, CORE, CORE::GLOBAL, DATA, @EXPORT, @INC, @ISA,
@ARGV %ENV.
.)
, : ,
.
: TIESCALAR, FETCH STORE (, DESTROY).
414
14.
Perl TIESCALAR.
FETCH,
STORE. ,
tie ( tied),
: FETCH
STORE .
, .
DESTROY, Perl
.
, untie,
, tie. untie
, ; DESTROY
, .
Tie::Scalar Tie::StdScalar,
Tie::Scalar,
,
. Tie::Scalar ,
, Tie::StdScalar ,
Perl. (
,
, ,
.)
,
, .
:
#!/usr/bin/perl
package Centsible;
sub TIESCALAR { bless \my $self, shift }
sub STORE { ${ $_[0] } = $_[1] } #
sub FETCH { sprintf "%.02f", ${ my $self = shift } } #
package main;
tie $bucks, "Centsible";
$bucks = 45.00;
$bucks *= 1.0715; #
$bucks *= 1.0715; # !
print " , , $bucks.\n";
:
, , 51.67.
, tie, :
, , 51.66505125.
, , .
415
, , ,
.
( ,
),
ScalarFile. , ,
, ,
. ( ,
, .)
:
use ScalarFile;
# ScalarFile.pm
tie $camel, "ScalarFile", "/tmp/camel.lot";
,
.
/tmp/camel.lot, $camel /tmp/
camel.lot , .
, ,
. ,
, :
$dromedary = $camel;
$camel, Perl FETCH
. , :
$dromedary = (tied $camel)>FETCH():
, tie,
, :
$clot = tie $camel, "ScalarFile", "/tmp/camel.lot";
$dromedary = $camel;
#
$dromedary = $clot>FETCH(); # ,
416
14.
package ScalarFile;
use Carp;
use strict;
use warnings;
# .
# .
#
# .
use warnings::register; # "use warnings
#'ScalarFile'".
my $count = 0;
# ScalarFile.
,
[] {}, bless ,
,
. ( ),
.
, ,
, camel.lot.
:
sub TIESCALAR { bless \$_[1], $_[0] } # ,
.
. ,
. ( ,
, .
Camel Lot .)
,
, undef , ,
417
carp. ( croak, .)
, ,
warnings:
sub TIESCALAR {
# ScalarFile.pm
my $class
= shift;
my $filename = shift;
my $fh;
if (open $fh, "<", $filename or
open $fh, ">", $filename)
{
close $fh;
$count++;
return bless \$filename, $class;
}
carp " $filename: $!" if warnings::enabled();
return;
}
, , $string
camel.lot:
tie ($string, "ScalarFile", "camel.lot") or die;
( , .
, ,
,
flock.
. 23 .)
SELF>FETCH
(. .
). ,
. .
sub FETCH {
my $self = shift;
confess " " unless ref $self;
return unless open my $fh, $$self;
read($fh, my $value, s $fh); # NB: s !
return $value;
}
(
), FETCH , . (
.)
, , ,
. Perl
, $self;
confess ,
418
14.
. ( .)
camel.lot, :
tie($string, "ScalarFile", "camel.lot");
print $string;
SELF>STORE(VALUE)
, (
). , SELF, ,
; VALUE ,
. (
, , STORE.)
sub STORE {
my($self,$value) = @_;
ref $self
or confess " ";
open my $fh, ">", $$self
or croak " $$self: $!";
syswrite($fh, $value) == length $value
or croak " $$self: $!";
close $fh
or croak " $$self: $!";
return $value;
}
, ,
. ,
croak.
,
.
, .
camel.lot:
tie($string, "ScalarFile", "camel.lot");
$string = " camel.lot\n";
$string .= " , .\n";
SELF>DESTROY
, ,
, ,
. ,
, . . Perl
. DESTROY,
:
sub DESTROY {
my $self = shift;
confess " " unless ref $self;
$count ;
}
419
. ,
, DESTROY
, ?
sub count {
# my $invocant = shift;
$count;
:
if (ScalarFile>count) {
warn " ScalarFiles...\n";
}
, , . ,
,
, ( ). ,
TIESCALAR.
Tie::Counter,
CPAN. , , 1
. :
tie my $counter, "Tie::Counter", 100;
@array = qw /Red Green Blue/;
for my $color (@array) {
# :
print " $counter $color\n";
# 100 Red
}
# 101 Green
# 102 Blue
, 0.
. :
package Tie::Counter;
sub FETCH
{ ++ ${ $_[0] } }
sub STORE
{ ${ $_[0] } = $_[1] }
sub TIESCALAR {
my ($class, $value) = @_;
$value = 0 unless defined $value;
bless \$value => $class;
}
1; #
, ? ,
.
420
14.
$_
$_.
use, import,
no
unimport. :
no Underscore;
$_
.
:
#!/usr/bin/perl
no Underscore;
@tests = (
"Assignment"
"Reading"
"Matching"
"Chop"
"Filetest"
"Nesting"
);
=>
=>
=>
=>
=>
=>
sub
sub
sub
sub
sub
sub
{
{
{
{
{
{
$_ = "Bad" },
print },
$x = /badness/ },
chop },
x },
for (1..3) { print } },
:
Testing
Testing
Testing
Testing
Testing
Testing
Assignment: detected
Reading: detected
Matching: detected
Chop: detected
Filetest: detected
Nesting: 123 missed!
missed
for .
Underscore. ( ,
?) ,
local. tie
, require .
package Underscore;
use Carp;
sub TIESCALAR { bless \my $dummy => shift }
421
use no
, ,
. Underscore>import
Underscore>unimport, use no. , ,
$_,
local, .
, , ,
, TIEARRAY, FETCH STORE.
: , DESTROY, STORESIZE
FETCHSIZE, $#array
scalar(@array). , Perl ,
CLEAR, Perl
, EXTEND.
POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE
EXISTS, , Perl
. Tie::Array
FETCH STORE. (
DELETE EXISTS Tie::Array croak.) FETCH
STORE , ,
.
, Tie::StdArray (
Tie::Array) ,
, .
, .
Tie::StdArray, ,
.
#!/usr/bin/perl
package ClockArray;
use Tie::Array;
our @ISA = 'Tie::StdArray';
sub FETCH {
my($self,$place) = @_;
$self>[ $place % 12 ];
}
sub STORE {
my($self,$place,$value) = @_;
422
14.
$self>[ $place % 12 ] = $value;
}
package main;
tie my @array, 'ClockArray';
@array = ( "a" ... "z" );
print "@array\n";
"y z o p q r s t u v w x".
, , ,
0 11. 15 ,
3. ,
24 .
. .
, .
,
. :
use BoundedArray;
tie @array, "BoundedArray", 2;
$array[0]
$array[1]
$array[2]
$array[3]
=
=
=
=
"";
"";
"";
"!"; # ; .
:
package BoundedArray;
use Carp;
use strict;
SPLICE,
Tie::Array:
use Tie::Array;
our @ISA = ("Tie::Array");
CLASSNAME>TIEARRAY(LIST)
TIEARRAY
, .
, ,
,
.
: BOUND
, DATA
. , ,
423
(, , ),
.
sub TIEARRAY {
my $class = shift;
my $bound = shift;
confess "usage: tie(\@ary, 'BoundedArray', max_subscript)"
if @_ || $bound =~ /\D/;
return bless { BOUND => $bound, DATA => [] }, $class;
}
:
tie(@array, "BoundedArray", 3); # 3
, .
FETCH STORE,
, , .
SELF>FETCH(INDEX)
. : ,
.
sub FETCH {
my ($self, $index) = @_;
if ($index > $self>{BOUND}) {
confess "Array OOB: $index > $self>{BOUND}";
}
return $self>{DATA}[$index];
}
SELF>STORE(INDEX, VALUE)
,
. : ,
, ,
. :
sub STORE {
my($self, $index, $value) = @_;
if ($index > $self>{BOUND} ) {
confess "Array OOB: $index > $self>{BOUND}";
}
return $self>{DATA}[$index] = $value;
}
SELF>DESTROY
Perl ,
.
, .
424
14.
SELF>FETCHSIZE
FETCHSIZE
, SELF. scalar(@ar
ray), $#array + 1.
sub FETCHSIZE {
my $self = shift;
return scalar @{$self>{DATA}};
}
SELF>STORESIZE(COUNT)
,
SELF, COUNT. ,
COUNT. ,
, .
BoundedArray ,
.
sub STORESIZE {
my ($self, $count) = @_;
if ($count > $self>{BOUND}) {
confess "Array OOB: $count > $self>{BOUND}";
}
$#{$self>{DATA}} = $count;
}
SELF>EXTEND(COUNT)
EXTEND Perl , , ,
, COUNT .
,
.
BoundedArray ,
.
SELF>EXISTS(INDEX)
INDEX. BoundedArray
Perl exists ,
.
sub EXISTS {
my ($self, $index) = @_;
if ($index > $self>{BOUND}) {
confess "Array OOB: $index > $self>{BOUND}";
}
exists $self>{DATA}[$index];
}
425
SELF>DELETE(INDEX)
DELETE INDEX SELF.
BoundedArray EXISTS,
.
sub DELETE {
my ($self, $index) = @_;
print STDERR "deleting!\n";
if ($index > $self>{BOUND}) {
confess "Array OOB: $index > $self>{BOUND}";
}
delete $self>{DATA}[$index];
}
SELF>CLEAR
, . ,
(
), , undef.
BoundedArray ,
:
sub CLEAR {
my $self = shift;
$self>{DATA} = [];
}
, CLEAR ,
. :
tie(@array, "BoundedArray", 2);
@array = (1, 2, 3, 4);
CLEAR .
STORE.
CLEAR STORE.
SELF>PUSH(LIST)
LIST.
BoundedArray:
sub PUSH
{
my $self = shift;
if (@_ + $#{$self>{DATA}} > $self>{BOUND}) {
confess " ";
}
push @{$self>{DATA}}, @_;
}
SELF>UNSHIFT(LIST)
LIST .
BoundedArray PUSH.
426
14.
SELF>POP
POP .
BoundedArray :
sub POP { my $self = shift; pop @{$self>{DATA}} }
SELF>SHIFT
SHIFT . Bo
undedArray POP.
SELF>SPLICE(OFFSET, LENGTH, LIST)
SELF. splice
Perl OFFSET ,
,
. LENGTH ,
. LIST .
,
LENGTH, OFFSET (
, LIST).
, ;
SPLICE Tie::Array,
Tie::Array. SPLICE
BoundedArray, .
BoundedArray.
. , .
.
.
. ,
. :
#!/usr/bin/perl
package RandInterp;
sub TIEARRAY { bless \my $self };
sub FETCH { int rand $_[1] };
package main;
tie @rand, "RandInterp";
print " , $_, $rand[$_]\n";
}
$rand[32] = 5;
# ?
427
:
, 1, 0
, 10, 3
, 100, 46
, 1000, 755
Can't locate object method "STORE" via package "RandInterp" at foo line 10.
[ "STORE" "RandInterp"]
, , STORE. ,
, .
, , .
TIEHASH . FETCH STORE
/. EXISTS , , DELETE
.1 CLEAR ,
/. FIRSTKEY NEXTKEY /,
keys, values each. ,
,
DESTROY. ( , , ,
, .
Tie::Hash, , . ,
Tie::StdHash , .)
, , ,
. , :
$h{$k} = "";
$h{$k} = "";
:
push @{ $h{$k} }, "";
push @{ $h{$k} }, "";
,
.
Tie::StdHash, . Tie::AppendHash, :
package Tie::AppendHash;
use Tie::Hash;
our @ISA = ("Tie::StdHash");
1
, Perl , ,
, undef.
exists defined .
428
14.
sub STORE {
my ($self, $key, $value) = @_;
push @{$self>{key}}, $value;
}
1;
: ,
dot (. . ,
,
Unix). ,
( ), dot.
:
use DotFiles;
tie %dot, "DotFiles";
if ( $dot{profile} =~ /MANPATH/ or
$dot{login} =~ /MANPATH/ or
$dot{cshrc} =~ /MANPATH/
) {
print ", MANPATH\n";
}
:
# , dot .
tie %him, "DotFiles", "daemon";
foreach $f (keys %him) {
printf "dot %s %d\n", $f, length $him{$f};
}
DotFiles ,
, {CONTENTS} ,
. :
USER
dot .
HOME
dot.
CLOBBER
dot.
CONTENTS
dot .
DotFiles.pm:
package DotFiles;
use Carp;
sub whowasi { (caller(1))[3] . "()" }
my $DEBUG = 0;
sub debug { $DEBUG = @_ ? shift : 1 }
429
, ,
$DEBUG.
: whowasi , (
whowasi).
DotFiles:
CLASSNAME>TIEHASH(LIST)
DotFiles:
sub TIEHASH {
my $self = shift;
my $user = shift || $>;
my $dotdir = shift || "";
croak "usage: @{[ &whowasi ]} [USER [DOTDIR]]" if @_;
$user = getpwuid($user) if $user =~ /^\d+$/;
my $dir = (getpwnam($user))[7]
or croak "@{ [&whowasi] }: no user $user";
$dir .= "/$dotdir" if $dotdir;
my $node = {
USER
HOME
CONTENTS
CLOBBER
};
=>
=>
=>
=>
$user,
$dir,
{},
0,
, , ,
, readdir,
( ). ,
chdir, , .
SELF>FETCH(KEY)
.
: ,
. ,
( , ).
430
14.
DotFiles:
sub FETCH {
carp &whowasi if $DEBUG;
my $self = shift;
my $dot = shift;
my $dir = $self>{HOME};
my $file = "$dir/.$dot";
unless (exists $self>{CONTENTS}>{$dot} || f $file) {
carp "@{[&whowasi]}: no $dot file" if $DEBUG;
return undef;
}
# .
if (defined $self>{CONTENTS}>{$dot}) {
return $self>{CONTENTS}>{$dot};
} else {
return $self>{CONTENTS}>{$dot} = `cat $dir/.$dot`;
}
}
, Unix cat(1),
( )
. , dot
Unix, .
SELF>STORE(KEY, VALUE)
,
( ).
: , ,
.
DotFiles
clobber ,
tie:
sub STORE {
carp &whowasi if $DEBUG;
my $self = shift;
my $dot = shift;
my $value = shift;
my $file = $self>{HOME} . "/.$dot";
croak "@{[&whowasi]}: $file not clobberable"
unless $self>{CLOBBER};
open(F, "> $file") or croak " $file: $!";
print F $value;
close(F);
}
431
, :
$ob = tie %daemon_dots, "daemon";
$ob>clobber(1);
$daemon_dots{signature} = " \n";
{CLOBBER} tied:
tie %daemon_dots, "DotFiles", "daemon";
tied(%daemon_dots)>clobber(1);
:
(tie %daemon_dots, "DotFiles", "daemon")>clobber(1);
clobber :
sub clobber {
my $self = shift;
$self>{CLOBBER} = @_ ? shift : 1;
}
SELF>DELETE(KEY)
.
,
delete. , ,
:
sub DELETE {
carp &whowasi if $DEBUG;
my $self = shift;
my $dot = shift;
my $file = $self>{HOME} . "/.$dot";
croak "@{[&whowasi]}: $file"
unless $self>{CLOBBER};
delete $self>{CONTENTS}>{$dot};
unlink $file or carp "@{[&whowasi]}: $file: $!";
}
SELF>CLEAR
, ,
.
dot ! ,
, CLOBBER 1,
:
sub CLEAR {
carp &whowasi if $DEBUG;
my $self = shift;
croak "@{[&whowasi]}: dot $self>{USER}"
unless $self>{CLOBBER} > 1;
432
14.
for my $dot ( keys %{$self>{CONTENTS}}) {
$self>DELETE($dot);
}
}
SELF>EXISTS(KEY)
exists
.
{CONTENTS}:
sub EXISTS {
carp &whowasi if $DEBUG;
my $self = shift;
my $dot = shift;
return exists $self>{CONTENTS}>{$dot};
}
SELF>FIRSTKEY
, ,
, keys, values each. keys
,
each return .
sub FIRSTKEY {
carp &whowasi if $DEBUG;
my $self = shift;
my $temp = keys %{$self>{CONTENTS}};
return scalar each %{$self>{CONTENTS}};
}
SELF>NEXTKEY(PREVKEY)
keys, values each. PREVKEY
, ,
Perl . , NEXTKEY
.
, CONTENTS ,
. Perl each:
sub NEXTKEY {
carp &whowasi if $DEBUG;
my $self = shift;
return scalar each %{ $self>{CONTENTS} }
}
SELF>DESTROY
,
. ,
. :
433
sub DESTROY {
carp &whowasi if $DEBUG;
}
, , :
, , @{[&whowasi]},
$whowasi,
.
, ,
: TIEHANDLE , , PRINT, PRINTF, WRITE,
READLINE, GETC READ. DESTROY
BINMODE, OPEN, CLOSE, EOF, FILENO, SEEK, TELL, READ WRITE,
Perl
. ( : WRITE syswrite
Perl write
.)
, Perl
(, Apache vi) STDOUT STDERR
.
.
,
.
print printf,
:
package ReversePrint;
use strict;
sub TIEHANDLE {
my $class = shift;
bless [], $class;
}
sub PRINT {
my $self = shift;
push @$self, join '', @_;
}
sub PRINTF {
my $self = shift;
my $fmt = shift;
push @$self, sprintf $fmt, @_;
}
sub READLINE {
my $self = shift;
pop @$self;
}
434
14.
package main;
my $m = " MORE \n";
tie *REV, "ReversePrint";
# print printf.
print REV "The fox is now dead.$m";
printf REV <<"END", int rand 10000000;
The quick brown fox jumps over
over the lazy dog %d times!
END
print REV <<"END";
The quick brown fox jumps
over the lazy dog.
END
# .
print while <REV>;
:
The quick brown fox jumps
over the lazy dog.
The quick brown fox jumps over
over the lazy dog 3179357 times!
The fox is now dead. MORE
,
.
<SHOUT>
</SHOUT> .
(wellformed) XML.
Shout.pm, :
package Shout;
use Carp;
# croak
Shout.pm.
CLASSNAME>TIEHANDLE(LIST)
, , , bless
.
sub TIEHANDLE {
my $class = shift;
my $form = shift;
open my $self, $form, @_ or croak " $form@_: $!";
if ($form =~ />/) {
print $self "<SHOUT>\n";
435
$$self>{WRITING} = 1;
}
return bless $self, $class;
#
# $self glob
, tie, <SHOUT>
bless . open
, , open or
die, , my $self open
, typeglob. , typeg
lob, , typeglob
/ , ,
, ($$$self), (@$$self)
(%$$self). ( , &$$self.)
$form , .
, @_ open .
$form .
open , .
, .
. $$self>{WRITING}
. , ,
, .
%$$self, .
$$$self,
. ( (self) ,
, .)
SELF>PRINT(LIST )
. LIST ,
print. LIST
:
sub PRINT {
my $self = shift;
print $self map {uc} @_;
}
SELF>READLINE
,
(<FH>) readline.
undef, .
sub READLINE {
my $self = shift;
return <$self>;
}
436
14.
<$self>,
.
SELF>GETC
,
getc.
sub GETC {
my $self = shift;
return getc($self);
}
Shout, GETC
Perl
.
SELF>OPEN(LIST)
TIEHANDLE , ,
Shout, open, .
sub OPEN {
my $self = shift;
my $form = shift;
my $name = "$form@_";
$self>CLOSE;
open($self, $form, @_)
or croak " $name: $!";
if ($form =~ />/) {
print $self "<SHOUT>\n" or croak " : $!";
$$self>{WRITING} = 1;
# ,
}
else {
$$self>{WRITING} = 0;
# ,
}
return 1;
}
CLOSE, ,
.
, open, shout.
SELF>CLOSE
.
</SHOUT>,
Perl close.
sub CLOSE {
my $self = shift;
if ($$self>{WRITING}) {
$self>SEEK(0, 2)
$self>PRINT("</SHOUT>\n")
or return;
or return;
437
}
return close $self;
}
SELF>SEEK(LIST)
seek SEEK.
sub SEEK {
my $self = shift;
my ($offset, $whence) = @_;
return seek($self, $offset, $whence);
}
SELF>TELL
tell .
sub TELL {
my $self = shift;
return tell $self;
}
SELF>PRINTF(LIST)
,
printf. LIST
.
sub PRINTF {
my $self = shift;
my $template = shift;
return $self>PRINT(sprintf $template, @_);
}
sprintf
PRINT .
sprintf.
.
SELF>READ(LIST)
,
read sysread. ,
, LIST, ,
read , .
sub READ {
my ($self, undef, $length, $offset) = @_;
my $bufref = \$_[1];
return read($self, $$bufref, $length, $offset);
}
438
14.
SELF>WRITE(LIST)
,
syswrite. , ,
.
sub WRITE {
my $self = shift;
my $string = uc(shift);
my $length = shift || length $string;
my $offset = shift || 0;
return syswrite $self, $string, $length, $offset;
}
SELF>EOF
, ,
Shout,
eof.
sub EOF {
my $self = shift;
return eof $self;
}
SELF>BINMODE(DISC)
/,
. ,
( :raw),
.
sub BINMODE {
my $self = shift;
my $disc = shift || ":raw";
return binmode $self, $disc;
}
, ,
open .
, , :
sub BINMODE { croak(" binmode") }
SELF>FILENO
(fileno),
.
sub FILENO {
my $self = shift;
return fileno $self;
}
439
SELF>DESTROY
, ,
. ,
. ,
. close
$self, CLOSE. ,
, DESTROY
.
sub DESTROY {
my $self = shift;
$self>CLOSE;
# CLOSE Shout.
}
Shout:
#!/usr/bin/perl
use Shout;
tie(*FOO, Shout::, ">filename");
print FOO "hello\n";
#
seek FOO, 0, 0;
#
@lines = <FOO>;
#
close FOO;
#
open(FOO, "+<", "filename");
#
seek(FOO, 8, 0);
#
sysread(FOO, $inbuf, 5);
#
print "found $inbuf\n";
#
seek(FOO, 5, 1);
#
syswrite(FOO, "ciao!\n", 6);
#
untie(*FOO);
#
HELLO.
.
READLINE.
.
FOO, OPEN.
"<SHOUT>\n".
5 FOO $inbuf.
"hello".
"hello".
6 FOO.
CLOSE.
:
<SHOUT>
CIAO!
</SHOUT>
,
. ,
, PATHNAME DEBUG.
,
(. 13 ):
# , !
use overload q("") => sub { $_[0]>pathname };
# , .
sub trace {
my $self = shift;
440
14.
local $Carp::CarpLevel = 1;
Carp::cluck("\ntrace magical method") if $self>debug;
}
# .
sub pathname {
my $self = shift;
confess " " unless ref $self;
$$self>{PATHNAME} = shift if @_;
return $$self>{PATHNAME};
}
# .
sub debug {
my $self = shift;
my $var = ref $self ? \$$self>{DEBUG} : \our $Debug;
$$var = shift if @_;
return ref $self ? $$self>{DEBUG} || $Debug : $Debug;
}
trace , :
sub GETC { $_[0]>trace;
my($self) = @_;
getc($self);
}
TIEHANDLE OPEN:
sub TIEHANDLE {
my $class
= shift;
my $form = shift;
my $name = "$form@_";
open my $self, $form, @_ or
if ($form =~ />/) {
print $self "<SHOUT>\n";
$$self>{WRITING} = 1;
}
bless $self, $class;
$self>pathname($name);
return $self;
}
#
croak " $name: $!";
# ,
# $fh glob
#
441
}
else {
$$self>{WRITING} = 0;
}
return 1;
# ,
$self>debug(1)
. Carp::cluck
. ,
, .
:
trace magical method at foo line 87
Shout::SEEK('>filename', '>filename', 0, 2) called at foo line 81
Shout::CLOSE('>filename') called at foo line 65
Shout::OPEN('>filename', '+<', 'filename') called at foo line 141
,
. ,
bc(1) ( ) :
use Tie::Open2;
tie *CALC, 'Tie::Open2', "bc l";
$sum = 2;
for (1 .. 7) {
print CALC "$sum * $sum\n";
$sum = <CALC>;
print "$_: $sum";
chomp $sum;
}
close CALC;
, :
1:
2:
3:
4:
5:
6:
7:
4
16
256
65536
4294967296
18446744073709551616
340282366920938463463374607431768211456
,
bc(1) Tie::Open2, , .
.
. (
442
14.
IPC::Open2;
.)
package Tie::Open2;
use strict;
use Carp;
use Tie::Handle; # !
use IPC::Open2;
sub TIEHANDLE {
my ($class, @cmd) = @_;
no warnings 'once';
my @fhpair = \do { local(*RDR, *WTR) };
bless $_, 'Tie::StdHandle' for @fhpair;
bless(\@fhpair => $class)>OPEN(@cmd) || die;
return \@fhpair;
}
sub OPEN {
my ($self, @cmd) = @_;
$self>CLOSE if grep {defined} @{ $self>FILENO };
open2(@$self, @cmd);
}
sub FILENO {
my $self = shift;
[ map { fileno $self>[$_] } 0,1 ];
}
for my $outmeth ( qw(PRINT PRINTF WRITE) ) {
no strict 'refs';
*$outmeth = sub {
my $self = shift;
$self>[1]>$outmeth(@_);
};
}
for my $inmeth ( qw(READ READLINE GETC) ) {
no strict 'refs';
*$inmeth = sub {
my $self = shift;
$self>[0]>$inmeth(@_);
};
}
for my $doppelmeth ( qw(BINMODE CLOSE EOF)) {
no strict 'refs';
*$doppelmeth = sub {
my $self = shift;
$self>[0]>$doppelmeth(@_) && $self>[1]>$doppelmeth(@_);
};
}
for my $deadmeth ( qw(SEEK TELL)) {
no strict 'refs';
*$deadmeth = sub {
443
, ,
. ,
8 .
.
.
use strict;
package Tie::DevNull;
sub TIEHANDLE {
my $class = shift;
my $fh = local *FH;
bless \$fh, $class;
}
for (qw(READ READLINE GETC PRINT PRINTF WRITE)) {
no strict 'refs';
*$_ = sub { return };
}
package Tie::DevRandom;
sub READLINE { rand() . "\n"; }
sub TIEHANDLE {
my $class = shift;
my $fh = local *FH;
bless \$fh, $class;
}
sub FETCH { rand() }
sub TIESCALAR {
my $class = shift;
bless \my $self, $class;
}
package Tie::Tee;
sub TIEHANDLE {
my $class = shift;
my @handles;
for my $path (@_) {
open(my $fh, ">$path") || die " $path";
push @handles, $fh;
}
bless \@handles, $class;
}
sub PRINT {
my $self = shift;
my $ok = 0;
for my $fh (@$self) {
444
14.
$ok += print $fh @_;
}
return $ok == @$self;
}
*SCATTER,
*RANDOM,
*NULL,
my $randy,
for my $i (1..10) {
my $line = <RANDOM>;
chomp $line;
for my $fh (*NULL, *SCATTER) {
print $fh "$i: $line $randy\n";
}
}
:
1: 0.124115571686165 0.20872819474074
2: 0.156618299751194 0.678171662366353
3: 0.799749050426126 0.300184963960792
4: 0.599474551447884 0.213935286029916
5: 0.700232143543861 0.800773751296671
6: 0.201203608274334 0.0654303290639575
7: 0.605381294683365 0.718162304090487
8: 0.452976481105495 0.574026269121667
9: 0.736819876983848 0.391737610662044
10: 0.518606540417331 0.381805078272308
! *SCATTER tie.
tmp1, tmp2 tmp4,
tmp3. (
*NULL, , ,
, ,
.)
, tie tied,
, , .
445
(, ) ,
, :
package Remember;
sub TIESCALAR {
my $class = shift;
my $filename = shift;
open(my $handle, ">", $filename)
or die " $filename: $!\n";
print $handle "\n";
bless {FH => $handle, VALUE => 0}, $class;
}
sub FETCH {
my $self = shift;
return $self>{VALUE};
}
sub STORE {
my $self = shift;
my $value = shift;
my $handle = $self>{FH};
print $handle "$value\n";
$self>{VALUE} = $value;
}
sub DESTROY {
my $self = shift;
my $handle = $self>{FH};
print $handle "\n";
close $handle;
}
1;
, Remember:
use strict;
use Remember;
my $fred;
$x = tie $fred, "Remember", "camel.log";
$fred = 1;
$fred = 4;
$fred = 5;
untie $fred;
system "cat camel.log";
, :
1
4
5
446
14.
. Remember ,
, , :
sub comment {
my $self = shift;
my $message = shift;
print { $self>{FH} } $handle $message, "\n";
}
, comment:
use strict;
use Remember;
my ($fred, $x);
$x = tie $fred, "Remember", "camel.log";
$fred = 1;
$fred = 4;
comment $x "changing...";
$fred = 5;
untie $fred;
system "cat camel.log";
, , , . .
,
. : ,
. untie
.
, DESTROY.
,
$x. , untie
. DESTROY ,
. :
.
.
w
use warnings
"untie". untie,
. , Perl
:
untie attempted while 1 inner references still exist
[ untie 1 ]
,
untie. :
undef $x;
untie $fred;
CPAN
447
,
.
CPAN
,
, . CPAN
, . (,
, .) . 14.1 .
14.1. CPAN
GnuPG::Tie::En
crypt
GNU Privacy Guard
IO::WrapTie
IO::Handle
MLDBM
,
, DBM
Net::NISplusTied
NIS+
Tie::Cache::LRU
Tie::Const
Tie::Counter
Tie::CPHash
Tie::DB_FileLock
Berkeley
DB 1.x
Tie::DBI
DBI
Tie::DB_Lock
Tie::Dict
RPC dict
Tie::Dir
Tie::DirHandle
Tie::FileLRUCache
, ,
LRU ( )
Tie::FlipFlop
Tie::HashDefaults
Tie::HashHistory
Tie::IxHash
Perl
Tie::LDAP
LDAP
Tie::Persistent
tie
448
14.
14.1 ()
Tie::Pick
( )
Tie::RDBM
Tie::SecureHash
Tie::STDERR
STDERR ,
Tie::Syslog
Tie::TextDir
Tie::TransactHash
Tie::VecArray
Tie::Watch
Perl
Win32::TieRegistry
Microsoft Windows
III
Perl
15
Unicode
, Unicode,
, Unicode .
( ,
. .)
, ,
.
,
. ,
, ,
,
,
. ,
. (
) , ?
, Unicode ( XML). Unicode
, .
Unicode (
), ,
1 Unicode
. Unicode,
1
, , .
452
15. Unicode
( ).
.
.
,
, . Unicode
. Perl ,
. Unicode Perl
.
, , Perl
, Perl
, Perl , ,
Perl
. Perl Unicode ,
: .
:
1:
,
.
2:
,
.
3:
,
.
4:
Perl ,
Perl Perl.
.
. , ,
, . . . Unicode
Perl.
, , ,
. ,
.
453
Perl 5.6
.1 5.6 ,
.
, 0 .. 2**321
(, 64 , 0 .. 2**641).
, , ,
, ; , , Perl
. Perl
( UTF8),
. ,
18 446 744 073 709 551 615 (. . \x{ffff_ffff_ffff_ffff}),
( 13 ),
0..127, , ,
, . . UTF8 ASCII
.
Perl UTF8, ,
0..255,
, ,
, Perl
8 UTF8. ,
, . .
.
, ,
255 , UTF8.
, Perl UTF8,
utf8 , ,
. ( , UTF8
, Unicode. utf8
,
. Perl ,
.)
, ,
. , v
IPv4:
$locaddr = v127.0.0.1;
# .
$oreilly = v204.148.40.9; # utf8.
$badaddr = v2004.148.40.9; # utf8.
1
, ,
, ,
.
454
15. Unicode
, $badaddr
IP. , O'Reilly's
UTF8, .
, . ,
IPv4, gethostbyaddr,
( $badaddr ).
Perl
.
.
, open),
,
, ,
, ,
, .1
Perl
, ,
, .
Perl
,
. ,
0..255, ,
utf8,
,
. 1.
,
,
, Perl
. , Perl
,
. ,
, ,
255,
utf8 Perl utf8,
. utf8
, , ,
, 255, ,
1
. C
( ${^WIDE_SYSTEM_CALLS} 1),
API
. ( Microsoft Windows.)
Linux ,
UTF8, $ENV{LC_CTYPE} UTF8.
. .
455
. 2.
, ,
, ,
/, .
use bytes
, utf8.
.
1
2.
3 ,
utf8,
Unicode,
.
4
. ,
Perl , ,
Perl 5.6 Perl
, ,
, , , .
,
4.
,
,
use bytes. use bytes,
8 (. .
utf8), ,
8 .
,
, Latin1.
utf8 , ,
, UTF8
, .
Unicode.
utf8 .
use bytes .
,
,
, use bytes.
456
15. Unicode
length,
, , .
:
use bytes (); # .
...
$charlen =
length("\x{ffff_ffff}"); # 1.
$bytelen = bytes::length("\x{ffff_ffff}"); # 7.
255:
use utf8;
$convergence = "L
7";
,
Unicode,
UTF8. ,
UTF8 ,
use utf8 .
Unicode ,
ASCII, \x.
Latin1 \x{ab} \xab,
,
. Unicode
\x. ,
Unicode \x{263A}. Perl
, , Unicode 16 ,
, , \u263A;
\x{263A}.
\N{CHARNAME}, .
use charnames 31 .
Perl
Unicode, ():
use utf8;
$ ++;
# .1
. . . .
457
Perl . . www.unico*
de.org .
, . ,
, . Unicode
Tengwar, ( ,
UTF8) :
"\N{TENGWAR LETTER SILME NUQUERNA}" =~ /^.$/1
\C (char C,
\C). \C ,
Malformed UTF8 character ( UTF8).
\C ,
.
,
, ,
Unicode. \w
():
"
Unicode
\p (
) \P ( ). , \p{Lu}
Unicode, \p{M}
.
,
\pM. ,
\p{IsMirrored} \p{InTibetan}:
"\N{greek:Iota}" =~ /\p{Lu}/
\p \P
. ( Perl 5.6.0 use utf8,
. .)
, Unicode, . 5
.
\X Unicode (
(combining character sequence)),
, ,
. (?:\PM\pM*):
"o\N{COMBINING TILDE BELOW}" =~ /\X/
458
15. Unicode
\X ,
.
tr/// .
Latin1 ,
:
tr/\0\x{10ffff}/\0\xff?/;
# utf8 latin1
, ,
Unicode. , uc
, ucfirst ( ,
). ,
:
$x
$x
$x
$x
=
=
=
=
"\u$word";
"\U$word";
"\l$word";
"\L$word";
#
#
#
#
$word
$word
$word
$word
,
,
,
.
,
, .
,
, ,
chop, substr, pos, index, rindex, sprintf, write length.
, , vec, pack
unpack. , , chomp,
, ,
, sort , .
use bytes;
$bytelen = length("I do &
no bytes;
$charlen = length("I do
.");
.");
# 15
# 9
chr ord :
chr(1).chr(20).chr(300).chr(4000) eq v1.20.300.4000
459
scalar reverse , :
"L
7"
eq reverse "7
L"
</para>
__PERLLIB/unicode,
, .
Unicode Unicode
Unicode.300 ( Unicode 3.0). mktables.PL
.pl ( Is/, In/
To/), Perl ,
, \p (. Is/ In/) uc (. To/).
, use charnames
(. Name.pl ).
, , :
ArabLink.pl
ArabLnkGrp.pl
Bidirectional.pl
Block.pl
Category.pl
CombiningClass.pl
Decomposition.pl
JamoShort.pl
Number.pl
To/Digit.pl
Unicode
__PERLLIB/unicode/Unicode3.html.
, Unicode ,
, .
__PERLLIB :
% perl MConfig le 'print $Config{privlib}'
, Unicode, , The
Unicode Standard, Version 3.0 (ISBN 0201616335).
,
(. . Perl 5.6.0)
Unicode. (
.)
460
15. Unicode
. ,
Unicode
( Unicode),
. ,
Unicode, Unicode.
,
, utf8.
, , ,
, .
, UTF8.
,
.
utf8
.
8
0..255,
,
( Unicode).
.
.
Unicode , , .
Books.Ru
ISBN 5932860200,
Perl, 3
Books.Ru .
,
. ,
(piracy@symbol.ru),
.
16
, .
. ,
. ,
,
, .
, , ,
:
. ,
.
IPC
(Interprocess Communication). Perl
IPC .
.
: ,
. Perl
,
Unix.
, Perl ,
. ,
:
, ,
. , ,
462
16.
,
.
, XML, Java Perl. .
, , ,
. ,
, FIFO System V IPC.
; Unix (
Apple Mac OS X) ,
, SysV IPC,
Microsoft, , ,
.1
Perl ( ,
) perlport. Microsoft
perlwin32 perlfork, ,
Microsoft. :
Perl : %SIG
( )
.
. ,
, ,
.
kill.
.2
,
.
, ,
. , ,
, .
1
2
AF_UNIX.
, , ,
, ,
, .
463
IPC,
,
.
,
, <Ctrl>+<C> <Ctrl>+<Z>,
, , ,
, ,
.
. ,
.
.
%SIG ,
:
$SIG{INT} = sub { die "\nOutta here!\n" };
$SIG{ALRM} = sub { die "Your alarm clock went off" };
. ,
( <Ctrl>+<C>
<Ctrl>+<\> ) :
sub catch_zap {
my $signame = shift;
our $shucks++;
die "
}
$shucks = 0;
$SIG{INT} = 'catch_zap';
$SIG{INT} = \&catch_zap;
$SIG{QUIT} = \&catch_zap;
SIG$signame!";
# &main::catch_zap
#
#
,
die.
, C .
,1
print ( ,
malloc (3))
,
C. (
die ,
1
Perl
Perl,
.
464
16.
eval, / die,
C. .)
sigtrap :
use sigtrap qw(die INT QUIT);
use sigtrap qw(die untrapped normalsignals
stacktrace any errorsignals);
,
,
.
,
, .
, , END
DESTROY .
Perl (,
die),
.
, ,
. use sigtrap 31
.
%SIG "IGNORE" "DE
FAULT", Perl
( ,
, KILL STOP; signal(3),
, , , ).
, ,
Perl, ,
. , %SIG
kill *l, .
Perl Config,
. . Config(3).
%SIG ,
.
.
local,
. ( , local ,
.)
{
local $SIG{INT} = 'IGNORE';
...
# , , SIGINT.
fn(); # SIGINT fn() !
...
# .
465
# $SIG{INT}.
fn();
( , Unix) ,
. ,
, ,
, ( )
.
, . ,
, ,
, ,
,
, . .
. ( , ID
$$.)
, (hangup)
, , ,
, ,
, . . setpgrp(0,0),
, ,
, . ,
: fork,
open system("cmd &").
,
( ,
UID,
).
{
local $SIG{HUP} = 'IGNORE';
kill(HUP, $$);
#
#
#
0.
, ,
UID. , ,
, .
unless (kill 0 => $kid_pid) {
warn " $kid_pid";
}
0 ,
Perl Microsoft Unix. Microsoft kill
.
466
16.
, .
. 0
.
CHLD,
,1 wait waitpid.
Perl , fork, Perl
,
fork , . ,
$SIG{CHLD} 'IGNORE'. ( )
. ,
, ,
, :
use POSIX ":sys_wait_h";
sub REAPER { 1 until waitpid(1, WNOHANG) == 1) }
,
CHLD:
$SIG{CHLD} = \&REAPER;
, , ,
. ,
,
C. ,
, ,
,
, :
our $zombies = 0;
$SIG{CHLD} = sub { $zombies++ };
sub reaper {
my $zombie;
our %Kid_Status; #
$zombies = 0;
while (($zombie = waitpid(1, WNOHANG)) != 1) {
$Kid_Status{$zombie} = $?;
}
}
while (1) {
reaper() if $zombies;
...
}
1
467
, .
SysV ,
. 5.003
Perl , , sigaction(2),
. ,
Perl,
. , BSD ( Linux, Solaris
Mac OS X), POSIX
, SysV
, .
. , (
, read, wait accept)
.
,
$! ($ERRNO) EINTR , ,
. INT;
TSTP ( <Ctrl>+<Z>) CONT (
) . Perl
,
. (feature).
,
POSIX, Config , $Config{d_sigaction}
. ,
, sigaction(2)
sigvec(3) C sys/signal.h SV_INTERRUPT
SA_RESTART. , , ,
.
. Unix (
POSIX , ALRM),
ALRM:
use Fcntl ':flock';
eval {
local $SIG{ALRM} = sub { die " " };
alarm 10;
# 10
eval {
flock(FH, LOCK_EX) # lock
or die "can't flock: $!";
};
alarm 0;
#
};
468
16.
alarm 0;
#
die if $@ && $@ !~ /alarm clock restart/; #
,
,
flock, Perl ,
. ,
die, eval .
( ,
long jmp(3) C,
.)
, flock ,
,
. alarm 0 ,
flock,
alarm 0. alarm
, : , .
, .
. , ,
, , . Perl
%SIG ,
POSIX sigprocmask(2):
use POSIX qw(:signal_h);
$sigset = POSIX::SigSet>new;
$blockset = POSIX::SigSet>new(SIGINT, SIGQUIT, SIGCHLD);
sigprocmask(SIG_BLOCK, $blockset, $sigset)
or die " INT,QUIT,CHLD: $!\n";
, ,
, . ,
, :
sigprocmask(SIG_SETMASK, $sigset)
or die " INT,QUIT,CHLD: $!\n";
,
.
, . ,
, .1
1
.
, .
469
, ,
CHLD, ( )
.
, .
, IPC,
,
, .
,
,
. IPC,
: ,
,
.1
.
.
, , . (
HTML, , .)
, .
,
, ;
, .
( )
,
.
, ,
, , .
IPC, .
,
,
. ,
,
, ,
.
1
, .
470
16.
(. . e $file)
,
,
(, ,
). .
23 .
Perl flock
(HANDLE, FLAGS), 29 . Perl
,
.
, ,
, , System V Windows NT. (
Microsoft , NT, , , ,
, Apple , Mac OS X.)
: (shared)
( LOCK_SH) ,
(exclusive), ( LOCK_EX). ,
,
. , flock *
,
.
,
, ,
, . ,
,
, .
.
flock .
, ,
,
. ,
:
use Fcntl qw(:DEFAULT :flock);
open(FH, "< filename") or die " : $!";
flock(FH, LOCK_SH)
or die " : $!";
# FH
,
LOCK_NB flock.
, ,
false. :
flock(FH, LOCK_SH | LOCK_NB)
or die " : $!";
471
,
, , , ,
/. , , ,
. ,
. ,
.
, ,
,
:
use Fcntl qw(:DEFAULT :flock);
open(FH, "< filename") or die " : $!";
unless (flock(FH, LOCK_SH | LOCK_NB)) {
local $| = 1;
print " ...";
flock(FH, LOCK_SH) or die " : $!";
print "got it.\n"
}
# FH
,
.
, , ,
,
. . ,
.
, .1
,
close
.
,
, .
open; < ,
, >,
. ,
, sysopen.
1
(filehandles), (file
descriptors), , . .
. , die
.
,
, ,
filename, filename
,
( ).
472
16.
,
. .
use Fcntl qw(:DEFAULT :flock);
sysopen(FH, "filename", O_WRONLY | O_CREAT)
or die " : $!";
flock(FH, LOCK_EX)
or die " : $!";
truncate(FH, 0)
or die " : $!";
# now write to FH
,
sysopen.
, . ,
,
.
,
.
.
, ( )
( ) , ,
. (
.)
use Fcntl qw(:DEFAULT :flock);
sysopen(FH, "counterfile", O_RDWR | O_CREAT)
or die " counterfile: $!";
flock(FH, LOCK_EX)
or die " counterfile: $!";
$counter = <FH> || 0; # undef
seek(FH, 0, 0)
or die " counterfile : $!";
print FH $counter+1, "\n"
or die " counterfile: $!";
# ,
#
truncate(FH, tell(FH))
or die " counterfile: $!";
close(FH)
or die " counterfile: $!";
, ,
. , , ,
,
. .
, ,
473
(deadlock), ,
,
.
.
, , ,
,
accept.
DBM,
,
.
DBM .
dbmopen tie, .
O_RDONLY,
LOCK_SH. LOCK_EX
. ( ,
.)
use Fcntl qw(:DEFAULT :flock);
use DB_File; # ;
$DBNAME = "/path/to/database";
$LCK
= $DBNAME . ".lockfile";
# O_RDWR,
sysopen(DBLOCK, $LCK, O_RDONLY | O_CREAT)
or die " $LCK: $!";
#
flock(DBLOCK, LOCK_SH)
or die " LOCK_SH $LCK: $!";
tie(%hash, "DB_File", $DBNAME, O_RDWR | O_CREAT)
or die " tie $DBNAME: $!";
%hash. ,
, , , :
untie %hash;
close DBLOCK;
#
#
GNU DBM,
GDBM_File. tie
GDBM_NOLOCK,
GDBM
.
fork
.
474
16.
. , ,
,
.
. ,
:
open(INPUT, "< /etc/motd")
or die "/etc/motd: $!";
if ($pid = fork) { waitpid($pid,0) }
else {
defined($pid)
or die "fork: $!";
while (<INPUT>) { print "$.: $_" }
exit; #
}
# INPUT EOF
, open,
;
.
ID ,
,
. , (
, , id),
, ,
.
,
,
.
, Perl
( )
exec
open, system qx// ( ).
STDIN, STDOUT STDERR ,
.
(filehandles):
open(INPUT, "< /etc/motd")
if ($pid = fork) { wait }
else {
defined($pid)
open(STDIN, "<&INPUT")
exec("cat", "n")
}
,
, , ,
475
. Perl
( , ), $^F
($SYSTEM_FD_MAX). ,
, $^F,
. Perl ,
.
,
,
:
# INPUT exec
{
local $^F = 10_000;
open(INPUT, "< /etc/motd") or die "/etc/motd: $!";
} # $^F
.
( , )
, .
/dev/fd /proc/$$/fd,
0 , ,
, . ( Li
nux , /proc , . , BSD
Solaris /dev/fd. ,
.)
exec ,
:
if ($pid = fork) { wait }
else {
defined($pid)
or die "fork: $!";
$fdfile = "/dev/fd/" . fileno(INPUT);
exec("cat", "n", $fdfile) or die "exec cat: $!";
}
fcntl,
exec.
, ,
.
use Fcntl qw/F_SETFD/;
fcntl(INPUT, F_SETFD, 0)
or die " exec INPUT: $!\n";
:
fcntl(INPUT, F_SETFD, 1)
or die " exec INPUT: $!\n";
476
16.
:
use Fcntl qw/F_SETFD F_GETFD/;
printf("INPUT %s exec\n",
fcntl(INPUT, F_GETFD, 1) ? "" : " ");
,
, , STDIN, STDOUT
STDERR, ,
.
.
Perl,
open.
&=, .
if (defined($ENV{input_fdno}) && $ENV{input_fdno}) =~ /^\d$/) {
open(INPUT, "<&=$ENV{input_fdno}")
or die " fdopen $ENV{input_fdno} : $!";
}
,
Perl, .
Perl open ( sysopen
open ), .
, Perl:
#!/usr/bin/perl p
# nl
printf "%6d ", $.;
, INPUT ,
exec,
:
$fdspec = '<&=' . fileno(INPUT);
system("nl", $fdspec);
:
@lines = `nl '$fdspec'`; # spec
,
, fork, (got
cha). , fork,
, ,
.
,
,
. ,
477
.
,
, ,
,
. ,
fork , .
fork , Unix, ,
, Unix/PO
SIX. , fork Microsoft
Perl 5.6 ( ) Windows 98 ( ). fork
, ,
;
. . 17 .
(pipe) ,
.
, . , ,
, .
Perl open , ,
.
, (
),
. ,
:
open SPOOLER, "| cat v | lpr h 2>/dev/null"
or die "can't fork: $!";
local $SIG{PIPE} = sub { die "spooler pipe broke" };
print SPOOLER "stuff\n";
close SPOOLER or die "bad spool: $! $?";
,
(cat) . (lpr)
.
(pipeline).
, , ,
,
.
Perl (/bin/sh Unix),
,
478
16.
.
,
:
open SPOOLER, "|", "lpr", "h"
or die "can't run lpr: $!";
# 5.6.1
, , STDOUT,
.
1 :
if (t STDOUT) {
#
my $pager = $ENV{PAGER} ||
open(STDOUT, "| $pager")
}
END {
close(STDOUT)
}
stdout
'more';
or die " : $!";
, ,
, .
.
,
:
open STATUS, "netstat an 2>/dev/null |"
or die "can't fork: $!";
while (<STATUS>) {
next if /^(tcp|udp)/;
print;
}
close STATUS or die "bad netstat: $! $?";
, .
, , ,
open:
open STATUS, "|", "netstat", "an"
or die "can't run netstat: $!";
# 5.6.1
/,
, Perl
.
, ,
, :
print grep { !/^(tcp|udp)/ } `netstat an 2>&1`;
die "bad netstat" if $?;
1
, .
479
,
,
.
, ,
.
, . .
. (
, .)
,
open close.
, ,
open, close. open ,
fork , ,
. ( Perl,
,
.) , ,
.
close, , ,
$? ($CHILD_ERROR).
, close, ,
open. ,
PIPE, ,
, .
IPC , , ,
.
.
,
, ,
, .
open, ,
. open "|"
"|" , .
fork, open ID
0 .
, , open,
. STDIN STDOUT.
, TO ( ) "|",
, STDIN:
if (open(TO, "|")) {
print TO $fromparent;
480
16.
}
else {
$tochild = <STDIN>;
exit;
}
FROM ( ) "|",
(filehandle), ,
STDOUT:
if (open(FROM, "|")) {
$toparent = <FROM>;
}
else {
print STDOUT $fromchild;
exit;
}
, . ,
, ,
, . Perl 5.6.1 (
)
open .
open, ,
, UID
GID. fork
,
,
, .
, 23 .
.
, .
, Unix tee(1)
. ,
( ),
:
tee("/tmp/foo", "/tmp/bar", "/tmp/glarch");
while (<>) {
print "$ARGV at line $. => $_";
}
close(STDOUT) or die " STDOUT: $!";
sub tee {
my @output = @_;
my @handles = ();
for my $path (@output) {
481
my $fh;
unless (open ($fh, ">", $path)) {
warn " $path: $!";
next;
}
push @handles, $fh;
}
# STDOUT
return if my $pid = open(STDOUT, "|");
die " : $!" unless defined $pid;
# STDIN
while (<STDIN>) {
for my $fh (@handles) {
print $fh $_ or die " tee: $!";
}
}
for my $fh (@handles) {
close($fh) or die " tee: $!";
}
exit; # !
}
,
, . ,
STDOUT ,
( STDIN)
.
, STDOUT.
, Perl printf, sprintf .
, , Perl,
:
badfunc("arg");
# , !
$string = forksub(\&badfunc, "arg"); #
@lines = forksub(\&badfunc, "arg"); #
sub forksub {
my $kidpid = open my $self, "|";
defined $kidpid
or die " : $!";
shift>(@_), exit
unless $kidpid;
local $/
unless wantarray;
return <$self>;
#
}
;
, , . ,
, .
482
16.
open
,
? , ,
:
open(PROG_TO_READ_AND_WRITE, "| |") # !
,
:
Can't do bidirectional pipe at myprog line 3.
[ myprog 3]
open ,
, .
, IPC::Open2,
STDIN STDOUT .
IPC::Open3 / (
STDERR ),
select, IO::Select.
Perl, <> (readline).
open2:
use IPC::Open2;
local (*Reader, *Writer);
$pid = open2(\*Reader, \*Writer, "bc l");
$sum = 2;
for (1 .. 5) {
print Writer "$sum * $sum\n";
chomp($sum = <Reader>);
}
close Writer;
close Reader;
waitpid($pid, 0);
print " $sum\n";
:
my ($fhread, $fhwrite);
$pid = open2($fhread, $fhwrite, "cat u n");
, , , /
.
( ),
,
.
: bc
(pipe) .
, ,
483
,
. , , ftp
, ;
tty. IO::Pty
Expect CPAN, tty (
tty, ).
, .
,
,
Perl ,
.
pipe, :
pipe(FROM_PARENT, TO_CHILD)
or die "pipe: $!";
pipe(FROM_CHILD, TO_PARENT)
or die "pipe: $!";
select((select(TO_CHILD), $| = 1))[0]); #
select((select(TO_PARENT), $| = 1))[0]); #
if ($pid = fork) {
close FROM_PARENT; close TO_PARENT;
print TO_CHILD " Pid $$ \n";
chomp($line = <FROM_CHILD>);
print " Pid $$ : `$line'\n";
close FROM_CHILD; close TO_CHILD;
waitpid($pid,0);
} else {
die " : $!" unless defined $pid;
close FROM_CHILD; close TO_CHILD;
chomp($line = <FROM_PARENT>);
print " Pid $$ : `$line'\n";
print TO_PARENT " Pid $$ \n";
close FROM_PARENT; close TO_PARENT;
exit;
}
Unix
pipe,
. socketpair
. .
use Socket;
socketpair(Child, Parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "socketpair: $!";
# Perl
my ($kidfh, $dadfh);
socketpair($kidfh, $dadfh, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "socketpair: $!";
Parent Child.
484
16.
, Child,
Parent.
, ,
, ,
, CPAN,
. (
. .)
(named pipe), FIFO,
.
, ,
,
, .1
FIFO ,
. FIFO ,
. , FIFO
, , , .
POSIX mkfifo
, POSIX.
Microsoft Win32::Pipe, ,
, , . ( Win32
pipe, .)
, , .signature
. ,
Perl
. ,
(, , finger . .)
,
.
"p", , (
) FIFO.2 , ,
.
open "> $fpath", ,
.signature ,
"p" . "+< $fpath" ,
FIFO (
1
2
Unix, open.
,
, , p STDIN.
System V IPC
485
,
, .
FIFO,
, ,
. ,
,
, . (
!)
System V IPC
System V IPC . , ,
,
,
.
, SysV IPC ipcs(1)
ipcrm(1) , .
, IPC SysV
. IPC:
486
16.
, .
,
.
. , .
, mmap(2),1
.
, Perl
, mmap(2).
mmap(2),
,
, ,
(unified buffer cache), , , (fly
catcher unibus), , , ,
SysV IPC
.
,
.
SysV IPC
, ,
. ,
.2
,
, .
,
. IPC::Shareable ,
tie.
,
<Ctrl>+<C> :
#!/usr/bin/perl w
use v5.6.0; #
use strict;
use sigtrap qw(die INT TERM HUP QUIT);
my $PROGENY = shift(@ARGV) || 3;
eval { main() }; # DESTROY,
die if $@ && $@ !~ /^Caught a SIG/;
print "\nDone.\n";
exit;
1
2
Mmap CPAN.
: .
IPC::Shareable CPAN. .
, , ,
, , SysV IPC:
,
, .
System V IPC
487
sub main {
my $mem = ShMem>alloc("Original Creation at " . localtime);
my(@kids, $child);
$SIG{CHLD} = 'IGNORE';
for (my $unborn = $PROGENY; $unborn > 0; $unborn ) {
if ($child = fork) {
print "$$ begat $child\n";
next;
}
die " : $!" unless defined $child;
eval {
while (1) {
$mem>lock();
$mem>poke("$$ " . localtime)
unless $mem>peek =~ /^$$\b/o;
$mem>unlock();
}
};
exit; #
}
while (1) {
print "Buffer is ", $mem>get, "\n";
sleep 1;
}
}
ShMem, .
( "1; " )
require . ( IPC,
, Perl.)
package ShMem;
use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU);
use IPC::Semaphore;
sub MAXBUF() { 2000 }
sub alloc {
#
my $class = shift;
my $value = @_ ? shift : '';
my $key = shmget(IPC_PRIVATE, MAXBUF, S_IRWXU) or die "shmget: $!";
my $sem = IPC::Semaphore>new(IPC_PRIVATE, 1, S_IRWXU | IPC_CREAT)
or die "IPC::Semaphore>new: $!";
$sem>setval(0,1)
or die "sem setval: $!";
my $self = bless {
OWNER => $$,
SHMKEY => $key,
SEMA
=> $sem,
} => $class;
$self>put($value);
return $self;
}
488
16.
, ,
,
, .
, , ,
ipcs ipcrm ( ).
,
:
, SysV IPC,
.
489
sub DESTROY {
my $self = shift;
return unless $self>{OWNER} == $$; #
shmctl($self>{SHMKEY}, IPC_RMID, 0)
or warn "shmctl RMID: $!";
$self>{SEMA}>remove()
or warn "sema>remove: $!";
}
IPC, , :
. (
, NFS (
), NFS ,
.)
.
(sockets) BSD,
Unix,
.
, .
(
TCP), ( UDP).
.
TCP
, .
,
, .
, ,
TCP.
UDP, . ,
, UDP
DNS, IP.
UDP, .
, TCP, ,
; ,
, .
UDP , TCP,
UDP , ,
, . ,
( DNS.)
, .
Unix,
. ,
490
16.
IP. , ,
.
Perl, , ,
C,
: , Perl ,
C; , , Perl ,
.
, , 29 .
Perl ,
,
, , .
, , , ,
undef ,
. ,
, ,
. ,
$AF_INET = 2, , .
, Socket
IO::Socket, .
,
.
, , ,
( T
):
#!/usr/bin/perl w
use strict;
use sigtrap;
use Socket; # IO::Socket
, Perl
C,
. , ,
TCP UDP. , ,
,
, ,
.
Unix. (, Unix
recvmsg(2).)
, ,
, , , FTP, Telnet,
Web . ., , ,
CPAN.
Net::SMTP ( Mail::Mailer), Net::NNTP, Net::DNS, Net::FTP, Net::Tel
net , HTTP.
491
libnet libwww .
CPAN, ,
5 IPC, 15 ,
WWW, 16 .
, , . . ,
, , 29 .
,
.
TCP, ,
IO::Socket::INET:
use IO::Socket::INET;
$socket = IO::Socket::INET>new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto
=> "tcp",
Type
=> SOCK_STREAM)
or die " $remote_host:$remote_port : $!\n";
# ,
print $socket " ?\n";
# ,
$answer = <$socket>;
# .
close($socket);
,
,
:
$socket = IO::Socket::INET>new("www.yahoo.com:80")
or die " 80 yahoo: $!";
Socket:
use Socket;
#
socket(Server, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
#
$internet_addr = inet_aton($remote_host)
or die " $remote_host : $!\n";
$paddr = sockaddr_in($remote_port, $internet_addr);
#
connect(Server, $paddr)
or die " $remote_host:$remote_port: $!\n";
492
16.
select((select(Server), $| = 1)[0]); #
#
print Server " ?\n";
#
$answer = <Server>;
#
close(Server);
,
,
, , shutdown
:
#
shutdown(Server, 1);
# Socket::SHUT_WR v5.6
.
IO::Socket::INET:
use IO::Socket::INET;
$server = IO::Socket::INET>new(LocalPort
Type
Reuse
Listen
or die " tcp
=>
=>
=>
=>
$server_port,
SOCK_STREAM,
1,
10 ) # SOMAXCONN
$server_port: $!\n";
Socket:
use Socket;
#
socket(Server, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
#
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1);
#
$my_addr = sockaddr_in($server_port, INADDR_ANY);
bind(Server, $my_addr)
or die " $server_port: $!\n";
#
listen(Server, SOMAXCONN)
or die " $server_port: $!\n";
493
#
while (accept(Client, Server)) {
# Client
}
close(Server);
,
. INADDR_ANY, ,
.
(,
),
. ( , .)
, , getpeer
name . IP,
( ):
use Socket;
$other_end = getpeername(Client)
or die " : $!\n";
($port, $iaddr) = unpack_sockaddr_in($other_end);
$actual_ip = inet_ntoa($iaddr);
$claimed_hostname = gethostbyaddr($iaddr, AF_INET);
,
IP ,
.
:
@name_lookup = gethostbyname($claimed_hostname)
or die "Could not reverse $claimed_hostname: $!\n";
@resolved_ips = map { inet_ntoa($_) } @name_lookup[ 4 .. $#name_lookup ];
$might_spoof = !grep { $actual_ip eq $_ } @resolved_ips;
,
/ . ,
,
.
, fork
. (
select.)
REQUEST:
while (accept(Client, Server)) {
if ($kidpid = fork) {
close Client;
#
next REQUEST;
}
defined($kidpid) or die " fork: $!" ;
494
16.
close Server;
select(Client);
$| = 1;
#
#
# Client
#
$input = <Client>;
print Client "output\n"; # STDOUT,
open(STDIN, "<<&Client")
open(STDOUT, ">&Client")
open(STDERR, ">&Client")
# ,
system("bc l");
#
#
print "done\n";
#
,
,
!
close Client;
exit; # accept!
}
fork.
, . (
, .) fork, listen
SOMAXCONN ( ) .
, ,
.
( Unix),
.
REAPER, ,
$SIG{CHLD} = 'IGNORE'.
,
, ( ) .
, STDIN STDOUT,
.
,
exec.
,
T ,
setuid setgid.
, ( CGI),
.
.
23 .
,
: ,
CRLF, : "\015\12", "\xd\xa"
495
, UDP
, ,
(
). , UDP Unreliable Datagram Proto
col .
, UDP TCP,
( ).
, , , TCP. ,
TCP ,
(
), .
, , UDP.
UDP , ,
, ,
.1 ,
, , , 4
,
, .
, 1900 .
1900 1970 ,
localtime gmtime.
#!/usr/bin/perl
# clockdrift
#
, , .
#
.
use v5.6.0; #
use warnings;
use strict;
1
, ifconfig *a
.
496
16.
use Socket;
unshift(@ARGV, inet_ntoa(INADDR_BROADCAST))
unless @ARGV;
socket(my $msgsock, PF_INET, SOCK_DGRAM, getprotobyname("udp"))
or die ": $!";
# "" (borked)1
.
setsockopt($msgsock, SOL_SOCKET, SO_BROADCAST, 1)
or die "setsockopt: $!";
my $portno = getservbyname("time", "udp")
or die " udp";
for my $target (@ARGV) {
print " $target:$portno\n";
my $destpaddr = sockaddr_in($portno, inet_aton($target));
send($msgsock, "x", 0, $destpaddr)
or die "send: $!";
}
# 32 1900
my $FROM_1900_TO_EPOCH = 2_208_988_800;
my $time_fmt = "N"; #
my $time_len = length(pack($time_fmt, 1)); #
my $inmask = ''; # fileno select
vec($inmask, fileno($msgsock), 1) = 1;
#
while (select(my $outmask = $inmask, undef, undef, 0.5)) {
defined(my $srcpaddr = recv($msgsock, my $bintime, $time_len, 0))
or die "recv: $!";
my($port, $ipaddr) = sockaddr_in($srcpaddr);
my $sendhost = sprintf "%s [%s]",
gethostbyaddr($ipaddr, AF_INET) || 'UNKNOWN',
inet_ntoa($ipaddr);
my $delta = unpack($time_fmt, $bintime)
$FROM_1900_TO_EPOCH time();
print " $sendhost $delta .\n";
Borked borken
broken. . .
17
,
. ,
,
. .
,
, .
, ,
, ,
, .
, ,
. ,
.
. ,
Unix,
, .
,
. ,
( ).
, , ,
, , .
.
498
17.
Perl .
(process model) (thread model).
,
. Perl Unix
, ,
.
, , . .
,
,
. ( ,
,
, .
, ,
,
(copyonwrite), ,
.)
Microsoft, Windows
( , ,
).
.
Perl 5.6
fork Windows
. ,
, fork,
Windows.
, ,
. (,
C, .)
ithreads, inter
preter threads, .
fork
Microsoft. ,
,
, ,
.
,
, ,
. , ,
,
499
, .
, .
,
.
fork Perl Microsoft. ,
,
. ,
, , ,
. ,
,
. . .
, , ,
.
,
.
, ,
.
Perl
5.005 . (
,
, 5.6.)
5.6, Perl ,
. Perl
,
, .
,
, ,
( ,
,
). ,
,
. Perl
,
. fork,
, ,
.
500
17.
,
.1
.
( ),
. ,
,
. ( ,
, , , ,
.)
, .
. , Perl
.
.
, . ,
, .
, ,
,
.
,
, .
, Perl C,
. Perl ,
100%, , C
, . ,
, .
, ,
.
? ? ,
,
.
,
( ),
.
, : , ,
, .
Perl,
, README.threads
1
System V, ,
.
501
Perl. , Perl
, Perl.
,
(POSIX, DEC, Microsoft .), ,
, Perl. , Perl Perl,
C++, Java . ,
( ).
(mutexes). , Thread::Se
maphore cond_wait.
? ,
. .
Thread
Perl
Thread. , Perl, lock.
lock .
.
Thread :
new
Thread
self
Thread
list
Thread
Thread :
Join
( )
Eval
( )
equal
Tid
ID
, Thread
:
yield
async
Thread
cond_signal
, cond_wait()
cond_broadcast , cond_wait()
cond_wait
cond_signal()
cond_broadcast()
502
17.
:
Thread>new async.
Thread. Thread>new ,
, , :
use Thread;
...
$t = Thread>new( \&func, $arg1, $arg2);
:
my $something;
$t = Thread>new( sub { say($something) } );
async (
):
use Thread qw(async);
...
my $something;
$t = async {
say($something);
};
, async .
, , Thre
ad::async, .
async , , ,
, .
,
, ,
,
. ,
.1
,
? , ,
.
: , ,
. join,
:
$retval = $t>join();
1
# $t
exit! , ,
. ,
, exit.
.
503
join waitpid .
, join ,
. , join
,
( ). ,
, , join.
, waitpid,
, join
. ,
.
, (
)
, (
).
.
join ,
:
use Thread 'async';
$t1 = async {
my @stuff = getpwuid($>);
return @stuff;
};
$t2 = async {
my $motd = `cat /etc/motd`;
return $motd;
};
@retlist = $t1>join();
$retval = $t2>join();
print "1st kid returned @retlist\n";
print "2nd kid returned $retval\n";
, ,
, join ;
.
, join
,
, .
. , join,
join. join
,
. , ,
, eval, ,
, $@:
504
17.
,
, ,
. ,
, .
, .
detach
,
,
, detach, Perl .
.
init Unix, ,
Unix .
detach :
, , detach,
, exit . detach
. Perl,
. , detach Perl,
join
. ,
join, detach,
,
, Perl (
), join .
, , ,
detach, join,
. eval {}
.
Perl ,
tid:
$his_tidno = $t1>tid();
, Thread>self.
ID : ID :
$mytid = Thread>self>tid();
505
:
Thread::equal($t1, $t2)
$t1>equal($t2)
$t1>tid() == $td>tid()
Thread>list.
, .
, .
for my $t (Thread>list()) {
printf "$t has tid = %d\n", $t>tid();
}
Thread yield.
, .
,
. ,
CPU:
use Thread 'yield';
yield();
.
, yeild:
use strict;
use Thread 'yield';
yeild;
# .
yield;
# Ok.
, , ,
. , ,
.
.
, ,
, Perl.
,
( ).
.
,
. ( , .)
506
17.
,
,
,
, .
( Thread>new), ( async),
, ,
. (, , .)
Perl
$! $_, @_,
. , , ,
, .
, ,
,
,
.
, , ,
,
.
, .
.
(lock) Perl
/ . lock
,
, ,
sub lock {}.
. CORE::lock
. ( perl, ,
lock ; ,
(noop), , ).
flock
flock, /, lock
lock, . , , *
, .1
,
.
1
( ), ,
. ,
,
!
507
lock $var;
lock @values;
lock %table;
:
lock @values;
...
lock $values[23];
# 1
# 2 !
,
, . . .1 ,
,
. ( .)
, , ,
. , lock
: , .
. ,
.
, eval.
.
. ,
,
.
, .
, ,
.
yield, ,
(preemptive scheduling):
use Thread qw/async yield/;
my $var = 0;
sub abump {
if ($var == 0) {
yield;
$var++;
}
}
my $t1 = new Thread \&abump;
my $t2 = new Thread \&abump;
for my $t ($t1, $t2) { $t>join }
print "var is $var\n";
, Perl .
508
17.
( ) 2,
, , 0,
, .
,
$var. 1:
sub abump {
lock $var;
if ($var == 0) {
yield;
$var++;
}
}
, unlock .
, , , ,
:
sub abump {
{
lock $var;
if ($var == 0) {
yield;
$var++;
}
} # !
# $var
}
, ,
,
. :
my $t1 = async {
lock $a; yield; lock $b;
$a++; $b++
};
my $t2 = async {
lock $b; yield; lock $a;
$b++; $a++
};
, ,
, .
. ( .
,
.)
509
:
lock &func;
, ,
.
, .
,
$done. ( yield .)
use Thread qw/async yield/;
my $done = 0;
sub frob {
my $arg = shift;
my $tid = Thread>self>tid;
print "thread $tid: frob $arg\n";
yield;
unless ($done) {
yield;
$done++;
frob($arg + 10);
}
}
:
my @t;
for my $i (1..3) {
push @t, Thread>new(\&frob, $i);
}
for (@t) { $_>join }
print "done is $done\n";
(
):
thread 1:
thread 2:
thread 3:
thread 1:
thread 2:
thread 3:
done is 3
frob
frob
frob
frob
frob
frob
1
2
3
11
12
13
:
for my $i (1..3) {
push @t, async {
lock &frob;
frob($i);
510
17.
};
}
for (@t) { $_>join }
print "done is $done\n";
:
thread 1:
thread 1:
thread 2:
thread 3:
done is 1
frob
frob
frob
frob
1
11
2
3
locked
, ,
, .
, .
,
.
locked. ,
lock &sub,
, .
, , .
:
sub frob : locked {
#
}
, :
sub frob ($) : locked {
#
}
,
. ,
, .
,
. method
:
sub frob : locked method {
#
}
,
,
511
.
, :
(Package>new, $obj>new)
.
Perl .
(condition variable)
, .
, ,
. ,
, ,
.
. ,
cond_wait,
,
.
(
%SIG).
, ,
, . Thread
: cond_wait, cond_sig
nal cond_broadcast. ,
Thread::Queue Thread::Semaphore.
, .
cond_wait ,
, , ,
cond_signal cond_broadcast
.
, cond_wait,
cond_wait. cond_wait
, ,
.
, cond_wait ,
.
cond_signal ,
, ,
cond_wait.
cond_wait, ,
, , . ,
cond_wait , .
cond_broadcast cond_signal,
, , cond_wait . (
512
17.
, ,
.)
cond_wait ,
. cond_signal
cond_broadcast , .
: ,
; , ,
cond_wait . ,
. (
.)
.
,
, : cond_wait ,
.
use Thread qw(async cond_wait cond_signal);
my $wait_var = 0;
async {
lock $wait_var;
$wait_var = 1;
cond_wait $wait_var until $wait_var == 2;
cond_signal($wait_var);
$wait_var = 1;
cond_wait $wait_var until $wait_var == 2;
$wait_var = 1;
cond_signal($wait_var);
};
async {
lock $wait_var;
cond_wait $wait_var until $wait_var == 1;
$wait_var = 2;
cond_signal($wait_var);
cond_wait $wait_var until $wait_var == 1;
$wait_var = 2;
cond_signal($wait_var);
cond_wait $wait_var until $wait_var == 1;
};
, cond_wait.
Thread::Queue
, .
:
513
new
Thread::Queue
enqueue
dequeue
. dequeue
,
.
, ,
(blessed) !
, perlthrtut:
use Thread qw/async/;
use Thread::Queue;
my $Q = Thread::Queue>new();
async {
while (defined($datum = $Q>dequeue)) {
print "Pulled $datum from queue\n";
}
};
$Q>enqueue(12);
$Q>enqueue("A", "B", "C");
$Q>enqueue($thr);
sleep 3;
$Q>enqueue(\%ENV);
$Q>enqueue(undef);
:
Pulled
Pulled
Pulled
Pulled
Pulled
Pulled
12 from queue
A from queue
B from queue
C from queue
Thread=SCALAR(0x8117200) from queue
HASH(0x80dfd8c) from queue
, $Q ,
async.
Perl .
, $Q async.
Thread::Semaphore
, p() v().
passeer () verlaat (),
down up . (
wait signal.) :
514
17.
new
Thread::Semaphore
down
up
new
. , 1.
( ,
, .)
use Thread::Semaphore;
$mutex = Thread::Semaphore>new($MAX);
down 1,
.
.
, ,
. :
$mutex>down();
up 1,
.
.
, down ,
up ,
down . :
$mutex>up();
Thread::Signal ,
%SIG .
Perl
.
. ,
. , , .
, .
18
, Perl, ,
, . perl ( /usr/bin/
perl) Perl. , ,
, ,
(
), .
, .
,
, .
, ,
.
,
. ,
, ,
.
, , , .
, , ,
, .
,
.
, .
, Perl
perl, ,
516
18.
.
, Perl 1
. ,
. ,
, ,
, ,
, .
Perl
, Perl,
, .
,
. . 18.1.
. 18.1. Perl
1.
, , Perl
,
.
Perl , :
BEGIN.
, ,
FIFO ( ).
use no,
BEGIN. CHECK, INIT END
.
,
. eval BLOCK, s///e
. ,
1
, , , , .
Perl
517
.
, CHECK
LIFO ( ).
CHECK , 2 4.
2. ()
CHECK ,
,
. ,
C Perl
, Perl.
C,
1, (execu*
table image) (image file).
. ,
4;
3.
3. ()
,
. ,
. Perl
, . Perl
, .
4.
, , : .
(run phase).
(
) . (,
,
, Perl.)
, ,
INIT FIFO.
.
, eval STRING, do FILE, require,
s///ee ,
.
1
,
, .
, , CPU
.
518
18.
END, LIFO.
, , .
( END , exec
.
.)
.
Perl :
, ,
. ,
, Perl
. ,
, Perl .
,
Perl, , Perl
,
. perl :
, . ( : perl
. ,
.)
(compile phase)
(compile time),
(run phase) (run time). Perl
, .
,
. , ,
, BEGIN.
, , ,
, eval STRING.
Perl
, . Perl
, ,
.1 ,
,
. :
1
,
perly.y Perl, yacc (1),
Perl. ,
, .
519
. 10 , Perl
.
, BEGIN, ,
, . *
(lexer)
. , ,
(lexemes, tokens).
tokener scanner. *
(parser) ,
, ,
Perl. (optimizer)
,
. ,
, , Perl
, .
, ,
.
, ,
. ( , (lexical scope)
, ,
.)
,
,
, , , ,
.
, Perl
, ,
,
. ,
, .
:
520
18.
,
, .
c, syntax OK
.
. *
(parse tree). , (node),
, (opco*
des) Perl, .
, , ,
,
.
Perl. , $a = ($b + $c)
, Perl , .
, . 18.2. ,
Perl.
Perl , .
( ,
.)
,
, . 1 2
,
;
3 , .
:
1. 1:
yacc(1)
, ,
(
521
). ,
, .
. 18.2,
, ,
( ).
, ,
. ,
. , ,
, ,
,
. ,
(constant folding), .
. ,
.
,
.
,
.
,
, .
,
, , . 18.2.
, .
2. 2:
Perl ( ),
, .
. , , :
.
,
, *
(context propagation).
, , (,
, , ),
. , ,
.
, .
3. 3:
,
. ( Perl
522
18.
scratchpad, .)
eval STRING, .
,
. ,
, , , ,
. ,
, Perl
(peephole optimizer).
,
,
, , ,
,
.
,
.
,
,
.
,
, .
, :
(bare) ,
, , , ,
, ,
.
4. 4:
, .
B::Bytecode, B::C B::CC,
.
Perl,
,
C,
.
C . B::C
runops(), Perl . B::CC
C
(
) .
Perl
. ,
. , ,
if (0) elsif else if (1).
523
. 18.3.
(constant folding).
, , ,
2**10 1024.
,
, ,
6 . Perl ,
,
FORTRAN
. 0.0
,
, , .1
, , , ,
:
if (2 * sin(1)/cos(1) < 3 && somefn()) { whatever() }
, .
, somefn,
whatever . ( ,
, :
.) somefn
1
. ,
.
, BEGIN.
524
18.
(inlinable constant) ,
:
if (somefn() && 2 * sin(1)/cos(1) < 3)) { whatever() }
,
. whatever ,
, :
,
.
: Useless use of a constant in void context.
, , . whatever
, ,
( ),
.
perl Dx. ( D
Perl ). . ,
B::Deparse.
, Perl ( )
, , ,
. ,
.
Sparc
Sparc, Intel Intel, Perl
Perl. Perl , Perl
:
, ,
. Perl ,
, , Perl.
,
Perl.
, ,
.
.
. , Perl (
PVM)1 . Perl
1
, ,
, PVM. . . Pneumonia Virus of Mice.
. .
525
PP codes (pushpop),
,
.
, Forth PostScript
HP1 ()
RPN (Reverse Polish Notation) , ,
. : 3 4,
3 4 + 3 + 4.
, 3, 4,
+ , 7
, ,
.
Perl Perl
.
Perl, . .
Perl. C, ?
. Perl
. Perl
, ,
, :
.
.
.
, ,
.
: ,
, .
caller.
, .
,
,
longjmp(3).
1
526
18.
jumpenv
longjmp(3),
.
, .
(mark stack)
,
.
.
, C, C. Perl
C
, longjmp(3)
.
,
, , ,
, . ,
C, , ,
, , ,
, ,
.
. .
, ,
,
, .
, ,
. ,
BEGIN
. ,
. ,
,
Perl, , .
,
,
, Perl.
.
? , ,
.
.
,
,
527
.
,
, .
17 .
Perl
Perl .
Perl ,
API C.1
Perl ,
, .
Apache mod_perl :
CGI mod_perl
Perl ,
, ,
. Apache Perl
.
, , . mod_perl
Writing Apache Modules with Perl and C (
Apache Perl C), OReilly, 1999.
, nvi, vim innd,
Perl .
, Perl, .
Perl ,
.
, Apache Perl
, ? Apache
Perl :
.
C API ,
. . Perl 21
, , Perl
C.
,
. ,
Perl, Perl,
(backends).
C.
1
: Perl 5.6.0
Microsoft Windows.
, , , Perl API ithreads,
.
528
18.
,
.
, . ,
,
, , .
backend ,
B::Bytecode, B::C B::CC. , ,
, B::Deparse, B::Lint B::Xref. (bac
kends)
,
Perl. backend CPAN,
( ) B::Fathom, B::Graph,
B::JVM::Jasmin B::Size.
Perl ,
, backend O
( O.pm). Backend ,
, , ,
(backend). , , B::Backend,
:
% perl MO=Backend SCRIPTNAME
backends , :
% perl MO=Backend, OPTS SCRIPTNAME
(backends) ,
, MO.
, perlcc(1) ,
.
,
(backends), Perl
, . (
, .)
, ,
,
CPU, .
. .
Bytecode
B::Bytecode
. Perl,
529
, ,
Perl.
, perlcc(1)
Perl
Perl. :
% perlcc b o pbyscript srcscript
pbyscript.
:
#!/usr/bin/perl
use ByteLoader 0.03;
^C^@^E^A^C^@^@^@^A^F^@^C^@^@^@^B^F^@^C^@^@^@^C^F^@^C^@^@^@
B^@^@^@^H9^A8M^?M^?M^?M^?7M^?M^?M^?M^?6^@^@^@^A6^@
^G^D^D^@^@^@^KR^@^@^@^HS^@^@^@^HV^@M2W<^FU^@^@^@^@X^Y@Z^@
...
,
. , ,
( dwimmer), .
(source filter),
, Perl.
,
. ,
, , cpp(1) m4(1):
. Perl,
Perl
. E perlibus unicode; cogito, ergo substr; carp dbm, et al. Caveat
scriptor .
ByteLoader ,
, B::By
tecode, .
Perl
. ,
, .
C
, B::C B::CC,
Perl, C.
, ,
. ,
C , Perl C,
. . 21.
B::C C,
Perl.
530
18.
, .
, B::Bytecode.
,
, B::Bytecode ,
Perl,
B::C C.
C Perl
Perl .
(, , ,
.) , ,
Perl, .
.
B::CC .
, B::C,1
. , B::C,
C, ,
, C, B::CC, ,
. C
, .
.
, .
. ,
.
,
, .
, .
O
.
Perl,
,
Perl.
B::Lint lint(1), C.
,
, .
:
% perl MO=Lint,all myprog
1
, . ,
!
531
,
, ,
( ), _.
. B::Lint(3).
B::Xref
( ,
),
. :
% perl MO=Xref myprog > myprof.pxref
:
Subroutine parse_argv
Package (lexical)
$on
i113, 114
$opt
i113, 114
%getopt_cfg
i107, 113
@cfg_args
i112, 114, 116, 116
Package Getopt::Long
$ignorecase
101
&GetOptions
&124
Package main
$Options
123, 124, 141, 150, 165, 169
%$Options
141, 150, 165, 169
&check_read
&167
@ARGV
121, 157, 157, 162, 166, 166
, parse_argv
; ,
main Getopt::Long. ,
: i ,
, , &
.
, $Options, %$Options.
B::Deparse ,
Perl ,
. , ,
Perl :
% perl MO=Deparse ne 'for (1 .. 10) { print if t }'
LINE: while (defined($_ = <ARGV>)) {
foreach $_ (1 .. 10) {
print $_ if t STDIN;
}
}
p , ,
Perl:
532
18.
q ,
:
% perl MO=Deparse,q e '"A $name and some @ARGV\n"'
'A ' . $name . ' and some ' . join($", @ARGV) . "\n";
, Perl
for while:
% perl MO=Deparse e 'for ($i=0;$i<10;$i++) { $x++ }'
$i = 0;
while ($i < 10) {
++$x;
}
continue {
++$i
}
B::Deparse Perl,
perlcc b,
. Perl ,
, , .
:
, , .
. Perl , ,
,
. Perl'
, , , ,
,
,
, Perl .
,
, ,
. ,
.
,
, . ,
, , ,
, .
, .
533
10 , AUTOLOAD
, . 12
DESTROY, , Perl
. 14
,
.
,
.
: BEGIN, CHECK, INIT END.
sub . ,
,
, .
, ,
, Perl , ,
. (
, shift pop ,
, @ARGV,
@_.)
:
BEGIN
ASAP (as soon as parsed
),
.
CHECK
, .
(CHECK ,
.)
INIT
.
END
.
,
, BEGIN CHECK,
INIT,
END,
. BEGIN INIT ,
(FIFO), CHECK END ,
(LIFO).
, , :
534
18.
#!/usr/bin/perl l
print
die
die
END
CHECK
INIT
END
BEGIN
INIT
BEGIN
CHECK
END
" main";
" main\n";
"XXX: \n";
{ print "1 END: " }
{ print "1 CHECK: " }
{ print "1 INIT: " }
{ print "2 END: " }
{ print "1 BEGIN: " }
{ print "2 INIT: " }
{ print "2 BEGIN: " }
{ print "2 CHECK: " }
{ print "3 END: " }
:
1 BEGIN:
2 BEGIN:
2 CHECK:
1 CHECK:
1 INIT:
2 INIT:
main
main
3 END:
2 END:
1 END:
BEGIN ,
, ,
. ,
, .
,
, .
,
, .
use BEGIN.
, END :
Perl, die
. ,
END ( DESTROY) . ,
exec. ,
, END. (. use
sigtrap 31 ,
.
535
16
.) END, POSIX::
_exit, , kill 9, $$, exec
/bin/true Unix.
END $? ,
. $? END,
. $?,
system .
END, , *
. , END
.
BEGIN
END. ,
BEGIN END, :
BEGIN { print "main begun" }
END { print "main ended" }
use Module;
:
BEGIN { print "module begun" }
END { print "module ended" }
, BEGIN ,
END . (, BEGIN , ,
, INIT
END .)
, , .
.
, Perl .
, ,
.
.
eval STRING
, BEGIN
.
. ,
BEGIN , ,
, , ,
. BEGIN
, . BEGIN
,
Perl. BEGIN
, ,
, .
536
18.
BEGIN INIT
Perl , (FIFO).
, , perlcc,
INIT XSUB. INIT
BEGIN, ,
, ,
. ,
.
,
. ,
.
END CHECK
Perl, , ,
(LIFO). CHECK ,
, END ,
. , CHECK
. ,
CHECK ,
, CHECK .
CHECK,
.
, . 18.1
, ,
....
18.1.
) )
)
use ...
C
no ...
C
BEGIN {...}
C
CHECK {...}
C
INIT {...}
C
END {...}
C
eval {...}
C
eval "..."
R
foo(...)
C
sub foo {...}
C
eval "sub {...}" R
s/pat/.../e
C
s/pat/"..."/ee
R
C
C
C
C
R
R
R
R
R
R
R
R
R
Inline
Inline
Inline
Inline
Inline
, ,
Perl .
19
, Perl
, . Perl,
.
.
.
, Perl Unix,
.
, ,
, . ,
, ,
. ,
MSDOS, Unix. , VMS,
Unix.
.
, Perl
.
538
19.
,
Perl ,
.
, ,
, (.
16 .
, Perl
use.
Perl .1
, (, )
.
, .
, , Perl
, , ,
, , .
Perl
.
Perl .
Perl .
Perl
:
1. e, :
% perl e "print 'Hello, World.'"
Hello, World.
2. .
, #!
, ,
.
3. .
;
, ,
2, . :
% echo "print qq(Hello, @ARGV.)" | perl World
Hello, World.
2 3 Perl
, x,
, #! perl,
. ,
1
, Unix ,
.
539
.
__END__.
x , #!
. ,
#! , ,
#! ,
, Perl,
x.
: Unix
#! 32 ,
, ;
, . ,
, ( )
32 . ,
,
, Perl ,
. I
.
, l 0. 32
( ), 0DIGITS
BEGIN{ $/ = "\0DIGITS"; }. , Unix,
.
#! ,
perl. *
emacs,
:
#!/bin/sh # * perl * p
eval 'exec perl S $0 ${1+"$@"}'
if 0;
Perl, ,
. Perl, perl5.6.1,
#!, env,
S #!.
#! perl, Perl
, #!. , ,
:
540
19.
#!/bin/sh
echo "I am a shell script"
#! , Unix
#!, Unix, :
Macintosh
Perl Macintosh Creator
Type, Perl .
MS*DOS
ALTERNATIVE_SHEBANG.
dosish.h,
Perl.
OS/2
:
extproc perl S your_switches
541
mysw ,
Perl. DCL,
perl program @program,
DCL$PATH, .
, Perl ,
perl "V:startperl". ,
.
Win??
Perl ActiveState
Microsoft Windows ( Win95,
Win98, Win00,1 WinNT, Win3.1), Perl
Windows, .pl
Perl.
Perl, Win32
Perl, Windows .
, .pl ,
Perl
perl. , Perl
.plx.
, Perl .pm.
, Unix,
. ,
( *,
\ ") ,
e.
% %%
, .
. Unix Plan9,
Unix, MKS Toolkit Cyg
win, Cygnus, Redhat.
Unix Interix, Microsoft,
.
, Unix Mac OS X :
% perl e 'print "Hello world\n"'
Macintosh ( Mac OS X) :
print "Hello world\n"
Myscript <Shift>+<Com
mand>+<R>.
1
...
542
19.
VMS:
$ perl e "print ""Hello world\n"""
qq//:
$ perl e "print qq(Hello world\n)"
MSDOS :
A:> perl e "print \"Hello world\n\""
qq// :
A:> perl e "print qq(Hello world\n)"
, :
. 4DOS,
:
perl e "print <Ctrlx>"Hello world\n<Ctrlx>""
Perl
, Perl ,
. , , /usr/
bin/perl /usr/local/bin/perl
. ,
Perl
, PATH
.
1
Mac OS X, , ,
BSD.
543
#!/usr/bin/perl
,
. Perl,
:
#!/usr/local/bin/perl5.6.0
,
,
:
use v5.6.0;
(: Perl , 5.005
5.004_05. 5.5.0 5.4.5, Perl
5.6.0 .)
, .
#!/usr/bin/perl spi.bak
# same as s p i.bak
00 Perl ,
$/ "". 0777
. ,
$/ . 0777, ASCII
. ( , Unicode
: \N{LATIN SMALL LETTER O WITH STROKE AND ACUTE}
o , ,
.)
544
19.
a , n
p. while, n p,
split @F.
% perl ane 'print pop(@F), "\n";'
:
LINE: while (<>) {
@F = split(' ');
print pop(@F), "\n";
}
, split F.
, :
% awk F: '$7 && $7 !~ /^\/bin/' /etc/passwd
% perl F: lane 'print if $F[6] && $F[6] !~ m(^/bin)' /etc/passwd
c Perl , ,
, .
: BEGIN CHECK, use,
, .
INIT END . ,
, :
BEGIN { $^C = 0; exit; }
C Perl API
, (
5.6.0, Microsoft).
${^WIDE_SYSTEM_CALLS} .
d Perl. . 20 Perl.
d:MODULE
, Perl Devel::MODULE. ,
d:DProf Devel::DProf.
. 20.
DLETTERS
DNUMBER
. (, Perl
, .)
, ,
. , , ,
D14 Dslt. D1024
Dx, . D512
Dr .
545
$^D.
. 19.1.
19.1. D
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
p
s
l
t
o
c
P
m
f
r
x
u
L
8192
16384
32768
65536
H
X
D
S
/
P
( DLEAKTEST
Perl)
values()
Perl,
. Perl
,
D, Perl
.
INSTALL Perl,
, C DDEBUGGING
Perl. ,
g, Configure
.
Perl ( , sh x
), Perl D .
:
#
$
#
%
(bash)
PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl dS program
csh
(setenv PERLDB_OPTS "NonStop=1 AutoTrace=1 frame=2"; perl dS program)
20.
546
19.
e PERLCODE
.
e, Perl .
PERLCODE ,
, e,
. (
, ,
.) , e
, , e,
,
sh, ksh bash,
e:
$ perl e 'print "Howdy, ";
print "@ARGV!\n";' world
Howdy, world!
csh e:
% perl e 'print "Howdy, ";' \
e 'print "@ARGV!\n";' world
Howdy, world!
, ,
print 2 e.
FPATTERN
, a
( ).
(//), ("")
('').
. ,
,
.
h Perl.
iEXTENSION
i , , <>,
.
,
print, printf write.1
EXTENSION
. EXTENSION ,
. EXTENSION
1
. ,
.
547
*,
. EXTENSION *,
*
. Perl :
($backup = $extension) =~ s/\*/$file_name/g;
:
% perl pi'orig_*' e 's/foo/bar/' xyx
# 'orig_xyx'
( , ):
% perl pi'old/*.orig' e 's/foo/bar/' xyx # 'old/xyx.orig'
:
%
%
%
%
perl
perl
perl
perl
#
#
# 'xyx.orig'
# 'xyx.orig'
:
% perl p i.orig e "s/foo/bar/;"
, :
#!/usr/bin/perl pi.orig
s/foo/bar/;
:
#!/usr/bin/perl
$extension = '.orig';
LINE: while (<>) {
if ($ARGV ne $oldargv) {
if ($extension !~ /\*/) {
$backup = $ARGV . $extension;
}
else {
($backup = $extension) =~ s/\*/$ARGV/g;
}
unless (rename($ARGV, $backup)) {
warn "cannot rename $ARGV to $backup: $!\n";
close ARGV;
next;
}
open(ARGVOUT, ">$ARGV");
select(ARGVOUT);
$oldargv = $ARGV;
548
19.
}
s/foo/bar/;
}
continue {
print; #
}
select(STDOUT);
i, , i $ARGV
$oldargv, , .
ARGVOUT
STDOUT
. , Perl
, .
eof eof
,
.
Perl ,
EXTENSION,
.
i
. ~
, ,
:
% perl pi~ e 's/foo/bar/' file1 file2 file3...
, i Perl,
.
, ,
, , STDIN STDOUT.
IDIRECTORY
, I, @INC,
. I
C, . C
P; /usr/include /usr/
lib/perl. C
( ),
use lib. , use lib, I
, .
use lib . 31 .
lOCTNUM
l .
: ,
n p, , $\
549
OCTNUM, print
ASCII, OCT
NUM. OCTNUM , l $\
$/, . ,
80 , :
% perl lpe 'substr($_, 80) = ""'
, $\ = $/
,
, l 0:
% gnufind / print0 | perl ln0e 'print "found $_" if p'
$\ ,
$/ . ( ,
0 l,
, n.)
m M
MODULE, use,
MODULE MODULE, no. , Mstrict
use strict, Mstrict no strict.
mMODULE
use MODULE () .
MMODULE
M'MODULE...'
use MODULE, .
M, ,
, M'MODULE qw(foo bar)'.
MMODULE=arg1, arg2...
, Mmodu
le=foo,bar M'module qw(foo bar)'.
.
Mmodule=foo,bar :
use module split(/,/, q{foo,bar})
, = m M,
.
M m
Perl , , #!.
( ,
use no ?)
550
19.
n Perl ,
,
sed n awk:
LINE:
while (<>) {
...
}
LINE , ,
.
: .
. p. ,
:
find . mtime +7 print | perl nle unlink
, exec find(1),
. : BEGIN
, awk.
p Perl ,
, sed:
LINE:
while (<>) {
...
#
}
continue {
print or die "p destination: $!\n";
}
LINE , ,
.
, ,
, Perl
. , .
, , .
: BEGIN END
, awk.
P C Perl.
( cpp(1) #,
,
C, if, else define.)
P Perl #line,
,
Perl . . Perl
24 .
551
s ,
,
.
@ARGV, Perl ,
. ,
, .
"true"
foo.
#!/usr/bin/perl s
if ($foo) { print "true\n" }
xxx=yyy, $xxx
,
( "yyy "). "true"
, foo=bar.
#!/usr/bin/perl s
if ($foo eq 'bar') { print "true\n" }
S Perl
PATH ( , ).
#! , #!.
, , C,
:
#!/usr/bin/perl
eval "exec /usr/bin/perl S $0 $*"
if $running_under_some_shell;
/bin/
sh, Perl .
Perl. $0
, S Perl,
. Perl
, ,
$running_under_some_shell true.
, $*, ${1+"$@"},
. ., ,
csh. sh,
csh, #! ,
, Perl .
(
) , csh,
sh, perl, :
eval '(exit $?0)' && eval 'exec /usr/bin/perl S $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl S $0 $argv:q' if 0;
552
19.
, , , 1
.
S Perl
, . ,
Win32 .bat .cmd,
,
. Perl ,
, , Perl Dp.
(
, ) ,
, ( Unix),
.
DOS ,
, ,
PATH. Unix
PATH ,
,
.
T (taint checks),
.
setuid setgid.
, ,
CGI. . 23 .
, Perl
; ,
#!.
, Perl .
u Perl .
undump ( ).
(
(stripping)
). , ,
Perl dump. :
undump ;
Perl Unix .
Perl C,
( ).
U Perl .
setuid,
1
553
. ,
,
.
v Perl,
.
V Perl
@INC.
V:NAME
STDOUT . NAME
, .
.*
.
% perl V:man.dir
man1dir='/usr/local/man/man1'
man3dir='/usr/local/man/man3'
% perl V:'.*threads'
d_oldpthreads='undef'
use5005threads='define'
useithreads='undef'
usethreads='define'
"UNKNOWN".
Config,
:
% perl MConfig le 'print $Config{man1dir}'
/usr/local/man/man1
. Config 32 .
w ,
, ,
.
, ,
.
, ,
, 100
. , (W) 33
.
$^W.
.
W X.
use warnings, 31.
554
19.
W
, no warnings
$^W = 0. , use,
require do. Perl
lint(1).
xDIRECTORY
x Perl , . ,
, , ,
#! "perl".
"perl" .
, Perl .
x , ,
. __END__ __DATA__,
, . (
, ,
DATA.
, .)
X
, W.
,
Perl, ,
.
, , ,
sh, ksh bash, ,
,
.
:
$ PATH='/bin:/usr/bin' perl myproggie
csh tcsh:
% (setenv PATH "/bin:/usr/bin"; perl myproggie)
.chsrc .profile,
. csh tcsh :
% setenv PATH '/bin:/usr/bin'
555
. ,
Perl:
HOME
, chdir .
LC_ALL, LC_CTYPE, LC_COLLATE, LC_NUMERIC, PERL_BADLANG
, , Perl
, . .
perllocale.
LOGDIR
Perl , chdir
, HOME .
PATH
,
S.
PERL5LIB
,
Perl,
.
,
.
PERL5LIB PERLLIB.
( setuid set
gid T) , ,
.
use lib.
PERL5OPT
, .
,
Perl. [DIMUdmw].
( setuid setgid,
T) . PERL5OPT
T, ,
.
PERL5DB
. :
BEGIN { require 'perl5db.pl' }
. 20
.
556
19.
PERL5SHELL ( Microsoft)
(),
Perl
system. cmd.exe /x/c WinNT
command.com /c Win95. ,
. ,
(, ),
.
, Perl COM
SPEC,
, .
, Perl ,
, COMSPEC
(
COMSPEC , ).
PERLLIB
,
Perl,
. PERL5LIB, PERLLIB
.
PERL_DEBUG_MSTATS
, Perl
malloc (. . perl V:d_mymalloc "define").
,
.
, ,
.
PERL_DESTRUCT_LEVEL
, Perl ,
.
Perl , ,
. , ,
. , use re
PERL_RE_TC PERL_RE_COLORS, Cwd PWD,
CGI ,
HTTP (. . ) CGI.
, setuid, ,
, :
$ENV{PATH} = '/bin:/usr/bin';
#
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
. 23.
20
Perl
, use warnings?
Perl d,
Perl. Perl,
, ,
, ,
. . ,
, ( eval)
Perl . (
DB
, .)
, ,
Perl. ,
Perl ,
:
% perl de 42
Perl , ,
,
. d
,
. ,
, .
,
Perl, .
% perl d /path/to/program
558
20. Perl
( .
) .
, ,
, , .
,
, ( ),
( ,
) , ,
.
.
, , :
DB<8>
:
DB<<17>>
.
csh
. , !17 17.
. ,
, ,
,
.
, ,
,
, ,
. :
DB<1> for (1..3) {
\
cont:
print "ok\n";
\
cont: }
ok
ok
ok
, (
camel_flea) , infested.
:
% perl d camel_flea
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
559
main::(camel_flea:2):
DB<1>
pests('bactrian', 4);
(. )
. , ,
, ,
, , .
, ,
.
,
infested, :
DB<1> b infested
DB<2> c
,
, :
main::infested(camel_flea:8):
, ,
w:
DB<2> w
5
}
6
7
sub infested {
8==>b
my $bugs = int rand(3);
9:
our $Master;
10:
contaminate($Master);
11:
warn "needs wash"
12
if $Master && $Master>isa("Human");
13
14:
print "got $bugs\n";
DB<2>
==>, 8, b
, .
, a. ,
, ,
.
,
T:
DB<2> T
$ = main::infested called from file `Ambulation.pm line 4
@ = Ambulation::legs(1, 2, 3, 4) called from file `camel_flea' line 5
. = main::pests('bactrian', 4) called from file `camel_flea' line 2
($, @, .) ,
, , .
560
20. Perl
, ,
.
:
, main::infes
ted, . ,
4 Ambulation.pm.
, ,
. . &infested infested().
, Ambulation::legs
5 camel_flea
.
, main::pests
2 camel_flea.
,
BEGIN CHECK use,
, require INIT,
(. 18 ).
, AutoTrace
PERLDB_OPTS.
Perl
Perl. , ,
,
. Perl
, , ,
:
$DB::single = 1;
$DB::single 2, n,
1 s.
t, $DB::trace 1.
load:
DB<7> b load c:/perl/lib/Carp.pm
Will stop on load of `c:/perl/lib/Carp.pm'.
R.
b compile subname,
.
. (
).
561
eval, my,
our local .
, ,
, ; +.
,
, :
DB<1> |h
, (
) ,
, , , ,
, , .
, h, .
h h,
, . h COM
MAND, .
, .
,
.
s
s EXPR
s .
, ,
, .
,
. EXPR,
, .
n
n EXPR
n , ,
( ).
EXPR, ,
.
<ENTER>
<Enter>
, n s.
. .
.
562
20. Perl
r
. ,
PrintRet, .
b
b
b
b
b
b
b
b
b
b
LINE
CONDITION
LINE CONDITION
SUBNAME
SUBNAME CONDITION
postpone SUBNAME
postpone SUBNAME CONDITION
compile SUBNAME
load FILENAME
b
LINE,
.
LINE , ,
. CONDITION,
, :
, CONDITION .
.
, if:
b 237 $x > 30
b 237 ++$count237 < 11
b 33 /pattern/i
b SUBNAME (,
) . SUBNAME
, , CONDITION
.
,
. b
(, ) SUBNAME
, .
b ,
SUBNAME. ,
postpone,
, ,
.
b load
. FILENAME
, %INC.
563
d
d LINE
LINE;
, ,
.
D .
L .
c
c LINE
,
LINE.
T .
t
t EXPR
,
. .
AutoTrace . EXPR,
. .
.
W
W EXPR
EXPR
(watch expressions). ,
. EXPR ,
.
Perl ,
, .
p
p EXPR
, DB::OUT EXPR
. , print,
Perl,
, x. DB::OUT
( ) ,
.
564
20. Perl
x
x EXPR
x
. ,
,
.
V
V PKG
V PKG VARS
( , VARS)
PKG (, , main),
. ,
,
. . x
, , x
. ,
, $ @, :
V Pet::Camel SPOT FIDO
, ,
.
l
l
l
l
l
LINE
SUBNAME
MIN+INCR
MINMAX
565
l
LINE SUBNAME,
.
MIN+INCR INCR+1 , MIN. l MIN
MAX MIN MAX.
.
w
w LINE
( ), LINE,
, , LINE .
f FILENAME
eval.
FILENAME ,
%INC,
.
/PATTERN/
PATTERN
; / .
PATTERN: , .
?PATTERN?
PATTERN
; ? . PATTERN
, .
S
S PATTERN
S !PATTERN
S , (,
!, ) PATTERN. PATTERN
, .
, ,
.
.
a
a COMMAND
a LINE
a LINE COMMAND
,
LINE ,
566
20. Perl
. , $foo
53:
a 53 print "DB FOUND $foo\n"
COMMAND , LINE
. LINE, COMMAND,
.
A A .
<
< ?
< EXPR
<< EXPR
< EXPR Perl,
.
, << EXPR,
< ? <.
>
> ?
> EXPR
>> EXPR
> <,
, .
{
{ ?
{ COMMAND
{{ COMMAND
{ <,
Perl, ,
. ,
. , ,
;{ ... } do { ... }.
!
! NUMBER
! NUMBER
! PATTERN
! . NUMBER ,
; , ! 3
, . NUMBER
, : ! 3
.
567
PATTERN ( ), ,
. . recallCommand.
!! CMD
CMD,
DB::IN DB::OUT.
. shellBang.
, $ENV{SHELL},
, . ,
,
$ENV{SHELL} /bin/sh.
|
|DBCMD
||PERLCMD
|DBCMD DBCMD, DB::OUT
$ENV{PAGER}. ,
, :
DB<1> |V main
, , ,
.
who more, :
DB<1> !!who | more
||PERLCMD |DBCMD, , ,
DB::OUT, , print, printf write
, . ,
,
print,
:
DB<1> sub saywho { print "Users: ", `who` }
DB<2> ||saywho()
q ^D
.
,
exit. inhibit_exit 0,
.
$DB::finished,
.
R , .
,
568
20. Perl
.
: ,
, ,
Perl w, I e.
=
= ALIAS
= ALIAS VALUE
ALIAS,
VALUE. ,
ALIAS. ALIAS, VALUE,
, :
= quit q
ALIAS
.
%DB::aliases.
. .
man
man MANPAGE
, MANPAGE .
man, %Con
fig. perl,
man debug man op.
, man,
perldoc; ,
$DB::doccmd , .
rc .
O
O OPTION ...
O OPTION? ...
O OPTION=VALUE...
O ,
.
O OPTION 1. OPTI
ON , .
O OPTION=VALUE ;
VALUE ,
. , O pager="less MQeicsNfr", less
.
, ,
569
,
.
( ),
,
. ,
,
.
, :
DB<1> O OPTION='this isn\'t bad'
OPTION = 'this isn\'t bad'
DB<2> O OPTION="She said, \"Isn't it?\""
OPTION = 'She said, "Isn\'t it?"'
=VALUE , ,
, ,
, . . .
VALUE =. OPTION ,
, , ,
.
. .
,
. ,
O,
PERLDB_OPTS
, rc.
, :
^p
^a, ,
, .
Term::ReadKey Term::ReadLine, CPAN,
,
GNU readline (3).
emacs,
Perl
, emacs
C. Perl ,
emacs ,
, () Perl.
emacs Perl. vi
570
20. Perl
vim (
gvim), Perl.
()
vi
X11.
, emacs,
. ,
Perl,
.
, .perldb
perldb.ini ( ),
.
Perl, , ,
PERLDB_OPTS. ,
, %DB::alias:
$alias{len}
$alias{stop}
$alias{ps}
$alias{quit}
$alias{help}
=
=
=
=
=
's/^len(.*)/p length($1)/';
's/^stop (at|in)/b/';
's/^ps\b/p scalar /';
's/^quit(\s*)/exit/';
's/^help\s*$/|h/';
,
API :
parse_options("NonStop=1 LineInfo=db.out AutoTrace=1 frame=2");
afterinit,
.
. Perl,
(root) ,
.
, perl5db.pl
Perl , .
PERL5DB
:
BEGIN { require "myperl5db.pl" }
PERL5DB,
. , ,
,
571
,
O , .
recallCommand, ShellBang
,
. !.
pager
, ,
(
"|"). $ENV{PAGER}.
,
escape ,
.
tkRunning
Tk ( ReadLine).
signalLevel, warnLevel, dieLevel
.
,
.
,
, , 0. 1
(
) ( ).
,
. dieLevel 1,
, eval ,
. dieLevel 2,
, :
, ,
. , ,
, , ,
,
.
572
20. Perl
INT, BUS SEGV.
( wait, accept read )
$SIG{INT},
<Ctrl>+<C>,
$SIG{INT}, , ,
, longjmp(3)
.
AutoTrace
( t,
PERLDB_OPTS).
LineInfo
,
. (, |visual_perl_db),
.
,
(hooks) vi emacs
ddd.
inhibit_exit
0, .
PrintRet
( ), ,
r.
ornaments
(.
Term::ReadLine).
(ornaments),
.
.
frame
. frame & 2, .
( ,
.)
frame & 4 ,
. frame & 8,
stringify tied FETCH
. & 16, ,
.
, ,
.
573
maxTraceLen
,
4 frame.
V, X x:
arrayDepth, hashDepth
n . n ,
.
compactDump, veryCompact
. com
pactDump, .
globPrint
typeglobs.
DumpDBFiles
, .
DumpPackages
.
DumpReused
.
quote, HighBit, undefPrint
. quote
auto;
, " ', .
.
UsageOnly
,
,
. ,
.
$ENV{PERLDB_OPTS} TTY,
noTTY, ReadLine NonStop.
.
:
parse_options("NonStop=1 LineInfo=tperl.out AutoTrace");
574
20. Perl
,
db.out. ( ,
LineInfo /dev/tty, .)
.
, parse_options("OPT=VAL").
TTY
, .
noTTY
, NonStop.
(
$DB::signal $DB::single Perl)
, TTY
Term::Rendezvous.
new,
: IN OUT.
. new ,
$ENV{PERLDB_NOTTY} , "/tmp/perldbtty$$".
,
.
ReadLine
,
ReadLine, ,
ReadLine.
NonStop
,
,
$DB::signal $DB::single.
,
,
.
PERLDB_OPTS
.1
.
tperl.out.
.
$ PERLDB_OPTS="NonStop frame=1 AutoTrace LineInfo=tperl.out" perl d myprog
1
sh
.
.
575
, O LineInfo=/dev/
tty ,
.
Perl
, , ,
.
perl D, , Perl
DDEBUGGING.
, Perl caller
DB ,
, @DB::args. Perl d
:
Perl
$ENV{PERL5DB} ( BEGIN {require 'perl5db.pl'}, ).
@{"_<$filename"} $filename ,
Perl. , eval,
, .
$filename eval (eval 34).
(re_eval 19).
%{"_<$filename"} ,
.
. Perl
, , perl5db.pl,
"$break_condition\0$action". (magic)
: ,
. ,
.
$filename , eval, (eval 34)
(re_eval 19).
${"_<$filename"} "_<$filename".
,
. $filename , eval,
(eval 34) (re_eval 19).
, require,
, DB::postponed(*{"_<$filename"}),
DB::postponed. $filename
%INC.
subname $DB::postpo
ned{subname}. DB::postponed
, DB::postponed(subname).
576
20. Perl
%DB::sub,
, filename: startlineendline. filename
(eval 34) , eval,
(re_eval 19) ,
.
,
, DB::DB(),
$DB::trace, $DB::single $DB::signal
. . ,
DB::DB(),
, $^D & (1<<30).
,
&DB::sub(args), $DB::sub
. ,
DB.
: &DB::sub ,
.
$DB::deep
(
).
:
sub DB::DB {}
, ,
PERL5DB:
% PERL5DB="sub DB::DB {}" perl d yourprogram
, , :
sub DB::DB {print ++$i; scalar <STDIN>}
.
, , :
{
package DB;
sub DB {}
sub sub {print ++$i, " $sub\n"; &$sub}
}
577
. , &DB::sub DB,
.
,
, .
.
PERLDB_OPTS
O ...,
.
,
@DB::dbline, %DB::dbline, @{":::_<
current_file"} %{"::_<current_file"}. current_file ,
f,
.
. DB::parse_options(STRING)
O. DB::dump_trace(SKIP [,COUNT])
,
, COUNT.
"context" (., $ @), "sub"
( eval), "args" (undef
), "file" "line". DB::print_trace(FH, SKIP [,COUNT [,SHORT]])
.
"<" "<<".
. ,
,
.
, ,
. ,
( , ).
, , d
,
, , .
, , , ,
:
(bugs) , Perl.
, .
.
, ()
?.
578
20. Perl
Perl
, ? , , .
: ,
?
,1 .
,
. (
.) ,
, .
, ,
, ,
.
Perl , Devel::DProf.
Perl mycode.pl, :
perl d:DProf mycode.pl
DProf ,
, ,
. DProf ,
, Perl
.
, DProf
tmon.out. dprofpp,
Perl, tmon.out .
dprofpp
p, .
:
outer();
sub outer {
for (my $i=0; $i < 100; $i++) { inner() }
}
sub inner {
my $total = 0;
for (my $i=0; $i < 1000; $i++) { $total += $i }
}
inner();
dprofpp :
Total Elapsed Time = 0.537654 Seconds
User+System Time = 0.317552 Seconds
1
, ,
.
Perl
579
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
85.0 0.270 0.269
101 0.0027 0.0027 main::inner
2.83 0.009 0.279
1 0.0094 0.2788 main::outer
, 100.
, ,
, .
,
, .
100 1000, :
Total Elapsed Time = 2.875946 Seconds
User+System Time = 2.855946 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
99.3 2.838 2.834 1001 0.0028 0.0028 main::inner
0.14 0.004 2.828
1 0.0040 2.8280 main::outer
,
, . : ,
(user), ,
,
(system). (
:
,
.)
user+system
dprofpp. r , s
u .
. Exclusive Times , outer
inner, , inner,
, outer.
inner outer, dprofpp I<.
: %Time
, ; ExclSec
, ,
; CumulS ,
; #Calls
; sec/call
, , ; Csec/c
, .
%Time,
, .
inner,
, () . :)
580
20. Perl
dprofpp
. dprofpp
, d:Dprof :
p SCRIPT
dprofpp
SCRIPT, . . Q.
Q p dprofpp
.
a
, .
R ,
.
main::__ANON__.
I
.
l .
(inlining).
O COUNT
COUNT . 15.
q .
T .
.
t .
. ,
()
.
S :
main::inner x 1
main::outer x 1
main::inner x 100
0.008s
0.467s = (0.000 + 0.468)s
0.468s
: inner
0,008 , outer
0,467 ,
(0 outer 0,468 , outer)
inner ( 0,468 ) 100 . , ?
(, inner
outer) .
U .
.
Perl
581
v
.
(inlining) .
g subroutine
, subroutine .
dprofpp(1),
.
DProf .
CPAN Devel::SmallProf, ,
.
Perl,
. ,
,
.
24 .
.
.
21
18 , perl ()
, , Perl
(). / Perl C.
, C ,
Perl . *
Perl, C, (extension)
Perl, (embedding) Perl
Perl1 C.
, ,
Perl: .
perlguts, perlxs, perlxstut, perlcall, perlapi h2xs, Perl.
, , Perl,
.
, ,
Perl.
C.
1
,
, /
,
, C ,
perl (); Perl
,
perl.
583
C.
, ,
C.
Unix . ,
Perl.
, .
Perl
Perl Perl,
:
, (tokens).
:
print "Hello, world!\n";
: print, "Hello,
world!\n" .
(parsing) ,
. Perl
, . (
. , ,
, new Critter
, Critter
new. ,
, .)
,
, (opco*
des), , , ,
Perl c (
), .
, , BEGIN,
CHECK use.
,
Perl, Perl ,
. Perl
C, Perl,
.
(typedef) C
Perl: SV ( ), AV ( ) HV (
). , IV ,
, I32 I16
584
21.
, 32 16
.
UV, U32 U16.
C, perlguts.
:
, SV: (IV),
(NV), (PV)
(SV). SV , ,
, ,
Perl. Perl RV, SV.
AV SV,
, .
HV C , ,
/ , HV.
GV ( glob),
, :
, , , , /
.
Perl
C. Perl
Perl, C.
Perl ( C Perl)
C C Perl
,
perl . (
,
perl.) XS (
.xs), .
Perl, XS
XSUB ( eXternal SUBroutine ).
XSUB C ,
C, XS, C
XSUB. xsubpp, Perl,
XS C, ,
Perl.
,
Perl, ,
, 100% Perl,
C.
Perl, Perl ,
C ( XSUB),
C , , ,
Perl ( C Perl)
585
, Perl. Perl
Perl ,
Perl. (
, ,
.)
,
. Perl ,
C, XSUB
, ,
, undef ,
C
.
perlxs perlxstut.
XS XSUB
XS :
C
Perl. ,
C,
Perl . XS
, , XS
xsubpp .
, XS ,
SWIG (Simplified Wrapper and Interface Generator)
XSUB.
http://www.swig.org.
XS C Perl.
Perl,
C, . XS
C Perl,
XSUB C.
h2xs ( Perl)
C XS,
C.
xsubpp , XSUB
Perl, , Perl XSUB.
XS C, ,
#include.
MODULE
XS XS XSUB.
XS ,
XSUB, Perl
C sin(3). XSUB (
, ),
586
21.
( ,
x), ( ):
double
sin(x)
double x
XSUB XS.
XSUB , ,
, INIT: CLEANUP:. XSUB
: ,
. , ,
INPUT:, .
perlxs,
, XSUB.
, XSUB ,
XSUB Perl, Perl.
XSUB , ,
, .
, . (
,
.)
,
,
make perl make.
make test_static make test.
xsubpp ,
Perl C. ,
, , ,
, ty*
pemap. , ,
PERLLIB/ExtUtils/typemap.
typemap . , TYPE
MAP, ,
C Perl. , INPUT, C,
Perl C. , OUTPUT,
C, C Perl.
:
XS, Perl
Perl ( C Perl)
587
, XS.
, h2xs,
, :
h2xs A n Mytest
Mytest, ,
ext/, . My*
test : MANIFEST, Makefile.PL, Mytest.pm, My*
test.xs, test.pl Changes. .
MANIFEST
MANIFEST ,
Mytest.
,
. ,
.
Makefile.PL
Perl, Makefile ( make
). Makefile.PL
CPAN 22 CPAN.
Mytest.pm
. ,
h2xs:
package Mytest;
use strict;
use warnings;
require Exporter;
require DynaLoader;
our @ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# [:
# ,
# . :
# ,
# EXPORT_OK.
# //.]
our @EXPORT = qw(
);
our $VERSION = '0.01';
bootstrap Mytest $VERSION;
# Preloaded methods go here.
588
21.
# [:
# (preloaded) .]
#
#
#
#
Autoload methods go after __END__, and are processed by the autosplit program.
[:
__END__
autosplit.]
1;
__END__
#
#
#
#
Below is the stub of documentation for your module. You better edit it!
[:
.
!]
Exporter
DynaLoader. @ISA ( ) @EXPORT (
, )
Perl
XS. Perl
perl .
Mytest.xs
Mytest.xs XSUB, Perl ,
C. My*
test.xs :
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = Mytest
PACKAGE = Mytest
XS, :
void
hello()
CODE:
printf("Hello, world!\n");
make (
, ):
% make
umask 0 && cp Mytest.pm ./blib/Mytest.pm
Perl ( C Perl)
589
, Perl
make.
make make,
Perl. , , :
% perl V:make
make blib (
build library).
. , ,
,
. blib Perl
@INC ExtUtils::testlib. , hello
:
use ExtUtils::testlib;
use Mytest;
Mytest::hello();
# blib/* @INC
Perl C:
% perl hello
Hello, world!
, make install.
Perl . (
,
CPAN 22.)
XSUB
, XSUB,
1, , 0,
:
int
is_even(x)
int x
CODE:
590
21.
RETVAL = (x % 2 == 0);
OUTPUT:
RETVAL
,
OUTPUT:. RETVAL Perl,
, XSUB.
, x
RETVAL.
, , , Makefile Makefi*
le.PL make.
, ,
test.pl. h2xs
, Perl.
, ,
ok, , not ok, . print
BEGIN test.pl print "1..4\n";
:
print Mytest::is_even(0) == 1 ? "ok 2" : "not ok 2", "\n";
print Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n";
print Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n";
make test.
C
C
XS.
C:
void
round(arg)
double arg
CODE:
if (arg > 0.0) {
arg = floor(arg + 0.5);
} else if (arg < 0.0) {
arg = ceil(arg 0.5);
} else {
arg = 0.0;
}
OUTPUT:
arg
, round, ,
,
.
591
Perl C
floor(3) ceil(3) C.
C
, lm;
LIBS Makefile.PL:
'LIBS' => ['lm'],
=
=
=
=
=
? "ok 5"
? "ok 6"
"ok 7" :
"ok 8" :
"ok 9" :
make test ,
.
Perl perlxstut
Perl, h2xs
C, Perl.
Perl C
Perl C Perl
C. Perl C,
, Perl
.
, ,
Perl
.
C Perl
, popen(3) C, ,
Perl open(PIPE, "| program") IPC::Open2
IPC::Open3 Perl
. ,
, C.
, (,
),
.
, Perl
. Perl Apache
mod_perl Perl ,
Apache. perlem*
bed , ,
Perl
( ).
592
21.
Perl C C ,
PerlInterpreter,
struct C, libperl,
Perl . ,
libperl ( EXTERN.h perl.h,
), .
:
% perl MConfig e "print $Config{archlib}"
,
perl. , ,
C Perl . :
% perl MConfig e "print $Config{cc}"
, ,
ExtUtils::Embed. C
interp.c, C cc,
:
% cc o interp interp.c `perl MExtUtils::Embed e ccopts e ldopts`
Perl C
, perl (C) Perl
(),
miniperlmain.c, Perl.
miniperlmain.c, :
#include <EXTERN.h>
#include <perl.h>
*/
*/
***/
interp Perl:
% interp e "printf('%x', 3735928559)"
deadbeef
Perl C
593
Perl, ,
argv[1] perl_run.
Perl C
Perl ,
C, Perl,
, call_
perlcall,
. , Perl showtime.pl:
print " .";
sub showtime {
print time;
}
call_argv
showtime C showtime.c:
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
int main(int argc, char **argv, char **env)
{
char *args[] = { NULL };
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, NULL, argc, argv, NULL);
/*** perl_run() ***/
call_argv("showtime", G_DISCARD | G_NOARGS, args);
perl_destruct(my_perl);
perl_free(my_perl);
}
, showtime Perl,
( G_NOARGS)
( G_DIS
CARD). perlcall.
showtime :
% cc o showtime showtime.c `perl MExtUtils::Embed e ccopts e ldopts`
% showtime showtime.pl
963852741
perl_run,
, DE
STROY END.
594
21.
Perl
NULL args, call_argv.
,
Perl. ,
perlcall, Perl.
Perl C
Perl Perl,
eval_sv eval_ pv, perlapi.
, ,
Perl C.
,
use, require do Perl.
eval_ pv Perl
C. , string.c,
Perl; int, float
char*:
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
main (int argc, char **argv, char **env)
{
STRLEN n_a;
char *embedding[] = { "", "e", "0" };
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, NULL, 3, embedding, NULL);
perl_run(my_perl);
/** $a **/
eval_pv("$a = 3; $a **= 2", TRUE);
printf("a = %d\n", SvIV(get_sv("a", FALSE)));
/** $a **/
eval_pv("$a = 3.14; $a **= 2", TRUE);
printf("a = %f\n", SvNV(get_sv("a", FALSE)));
/** $a **/
eval_pv("$a = 'relreP kcaH rehtonA tsuJ'; $a = reverse($a);", TRUE);
printf("a = %s\n", SvPV(get_sv("a", FALSE), n_a));
perl_destruct(my_perl);
perl_free(my_perl);
}
, Sv, Perl C.
perlguts perlapi.
,
SvIV int, SvNV float SvPV C:
Perl C
595
a = 9
a = 9.859600
a = Just Another Hack Perler
.
(, , ) , eval_pv,
:
SV *val = eval_pv("reverse 'relreP kcaH rehtonA tsuJ'", TRUE);
printf("%s\n", SvPV(val,n_a));
perlembed, Perl,
eval_sv, ,
Perl C.
Perl C
, ,
1
: , , ,
. : C
Perl, ,
, ,
Perl, .
.
, , ,
C Perl newSViv sv_setnv, ne
wAV, , perlguts perl*
api. perlcall, ,
Perl.
C
, Perl ** C. (
, , Perl **
C pow(3).)
power.p l:
sub expo {
my ($a, $b) = @_;
return $a ** $b;
}
C, power.c, PerlPower,
, expo
:
#include <EXTERN.h>
#include <perl.h>
1
Perl.
596
21.
/*
/*
/*
.*
/*
/*
/*
/*
call_pv("expo", G_SCALAR);
/*
SPAGAIN;
/*
/*
printf ("%d to the %dth power is %d.\n", a, b, POPi);
PUTBACK;
FREETMPS;
/*
LEAVE;
/* ... "" XPUSHed
*/
*/
*/
*.
*/
*/
*/
*/
*/
*/
*/
*/
*/
}
int main (int argc, char **argv, char **env)
{
char *my_argv[] = { "", "power.pl" };
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
perl_run(my_perl);
PerlPower(3, 4);
/*** 3 ** 4 ***/
perl_destruct(my_perl);
perl_free(my_perl);
}
power.c power :
% cc o power power.c `perl MExtUtils::Embed e ccopts e ldopts`
% power
3 to the 4th power is 81.
power .
C,
Perl. ,
, . (
, : !)
IV
Perl
22
CPAN
CPAN (Comprehensive Perl Archive Network Perl)
, Perl.
Perl: Perl,
, ,
Perl. Perl, , ,
, CPAN. CPAN
, CPAN http://
www.perl.com/CPAN. , ,
http://www.perl.com/CPAN/ (
)
. www.cpan.org. ,
.
CPAN, :
authors
,
, . ,
CGI,1
(Lincoln Stein), , ,
authors/Lincoln_Stein. , ,
modules, .
Perl.
600
22. CPAN
doc
Perl,
Perl
, , , HTML, PostScript Perl
pod, 26
pod.
modules
, Perl Perl
C. . modules.
ports
Perl , UNIX,
.
, .
,
. , ,
, ,
Perl.
scripts
Perl, . ,
, (
). ,
, .
Perl Power Tools (PPT) . PPT
Unix Perl.
.
src
Perl. Perl.
stable (), devel ().
( .)
.
stable.tar.gz perl*
5.6.0.tar.gz,1 , , ,
1
, ,
, , .
.tar.gz, .tgz, Unix
tar, GNUzip, tarball.
modules CPAN
601
.
Perl.
. , . ports .
modules CPAN
CPAN Perl
, C,
, CPAN .
, : , 100
Perl ( 11 12
), (, C, 21
) (,
Perl, 31 ).
CPAN (bundles) .
,
.
(, , ),
. . BundleXML.
CPAN http://se*
arch.cpan.org, CPAN.
, modules
() CPAN, : by*authors, by*
category by*module. by*module ,
, ( )
.
:
Perl core modules ( Perl), language extensions (
), documentation tools ( )
, ,
Perl , ,
, Perl,
, pod Perl.
Java.
Development support ( )
,
Perl .
Operating system interfaces and hardware drivers ( *
)
, , PalmPilot .
602
22. CPAN
modules CPAN
603
604
22. CPAN
Miscellaneous modules ( )
, , ,
( ) , , , ,
MIDI, .
CPAN
CPAN tar.
.tar.gz ,
, READ*
ME Makefile.PL.
CPAN ,
: , ,
.
, ,
.
README INSTALL, , ,
. perlmodinstall.
, ,
CPAN (
Perl) PPM (Perl Package Manager Perl,
Perl ActiveState).
CPAN ( CPAN),
:
% perl MCPAN e "shell"
. ,
, ,
CPAN:
install Some::Module
:
% perl MCPAN e "install 'Some::Module'"
, CPAN PPM,
, .
Unix, Windows Macintosh;
perlmodinstall.
CPAN
, CPAN, ,
. ,
(unzipping)
CPAN
605
CPAN
CPAN C,
, , ,
, C.
CPAN ( C )
, . (
make,
, . Windows
, make nmake.)
% perl Makefile.PL
% make
% make test
CPAN Perl
, ,
,
Perl. Perl lib
, .
Perl, /usr/local/lib/perl5
Unix C:\PERL\LIB Windows. ,
Perl, site_perl
Perl. (
) , :
% perl V
, :
% make install
Perl .
, ,
Perl (, ).
606
22. CPAN
\
\
\
\
/my/dir/perllib,
, . (, ,
,
Perl, . Perl
.)
Perl
, :
use lib "/my/dir/perllib";
.
PERL5LIB
Perl I. . use lib
31 .
CPAN
, , ,
, , CPAN
. ,
, PAUSE (Perl Authors Upload Server)
https://pause.kbx.de/pause/. ,
PAUSE .
Perl.
Perl,
, . Perl
. (
.) pod
( Plain Old Documentation )
26 POD.
, .
. 17 .
,
, ,
,
, ,
(). 23 , ,
CPAN
607
.
, CPAN,
Perl Makefile.PL, Makefile
README ,
. Makefile ,
.
h2xs:
h2xs X n Foo::Bar
( X A, , XS.
XS 21 .) h2xs
, .
, PAUSE.
Makefile.PL, h2xs, :
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME
=> 'Mytest',
VERSION_FROM => 'Mytest.pm', # finds $VERSION
LIBS
=> [''], # e.g., 'lm'
DEFINE
=> '',
# e.g., 'DHAVE_SOMETHING'
INC
=> '',
# e.g., 'I/usr/include/other'
);
608
22. CPAN
Makefile.PL. , ,
, Perl 5.6 :
eval { require 5.6.0 }
or die <<'EOD';
############
### , ,
### Perl 5.6. , !
############
EOD
,
make test make.
, ,
, CPAN. ok/not
ok, Perl ,
.
test.pl, h2xs. 21
, test.pl.
,
Perl t/
t . make test
.
, CPAN,
. CPAN
PASS (), FAIL ( ),
NA ( ) UNKNOWN (),
. CPAN cpan*tes*
ters@perl.org; http://testers.cpan.org/.
, .
,
, .
,
.
, . ,
...
23
, ,
, ,
, ,
, ,
, , . Perl
,
(taint mode),
, ,
, .
, ,
, ,
.
.
. , ,
( , ),
,
, ,
. Perl , ,
, ,
, ,
.
.
610
23.
, , ,
, ,
, . Perl
, ,
. ,
Safe ,
, , .
.
Perl ,
, , .
,
. Unix
setuid setgid, ,
, ,
. , ,
,
(, CGI,
, /etc/inetd.conf).
, .
, ,
,
, .
, ,
, ,
LWP::* Net::* ,
;
, ,
. , ,
, , .
Unix, , ,
, Perl
, .
,
, Perl
. ,
, (, ,
) .
Unix, Perl ,
, . ,
Perl ,
611
. Perl ,
,
(effective) (real) ID .1
, Perl, setuid setgid,
,
. ,
, ID.
Perl,
, ,
. , ,
. Perl .
T. ,
, , CGI.
,
, .
: !
.
, , CGI
T : . ,
. , , ,
. CGI
, ,
, Perl.
, Perl
, .
, ,
, , PATH,
;
. , , ,
,
Perl , C,
Perl CGI ,
1
612
23.
. (, , ,
Perl.)
: ,
, ,
, . ,
,
(tainted), ,
.
,
, , ,
. , ,
, ,
, .
,
, . (
, .)
,
, . ,
, ,
OK, .
$arg = shift(@ARGV);
$hid = "$arg, 'bar'";
$line = <>;
$path = $ENV{PATH};
$mine = 'abc';
#
#
#
#
#
$arg ( @ARGV).
$hid ( $arg).
( ).
%ENV, .
.
system
system
system
system
#
#
#
#
, PATH.
: sh $arg.
OK, PATH ( sh).
: , PATH.
"echo $mine";
"echo $arg";
"echo", $arg;
"echo $hid";
$oldpath = $ENV{PATH};
# $oldpath ( %ENV).
$ENV{PATH} = '/bin:/usr/bin'; # ( .)
$newpath = $ENV{PATH};
# $newpath .
delete @ENV{qw{IFS
CDPATH
ENV
BASH_ENV}};
# %ENV .
# OK, .
# $hid.
# OK (open ).
# ( "" ).
# OK: . ""
613
or exec "echo", $arg
# .
or die " exec echo: $!";
open(OOF,"|", "echo", $arg) # , , OK.
or die " echo: $!";
$shout = `echo $arg`;
$shout = `echo abc`;
$shout2 = `echo $shout`;
# "" $arg.
# $shout .
# "" $shout.
# "" $arg.
# "" $arg.
# "" $arg,
.
# OK! ( .)
# OK, !
,
(, , ),
, Insecure dependency Insecure $ENV{PATH}. .
.
system, exec open ,
,
, Perl
, .
system, exec open ,
.
, . . , , ,
.
, , , .
,1
, ,
. ,
:
system @args;
system { $args[0] } @args;
# , @args == 1.
# , .
,
, is_tainted,
. , eval STRING
. , $nada,
, ; ,
$arg. eval BLOCK .
1
, .
614
23.
, ,
eval . ,
$@ eval,
, ,
,
:
sub is_tainted {
my $arg = shift;
my $nada = substr($arg, 0, 0); #
local $@; #
eval { eval "# $nada" };
return length($@) != 0;
}
. ,
, ,
.
,
.1 ,
$1, $2 . .
. ,
, , ,
, .
: ,
.
, ,
, ,
, . , , ,
, $string ,
(, ), , @ :
if ($string =~ /^([\@\w.]+)$/) {
$string = $1;
}
else {
die " $string";
}
# $string .
# .
,
.
SV ( scalar values
), .
, .
,
.
615
$string
, . . /\w+/
,
.1 /(.+)/s,
, . Perl .
.
*
.
.
, . . setid,
, T, ,
(forking a child) ; .
.
use re 'taint'
.
,
, , ,
, .
, , $fullpath
:
($dir, $file) = $fullpath =~ m!(.*/)(.*)!s;
$dir $file . , ,
,
. , $file
"; rm rf * ;", .
, $fullpath:
{
use re 'taint';
($dir, $file) = $fullpath =~ m!(.*/)(.*)!s;
}
:
use re 'taint';
# $1 . .
.
Perl .
use locale ,
, \w [[:alpha:]], .
616
23.
{
no re 'taint';
#
if ($num =~ /^(\d+)$/) {
$num = $1;
}
}
,
, DATA.
untaint IO::Handle:
use IO::Handle;
IO::Handle::untaint(*SOME_FH);
SOME_FH>untaint();
# ,
# .
. , ?
, , ,
, .1
Unix ( chown(2)
), :
use File::stat;
use Symbol 'qualify_to_ref';
sub handle_looks_safe(*) {
my $fh = qualify_to_ref(shift, caller);
my $info = stat($fh);
return unless $info;
# , "",
# uid $<
if ($info>uid != 0 && $info>uid != $<) {
return 0;
}
# , .
# 066
if ($info>mode & 022) {
return 0;
}
return 1;
}
use IO::Handle;
SOME_FH>untaint() if handle_looks_safe(*SOME_FH);
1
,
. ,
, stat.
617
stat , ,
, . .
.
, .
,
, stat
.
, ,
(rce conditions).
Perl , ,
, ,
.
. Perl ,
, .
, , ,
, ,
. ,
. web
,
HTML, . ,
.
,
,
.
HTML <SCRIPT>...</SCRIPT>,
(, JavaScript),
.
,
,
,
web , .
, , ,
:
$new_guestbook_entry =~ tr[_azAZ09 ,./!?()@+*][]dc;
, ,
.
HTML,
. ,
, .
.
618
23.
Perl ,
, Perl ,
PATH. , PATH
, Perl
Insecure $ENV{PATH}.
PATH , Perl ,
,
; ,
Insecure directory.
, Perl PATH ,
, .
, , PATH
, .
, , , 180
,
PATH. Perl ,
PATH,
.
PATH ,
. IFS, CDPATH, ENV
BASH_ENV, Perl , ,
, .
, :
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
# %ENV
, ,
.
, ,
, open .
, ,
open
,
,
.
,
. , Perl
, (
), ,
,
.
,
619
. , ,
sysopen open :
#
open(FH, $file)
or die " $file: $!";
# fork, "",
# .
open(FH, "< $file")
or die " $file: $!";
# WYSIWYG: .
open(FH, "<", $file)
or die " $file: $!";
# , 3 WYSIWYG.
require Fcntl;
sysopen(FH, $file, O_RDONLY)
or die " sysopen $file: $!";
. Perl
,
, . ,
, ,
, .
? ?
?
,
,1 . ,
, .
../../../../../../../etc/passwd.
, ,
( ).
,
.
.
,
Unix .
(, , ) .
setid,
, .
, , open, sysopen, system,
1
,
, .
620
23.
, ,
UID GID UID GID. Perl
setuid, $> = $< ( $EUID = $UID use
English), setgid, $( = $) ($EGID = $GID).
ID, . ,
.
Perl
setid. ,
open,
. ID
.
,
, ,
.
, , , open
, , ,
,
.
system exec ,
,
.
,
( ) :
use English; # $UID
die " open: $!" unless defined($pid = open(FROMKID, "
|"));
if ($pid) {
#
while (<FROMKID>) {
#
}
close FROMKID;
}
else {
$EUID = $UID; # setuid(getuid())
$EGID = $GID; # setgid(getgid()), initgroups(2) getgroups(2)
chdir("/")
or die " chdir /: $!";
umask(077);
$ENV{PATH} = "/bin:/usr/bin";
exec 'myprog', 'arg1', 'arg2';
die " exec myprog: $!";
}
setid.
,
,
. ( , system, exec
621
,
, .)
,
, :
open(FROMKID, "|") or exec("myprog", "arg1", "arg2")
or die "can't run myprog: $!";
set
id. ID , .
, setuid,
. open
, .
,
, open.
, ,
.
use English;
defined ($pid = open(SAFE_WRITER, "|"))
or die " : $!";
if ($pid) {
# . SAFE_WRITER
print SAFE_WRITER "@output_data\n";
close SAFE_WRITER
or die $! ? "Syserr SAFE_WRITER: $!"
: " $? SAFE_WRITER ";
}
else {
# ,
($EUID, $EGID) = ($UID, $GID);
#
open(FH, "> /some/file/path")
or die " /some/file/path : $!";
# ( stdin)
while (<STDIN>) {
print FH $_;
}
close(FH) or die " : $!";
exit;
# , SAFE_WRITER .
}
622
23.
.
,
(SIGPIPE),
, . .
16 .
, .
, , ,
(,
). ,
, ,
. ,
CPU,
. .
. , ,
, .
,
. , :
$var++ if $var == 0;
:
{
lock($var);
$var++ if $var == 0;
}
,
. (. 17
.) ,
, .
, , , , .
.
,
, .
, Unix,
setid.
(rce conditions) ,
, .
623
Unix
, ,
,
, Unix ,
setid ,
. , ,
setid . (
, #!.)
, , ,
, (
setid) .
, .
, . ,
.
setid, . ,
setid . Perl
setuid setgid, setid (
) Perl.
suidperl,
.1
setid, Perl
, setuid .
setid,
C. C ,
Perl.
,
setid.
, C:
#define REAL_FILE "/path/to/script"
main(ac, av)
char **av;
{
execv(REAL_FILE, av);
}
setid. , . .
, PATH.
. Perl ,
, , nosuid,
. Perl,
.
624
23.
(
C Perl.
. . 18 .)
, ,
setid.
setid , ,
, ,
/dev/fd/3. ,
,
.1 Unix ,
,
.
(race conditions).
?
. ( ,
. .) ,
,
(
). ,
,
.
, .
, ,
, , .
, : ,
. , .
,
.
, ,
, ,
.
:
flock.
, .
,
. . 16.
1
Perl DSETUID_SCRIPTS_ARE_SECU
RE_NOW. Configure, Perl,
, .
625
,
.
, c ,
. :
if (e $file) {
open(FH, "< $file")
or die " $file : $!";
}
else {
open(FH, "> $file")
or die " $file : $!";
}
, ,
. , e
open. if
, ,
, , , , ,
. else ,
open , , , ,
, . open ,
. ,
,
,
? ,
,
,
, .
,
sysopen, ,
.
e,
, .
use Fcntl qw/O_WRONLY O_CREAT O_EXCL/;
open(FH, "<", $file)
or sysopen(FH, $file, O_WRONLY | O_CREAT | O_EXCL)
or die " $file: $!";
,
open sysopen ,
, , , sysopen
.
,
, , ,
.
626
23.
,
. ,
(, , )
, .
, . .
, . (
. 16.)
handle_looks_safe,
Perl stat ( )
.
: ,
, , ,
, ( ).
stat open.
, ;
. ,
, . . ,
,
, ,
.
, ,
. ,
(exploits).
stat , ,
.
.1
,
,
, .
(, ) ,
, .
( Perl
) (
),
. ,
1
, .
,
: a () + tomos (). ,
. ( .)
627
,
.
, ,
,
.
:
open(TMP, ">/tmp/foo.$$")
or die "can't open /tmp/foo.$$: $!";
, .
.
,
, . PID
, PID
.1
open,
.
? .
, , .
( ), ,
,
, /etc/passwd. , /tmp
,
.
Perl ,
. POSIX::tmpnam,
, :
# , .
use POSIX;
do {
$name = tmpnam();
} until sysopen(TMP, $name, O_RDWR | O_CREAT | O_EXCL, 0600);
# TMP.
IO::File::new_tmpfile,
:
# .
use IO::File;
my $fh = IO::File::new_tmpfile(); # this is POSIX's tmpfile(3)
# $fh.
1
OpenBSD, PID
.
628
23.
, ,
. , , ,
Perl tmpfile(3),
C,
, , open,
. ( , , .)
,
. ,
,
, .
,
, mkstemp(3) C. ,
. O_EXCL
,
,
, . ,
, , ,
, ,
,
, .1
, ,
. , O_EXCL sys
open ,
(sticky) .
Perl 5.6.1 . File::Temp
, .
:
use File::Temp "tempfile";
$handle = tempfile();
:
use File::Temp "tempfile";
($handle, $filename) = tempfile("plughXXXXXX",
DIR => "/var/spool/adventure",
SUFFIX = '.dat');
File::Temp
( ,
, ,
). . 32
.
1
, , sys
open OR O_NOFOLLOW. ,
.
629
, .
, ,
, .
, ,
, ,
.1
, Perl
,
. fcntl,
exec ( open 29 ),
.
, /dev/fd/,
, ,
:
$virtname = "/dev/fd/" . fileno(TMP);
Perl,
, ,
open, Perl
:
$virtname = "=&" . fileno(TMP);
open
( , ),
.
, /dev/fd/,
, Perl, ,
/dev/fd/. ,
open
Perl, , .
, ,
, , .
,
Perl; ,
1
, stat
( device/inode).
, . ,
, (, ,
).
630
23.
. ,
, . .
,
. ( , ,
.)
, , () ,
, , ,
.
. ,
, .
, ,
, ?
, , ,
; .
,
, :
. , ,
, .
.
, ,
. ,
, . Perl
Safe.
Safe sandbox (),
.
,
.
Safe ,
, . Unix
chroot(2),
,
, , ,
. ,
,
.1 Safe ,
1
CGI,
.
, , ,
.
631
,
Perl, ,
.
Safe
,
. , ,
, .
Safe .
, :
use Safe;
my $sandbox = Safe>new("Dungeon");
$Dungeon::foo = 1; # .
, new,
, , .
, . .
.
Safe, .
, ,
Safe,
. ( , $Dungeon::foo
.)
use Safe;
$Dungeon::foo = 1; # , .
my $sandbox = Safe>new("Dungeon");
Safe ,
.
( )
reval:
use Safe;
my $sandbox = Safe>new();
$sandbox>reval('$foo = 1');
( ,
.) ,
, , main.
, $Dungeon::foo,
$main::foo $::foo, $foo, use
strict. $Dungeon::foo ,
$Dungeon::Dungeon::foo.
, Safe main,
.
632
23.
,
reval (restricted eval), .
eval STRING,
reval .
reval
$@,
reval.
foo
2:
$sandbox>reval('$foo++; print "foo $main::foo\n"');
if ($@) {
die " : $@";
}
, ,
:
$sandbox>reval(q{
our $foo;
sub say_foo {
print "foo $main::foo\n";
}
}, 1);
die if $@;
#
reval , , ,
reval strict.
strict ,
,
Safe . .
say_foo,
:
$sandbox>reval('say_foo()');
die if $@;
# .
$sandbox>varglob('say_foo')>();
# .
Dungeon::say_foo();
# , .
Safe , Perl
, (sandbox). (
,
, , .)
, .
Perl Safe reval rdo (
do FILE)
633
. ()
, ,
,
. ( .)
,
, ,
, .
Safe.
Safe *
(DoS attack),
.
,
.
, CPU
,
. ,
.
, .
Safe
, CGI, ,
Perl
.1 ,
, Perl :
. ,
, .
eval. ( ,
.)
reval. CGI,
, ( ) ,
, :
#!/usr/bin/perl lTw
use strict;
use CGI::Carp 'fatalsToBrowser';
use CGI qw/:standard escapeHTML/;
use Safe;
print header(type => "text/html;charset=UTF8"),
start_html("Perl Expression Results");
1
. ,
. Safe!
634
23.
,
"print `cat /etc/passwd`" ( ).
, , Perl
. $@
"quoted execution (``, qx) trapped by operation mask" (
, )
, .
,
.
,
. ,
. Safe,
.
Safe,
Perl. ,
.
. eval
29 ,
. , Safe
.
, Perl ,
, Safe.
#!/usr/bin/perl w
# safecalc Safe
use strict;
use Safe;
my $sandbox = Safe>new();
while (1) {
print "Input: ";
my $expr = <STDIN>;
exit unless defined $expr;
chomp($expr);
print "$expr produces ";
local $SIG{__WARN__} = sub { die @_ };
my $result = $sandbox>reval($expr, 1);
if ($@ =~ s/at \(eval \d+\).*//) {
printf "[%s]: %s", $@ =~ /trapped by operation mask/
? "Security Violation" : "Exception", $@;
635
}
else {
print "[Normal Result] $result\n";
}
}
: Safe ,
Perl . ( mod_
perl Apache Perl.)
, ,
, ,
. Perl ,
5.6, perldelta(1),
, Safe. (,
, ?)
,
Safe ,
, ,
.
,
. ,
, ,
.
Perl . Perl
,
.
,
,
. , Perl
. ,
,
.
, ,
,
, Perl.
system, exec open
, .
, ,
,
. (
,
, readpipe;
.)
636
23.
(
), ,
Perl, ,
.1 , Perl
, .
$new = $old;
print "$new items\n";
# .
# $new .
# .
# ok.
.
:
$count = '1 + int rand(6)';
$saying = "$count hit points";
$saying = "@{[$count]} hit points";
# .
# .
# .
, , HTML,
JavaScript, ,
.
637
$cnt = $n = 0;
while ($data =~ /( \d+ (?{ $n++ }) | \w+ )/gx) {
$cnt++;
}
print "Got $cnt words, $n of which were digits.\n";
, ,
, , .
,
. Perl ,
. ,
use re 'eval'
. (
.)
,
, .
, ,
.
, ,
Perl.
,
, ,
.
, ,
, . ,
eval BLOCK, eval STRING,
Perl. :
if (not eval { "" =~ /$match/; 1 }) {
# ( .)
}
else {
# , , , .
if ($data =~ /$match/) { ... }
}
,
. ,
. ,
.
,
,
638
23.
Out of memory!,
, , , ,
.
,
. alarm,
. , Perl
( ) ,
. ( .)
,
, ,
.
,
, ,
Perl, BSD::Resource CPAN,
Perl. Apache
,
CGI.
, ,
. , , ,
. .
24
Perl,
, . (
).
Perl,
Perl. , , ,
,
, Perl. ,
, ,
, .
. .
,
, , ,
. , ,
.
, ,
use warnings,
. use strict .
,
. ( .)
640
24.
FAQ. , , Perl round.
FAQ:
% perlfaq round
. ,
, , .
.
print.
:
print STDOUT, "goodbye", $adj, "world!\n";
, , .
:
print STDOUT "goodbye", $adj, "world!\n";
# ok
:
print $filehandle "goodbye", $adj, "world!\n";
$filehandle ,
. , :
print $notafilehandle, "goodbye", $adj, "world!\n";
$notafilehandle ,
. . .
== eq != ne. == !=
. .
"123" "123.00" , .
, .
,
.
. Perl
.
, awk, Python FORTRAN.
, Perl C.
, ,
. :
print <<'FINIS';
A foolish consistency is the hobgoblin of little minds,
adored by little statesmen and philosophers and divines.
Ralph Waldo EmersonFINIS
641
, BLOCK .
. , while
if, ,
. , Perl C.
$1, $2 . . .
, m/atch/ s/ubsti/tution/
( , ) $1, $2..., $`, $&, $'
.
:
my ($one, $two) = /(\w+) (\w+)/;
, local ,
,
. , local
, ,
C. .
4 .
my.
.
. . ( .)
do {} while.
, .
:
@foo[0] = <STDIN>;
, STDIN, $foo[1]
, .
. , $
, @ , .
my:
my $x, $y = (4, 8);
my ($x, $y) = (4, 8);
#
# ok
,
$^, $~ $|. ,
642
24.
select(FILEHANDLE).
, , STDOUT.
Fi
leHandle. . 28 .
Perl :
,
. :
($x) = (4, 5, 6);
$x = (4, 5, 6);
@a = (4, 5, 6);
$x = @a;
# ; $x 4
# ; $x 6
# ; $x 3
# ( )
(bare) ,
. , , ,
.
, .
use strict
, , , .
,
( chop chdir),
( print unlink), ( time).
29 . , ,
,
. ,
,
($)
().
,
$_, @ARGV , .
, ,
.
<FH> ; ,
.
, print :
print <FH> "hi";
# ,
, , ,
$_,
while:
while (<FH>) { }
# $_.
643
<FH>;
# !
=, =~;
:
$x = /foo/; # $_ "foo", $x
$x =~ /foo/; # $x "foo",
my , .
local
,
.
local .
,
.
, .
C
C :
if while .
. else , if
. Perl C.
:
if (expression) {
block;
}
elsif (another_expression) {
another_block;
}
, elif file.
Algol ,
, .
switch. ( ; .
Case 4.)
644
24.
Perl $, @ %.
#, /*.
, Perl
( ), .
%SIG , .
(shells)
:
$, @ %,
. :
camel='dromedary';
, . :
$camel='dromedary';
# ok
foreach $. csh :
foreach hump (one two)
stuff_it $hump
end
Perl :
foreach $hump ("one", "two") {
stuff_it($hump);
}
,
.
. Perl ,
:
chomp($thishost = `hostname`);
( csh)
. Perl
, ,
, .
645
. Perl
(
BEGIN, , ).
@ARGV, $1, $2
. .
. ,
Env.
Perl
Perl 4 ( )
5 4,
:
@ ,
.
@, .
, Perl ,
,
. :
sub SeeYa { die "Hasta la vista, baby!" }
$SIG{'QUIT'} = SeeYa;
Perl .
! w,
, use strict,
.
, _,
main, ,
($_, @_ . .).
. , :
print "$a::$b::$c\n";
$a:: ,
$a .
:
print "$var::abc::xyz\n";
$var::abc::xyz,
$var
::abc::xyz.
646
24.
s'$pattern'replacement' $pattern.
($ .)
; $pattern
.
splice
, .
:
shift @list + 20;
# shift(@list + 20), !
$n = keys %map + 20; # keys(%map + 20), !
, :
sleep $dormancy + 20;
,
. Perl
.
:
/foo/ ? ($a += 2) : ($a = 2);
:
/foo/ ? $a += 2 : $a = 2;
:
(/foo/ ? $a += 2 : $a) = 2;
,
$a += /foo/ ? 1 : 2;
, C.
. ,
.
goto ,
. .
.
.
647
caller
, . ,
require .
m//g ,
, . 5
.
reverse
sort.
taintperl . T
,
.
$ @ \
.
if BLOCK BLOCK .
** , .
$#array .
delete
,
.
"this is $$x", ID
, $x. $$
.
foreach , ,
. ,
. ,
, .
, grep !
:
%
1
%
2
Perl,
. ,
foreach $var (grep /x/, @list) { ... }
648
24.
:
foreach $var (my @tmp = grep /x/, @list) { ... }
$var @list. (
$_
,
$_.)
,
, ,
Perl.
, Perl
.
Perl ,
. ,
, , .
.
,
Perl, 18 ,
C,
21 .
,
( ).
,
, , ?
. ,
@keywords, , $_ ,
:
my %keywords;
for (@keywords) {
$keywords{$_}++;
}
, $_ ,
$keyword{$_} .
649
, fo
reach .
, , ( )
,
.
.
foreach, shift splice. use integer.
goto.
.
printf, print.
$& $` $'.
, ,
, . (
, , , .)
# $variable 7
, .
/pattern/o (),
, .
, ,
, ,
:
"foundstring" =~ /$currentpattern/;
while (<>) {
print if //;
}
# ( ).
qr.
eval ,
( ).
,
.
650
24.
,
,
print if /onehump/ || /two/;
, , ,
print if /onehump|two/;
, onehump two.
,
(BoyerMoore).
.
next if. , ,
. , .
,
split chop:
while (<>) {
next if /^#/;
next if /^$/;
chop;
@piggies = split(/,/);
...
}
{MIN, MAX} .
,
.
(?>...),
.
.
, , ,
,
, .
Dr ,
Dr. Perl .
.
,
.
,
, (inlining)
651
C. ( ,
, .)
getc
.
. sysread.
substr ,
UTF8.
, , substr
, substr
"":
while ($buffer) {
process(substr($buffer, 0, 10, ""));
}
substr
. , $foo
$bar :
$foo = substr($foo,0,3) . $bar . substr($foo,7);
:
substr($foo, 3, 4) = $bar;
, $foo , $bar
,
. Perl ,
, ,
substr .
s/// .
,
. .
and or
.
( $ring = 0 unless $engaged)
, .
.
$foo = $a || $b || $c. ( ),
:
if ($a) {
$foo = $a;
}
elsif ($b) {
652
24.
$foo = $b;
}
elsif ($c) {
$foo = $c;
}
,
$pi ||= 3;
, .
, ,
/^a/, /^b/ . .
, , .
last elsif,
.
, study,
, pack 'u' unpack '%'.
, .
(<STDIN>)[0] Perl .
Unix, Perl ,
, .
. Perl
. ,
.
. ,
,
.
, ,
, .
.
my , local.
, .
,
,
.
, tr/abc//d , s/[abc]//g.
print
. , :
print $fullname{$name} . " has a new home directory " .
$home{$name} . "\n";
653
,
, :
print $fullname{$name}, " has a new home directory ",
$home{$name}, "\n";
. ,
. .
join("", ...) , .
. join .
split , split .
split(/ /, ...) split(/ +/, ...), ,
. /\s+/, /^/ / /
split .
.
Perl ,
. x
$#array
.
undef ,
.
,
.
system("mkdir ...") ,
mkdir .
eof,
.
( passwd group),
. ,
. , ,
gethostbyaddr ( 204.148.40.9)
( www.oreilly.com), :
sub numtoname {
local ($_) = @_;
unless (defined $numtoname{$_}) {
my (@a) = gethostbyaddr(pack('C4', split(/\./)),2);
$numtoname{$_} = @a > 0 ? $a[0] : $_;
}
return $numtoname{$_};
}
654
24.
.
. , ,
time, $now.
_,
stat(2).
.
system. system
, , ,
.
.
, .
pwd, hostname find
, .
, .
,
pwd. ( . . Cwd 30
Perl.)
, system exec .
(sticky) Perl
:
chmod +t /usr/bin/perl
Memoize
CPAN.
vec, . (
UTF8.)
substr,
.
,
, Tie::SubstrHash.
__END__ DATA,
.
each keys, .
.
655
DBM.
tr///. tr///
.
(inline)
.
,
a, n, p, s i.
for foreach.
<*> .
, .
*, + {}.
getc.
close
open.
.
.
.
$_[EXPR].
, .
foreach foreach.
next last.
656
24.
, , and, or
( exit if $done).
, .
, , .
my.
() .
pod.
use warnings.
use strict.
, . ,
, eval.
Config $^O,
, .
,
pack unpack .
. ASCII.
UTF8. .
$] $^V, ,
.
eval exec, ,
, Unix
, #!.
#!/usr/bin/perl, .
Unix. find,
, xdev.
Unix,
. Unix MSDOS VMS.
, .
657
CPAN. ,
.
, .
, GUI Perl/Tk,
. (Perl/Tk
CPAN.)
,
.
, ,
.
,
.
,
, .
, .
.
, .
,
() .
, ,
. ,
, :
open(FILE, $file) or die "$0: $file : $!\n";
.
, .
, .
,
!
658
24.
, !!!
, , ,
,
, .
use warnings. (
no warnings.)
use strict,
, .
use sigtrap use diagnostics.
,
,
,
. , ,
. ( )
:
( ).
, ,
, ;
.
while ($condition) {
#
}
# ,
while ($this_condition and $that_condition
and $this_other_long_condition)
{
#
}
BLOCK.
( ) .
659
,
.
else
.
.
( and or,
&& ||).
,
.
,
, ( ) ,
.
:
,
, . Perl ,
, ,
. :
open(FOO,$foo) or die "Can't open $foo: $!";
:
die "Can't open $foo: $!"
unless open(FOO,$foo);
. ,
print "Starting analysis\n" if $verbose;
,
$verbose and print "Starting analysis\n";
, v .
, ,
, .
,
. , ,
.
, ,
, , :
660
24.
return print reverse sort num values %array;
return print(reverse(sort num (values(%array))));
, . ,
% vi.
, ,
.
,
. Perl last,
.
:
LINE:
for (;;) {
statements;
last LINE if $foo;
next LINE if /^#/;
statements;
}
,
. . .
grep, map ,
. . .
, .
foreach system.
,
, eval,
, .
, $] ($PERL_VERSION
English), , . Con
fig ,
Configure Perl.
.
, , .
$gotit ,
.
, $var_names_like_this, $VarNamesLike
This, , .
, $VAR_NAMES_LIKE_THIS.
. Perl
, integer strict.
,
661
, ,
.
. :
$ALL_CAPS_HERE
# (
Perl!)
$Some_Caps_Here # /
$no_caps_here
# my() local()
, .
, $obj>as_string().
,
,
. (Perl ,
.)
,
/x ,
.
,
.
,
. q//, qq// qx//.
and or
&& ||. ,
,
.
print.
,
:
$IDX
$IDX
$IDX
$IDX
=
=
=
=
$ST_MTIME;
$ST_ATIME
$ST_CTIME
$ST_SIZE
if $opt_u;
if $opt_c;
if $opt_s;
, :
, .
, .
662
24.
STDERR
,
. ,
.
, :
opendir(D, $dir) or die " opendir $dir: $!";
, :
tr [abc]
[xyz];
.
,
? .
.
use strict w.
.
. ... , .
Perl
(
), ,
, Perl.
Perl ,
Perl .
Perl, ,
.
,
, . .
. Perl Cookbook, Tom Christiansen and Nathan Torkington,
O'Reilly&Associates (Perl: , .
. , , 2000).
, ,
, , ,
.
=> , :
return bless $mess => $class;
Perl
663
: .
=> ,
:
sub foo () { "FOO" }
sub bar () { "BAR" }
print foo => bar; # fooBAR, FOOBAR;
=> ,
:
join(", " => @array);
Perl ,
. !
:
for (@lines) {
$_ .= "\n";
}
$_ Perl .
.
:
$_ .= "\n" for @lines;
$_ Perl,
grep map.
expensive:
%cache = map { $_ => expensive($_) } @common_args;
$xval = $cache{$x} || expensive($x);
( $_ ).1
,
.
while (<>) {
next if /^=for\s+(index|later)/;
$chars += length;
$words += split;
$lines += y/\n//;
}
,
.
664
24.
,
.
, , ,
.
next,
.
$_ grep map,
:
@haslen = grep { length } @random;
,
.
for :
for ($episode) {
s/fred/barney/g;
s/wilma/betty/g;
s/pebbles/bambam/g;
}
, ? ,
, . . $_.
(topicalization) , .
, @_.
:
sub bark {
my Dog $spot = shift;
my $quality = shift || "yapping";
my $quantity = shift || "nonstop";
...
}
Perl, @_,
, (them). .
shift , @_,
.
|| ,
, Perl
. Perl
; ,
, , quantity .
$quality $quantity ,
. defined
exists. , .
, .
Perl
665
,
:
$xval = $cache{$x} ||= expensive($x);
.
, ||= expensive($x) $cac
he{$x}, $cache{$x} .
, $cache{$x}.
,
, expensive($x) . ,
, , expensive($x) ,
. , , expensi
ve($x) . ,
.
.
,
. ...
:
while (<>) {
$comments++, next if /^#/;
$blank++, next
if /^\s*$/;
last
if /^__END__/;
$code++;
}
print "comment = $comments\nblank = $blank\ncode = $code\n";
,
, next .
,
. ( ,
.)
:
while (<>) {
/^#/
/^\s*$/
/^__END__/
$code++;
}
print "comment
, .
Perl , ( )
, .
666
24.
if and ( &&),
unless or ( ||).
, n p.
:
#!/usr/bin/perl n
$comments++, next LINE if /#/;
$blank++, next LINE
if /^\s*$/;
last LINE
if /^__END__/;
$code++;
END { print "comment = $comments\nblank = $blank\ncode = $code\n" }
, , , .
LINE ,
, . . LINE,
n, .
END,
, awk.
,
.
:
END { print <<"COUNTS" }
comment = $comments
blank = $blank
code = $code
COUNTS
,
Perl, , ,
.
,
, END. (
,
.)
:
($new = $old) =~ s/bad/good/g;
l , ,
.
(
):
chomp($answer = <STDIN>);
, ,
. , !
Perl
667
:
for (@new = @old) { s/bad/good/g }
@old @new, ( ,
, , ).
=>.
/
:
sub bark {
my DOG $spot = shift;
my %parm = @_;
my $quality = $parm{QUALITY} || "yapping";
my $quantity = $parm{QUANTITY} || "nonstop";
...
}
$fido>bark( QUANTITY => "once",
QUALITY => "woof" );
(
). Perl ,
.
, .
/e :
#!/usr/bin/perl p
1 while s/^(.*?)(\t+)/$1 . ' ' x (length($2) * 4 length($1) % 4)/e;
, ,
, ,
4 , 8. .
, 1 while , ,
, . (Perl
, 1 .)
, ,
,
.
(.*?) ,
( ).
*
: ([^\t]*). ,
,
, .
,
668
24.
,
.
/e , ,
.
, .
:
#!/usr/bin/perl p
1 while s{
^
#
(
#
.*?
#
)
#
(
#
\t+
#
)
#
}
{
my $spacelen = length($2) * 4; #
$spacelen = length($1) % 4;
#
$1 . ' ' x $spacelen;
#
}ex;
, , , ,
. .
$`, :
1 while s/(\t+)/' ' x (length($1) * 4 length($`) % 4)/e;
, $`, ,
. ,
, .
@ (@LAST_MATCH_START)
@+ (@LAST_MATCH_END):
1 while s/\t+/' ' x (($+[0] $[0]) * 4 $[0] % 4)/e;
. ( ,
.) . @ @+ 28.
eval :
sub is_valid_pattern {
my $pat = shift;
return eval { "" =~ /$pat/; 1 } || 0;
}
, eval {}
. 1, .
Perl
669
$pat , eval
undef ||,
0 ( , undef
, ,
is_valid_pattern, ).
:
use XML::Parser;
$p = new XML::Parser Style => 'subs';
setHandlers $p Char => sub { $out[1] .= $_[1] };
push @out, "";
sub literal {
$out[1] .= "C<";
push @out, "";
}
sub literal_ {
my $text = pop @out;
$out[1] .= $text . ">";
}
...
250 ,
XML Camel pod,
Real Text Editor.
, , XML::Parser (
CPAN) XML,
, .
( ,
Perl , XML::Parser ,1
UTF8).
XML::Parser ,
(object factory). (
). XML::Parser, ,
, .
,
, . subs
1
XML::Parser XML
expat .
670
24.
XML::Parser.
, ,
.
setHandlers
, ,
.
, .
(callback).
, , .
XML, <literal>,
(
).
,
Char setHandlers.
@out,
. , ,
(
0).
.
,
$out[1] .
$out[1] , $out[0], $out[0]
. ( .
.)
, <literal>.
literal,
@out, @out.
.
, $text
@out
(. . ) ,
XML <literal>text</literal>
pod, C<text>.
, .
my .
,
.
,
.
Perl
671
%seen
.
my
.
.
?:
printf.
printf %:
my %seen;
while (<>) {
my ($a, $b, $c, $d) = split;
print unless $seen{$a}{$b}{$c}{$d}++;
}
if (my $tmp = $seen{fee}{fie}{foe}{foo}) {
printf qq(Saw "fee fie foe foo" [sic] %d time%s.\n"),
$tmp, $tmp == 1 ? "" : "s";
}
.
, .
, $_,
split , .
.
, ( )
,
. ( use
warnings
,
.)
, ,
,
,
, .
%seen, ,
.
,
, .
$seen{"fee"}{"fie"}{"foe"}{"foo"},
, .
$seen{fee}{fie}{foe}{foo}
, if.
672
24.
,
. my, ,
, ,
. my,
Perl , =
==. ( Perl , ,
. Pascal
.)
printf, ,
qq(),
.
$tmp, , ,
, printf.
$tmp , ,
,
?:, , time
. , ,
% printf.
,
, .
! ? , ,
.
Perl .
, ,
, .
. ( , , ,
, RPG Report Program Generator
, Role Playing Game
.) , , ,
.
, , ,
, .
,
, ( , ).
, , , ,
. Perl ,
Perl , .
(, Perl Perl,
, .)
673
Perl
Perl ( ) ,
. , Perl
, . , ,
s2p, sedperl:
print &q(<<"EOT");
:
#!$bin/perl
:
eval 'exec $bin/perl S \$0 \${1+"\$@"}'
:
if \$running_under_some_shell;
:
EOT
, Perl sh.
,
:
,
,
, . ,
$bin, ,
, .
, .
CGI, print,
. ,
, ... ( ,
print .)
(, HTML), ,
, Perl , ,
PHP:
print <<"XML";
<stuff>
<nonsense>
blah blah blah @{[ scalar EXPR ]} blah blah blah
blah blah blah @{[ LIST ]} blah blah blah
</nonsense>
</stuff>
XML
.
,
, .
22 CPAN , Makefile.PL
674
24.
Makefile. Makefile
100 , Makefile.PL. ,
. ,
, .
Perl
Perl ,
. Perl ,
. ,
.
, ,
. ,
, .
, Perl
( Perl), #line. Perl
,
.
, , eval (, ,
Perl, Perl). ,
C: Perl # line,
, __LINE__
, __FILE__ .1
, ,
perl. <Ctrl>+<D>,
Unix. DOS/Windows VMS <Ctrl>+<Z>.
,
, perl .
, __END__, ,
.
Perl warn
:
% perl
# line 2000 "Odyssey"
# the "#" on the previous line must be the first char on line
warn "pod bay doors"; # or die
^D
pod bay doors at Odyssey line 2001.
, /^#\s*line\s+(\d+)\s*(?:\s"([^"]+)")?\s*$/,
$1 , $2
. ( __FILE__ .)
675
, die eval,
$@ ($EVAL_ERROR) :
# line 1996 "Odyssey"
eval qq{
#line 2025 "Hal"
die "pod bay doors";
};
print "Problem with $@";
warn "I'm afraid I can't do that";
^D
Problem with pod bay doors at Hal line 2025.
I'm afraid I can't do that at Odyssey line 2001.
, #line
( eval STRING)
.
eval STRING
do FILE, .
Perl P, C,
#line. C
#line, ,
,
. Perl, ,
, AutoSplit. JPL (Java Perl Lingo)
.jpl .java, .pl, .h .c.
, #line.
Perl sedtoperl s2p.
Perl, s2p
awktoperl (a2p), , Perl .
, , .
,
find2perl.
Perl,
Perl?
,
, :
#!/usr/bin/perl
use MyDecryptFilter;
@*x$]`0uN&k^Zx02jZ^X{.?s!(f;9Q/^A^@~~8H]|,%@^P:q=
...
676
24.
. x, 19
, ,
, , Perl .
Filter CPAN ,
Perl awk:
#!/usr/bin/perl
use Filter::exec "a2p";
1,30 { print $1 }
# awktoperl
.
, .
25
Perl
, ,
, .
,
. Perl
, Perl
,
.
, . Perl
,
,
.
Perl. ,
, ,
, .
,
. ,
, . . ,
Perl.
, Perl .
Perl
Unix Macintosh,
678
25. Perl
Windows. ,
.1
, ID , (home)
.
$^O ,
Perl. ,
use Config
$Config{osname}. ( Config
, .)
,
%Config,
Config. , , ls
tat, $Config{d_lstat}.
Config,
Perl
perlport. Perl,
:
X ( ), accept, alarm, bind, binmode, chmod, chown, chroot, connect,
crypt, dbmclose, dbmopen, dump, endgrent, endhostent, endnetent, endprotoent, endp
went, endservent, exec, fcntl, fileno, flock, fork, getgrent, getgrgid, getgrnam,
gethostbyaddr, gethostbyname, gethostent, getlogin, getnetbyaddr, getnetbyname,
getnetent, getpeername, getpgrp, getppid, getpriority, getprotobyname, getpro
tobynumber, getprotoent, getpwent, getpwnam, getpwuid, getservbyport, getservent,
getservbyname, getsockname, getsockopt, glob, ioctl, kill, link, listen, lstat, ms
gctl, msgget, msgrcv, msgsnd, open, pipe, qx, readlink, readpipe, recv, select, sem
ctl, semget, semop, send, sethostent, setgrent, setnetent, setpgrp, setpriority,
setprotoent, setpwent, setservent, setsockopt, shmctl, shmget, shmread, shmwrite,
shutdown, socket, socketpair, stat, symlink, syscall, sysopen, system, times, trun
cate, umask, utime, wait, waitpid
, .
. Unix \012 (. .
ASCII 12),
DOS \015\012, Mac
\015. Perl
1
. Perl
(the Right Thing),
. Perl (
) , .
679
,
$/ \n, ,
, ,
.
\012, \015\012:
use Socket qw(:DEFAULT :crlf);
local ($/) = LF;
# , $/ \012
while (<SOCKET>) {
s/$CR?$LF/\n/;
}
# LF CRLF
, , , ,
. :
$data =~ s/\015?\012/\n/g;
return $data;
680
25. Perl
,
(big*endians) (little*
endians)1 (32 64
). .
, ,
.
. (, Intel CPU) 012345678
( 305 419 896), (, CPU Moto
rola) 0x78563412 ( 2 018 346).
( ) , pack
unpack n N, unsigned short long
big*endian ( )
.
,
, , :
print unpack("h*", pack("s2", 1, 2)), "\n";
# '10002000' , , Intel x86 Alpha 21064 littleendian
# '00100020' , , Motorola 68040
:
$is_big_endian
= unpack("h*", pack("s", 1)) =~ /01/;
$is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;
,
32
64 .
.
, ,
Data::Dumper Storable, .
, :
, , .
, XML Unicode
. , ,
Perl 5.6.0 ( ),
, utf8 (Perl UTF8).
64,
1
,
BYTE_ORDER. . . .
681
64 .
32 . pack
U*, unpack U*.
Unix /, Windows
\, Mac (:).
(link), (symlink, readlink, lstat).
,
,
, .
, .
File::Spec ,
:
use File::Spec::Functions;
chdir( updir() );
#
$file = catfile( curdir(), 'temp', 'file.txt' );
,
test.pl Test.pl,
.
8.3 (
).
,
8.3. ( ,
.)
, ,
.
, ,
8.3. (,
.)
, AutoSplit,
.
682
25. Perl
,
.
< ,
, , ,
>,
, , |,
. , open
, >, < |,
. ( ,
.)
open(FILE,
$existing_file) or die $!; #
open(FILE,
"<$existing_file") or die $!; #
open(FILE, "<", $existing_file) or die $!; #
,
. , ,
.
, ,
,
,
, .
:
,
,
.
. tie open
; untie close.
,
.
%ENV
, %ENV
. Unix
;
.
%SIG.
.
opendir, readdir closedir. ( Perl 5.6.0,
,
(XS)
683
,
.)
,
$!.
(IPC)
, .
, system, exec, fork, pipe, ``, qx// open |.
; ,
,
( ). ,
, ,
, ,
.
Perl sendmail,
:
open(MAIL, '|/usr/lib/sendmail t') or die "cannot fork sendmail: $!";
, sendmail.
,
CPAN, , Mail::Mailer Mail::Send MailTools Ma
il::Sendmail.
Unix System V IPC (msg*(), sem*(), shm*())
Unix.
(XS)
XS ,
XS .
, , XS
.
XS:
C . C
, XS
. Perl,
, Perl
C.1
1
Perl Configure
. , Configure races
, .
.
684
25. Perl
(, Perl)
. CPAN.pm (
,
), (,
ExtUtils::MM_VMS) DBM.
DBM, . SDBM_File
Unix DOS, MacPerl,
NBDM_File DB_File.
, DBM, AnyDBM_File
, .
,
DBM. , 1 K.
AnyDBM_File.
ISO8601 ("YYYY
MMDD"). "19871218"
Date::Parse.
(, localtime)
Time::Local.
time ,
. 1 1970
00:00:00 UTC, Macintosh 66 , VMS
17 1858 00:00:00.
:
require Time::Local;
$offset = Time::Local::timegm(0, 0, 0, 1, 0, 70);
685
Unicode.
. . 15
Unicode.
Unicode
.
,
. ,
; ,
a A b;
, b.
POSIX (
),
POSIX perllocale.
,
.
,
. ,
Unix .
, ,
,
. Config
$^O.
,
. ,
. ,
,
, ( )
. ,
,
$! .
Errno.
, ,
,
, .
;
, , (
), . ,
.
26
POD
, Perl, ,
,
. .
Perl , pod,
, . Pod
, .
Pod , XML, LATEX, troff (1)
HTML. :
. ,
, ,
. ,
, , .
,
. Pod ,
( ) . , pod
, , .
pod
,
. Pod : pod
pod
687
, pod pod.
100 pod. ,
Perl, pod
, . Perl
pod .
Perl , ,
, ,
, , :
=head1 Here There Be Pods!
, , , =cut,
.
, :
=item snazzle
The snazzle() function will behave in the most spectacular
form that you can possibly imagine, not even excepting
cybernetic pyrotechnics.
=cut
sub snazzle {
my $arg = shift;
....
}
=item razzle
The razzle() function enables autodidactic epistemology generation.
=cut
sub razzle {
print "Epistemology generation unimplemented on this platform.\n";
}
Perl
CPAN, pod.
pod , , .
pod Perl
, pod
.
pod =for, , =begin/=end
. pod .
, , pod,
=cut.
print " 1\n";
688
26. POD
=for commentary
,
"commentary".
pod, .
print " 2\n";
=cut
# ok,
print " 3\n";
=begin comment
print " 4\n";
, ,
, 1, 3 6. ,
pod . ,
,
.
Perl pod ,
pod . pod
,
. pod pod
, Pod::Parser.
, ,
.
: (verbatim paragraphs),
(command paragraphs) (prose paragraphs).
,
, , , .
, . .
. ,
,
.
, .
< < .
pod
689
pod
pod =
. ,
.
,
. (
pod):
=head1
=head2
...
=head1, =head2,... .
.
.SH .SS man(7)
<H1>...</H1> <H2>...</H2> HTML.
.
=cut
=cut pod. (
pod, pod.)
=pod
=pod ,
=cut.
, pod .
=over NUMBER
=item SYMBOL
=back
=over ,
, =item.
=back. NUMBER ( )
(), .
,
, , , ,
, . ( ,
.)
SYMBOL
. :
=over 4
=item *
Mithril armor ( )
=item *
690
26. POD
Elven cloak ( )
=back
:
=over 4
=item 1.
First, speak "friend" ( "").
=item 2.
Second, enter Moria ( ).
=back
:
=over 4
=item armor()
Description of the armor() function
=item chant()
Description of the chant() function
=back
,
:
=item =over/=back; =item
=over/=back; , , ,
.
=item *, , =item 1.,
=item 2. . ., ,
=item foo, =item bar . .
, ,
=item ,
.
, pod,
.
( , ), =item,
. , pod2html
: ,
,
<OL> </OL>,
HTML. (feature) ,
, .
pod
691
=for TRANSLATOR
=begin TRANSLATOR
=end TRANSLATOR
=for, =begin =end ,
,
. , TRANSLATOR
, ;
. =for ,
.
=for html
<p> This is a <flash>raw</flash> <small>HTML</small> paragraph </p>
TRANSLATOR, ,
roff, man, troff, nroff, tbl, eqn, latex, tex, html text.
.
comment ,
.
.
=for later.
, =begin =end ,
,
pod, =word.
, =begin foo,
, =end foo.
692
26. POD
pod
. ,
,
: ,
.
.
,
. ,
. pod
.
.
/ ( )
.
,
, (
) .
.
pod :
I<text>
, ,
, , ,
perlpod(1).
B<text>
, ,
.
C<text>
, ,
Courier. ,
,
.
S<text>
.
.
L<name>
:
L<name>
L<name/ident>
pod
693
L<name/"sec">
L<"sec">
( )
L</"sec">
, ,
text, , HTML:
L<text|name>
L<text|name/ident>
L<text|name/"sec">
L<text|"sec">
L<text|/"sec">
text / | < >
.
F<pathname>
. , I.
X<entry>
. , , .
pod .
E<escape>
, escape HTML:
E<lt>
< ( ,
)
E<gt>
> ( ,
)
E<sol>
/ ( L<>)
E<verbar>
| ( L<>)
E<NNN>
NNN, , ISO88591, Uni
code. , ...
E<entity>
HTML, E<Agrave>.
694
26. POD
Z<>
.
, . ,
, :
Z<>=can you see
From, >:
Z<>From here on out...
pod
. < >
. (
C<> .)
Perl, .
E:
C<$a E<lt>=E<gt> $b>
,
,
< > .
:
C<<< $a <=> $b >>>
C<<<< $a <=> $b >>>>
$a <=> $b .
,
, . ,
,
>>,
:
The C<< >> >> right shift operator.
Pod
695
, pod .
, "The I<Santa MarE<iacute>a> left port already",
The Santa Mara left port already, "B<touch> S<B<t>
I<time>> I<file>", touch t time file,
.
Pod
Perl pod,
pod ( pod )
. 8 .
pod2text
pod . 7
ASCII; 8, 8 ,
LE<uacute>thien ( Lthien)
EE<auml>rendil ( Erendil), ISO
88591 ( Unicode).
, pod, (, ,
) pod
:
% pod2text File.pm | more
, pod .
pod2man
pod Unix,
nroff(1)
troff(1). :
% pod2man File.pm | nroff man | more
:
% lpr Ppostscript tmppage.ps
pod2html
pod HTML,
:
% pod2html File.pm > tmppage.html
% lynx tmppage.html
% netscape remote "openURL(file:`pwd`/tmppage.html)"
696
26. POD
netscape, ,
netscape ,
. ,
lynx.
pod2latex
pod LATEX.
CPAN .
.
, pod :
This is a $variable right here
pod2html :
This is a <STRONG>$variable</STRONG> right here
pod2text ,
.
pod, ,
. , ,
. ,
, , ,
,
, . .
Perl, 1
:
FILEHANDLE
$scalar
@array
function()
manpage(3r)
somebody@someplace.com
http://foo.com/
Perl
pod, Pod::Checker (
podchecker) pod, Pod::Find
pod Pod::Parser
pod.
, pod
, pod ( ),
1
pod ,
Perl, .
pod
697
escape pod
. , secret
stuff , :
$a=3;
=secret stuff
warn "Neither POD nor CODE!?"
=cut back
print "got $a\n";
, warn pod.
pod ,
.
pod
Pod , , , .
pod
.
pod, (,
00) ,
pod.
, , olpod, pod:
#!/usr/bin/perl l00n
# olpod outline pod
next unless /^=head/;
s/^=head(\d)\s+/ ' ' x ($1 * 4 4)/e;
print $_, "\n";
, :
pod
pod
pod
pod
pod
pod
pod
Perl
pod
, pod .
pod pod ,
. , ,
698
26. POD
pod. , pod
pod pod:
#!/usr/bin/perl 00
# catpod cat pod
while (<>) {
if (! $inpod) { $inpod = /^=/;
}
if ($inpod) { $inpod = !/^=cut/; print; }
} continue {
if (eof)
{ close ARGV; $inpod = ''; }
}
Perl,
. ,
wc(1)1 , ,
catpod, pod:
% catpod MyModule.pm | wc
, pod
, Perl.
catpod, ,
:
#!/usr/bin/perl n00
# podlit pod
print if /^\s/;
? , perl
*wc , . grep(1)2,
:
% catpod MyModule.pm | podlit | grep funcname
,
( ) ,
. , ,
,
, .
, , .
, .
, , Perl
.
. pod, ,
,
1
2
pod
699
.
,
.
, .
Pod::Parser. ,
, ,
,
. . c ,
. ,
.
, catpod .
Pod::Parser .
, ,
. ,
, ,
, .
Pod::Parser.
, ,
.
, .
, . . ,
.
,
lugandlay.
catpod,
Pod::Parser
:
#!/usr/bin/perl
# catpod2,
package catpod_parser;
use Pod::Parser;
@ISA = qw(Pod::Parser);
sub command {
my ($parser, $command, $paragraph, $line_num) = @_;
my $out_fh = $parser>output_handle();
$paragraph .= "\n" unless substr($paragraph, 1) eq "\n";
$paragraph .= "\n" unless substr($paragraph, 2) eq "\n\n";
print $out_fh "=$command $paragraph";
}
sub verbatim {
my ($parser, $paragraph, $line_num) = @_;
my $out_fh = $parser>output_handle();
700
26. POD
print $out_fh $paragraph;
}
sub textblock {
my ($parser, $paragraph, $line_num) = @_;
my $out_fh = $parser>output_handle();
print $out_fh $paragraph;
}
sub interior_sequence {
my ($parser, $seq_command, $seq_argument) = @_;
return "$seq_command<$seq_argument>";
}
if (!caller) {
package main;
my $parser = catpod_parser::>new();
unshift @ARGV, '' unless @ARGV;
for (@ARGV) { $parser>parse_from_file($_); }
}
1;
__END__
=head1 NAME
, catpod
, . ,
, , ,
, ,
.
, !caller, , :
. , caller .
(
new), .
, ,
.
__END__,
pod /.
, , ,
. , .
pod
Pod , :
=back.
Perl
701
=for comment. =begin/=end.
=begin/=end
( pod).
=for.
pod ,
, . .
(\n). ,
.
.
pod ,
, . (, ,
pod , .)
L<>, "L<foo(1)>" ,
, the foo(1) manpage.
"the L<foo> manpage, ,
: the the foo(1) manpage man
page.
,
, L<show this text|foo>.
podchecker pod
. ,
pod ,
.
pod .
, .
, , (Eve
rything is Subject To Change at the Whim of the Random Hacker).
Perl
, ,
. ,
pod :
=head1 NAME
.
=head1 SYNOPSIS
, , ()
.
=head1 DESCRIPTION
. ( .)
702
26. POD
=head1 AUTHOR
. ( , .)
=head1 BUGS
(
).
=head1 SEE ALSO
(
).
=head1 COPYRIGHT
.
, :
Copyright 2013, Randy Waterhouse. All Rights Reserved.
:
This program is free software. You may copy or
redistribute it under the same terms as Perl itself.
[ .
, Perl]
: pod
__END__ __DATA__,
pod:
__END__
=head1 NAME
Modern I am the very model of a modern major module
=head1 , pod
(, ) .
27
Perl
Perl, ,
, Perl.
. Perl
www.perl.org www.perl.com. (
() www.wall.org/~larry.)
Perl. ,
:
Perl .
, Perl , ( ),
, Perl.
,
, 1986 ,
.
, VAX Sun
, 1200
.
(
, ),
(, ),
704
27. Perl
, rn, patch warp.1
, rn
, : ,
VAX Sun. . !
, , , :
, ,
, ,
. :
Bnews.2
, : append,
, synchronize,
.
(Re
vision Control System, RCS),
rn. .
.
,
. :
awk. , awk
,
.
. .
Perl.
( (Dan Faigin),
, (Mark Biggar),
, ).
.
Gloria, ( ).
, .
1
feeping creaturism (
. , , ,
creeping featurism, , ,
, . . .)
. , ,
? , rn,
, .
, , patch .
Usenet.
705
Pearl, Perl,
, PE
ARL, , ,
. , , Perl
. (
: Practical Extraction And Report Language.)
Perl Perl.
, ,
,
, rn, .
15 . Perl , sed awk,
.
.
, : ,
. Okay, . Perl
, .
(Henry Spencer) ,
. ,
, , .
Perl .1 , , .2
. Perl 1.0 18 1987
; Perl . Perl 2.0
1988, (Randal Schwartz)
Just Another Perl Hacker. 1989 (Tom
Christiansen) Perl Usenix
. Perl 3.0 1989
GNU Public License.
1990 Perl Poem (.
). The Pink
Camel ; 1991 .
Perl 4.0; GPL
(Artistic License).
Perl 5 1994.
Perl, .
Perl 5 The Economist. 1995
1
, ,
Jet Propulsion Lab, NetLabs Seagate.
, ,
OReilly & Associates ( ,
).
. Perl, rn
. Perl,
rn. .
rn Perl, .
706
27. Perl
Perl
, , Usenet
1 1990 . ,
,
. ,
. , Black Perl,
Perl 3,
Perl 5.
, , ,
Perl (Sharon Hopkins).
Perl, Perl,
Usenix 1992 ,
Camels and Needles: Computer Poetry Meets the Perl Programming Lan
guage ( :
Perl). ( misc/poetry.ps CPAN.)
Perl, ;
Economist Guardian:
#!/usr/bin/perl
APPEAL:
listen (please, please);
open yourself, wide; join (you, me),
connect (us,together),
tell me.
do something if distressed;
@dawn, dance;
@evening, sing;
read (books,$poems,stories) until peaceful;
study if able; write me ifyouplease;
sort your feelings, reset goals, seek (friends, family, anyone);
do*not*die (like this) if sin abounds;
keys (hidden), open (locks, doors), tell secrets;
do not, Ibegyou, close them, yet.
accept (yourself, changes), bind (grief, despair);
require truth, goodness ifyouwill, each moment;
select (always), length(ofdays)
# listen (a perl poem)
# Sharon Hopkins
# rev. June 19, 1995
707
Perl
Perl Poetry
Article 970 of comp.lang.perl:
Path: jpldevvax!pldexxav!lwall
From: lwall@jpldexxav.JPL.NASA.GOV (Larry Wall)
Newsgroups: news.groups,rec.arts.poems,comp.lang.perl
Subject: CALL FOR DISCUSSION: comp.lang.perl.poems
MessageID: 0401@jpldevvax.JPL.NASA.GOV
Date: 1 Apr 90 00:00:00 GMT
ReplyTo: lwall@jpldevvax.JPL.NSAS.GOV (Larry Wall)
Organization: Jet Prepulsion Laboratory, Pasadena, CA
Lines: 61
, ,
.
,
. , , comp.lang.perl rec.arts.poems,
Perl , . :
, , :
study, write, study,
do review (each word) if time.
close book. sleep? what's that?
Fort Lauderdale:
sleep, close together,
sort of sin each spring & wait;
50% die
, , . (
Pearl , , .)
BEFOREHAND: close door, each window & exit; wait until time.
open spellbook, study, read (scan, select, tell us);
write it, print the hex while each watches,
reverse its length, write again;
kill spiders, pop them, chop, split, kill them.
unlink arms, shift, wait & listen (listening, wait),
sort the flock (then, warn the "goats" & kill the "sheep");
kill them, dump qualms, shift moralities,
values aside, each one;
die sheep! die to reverse the system
you accept (reject, respect);
next step,
kill the next sacrifice, each sacrifice,
wait, redo ritual until "all the spirits are pleased";
do it ("as they say").
do it(*everyone***must***participate***in***forbidden**s*e*x*).
return last victim; package body;
exit crypt (time, times & "half a time") & close it,
select (quickly) & warn your next victim;
AFTERWORDS: tell nobody.
wait, wait until time;
wait until next year, next decade;
sleep, sleep, die yourself,
die at last
, Perl.
, ... comp.lang.perl.po
ems , perl poems
, . rec.arts.po
ems.perl , ,
. ( , rec.arts.poems
.) comp.lang.perl.poems ,
, :
print STDOUT q
Just another Perl hacker,
unless $spring
Larry Wall
lwall@jpldevvax.jpl.nasa.gov
28
, Perl .
, ,
( ).
,
:
use English;
.
, awk. ,
, , ,
.
, ( ),
.
. (
, 14 .)
.
.
,
Perl , ,
, .
( ).
712
28.
,
.
, ,
,
( $*). ,
, local,
. . 5 .
$*
$digits
@+ (@LAST_MATCH_END)
@ (@LAST_MATCH_START)
$+ ($LAST_PAREN_MATCH)
$^R ($LAST_REGEXP_CODE_RESULT)
$& ($MATCH)
$' ($POSTMATCH)
$` ($PREMATCH)
local,
,
,
. ,
, ,
. .
FileHandle 32 .
$|
$
$=
$~
$%
$^
($AUTOFLUSH, $OUTPUT_AUTOFLUSH)
($FORMAT_LINES_LEFT)
($FORMAT_LINES_PER_PAGE)
($FORMAT_NAME)
($FORMAT_PAGE_NUMBER)
($FORMAT_TOP_NAME)
.
, sort $a $b,
, , (
, strict).
713
$a
$b
@EXPORT
@EXPORT_OK
%EXPORT_TAGS
%FIELDS
@ISA
$VERSION
, . .
main, (
@F, main,
). ,
.
%ENV
%INC
%SIG
%!
%^H
@_
@ARGV
@F
@INC
$_ ($ARG)
$0 ($PROGRAM_NAME)
$ARGV
$!
$"
$#
$$
$(
$)
$,
$.
$/
$:
$;
($ERRNO, $OS_ERROR)
($LIST_SEPARATOR)
($PID, $PROCESS_ID)
($GID, $REAL_GROUP_ID)
($EGID, $EFFECTIVE_GROUP_ID)
($OFS, $OUTPUT_FIELD_SEPARATOR)
($NR, $INPUT_LINE_NUMBER)
($RS, $INPUT_RECORD_SEPARATOR)
($FORMAT_LINE_BREAK_CHARACTERS)
($SUBSEP, $SUBSCRIPT_SEPARATOR)
714
28.
DATA, ,
main,
:
_ ()
ARGV
ARGVOUT
DATA
STDIN
STDOUT
STDERR
Perl .
,
. ,
.
(. 10 ):
AUTOLOAD
(. 12 ):
DESTROY
(. die ):
PROPAGATE
(.
18 ):
BEGIN, CHECK, INIT, END
(. 14 ):
BINMODE, CLEAR, CLOSE, DELETE, EOF, EXISTS, EXTEND, FETCH, FETCHSIZE, FILENO, FIRSTKEY,
GETC, NEXTKEY, OPEN, POP, PRINT, PRINTF, PUSH, READ, READLINE, SEEK, SHIFT, SPLICE,
STORE, STORESIZE, TELL, TIEARRAY, TIEHANDLE, TIEHASH, TIESCALAR, UNSHIFT, WRITE
.
,
. (, , .)
715
,
:
XXX
, .
NOT
ALL
, .
PKG
; .
FHA
; .
DYN
( ALL).
LEX
RO
,
.
,
. English
,
[ALL].
method HANDLE EXPR
, File
Handle IO::. (
HANDLE>method (EXPR).)
select
.
FileHandle; ,
EXPR. ,
, autoflush, 1
, .
_ ()
[ALL] ,
stat, lstat
( w $file d $file).
$
[DYN,RO] $1, $2 . . (
)1 ,
. (: \.)
1
, Perl ,
$768, Perl , ,
, .
716
28.
$[ [XXX,LEX]
. 0, 1, Perl
awk ( FORTRAN)
index substr. $[ ,
. (
: [ .)
$# [XXX,ALL] , printf. $#
print,
OFMT awk. (: # ,
(sharp)1, ,
.)
$* [XXX,ALL] , !
( ) ,
Perl /m , /s.
(: * .)
$a [PKG] sort
($b ).
, $a, ,
sort, ,
.
sort. , use strict.
,
, , . .
sort.
$ACCUMULATOR
$^A
[ALL] write format.
formline, $^A.
write $^A .
$^A,
formline , .
. formline.
$ARG
$_ [ALL] .
:
while (<>) {...}
# while
while (defined($_ = <>)) {...}
chomp
chomp($_)
sharp ( #). . .
717
/^Subject:/
$_ =~ /^Subject:/
tr/az/AZ/
$_ =~ tr/az/AZ/
, Perl $_,
, :
, , print, unlink
, ord, pos int, ( t,
STDIN). ,
$_, 29 .
m// s///,
y/// tr///, =~.
foreach ( for
),
.
grep map. (
.)
,
<FH>, readline glob
while. whi
le while .
(:
.)
@ARG
@_ [ALL] ,
. . 6 .
split ,
.
ARGV
[ALL] ,
@ARGV.
: <>.
$ARGV
[ALL] ARGV
<> readline.
@ARGV
[ALL] , ,
. , $#ARGV
, . . $ARGV[0] ,
; scalar @ARGV
. $0.
718
28.
ARGVOUT
[ALL]
ARGV i $^I. . i
19 .
$b [PKG] , $a, sort.
. $a sort.
$BASETIME
$^T
[ALL] , ,
( Unix 1970 ). ,
M, A C,
.
$CHILD_ERROR
$? [ALL] , , ``
( ) wait, waitpid system.
, , 16 ,
wait(2) wa*
itpid(2) ( ). ,
, . . $? >> 8; $? & 127
, ( )
, $? & 128 ,
. (: $? sh .)
END $? ,
exit. $? END,
.
VMS use vmsish 'status' $?
VMS POSIX.
h_errno C,
$?, get
host*().
$COMPILING
$^C
[ALL] , c,
MO perlcc(1),
.
, , , AUTOLOAD
,
. . 18 .
DATA
[PKG] ,
__END__ __DATA__. __END__
main::DATA
719
. __DATA__ DATA ,
,
DATA, ()
.
$DEBUGGING
$^D
[ALL] ,
D; . 19
. (: D.)
$EFFECTIVE_GROUP_ID
$EGID
$) [ALL] GID (ID ) .
, $)
,
. , getegid(2),
, getgroups(2),
.
, , $),
.
GID, ( )
setgroups(2). setgroups ,
GID; , GID
5 setgroups, :
$) = "5 5";
(: .
GID , setgid.) :
$<, $>, $( $) ,
setid. $( $)
, setregid(2).
$EFFECTIVE_USER_ID
$EUID
$> [ALL] UID ,
geteuid(2). :
$< = $>;
# uid
($<,$>) = ($>,$<); # uid
(: UID, setuid.)
: $< $> ,
setreuid(2). .
%ENV
[ALL] , .
%ENV ,
720
28.
, . (
, Unix.)
$ENV{PATH}
$ENV{PAGER}
$ENV{LESS}
system "man
= "/bin:/usr/bin";
= "less";
= "MQeicsnf"; # less(1)
perl";
#
delete,
undef .
, , cron*
tab(5), .
( ,
cron, , , .) ,
$ENV{PATH}, $ENV{SHELL}, $ENV{BASH_ENV} $ENV{IFS},
setuid. . 23 .
$EVAL_ERROR
$@ [ALL]
Perl eval. (: (at)
?) $! ($OS_ERROR),
, , $@
( ), eval
,
( ), .
.
,
$SIG{__WARN__}, .
, $@ ,
. , ,
,
.
:
die if $@;
$@>PROPAGATE, ,
. (
propagated at.)
$EXCEPTIONS_BEING_CAUGHT
$^S
[ALL] ,
eval
. ,
, $SIG{__DIE__}
$SIG{__WARN__}. (: state of eval.)
721
$EXECUTABLE_NAME
$^X
[ALL] , perl,
argv[0] C (. $PROGRAM_NAME ).
@EXPORT
[PKG] import Expor
ter ,
use
:DEFAULT.
use strict, our
,
. ,
EXPORT, .
. 11 .
@EXPORT_OK
[PKG] import Expor
ter, , .
use strict. . 11 .
%EXPORT_TAGS
[PKG] import Exporter,
, use
POSIX ":sys_wait_h".
. ,
, ,
, @EXPORT @EX
PORT_OK. use strict. .
11 .
$EXTENDED_OS_ERROR
$^E
[ALL] ,
. Unix $^E $! ($OS_ERROR),
OS/2, VMS, Microsoft MacPerl.
Perl . ,
$!, $^E. (:
.)
@F
[PKG] , ,
a. ,
(
@main::F, ).
722
28.
%FIELDS
[NOT,PKG]
use fields .
. use fields, use base use fields
12 .
format_formfeed HANDLE EXPR
$FORMAT_FORMFEED
$^L
[ALL] , write
. "\f".
format_lines_left HANDLE EXPR
$FORMAT_LINES_LEFT
$ [FHA] ,
, format
write. (: __ _.)
format_lines_per_page HANDLE EXPR
$FORMAT_LINES_PER_PAGE
$= [FHA] ( ),
format write. 60. (
: = .)
format_line_break_characters HANDLE EXPR
$FORMAT_LINE_BREAK_CHARACTERS
$: [ALL] ,
( ^) .
" \n",
. (: colon ()
, .
)
format_name HANDLE EXPR
$FORMAT_NAME
$~ [FHA]
. . (
: $^.)
format_page_number HANDLE EXPR
$FORMAT_PAGE_NUMBER
$% [FHA]
, format write. (: %
troff(1). , , troff?)
723
@INC
[ALL] , , do FILE, require use
Perl.
I PERL5LIB,
Perl , :
/usr/local/lib/perl5/5.6.0/sun4solaris
/usr/local/lib/perl5/5.6.0
/usr/local/lib/perl5/site_perl/5.6.0/sun4solaris
/usr/local/lib/perl5/site_perl/5.6.0
/usr/local/lib/perl5/site_perl/5.00552/sun4solaris
/usr/local/lib/perl5/site_perl/5.00552
/usr/local/lib/perl5/site_perl/5.005/sun4solaris
/usr/local/lib/perl5/site_perl/5.005
/usr/local/lib/perl5/site_perl
724
28.
., .
, use lib,
,
, (
, , ,
XS):
use lib "/mypath/libdir/";
use SomeMod;
$INPLACE_EDIT
$^I
[ALL] .
undef.
, i. ,
:
% perl i.orig pe 's/foo/bar/g' *.c
:
local $^I = '.orig';
local @ARGV = glob("*.c");
while (<>) {
s/foo/bar/g;
print;
}
(: i.)
$INPUT_LINE_NUMBER
$NR
$. [ALL] ( )
, (
seek tell).
,
, . $/
($INPUT_RECORD_SEPARATOR). close
. <> close ,
ARGV ( . eof).
$. Perl ,
. (:
. .)
$INPUT_RECORD_SEPARATOR
$RS
$/ [ALL] ,
, readline, <FH>
725
chomp. RS awk
. (
.) ,
,
awk .
, $/ "\n\n" ,
"", ,
. ""
. "\n\n" ,
Perl
.
$/ ,
:
undef $/;
$_ = <FH>;
s/\n[ \t]+/ /g;
# $_
undef, $/
local:
{
local $/;
$_ = <FH>;
}
$/ , , ,
, ,
readline <FH> (
)
, . :
$/ = \32768; # \"32768" \$scalar_var_containing_32768
open(FILE, $myfile);
$record = <FILE>;
32 768 FILE.
, (
, ), ,
726
28.
, .
, .
, /
read(3); VMS.
chomp, $/
, . . 0
() l () 19 . (
: /
.)
@ISA
[PKG] ,
, .
, .
use base. use strict.
. 12.
@LAST_MATCH_END
@+ [DYN,RO]
. $+[0]
. , pos
, . ( ,
,
,
.) n
n ,
$+[1] $1, $+[2] $2 . .
$#+ ,
. . @ (@LAST_MATCH_START).
$var:
$` , substr($var, 0, $[0])
@LAST_MATCH_START
@ [DYN,RO]
. $[0]
. n n
, $[1] $1, $[2]
727
$2 . . $# ,
. . @+ (@LAST_MATCH_END).
$LAST_PAREN_MATCH
$+ [DYN,RO]
. , ( ),
. (:
.) :
$rev = $+
$LAST_REGEXP_CODE_RESULT
$^R
[DYN] ,
, (?{ CO
DE }). $^R
.
Perl
(?{ CODE }).
$^R,
, $^R.
, $^R ,
$1 .
$^R
, . , *
.
$^R .
(?{ CODE })
(?(COND) IFTRUE|IFFALSE), $^R .
$LIST_SEPARATOR
$" [ALL]
( ), ,
.
. (: , .)
$^M
[ALL] .
perl
$^M, .
Perl DPERL_EMERGENCY_SBRK malloc Perl, :
$^M = 'a' x (1 << 16);
64 K . ,
, INSTALL
Perl.
728
28.
, (
, ).
$MATCH
$& [DYN,RO] ,
. (
: & .)
$OLD_PERL_VERSION
$] [ALL] + (patchlevel)/1000.
Perl. (: Perl
?) :
warn "No checksumming!\n" if $] < 3.019;
die "Must have prototyping available\n" if $] < 5.003;
729
C #define, ENOENT.
.
autoflush HANDLE EXPR
$OUTPUT_AUTOFLUSH
$AUTOFLUSH
$| [FHA] ,
print, printf write
. ( .
,
.)
, , STDOUT
, ,
, .
, , ,
Perl rsh(1)
.
,
,
.
, STDOUT . select
. (: ,
.)
,
getc 29
POSIX 32.
$OUTPUT_FIELD_SEPARATOR
$OFS
$, [ALL] ( ) print.
print ,
. ,
awk OFS , . (
: , , print ,.)
$OUTPUT_RECORD_SEPARATOR
$ORS
$\ [ALL] ( )
print. print
.
, awk ORS
, print. (: $\
"\n", . , /, ,
Perl.) .
l ( line) 19.
730
28.
%OVERLOAD
[NOT,PKG]
overload
. . 13 .
$PERLDB
$^P
[NOT,ALL] Perl (perld).
$PERL_VERSION
$^V
[ALL] , Perl,
. V, ,
, $^V ,
$], . .
, + /1000 +
/1 000 000. UTF8:
chr($revision) . chr($version) . chr($subversion). , $^V
. :
printf "%vd", $^V;
,
, ,
Perl, , . (
, v,
Perl.) :
warn " 'our'!\n" if $^V lt v5.6;
# abc:def:ghi
Perl ,
,
$` $'
.
,
731
,
, . ,
:
$_ = 'abcdefghi';
/(.*?)(def)(.*)/s;
print "$1:$2:$3\n";
# /s , $1
# abc:def:ghi
$PREMATCH
$' [DYN,RO] , ,
. (: `
.) . $'.
$PROCESS_ID
$PID
$$ [ALL] (PID) Perl, .
fork.
$$ ; , ,
PID . . (: ,
.)
$$,
: $$alphanum.
${$}alphanum, ${$alphanum}.
$PROGRAM_NAME
$0 [ALL] , Perl.
$0 :
, ps(1).
,
. .
(: , sh, ksh, bash . .)
$REAL_GROUP_ID
$GID
$( [ALL] ID (GID) .
, $(
, .
, getgid(2),
, getgroups(2),
.
, $(, ,
GID. , $(,
$(,
, , .
, . . $) ($EF
FECTIVE_GROUP_ID), .
732
28.
(: .
GID , (left),
setgid.)
$REAL_USER_ID
$UID
$< [ALL] ID (UID) ,
getuid(2). ,
. $> ($EFFEC
TIVE_USER_ID). (: UID, ,
setuid.)
%SIG
[ALL] ,
. (. 16 .)
:
sub handler {
my $sig = shift; # 1
syswrite STDERR, " SIG$sig \n";
#
# , .
# ( .)
close LOG;
# I/O, !
exit 1;
# , .
}
$SIG{INT}
$SIG{QUIT}
...
$SIG{INT}
$SIG{QUIT}
= \&handler;
= \&handler;
= 'DEFAULT';
= 'IGNORE';
#
# SIGQUIT
%SIG ,
, .
. ,
"DEFAULT" "IGNORE",
, ,
, main.
:
$SIG{PIPE} = "Plumber";
$SIG{PIPE} = \&Plumber;
# okay, main::Plumber
# , Plumber
%SIG .
, $SIG{__WARN__}, ,
.
. __WARN__
STDERR.
, :
733
:
use warnings qw/FATAL all/;
eval $proggie;
,
, .
, $SIG{__DIE__},
, .
,
,
. ,
! .
.
__DIE__
, ,
goto, die.
__DIE__ ,
die __DIE__. ( ,
.)
$SIG{__WARN__} .
$SIG{__DIE__} ,
. ,
$SIG{__DIE__}.
,
, ,
.
, ,
local, .
.
eval {}.
STDERR
[ALL]
.
STDIN
[ALL]
.
STDOUT
[ALL]
.
734
28.
$SUBSCRIPT_SEPARATOR
$SUBSEP
$; [ALL] .
:
$foo{$a,$b,$c}
:
$foo{join($;, $a, $b, $c)}
:
@foo{$a,$b,$c}
# , @
:
($foo{$a},$foo{$b},$foo{$c})
$VERSION
[PKG] ,
, use SomeMod 2.5. $Some
735
Mod::VERSION ,
. UNIVERSAL>VERSION,
VERSION ,
, . . 12.
$WARNING
$^W
[ALL] (
,
).1 . use warnings
31 W X
,
. (: w.)
${^WARNING_BITS}
[NOT,ALL] , use
warnings. . use warnings 31.
${^WIDE_SYSTEM_CALLS}
[ALL] , ,
Perl, API ,
, .
C. 0
Perl 5.6, Perl
1,
(, $ENV{LC_CTYPE}).
use bytes
.
29
Perl ,1
.
. ,
THIS, , ,
, (
) .
,
.
, , .
.
() LIST (
). , LIST, ( =>,
).
,
,
. ,
,
. , .
, , LIST
. (
1
, . ,
endpwent, getpwent.
737
.)
Perl
;
. , ,
:
, ,
. ,
. ,
,
:
print 1+2*4;
print(1+2) * 4;
print (1+2)*4;
print +(1+2)*4;
print ((1+2)*4);
#
#
#
#
#
9.
3!
3!
12.
12.
Perl w, .
,
:
print (...) interpreted as function at line 2.
[print (...) 2]
Useless use of integer multiplication in void context at line 2.
[ 2]
.
, chmod
() :
chmod 0644, @array;
chmod :
chmod LIST
:
unshift @array, 0644;
chmod @array;
, chmod
, ,
.
,
.
738
29.
LIST,
. , push
:
push ARRAY, LIST
, push
, .
LIST . ( , . .
.) ,
LIST, ,
.
.
, push. ( , .)
, C,
.
function function(2), ,
C ,
.
, ,
(manpages). ( , .)
, , (/etc/shadow),
. . Perl,
C Unix, Unix
. ,
flock(2) fork(2), Perl, ,
, .
, C
, Perl.
, Perl , ,
, Perl .
Perl C, /.
Perl,
(, chown(2), fork(2), closedir(2) . .),
undef ,
. C
, 1 .
wait, waitpid syscall.
$! ($OS_ERROR).
, .
, ,
,
Perl
739
( undef)
.
, ( ).
: ,
, . .
, . ,
,
, .
,
.
. .
,
, .
. Perl ,
, .
:
.
. ,
, 8 . ,
, Unicode,
.
. Perl
use bytes, ,
, use bytes
Unicode . , use bytes
, ,
Perl. , , ,
reverse , ,
,
.
Perl
Perl , ,
. .
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q//, qq//,
reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
m// pos, qr//, quotemeta, s///, split, study
abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand
740
29.
pop, push, shift, splice, unshift
grep, join, map, qw//, reverse, sort, unpack
delete, each, exists, keys, values
binmode, close, closedir, dbmclose, dbmopen, die, eof, fileno, flock, format, getc,
print, printf, read, readdir, readpipe, rewinddir, seek, seekdir, select (
), syscall, sysread, sysseek, syswrite, tell, telldir,
truncate, warn, write
pack, read, syscall, sysread, sysseek, syswrite, unpack, vec
,
chdir, chmod, chown, chroot, fcntl, glob, ioctl, link, lstat, mkdir, open, opendir,
readlink, rename, rmdir, select ( ), select (
), stat, symlink, sysopen, umask, unlink, utime
caller, continue, die, do, dump, eval, exit, goto, last, next, redo, return, sub,
wantarray
caller, import, local, my, no, our, package, use
defined, dump, eval, formline, lock, prototype, reset, scalar, undef, wantarray
alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe, qx//, setpgrp, set
priority, sleep, system, times, wait, waitpid
do, import, no, package, require, use
bless, dbmclose, dbmopen, package, ref, tie, tied, untie, use
accept, bind, connect, getpeername, getsockname, getsockopt, listen, recv, send,
setsockopt, shutdown, socket, socketpair
System V
msgctl, msgget, msgrcv, msgsnd, semctl, semget, semop, shmctl, shmget, shmread,
shmwrite
Perl
741
endgrent, endhostent, endnetent, endpwent, getgrent, getgrgid, getgrnam, getlo
gin, getpwent, getpwnam, getpwuid, setgrent, setpwent
endprotoent, endservent, gethostbyaddr, gethostbyname, gethostent, getnetby
addr, getnetbyname, getnetent, getprotobyname, getprotobynumber, getprotoent,
getservbyname, getservbyport, getservent, sethostent, setnetent, setprotoent,
setservent
Perl
.
:
$_ ($ARG) .
$! ($OS_ERROR) .
; eval
$@ ($EVAL_ERROR).
$? ($CHILD_ERROR) .
.
, .
.
,
.
.
,
.
,
, , .
, %ENV @ARGV,
.
, , ,
, (,
, bless . .).
, ,
. ,
,
742
29.
.
, , ,
, .
.
C Unix,
, , Unix, .
, , :
, fork Win32, 5.6 Perl.
perlport,
, Perl
.
,
, , ,
, sqrt(1).
abs
abs VALUE
abs
.
$diff = abs($first $second);
:
( use strict) my
, :
my $diff = abs($first $second);
my .
, ,
.
accept
accept SOCKET, PROTOSOCKET
,
. PROTOSOCKET
, socket
INADDR_ANY.
, SOCKET
. PROTOSOCKET
; ,
. ,
,
. :
atan2
743
, ,
exec ,
$^F ($SYSTEM_FD_MAX).
. accept(2). . 16
.
alarm
alarm EXPR
alarm
SIGALRM EXPR
.
.
. EXPR, 0,
.
.
print " : ";
alarm(60);
#
$answer = <STDIN>;
$timeleft = alarm(0); #
print " $timeleft \n";
alarm sleep ,
sleep(3)
alarm(2).
.
,
. . 16 .
,
, syscall setitimer(2),
. CPAN Time::HiRes
.
atan2
atan2 Y, X
Y/X
. :
$pi = atan2(1,1) * 4;
744
29.
tan
Math::Trig POSIX :
sub tan { sin($_[0]) / cos($_[0]) }
bind
bind SOCKET, NAME
() ,
SOCKET.
. NAME
, .
use Socket;
$port_number = 80;
# ,
$sockaddr = sockaddr_in($port_number, INADDR_ANY);
bind SOCK, $sockaddr or die " $port_number: $!\n";
. bind(2). . 16.
binmode
binmode FILEHANDLE, DISCIPLINES
binmode FILEHANDLE
FILEHANDLE ,
DISCIPLINES. DISCIPLINES ,
( raw) . FILEHANDLE
,
.
binmode open,
.
,
. .
binmode
,
. binmode
, .
Unicode
, Unix Mac.
( Perl),
, Perl
. Perl Uni
code: UTF8. ,
UTF8,
binmode
745
Unicode. Perl,
( ) .1
, ":text" Perl
, ,
. , ":utf8" ":latin1", Perl,
. ,
":raw" Perl,
. , ( )
, open.
, binmode DISCIP
LINES, . . binmode, :
binmode FILEHANDLE, ":raw";
, Perl ,
. ,
\n ( ).
\n ,
,
,
. ( MSDOS VMS)
, \n, ,
. , ,
\cM\cJ,
\n, \n
\cM\cJ. binmode
.
DISCIPLINES binmode
Unix Mac OS, \n
. (
: Unix \cJ, Mac \cM.
.)
, Perl
GIF .
,
, .
":raw"
GIF,
, STDOUT:
binmode STDOUT;
open(GIF, "vimpower.gif") or die " vimpower.gif: $!\n";
binmode GIF;
1
, ,
.
746
29.
bless
bless REF, CLASSNAME
bless REF
, REF,
CLASSNAME
, CLASSNAME . REF
, . bless
, . .
. :
$pet = Beast>new(TYPE => "cougar", NAME => "Clyde");
# then in Beast.pm:
sub new {
my $class = shift;
my %attrs = @_;
my $self = { %attrs };
return bless($self, $class);
}
, ,
.
Perl (
). (, SCALAR, AR
RAY, HASH . ., UNIVERSAL)
,
.
, CLASSNAME ;
.
curse ()
. ( sin ().) . 12 ,
( ) .
caller
caller EXPR
caller
. , ,
,
:
($package, $filename, $line) = caller;
chdir
747
,
__PACKAGE__ __FILE__, 2
:
sub careful {
my ($package, $filename) = caller;
unless ($package eq __PACKAGE__ && $filename eq __FILE__) {
die " , $package!\n";
}
print " \n";
}
sub safecall {
careful();
}
caller EXPR
, . , 0
, 1 , 2 ,
, . .
, :
$i = 0;
while (($package, $filename, $line, $subroutine,
$hasargs, $wantarray, $evaltext, $is_require,
$hints, $bitmask) = caller($i++) )
{
...
}
, $hasargs
, @_ (
). $subroutine "(eval)",
, eval.
$evaltext $is_require: $is_require
, require use, $evaltext
eval EXPR. , eval BLOCK
$filename "(eval)", $evaltext . (
, use require eval EXPR.)
$hints $bitmask :
, ,
.
caller
@DB::args , ,
DB. . 20 Perl.
chdir
chdir EXPR
chdir
748
29.
EXPR,
. EXPR ,
.
.
chdir "$prefix/lib" or die " cd $prefix/lib: $!\n";
. Cwd, 32 ,
.
chmod
chmod LIST
.
, chmod(2).
. ,
$cnt = chmod 0755, 'file1', 'file2';
$cnt 0, 1 2 ,
. ,
, .
,
, . .
. $!, .
:
chmod(0755, @executables) == @executables
or die " chmod @executables: $!";
, ,
:
@cannot = grep {not chmod 0755, $_} 'file1', 'file2', 'file3';
die "$0: could not chmod @cannot\n" if @cannot;
grep
, chmod .
oct.
, Perl
, 0.
$DEF_MODE = 0644; # !
PROMPT: {
print "New mode? ";
$strmode = <STDIN>;
exit unless defined $strmode; # eof
if ($strmode =~ /^\s*$/) {
#
749
chomp
$mode = $DEF_MODE;
}
elsif ($strmode !~ /^\d+$/) {
print " , $strmode\n";
redo PROMPT;
}
else {
$mode = oct($strmode);
# "755" 0755
}
chmod $mode, @files;
}
Unix chmod(2).
, chmod(1),
File::chmod CPAN.
S_I* Fcntl:
use Fcntl ':mode';
chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
, , 0755. .
chomp
chomp VARIABLE
chomp LIST
chomp
() ,
, .
chop ( ),
,
. ,
$/, .
chop, chomp .
$/ "" ( , ), chomp
(
, LIST). chomp
, .
:
while (<PASSWD>) {
chomp; # \n
@array = split /:/;
...
}
5.6 chomp ,
$/
750
29.
. ,
(,
Unicode), , , chomp
.
chop
chop VARIABLE
chop LIST
chop
. chop
, ,
. , ,
chomp, chop ,
, , chomp .
chop , .
chop ,
:
@lines = `cat myfile`;
chop @lines;
chop , ,
:
chop($cwd = `pwd`);
chop($answer = <STDIN>);
,
$answer = chop($tmp = <STDIN>); #
$answer, chop
, (
$tmp).
substr:
$answer = substr <STDIN>, 0, 1;
:
chop($answer = <STDIN>);
chop substr:
$last_char = chop($var);
$last_char = substr($var, 1, 1, "");
chown
751
, .
substr ,
. $caravan:
substr($caravan, 5) = "";
substr ,
.
substr ,
chop:
$tail = substr($caravan, 5, 5, "");
chown
chown LIST
.
UID GID .
1
.
. :
chown($uidnum, $gidnum, 'file1', 'file2') == 2
or die " chown file1 file2: $!";
$cnt 0, 1 2 ,
( , ,
). :
chown($uidnum, $gidnum, @filenames) == @filenames
or die " chown @filenames: $!";
, ,
ID chown:
sub chown_by_name {
my($user, @files) = @_;
chown((getpwnam($user))[2,3], @files) == @files
or die " chown @files: $!";
}
chown_by_name("fred", glob("*.c"));
, ,
, /etc/passwd
, ,
/etc/gro*
up. , 1 GID,
. 1 UID GID,
, .
752
29.
,
.
, .
POSIX , ,
:
use POSIX qw(sysconf _PC_CHOWN_RESTRICTED);
# ,
if ($> == 0 || !sysconf(_PC_CHOWN_RESTRICTED) ) {
chown($uidnum, 1, $filename)
or die " chown $filename $uidnum: $!";
}
chr
chr NUMBER
chr
,
NUMBER. , chr(65) A ASCII, Unicode,
chr(0x263a) Unicode. ,
chr, ord.
, (,
"\N{WHITE SMILING FACE}" Unicode),
charnames 31 .
chroot
chroot FILENAME
chroot
FILENAME
, /.
exec ,
fork chroot. chroot .
.
, , FTP:
chroot((getpwnam('ftp'))[7])
or die " ftp: $!\n";
, , ,
Unix. . chroot(2).
close
close FILEHANDLE
close
close
753
, , FI
LEHANDLE. ( ,
.) , ,
. FILEHANDLE,
open,
open . (. open.) close
($.),
open .
FILEHANDLE ,
(
, ,
).
, close
. close
$! ($OS_ERROR) . , close
, $!, ,
( ) (
). $? ($CHILD_ERROR)
(. system) ,
. :
open(OUTPUT, '| sort rn | lpr p') # sort lpr
or die " sortlpr: $!";
print OUTPUT @lines;
# output
close OUTPUT
# sort
or warn $! ? " sortlpr: $!"
: " $? sortlpr";
, dup(2) ,
, close
.
.
:
open(NETSTAT, "netstat rn |")
or die "can't run netstat: $!";
open(STDIN, "<&NETSTAT")
or die "can't dup to stdin: $!";
STDIN, , NETSTAT, .
, .
, $SIG{CHLD},
,
waitpid ID , open.
754
29.
closedir
closedir DIRHANDLE
, opendir,
, . .
readdir. DIRHANDLE ,
,
.
connect
connect SOCKET, NAME
,
accept.
. NAME
. ,
, SOCK :
use Socket;
my ($remote, $port) = ("www.perl.com", 80);
my $destaddr = sockaddr_in($port, inet_aton($remote));
connect SOCK, $destaddr
or die "Can't connect to $remote at port $port: $!";
, close shutdown. .
16. . connect(2).
cos
cos EXPR
cos
EXPR ( ). ,
,
:
# "" .
$pi = atan2(1,1) * 4;
$piover180 = $pi/180;
# .
for ($deg = 0; $deg <= 90; $deg++) {
printf "%3d %7.5f\n", $deg, cos($deg * $piover180);
}
, ,
acos() Math::Trig POSIX :
sub acos { atan2( sqrt(1 $_[0] * $_[0]), $_[0] ) }
755
crypt
crypt
crypt PLAINTEXT, SALT
crypt(3).
,1
, .
crypt ,
. ()
,
.
SALT (, crypt($plain, $crypted) eq $crypted).
crypt,
.
SALT, , ,
, [./09AZaz] (,
join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]).
crypt SALT, ,
, .
crypt(3).
, , ,
, :
$pwd = (getpwuid ($<))[1];
# , Unix.
, , ,
.
,
,
.
, ,
1
, .
756
29.
,
crypt , /etc/passwd /etc/shadow.
crypt
, .
by*module/Crypt by*module/PGP
CPAN, ,
.
dbmclose
dbmclose HASH
DBM ( ) .
dbmclose untie
, Perl.
dbmopen
dbmopen HASH, DBNAME, MODE
DBM (. . ).
(DBM database management
C,
.) HASH
( %). DBNAME (
.dir .pag).
MODE, ;
MODE, umask.
, MODE undef,
, .
, dbmopen, .
dbmopen tie
,
Perl. , DBM ,
tie
dbmopen. ,
DB_File,
Netscape:
use DB_File;
dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.dat", undef)
or die " netscape history: $!";
while (($url, $when) = each %NS_Hist) {
next unless defined($when);
chop ($url, $when);
#
printf "Visited %s at %s.\n", $url,
scalar(localtime(unpack("V",$when)));
}
defined
757
DBM,
, . ,
, w $file,
eval {},
.
, keys values,
DBM.
DBM
each, .
, DBM, ,
DBM,
.
. . DB_File 32
.
, DBM
, null,
C. Netscape
sendmail. "$key\0"
, null:
$alias = $aliases{"postmaster\0"};
chop $alias; # null
DBM
. . GDBM_File
.
.
defined
defined EXPR
defined
, , EXPR
. ,
, , ,
, ,
, , , undef.
,
,
undef.
undef
, , ,
, . .
undef ,
undef, ,
758
29.
0 . defined
, ,
.
, :
print if defined $switch{D};
defined ,
, , .
, ,
, . exists, ,
.
,
, :
print "$val\n" while defined($val = pop(@ary));
getpwent,
.
setpwent();
while (defined($name = getpwent())) {
print "<<$name>>\n";
}
endpwent();
,
, :
die "Can't readlink $sym: $!"
unless defined($value = readlink $sym);
defined , ,
.
( , , ):
indir("funcname", @arglist);
sub indir {
my $subname = shift;
no strict 'refs'; # subname
if (defined &$subname) {
&$subname(@_);
# $subname>(@_);
}
else {
warn " $subname";
}
}
defined ( )
. ( ,
delete
759
.)
:
if (@an_array) { print " \n" }
if (%a_hash) { print " \n" }
. undef exists.
delete
delete EXPR
( )
. (. unlink, .)
, ,
DBM, .
exists .
( : undef exists
, undef
, .)
%ENV . ,
DBM ( ),
DBM.
, Perl 5.6
. ,
, , . .
.
splice. (
, ( ,
( ))).
EXPR ,
:
#
$dungeon[$x][$y] = \%properties;
# property
delete $dungeon[$x][$y]{"OCCUPIED"};
# properties
delete @{ $dungeon[$x][$y] }{ "OCCUPIED", "DAMP", "LIGHTED" };
# %properties
delete $dungeon[$x][$y];
%hash:
760
29.
:
delete @hash{keys %hash};
,
( , ):
%hash = ();
undef %hash;
# %hash
# , %hash
, :
foreach $index (0 .. $#array) {
delete $array[$index];
}
:
delete @array[0 .. $#array];
, :
@array = ();
undef @array;
# @array
# , @array
die
die LIST
die
eval , LIST,
STDERR $! ( errno
C). $! 0, $? >> 8
(
system, wait, close `command`). $? >> 8 0,
255.
eval $@ ,
, eval,
undef. die , ,
,
. . eval
.
LIST , ,
,
$@.
761
die
LIST $@ (
eval),
"\t...propagated". (
) :
eval { ... };
die unless $@ =~ / /;
LIST $@ ,
$@>PROPAGATE, ,
.
LIST $@ , "Died".
LIST
( ),
,
( ), . :
, ", stopped",
"at scriptname line 123". ,
canasta;
die:
die "/usr/games is no good";
die "/usr/games is no good, stopped";
, :
/usr/games is no good at canasta line 123.
/usr/games is no good, stopped at canasta line 123.
,
, __FILE__
__LINE__:
die '"', _ _FILE_ _, '", line ', _ _LINE_ _, ", phooey on you!\n";
:
"canasta", line 38, phooey on you!
:
:
die "Can't cd to spool: $!\n"
chdir '/usr/spool/news'
chdir,
.
. exit, warn, %SIG Carp.
762
29.
do (block)
do BLOCK
do BLOCK
, .
while until, Perl BLOCK,
. (
.) do BLOCK ,
next, last redo
. , , .
4 .
do (file)
do FILE
do FILE FILE
Perl. (,
, ) Perl,
:
do 'stat.pl';
:
scalar eval `cat stat.pl`;
, do , ,
, ,
@INC %INC, . (.
28 .) , ,
do FILE,
, eval FILE .
, , ,
; ,
.
do , undef $!
. do , ,
undef, $@.
, do
.
( .pm)
use require,
. :
,
.
dump
763
do FILE , ,
. :
# : ,
for $file ("/usr/share/proggie/defaults.rc",
"$ENV{HOME}/.someprogrc")
{
unless ($return = do $file) {
warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!"
unless defined $return;
warn "couldn't run $file"
unless $return;
}
}
,
, do, .
do , require use.
do (subroutine)
do SUBROUTINE (LIST)
do SUBROUTINE (LIST)
. SUBROUTINE ,
. . 6 .
dump
dump LABEL
dump
.
, undump ( )
.
goto LABEL ( , goto).
, goto,
. LABEL , .
: , ,
, Perl. .
u 19 .
,
,
,
C.
, dump
,
764
29.
24 ,
Perl 18 .
, ,
, , .
each
each HASH
, /.
each ,
,
. each
. ,
,
, .
each .
, %ENV:
while (($key,$value) = each %ENV) {
print "$key=$value\n";
}
, .
each ,
, .
Perl,
, , keys (
values) () .
,
each, keys values;
, keys %hash values
%hash.
, :
.
. keys, values sort.
eof
eof FILEHANDLE
eof()
eof
,
FILEHANDLE FILEHANDLE
. FILEHANDLE ,
,
. eof
eval
765
. eof() ()
ARGV (
<>). while (<>) eof()
. eof
( ) while (<>). ,
:
while (<>) {
if (eof()) {
print "" x 30, "\n";
}
print;
}
,
:
#
while (<>) {
next if /^\s*#/;
#
print "$.\t$_";
} continue {
close ARGV if eof;
# eof()!
}
(..)
. , .
, , ,
, . , , eof
( ),
@ARGV.
: eof
ungetc(3),
. Perl
eof,
while. . foreach 4
.
eval
eval BLOCK
eval EXPR
eval
766
29.
eval Perl , .
: eval BLOCK eval EXPR.
() ,
, try C++
Java.
,
, .
,
. ,
. , eval
Perl.
eval
, .
return eval.
, , ,
eval.
, ,
wantarray.
(
die), eval undef
( ) $@. , $@
,
. :
eval { ... };
#
if ($@) { ... }
#
eval BLOCK ,
. (, eval EXPR,
.)
, , , eval
.
eval EXPR ,
. (
, , , $@.)
EXPR ,
Perl. Perl,
eval ,
. eval ,
, eval,
eval. (. my local.)
, .
Perl.
Perl,
, :
eval
767
rename,
Perl:
#!/usr/bin/perl
# rename
$op = shift;
for (@ARGV) {
$was = $_;
eval $op;
die if $@;
# ,
#
rename($was,$_) unless $was eq $_;
}
:
$ rename 's/\.orig$//'
*.orig
$ rename 'y/AZ/az/ unless /^Make/' *
$ rename '$_ .= ".bad"'
*.f
eval ,
, ,
(, fork symlink).
eval BLOCK
, . ,
eval
EXPR, eval BLOCK, BLOCK. :
#
eval { $answer = $a / $b; };
warn $@ if $@;
# ,
eval '$answer = $a / $b';
warn $@ if $@;
# ( )
eval { $answer = };
#
#
eval '$answer =';
# $@
BLOCK Perl,
. EXPR ,
.
768
29.
eval BLOCK ,
next, last redo .
exec
exec PATHNAME LIST
exec LIST
exec
, !!! system,
exec, .
exec ,
,
( ).
,
. ,
(/bin/sh Unix).
,
,
.
,
.
LIST LIST ,
, .
.
, ,
, ,
escape .
Perl echo,
:
exec 'echo', ' : ', @ARGV;
, exec
, .
exec "sort $outfile | uniq"
or die "Can't do sort/uniq: $!\n";
exec , ,
, $!, , . ,
Perl exec ( system) ,
$|
, exec
system.
Perl 5.6.
exec
769
( Perl exec),
, ,
( ) ,
.
, , . .
C. , ,
,
, ,
. ,
, . , vi ,
"vi" "view". "view",
,
R.
PATHNAME exec.
,
print printf. ,
. ( Perl
, ,
, ,
.) :
$editor = "/usr/bin/vi";
exec $editor "view", @files
#
or die " $editor: $!\n";
,
, , , ,
:
exec { "/usr/bin/vi" } "view" @files
# " "
or die " /usr/bin/vi: $!\n";
, exec
, .
. exec ( system )
,
.
@args = ("echo surprise"); #
exec @args
# escape
or die "exec: $!";
# , @args == 1
, PATHNAME,
,
, :
exec { $args[0] } @args #
or die "can't exec @args: $!";
770
29.
, , echo,
"surprise". :
, echo surprise,
( ) $!
, .
exec fork,
, , Perl,
. exec Perl END
DESTROY, . ,
,
. (
.)
exec system, Perl
, die, warn exit
w use warnings
qw(exec syntax). exec
, ,
:
exec ('foo') or print STDERR "couldn't exec foo: $!";
{ exec ('foo') }; print STDERR "couldn't exec foo: $!";
, exec
.
. system.
exists
exists EXPR
,
. ,
.
print "True\n"
print "Defined\n"
print "Exists\n"
if
$hash{$key};
if defined $hash{$key};
if exists $hash{$key};
print "True\n"
print "Defined\n"
print "Exists\n"
if
$array[$index];
if defined $array[$index];
if exists $array[$index];
, ,
, ,
.
EXPR , ,
:
771
exit
if (exists $hash{A}{B}{$key}) { ... }
,
, .
$$hash{"A"} $hash{"A"}>{"B"} .
exists ; ,
( ):
undef $ref;
if (exists $ref>{"Some key"}) { }
print $ref; # HASH(0x80d3d5c)
"Some key" ,
$ref .
, ( )
. ,
. ,
:
if ($ref
exists
exists
exists
exists
and
$ref>[$x]
and
$ref>[$x][$y]
and
$ref>[$x][$y]{$key} and
$ref>[$x][$y]{$key}[2] ) { ... }
EXPR , exists
,
. "Exists":
sub flub;
print "Exists\n"
print "Defined\n"
if exists &flub;
if defined &flub;
exists
AUTOLOAD, , ,
. ,
sub flub.
exit
exit EXPR
exit
EXPR
. EXPR
, 0 ( ).
, , x
X:
$ans = <STDIN>;
exit if $ans =~ /^[Xx]/;
772
29.
exit ,
,
. die,
eval. die Carp,
croak confess.
, exit ,
. , ,
END .
,
$?. ,
DESTROY, ,
.
,
_exit POSIX END .
POSIX , exec "/bin/false"
.
exp
exp EXPR
exp
e EXPR. e,
exp(1).
**, FORTRAN:
use Math::Complex;
print exp(1) ** (i * pi);
# 1
fcntl
fcntl FILEHANDLE, FUNCTION, SCALAR
, fcntl(2).
fcntl, , :
use Fcntl;
.
SCALAR ( )
FUNCTION. SCALAR
fcntl. ( SCALAR
, ,
, .) .
Fcntl FUNCTION.
fcntl ,
fcntl(2). , ,
773
fileno
, , exec (
$^F ($SYSTEM_FD_MAX)),
, lockf(3)
SIGIO .
REMOTE,
.
, ,
, .
. (
, , $|.)
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
$flags = fcntl(REMOTE, F_GETFL, 0)
or die " : $!\n";
$flags = fcntl(REMOTE, F_SETFL, $flags | O_NONBLOCK)
or die " : $!\n";
, fcntl ( ioctl):
,
, Perl
1
undef
, Perl
,
, :
$retval = fcntl(...) || 1;
printf "fcntl %d\n", $retval;
fileno
fileno FILEHANDLE
(file descriptor),
(filehandle). , fileno
undef.
, 0 1, , STDIN
STDOUT, . ,
.
774
29.
,
Perl ,
.
, , fileno
select ,
syscall(2). ,
open , ,
.
if (fileno(THIS) == fileno(THAT)) {
print "THIS THAT \n";
}
FILEHANDLE ,
,
.
:
Perl
. ,
. Perl ,
, , open
, ,
$^F
($SYSTEM_FD_MAX), 2. STDIN,
STDOUT STDERR 0, 1 2 (
Unix), ,
. 0, 1 2 ,
.
Unix , ,
.
flock
flock FILEHANDLE, OPERATION
flock Perl
, , .
, FILEHANDLE,
.
, Perl FILEHANDLE
. Perl flock flock(2),
fcntl(2), lockf(3)
, , flock
. .
16.
OPERATION LOCK_SH, LOCK_EX LOCK_UN,
LOCK_NB.
flock
775
1, 2, 8 4,
, Fcntl
, :flock.
LOCK_SH ,
. LOCK_EX
, . LOCK_UN
; .
LOCK_NB LOCK_SH LOCK_EX, flock
, .
, , . LOCK_NB
, ,
.
, flock ,
.
, ,
. , , flock,
, flock.
, ,
, , .
.
flock .
fcntl, (
) , (
).
Unix, flock(2) :
use Fcntl qw/:flock/;
# LOCK_*
sub mylock {
flock(MBOX, LOCK_EX)
or die " mailbox: $!";
# , , ,
# stdio
seek(MBOX, 0, 2)
or die " seek mailbox: $!";
}
open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
or die " mailbox: $!";
mylock();
print MBOX $msg, "\n\n";
close MBOX
or die " mailbox: $!";
, flock(2),
fork. ,
776
29.
, , . .
16 flock.
fork
fork
fork(2). ID
0 .
,
undef. (
) , ,
, .
Perl 5.6
, $|
, .
:
use Errno qw(EAGAIN);
FORK: {
if ($pid = fork) {
#
# pid $pid
}
elsif (defined $pid) { # $pid ,
#
# pid getppid
}
elsif ($! == EAGAIN) {
# EAGAIN
sleep 5;
redo FORK;
}
else {
#
die " : $!\n";
}
}
,
fork(2), , system,
, Perl
, fork
. ,
exit,
,
.
777
format
fork ,
( ,
). $SIG{CHLD}
"IGNORE"; wait
. . wait,
SIGCHLD 16.
,
STDIN STDOUT, ,
/dev/null.
,
.
(, CGI ,
) , . .
.
.
, fork(2),
, (,
(copyonwrite) ),
. , fork
( ) , Unix.
, Perl 5.6 fork Microsoft,
. ,
Win32::Process.
format
format NAME =
picture line
value list
...
.
(pic
ture lines) write.
NAME , STDOUT,
STDOUT. ,
sub, ,
, ,
, . ,
,
write. (
, $cost $quantity):
778
my $str = "widget";
29.
# .
format Nice_Output =
Test: @<<<<<<<< @||||| @>>>>>
$str,
$%,
'$' . int($num)
.
local $~ = "Nice_Output";
local $num = $cost * $quantity;
write;
# .
# .
, ,
(),
. typeglob
,
, , , ,
. , , Whate
ver local, *Whatever.
, , type
glob, .
7
. 28
, English IO::Handle
.
formline
formline PICTURE, LIST
, format,
. .
PICTURE,
, $^A ( $ACCUMULATOR,
English). , write $^A
, $^A
, $^A "".
formline , formline
, PICTURE. ,
~ ~~ PICTURE .
formline
, .
, ,
@ .
. 6 .
getc
getc FILEHANDLE
getc
getgrent
779
,
FILEHANDLE. undef
. FILEHANDLE ,
STDIN.
,
( , ) ,
.
. ,
,
,
.
()
. Unix :
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
} else {
system "stty", "icanon", "eol", "\001";
}
$key = getc;
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
} else {
system "stty", "icanon", "eol", "^@"; # ASCII NUL
}
print "\n";
(), ,
$key. stty , cbreak,
, $BSD_STYLE .
,
. stty(1)
.
POSIX
POSIX::getattr.
Term::ReadKey CPAN.
getgrent
getgrent
setgrent
endgrent
/etc/group ( /etc/group,
). getgrent
($name, $passwd, $gid, $members)
780
29.
$members
. GID :
while (($name, $passwd, $gid) = getgrent) {
$gid{$name} = $gid;
}
getgrent .
User::grent
. . getgrent(3).
getgrgid
getgrgid GID
.
($name, $passwd, $gid, $members)
$members
. ,
getgrent.
getgrgid .
User::grent . . get*
grgid(3).
getgrnam
getgrnam NAME
.
($name, $passwd, $gid, $members)
$members
. ,
getgrent.
getgrnam ID .
User::grent .
. getgrnam(3).
gethostbyaddr
gethostbyaddr ADDR, ADDRTYPE
( ). ADDR
, ADDRTYPE
gethostbyname
781
AF_INET ( Socket).
($name, $aliases, $addrtype, $length, @addrs) =
gethostbyaddr($packed_binary_address, $addrtype);
@addrs .
()
, , :
($a, $b, $c, $d) = unpack('C4', $addrs[0]);
v sprintf:
$dots = sprintf "%vd", $addrs[0];
inet_ntoa Socket
print. ,
IPv6.
use Socket;
$printable_address = inet_ntoa($addrs[0]);
gethostbyaddr .
ADDR , :
use Socket;
$ipaddr = inet_aton("127.0.0.1");
# localhost
$claimed_hostname = gethostbyaddr($ipaddr, AF_INET);
. 16. Net::hos
tent .
. gethostbyaddr(3).
gethostbyname
gethostbyname NAME
( ). :
($name, $aliases, $addrtype, $length, @addrs) =
gethostbyname($remote_hostname);
@addrs . (
) , , :
782
29.
v sprintf:
$dots = sprintf "%vd", $addrs[0];
gethostbyname :
use Socket;
$ipaddr = gethostbyname($remote_host);
printf "%s has address %s\n",
$remote_host, inet_ntoa($ipaddr);
16. Net::
hostent .
. gethostbyname(3).
gethostent
gethostent
sethostent STAYOPEN
endhostent
/etc/hosts
. gethostent :
($name, $aliases, $addrtype, $length, @addrs)
@addrs .
() ,
, :
($a, $b, $c, $d) = unpack('C4', $addrs[0]);
, gethostent, .
,
, .
gethostent .
gethostent(3).
Net::hostent
.
getlogin
getlogin
, . Un
ix utmp(5).
, getpwuid. :
$login = getlogin() || (getpwuid($<))[0] || "!!";
getpeername
783
getnetbyaddr
getnetbyaddr ADDR, ADDRTYPE
. :
use Socket;
($name, $aliases, $addrtype, $net) = getnetbyaddr(127, AF_INET);
getnetbyaddr . Net::
netent .
. getnetbyaddr (3).
getnetbyname
getnetbyname NAME
.
:
($name, $aliases, $addrtype, $net) = getnetbyname("loopback");
getnetbyname .
Net::netent
. . getnetbyname(3).
getnetent
getnetent
setnetent STAYOPEN
endnetent
/etc/networks.
:
($name, $aliases, $addrtype, $net) = getnetent();
getnetent .
Net::netent
. . getnetent(3).
;
IP ( ) .
getpeername
getpeername SOCKET
SOCKET. :
784
use Socket;
$hersockaddr
($port, $heraddr)
$herhostname
$herstraddr
29.
=
=
=
=
getpeername SOCK;
sockaddr_in($hersockaddr);
gethostbyaddr($heraddr, AF_INET);
inet_ntoa($heraddr);
getpgrp
getpgrp PID
PID
( PID, 0). getpgrp
, getpgrp(2).
PID ,
( , PID, 0). ,
POSIX getpgrp(2), PID
0.
getppid
ID .
Unix, ID 1, ,
init(8).
getpriority
getpriority WHICH, WHO
,
. . getpriority(2). getpriority
, getpriority(2).
BSD::Resource CPAN ,
PRIO_PROCESS, PRIO_PGRP PRIO_USER,
WHICH. 0, 1 2 ,
, #include C.
0 WHO ,
, :
$curprio = getpriority(0, 0);
getprotobyname
getprotobyname NAME
.
:
($name, $aliases, $protocol_number) = getprotobyname("tcp");
getpwent
785
getprotobyname
. Net::proto
. . getprotobyname(3).
getprotobynumber
getprotobynumber NUMBER
.
:
($name, $aliases, $protocol_number) = getprotobynumber(6);
getprotobynumber
. Net::proto
. . getprotobynumber (3).
getprotoent
getprotoent
setprotoent STAYOPEN
endprotoent
/etc/protocols.
getprotoent :
($name, $aliases, $protocol_number) = getprotoent();
getprotoent
. Net::proto
. . getprotent(3).
getpwent
getpwent
setpwent
endpwent
/etc/passwd,
/etc/shadow,
, NIS ( YP) NIS+,
. :
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwent();
, ,
. UID,
:
786
29.
getpwent .
User::pwent .
. getpwent(3).
getpwnam
getpwnam NAME
/etc/passwd. :
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwnam("daemon");
, ,
, . C
, , /etc/
shadow ( shadow). ,
. C , ,
Perl.
getpwent.
getpwnam ID
. User::pwent
. . getpwnam(3) passwd(5).
getpwuid
getpwuid UID
ID
/etc/passwd.
:
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwuid(2);
getpwent.
getpwuid .
User::pwent . . get*
pwnam(3) passwd(5).
getservbyname
getservbyname NAME, PROTO
()
. PROTO , "tcp".
:
getsockname
787
getservbyname
. Net::servent
. . getservbyname(3).
getservbyport
getservbyport PORT, PROTO
() .
PROTO , "tcp".
:
($name, $aliases, $port_number, $protocol_name) = getservbyport(80, "tcp");
getservbyport .
Net::servent . . get*
servbyport(3).
getservent
getservent
setservent STAYOPEN
endservent
/etc/services
. :
($name, $aliases, $port_number, $protocol_name) = getservent();
getservent .
Net::servent .
. getservent(3).
getsockname
getsockname SOCKET
SOCKET. ( ?
, , ,
, accept ,
.
, inetd.)
use Socket;
$mysockaddr = getsockname(SOCK);
($port, $myaddr) = sockaddr_in($mysockaddr);
$myname = gethostbyaddr($myaddr,AF_INET);
printf "I am %s [%vd]\n", $myname, $myaddr;
788
29.
getsockopt
getsockopt SOCKET, LEVEL, OPTNAME
undef
. . setsockopt.
glob
glob EXPR
glob
EXPR (expansions)
, (shell). ,
<*>.
csh(1), . Perl 5.6
, 5.6
. , .,
, . *
(
). ? .
[...]
, [chy09].
, *.[^oa], ,
, , ,
a o . ~
, ~/.*rc rc
, ~jane/Mail/* Jane.
, ~/.{mail,
ex,csh,twm,}rc rc.
,
, File::Glob
, glob
, <*.c *.h>.
File::Glob 32. glob ( <*>)
use,
,
.
open Perl , .
glob.
open(MAILRC, "~/.mailrc")
# :
or die " ~/.mailrc: $!";
open(MAILRC, (glob("~/.mailrc"))[0])
#
or die " ~/.mailrc: $!";
gmtime
789
gmtime
gmtime EXPR
gmtime
, time,
,
( GMT UTC, Zulu
, , , Zulu). (
, ) :
# 0
1
2
3
4
5
6
7
8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
, , EXPR , gmtime(ti
me()). Perl Time::Local timegm,
.
struct tm (
C ). ,
$mon 0..11, 0, $wday
0..6, 0. ,
,
,
.
, :
$london_month = (qw(Jan Feb Mar Apr May Jun
Jul Aug Sep Oct Nov Dec))[(gmtime)[4]];
790
29.
%a %b,
,
.
goto
goto LABEL
goto EXPR
goto &NAME
goto &NAME ,
,
. AUTOLOAD,
,
, ,
( @_
). goto caller ,
AUTOLOAD.
, ,
, ,
.
,
.
791
hex
grep
grep EXPR, LIST
grep BLOCK LIST
EXPR BLOCK
LIST, $_ ,
foreach .
, . (
Unix, ,
. Perl ,
.) grep
, .
@all_lines ,
:
@code_lines = grep !/^\s*#/, @all_lines;
$_
, $_ .
,
, . :
@list = qw(barney fred dino wilma);
@greplist = grep { s/^[bfd]// } @list;
hex
hex EXPR
hex
EXPR
. 0x, ,
. , 0,
0b 0x, oct. $number
4 294 906 560:
$number = hex("ffff12c0");
sprintf:
sprintf "%lx", $number;
# (l , .)
.
, , .
792
29.
import
import CLASSNAME LIST
import CLASSNAME
import . ,
( ) ,
use. . use.
index
index STR, SUBSTR, OFFSET
index STR, SUBSTR
.
SUBSTR STR. OFFSET, , ,
, .
0 ( ,
$[, ). ,
, . . 1.
, :
$pos = 1;
while (($pos = index($string, $lookfor, $pos)) > 1) {
print "Found at $pos\n";
$pos++;
}
int
int EXPR
int
EXPR. C
int , Perl
:
$average_age = 939/16;
# 58.6875 (58 C)
$average_age = int 939/16; # 58
,
0,
.
, int(6.725/0.025) 268, 269, . . ,
, 268.99999999999994315658.
sprintf, printf POSIX::floor
POSIX::ceil, int.
$n = sprintf("%/0f", $f);
# ( )
793
ioctl
ioctl
ioctl FILEHANDLE, FUNCTION, SCALAR
ioctl(2),
. , ,
, :
require "sys/ioctl.ph";
# , /usr/local/lib/perl/sys/ioctl.ph
sys/ioctl.ph ,
, C,
sys/ioctl.h. ( Perl h2ph,
, .) SCALAR
( )
FUNCTION SCALAR
ioctl(2). ( SCALAR
, ,
, .)
, ioctl,
pack unpack. FIONREAD ioctl ,
:
require 'sys/ioctl.ph';
$size = pack("L", 0);
ioctl(FH, FIONREAD(), $size)
or die "Couldn't call ioctl: $!\n";
$size = unpack("L", $size);
h2ph ,
grep
C .
ioctl ( fcntl) :
,
1
,
Perl
undef
0 but true
, Perl
,
, :
$retval = ioctl(...) || 1;
printf " %d\n", $retval;
0 but true
w.
794
29.
ioctl . , ,
, :
system "stty echo";
# Unix.
Perl, ,
. , , .
Term::ReadKey CPAN.
join
join EXPR, LIST
LIST ,
EXPR, . :
$rec = join ':', $login,$passwd,$uid,$gid,$gcos,$home,$shell;
split.
pack.
(join) :
$string = join "", @array;
split, join
, .
keys
keys HASH
, .
, ,
, values each ( ,
).
HASH. ( )
:
@keys = keys %ENV;
# ,
@values = values %ENV;
# ,
while (@keys) {
print pop(@keys), '=', pop(@values), "\n";
}
, :
foreach $key (sort keys %ENV) {
print $key, '=', $ENV{$key}, "\n";
}
795
kill
,
.
, , ,
, ,
.
:
foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
printf "%4d %s\n", $hash{$key}, $key;
}
keys , DBM,
, .
each,
,
.
keys (
each).
, DBM, Perl ,
. keys .
keys
, . (
$#array.)
, , , , .
:
keys %hash = 1000;
%hash 1000 (
1024 , . . ).
, , keys
, ( ,
). , %hash = ().
undef %hash, , %hash
.
. each, values sort.
kill
kill SIGNAL, LIST
. SIGNAL
( "SIG" ).
. ,
. SIGNAL ,
, . ( SysV
796
29.
, .) PID,
, ID ,
. :
$cnt = kill 1, $child1, $child2;
kill 9, @goners;
kill 'STOP', getppid
# ** ...
unless getppid == 1;
# ( init(8).)
SIGNAL, 0, ,
. .
, UID.
use Errno qw(ESRCH EPERM);
if (kill 0 => $minion) {
print "$minion is alive!\n";
} elsif ($! == EPERM) {
# UID
print "$minion !\n";
} elsif ($! == ESRCH) {
print "$minion .\n"; #
} else {
warn ", $minion: $!\n";
}
. 16.
last
last LABEL
last
last ,
break C Java ( ). LABEL
,
. continue, , .
LINE: while (<MAILMSG>) {
last LINE if /^$/; #
#
}
last , ,
, eval {}, sub {} do {},
grep map.
, Perl last ,
, ,
.
, .
last
.
. 4 , last, next, redo continue.
797
link
lc
lc EXPR
lc
EXPR,
. , escape
\L , .
LC_CTYPE , use locale,
Unicode , ,
.
perllocale.
lcfirst
lcfirst EXPR
lcfirst
EXPR,
. , escape
\l , .
LC_CTYPE use loca
le; , Unicode.
length
length EXPR
length
EXPR.
EXPR , $_. ( ,
EXPR, Perl
. , length < 10 .
.)
length .
scalar @array, /
scalar keys %hash. (scalar ,
.)
, , :
$blen = do { use bytes; length $string; };
:
$blen = bytes::length($string);
link
link OLDFILE, NEWFILE
# use bytes
798
29.
,
.
. . symlink . ,
, Unix.
listen
listen SOCKET, QUEUESIZE
,
SOCKET
, QUEUESIZE. ,
, 17 .
( !)
.
use Socket;
listen(PROTOSOCK, SOMAXCONN)
or die " PROTOSOCK: $!";
. accept. . 16
listen(2).
local
local EXPR
,
my. , . .
, eval .
,
, , .
, . . .
.
,
, eval . local,
,
() , (),
,
.
. . 4.
EXPR ,
.
, undef,
(). ,
local
799
( ),
.
.
,
,
.
, ,
:
if ($sw eq 'v') {
#
local @ARGV = @ARGV;
unshift @ARGV, 'echo';
system @ARGV;
}
# @ARGV
:
# %digits
if ($base12) {
# (: , !)
local(%digits) = (%digits, T => 10, E => 11);
parse_num();
}
local
, :
if ($protected) {
local $SIG{INT} = 'IGNORE';
precious();
#
}
# ( )
local typeglob
:
local *MOTD;
# MOTD
my $fh = do { local *FH }; #
800
29.
localtime
localtime EXPR
localtime
, time,
, , .
:
# 0
1
2
3
4
5
6
7
8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
, , EXPR , localtime(time()).
struct
tm. ( C .)
, , $mon 0..11,
0, $wday 0..6,
0. , ,
,
, .
, :
$thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime)[6]];
. strftime POSIX,
. Time::localti
me .
lock
lock THING
lock ,
, THING,
.
1
date(1) , localtime .
map
801
, Perl
use Threads. Perl
, . . 17 .
log
log EXPR
log
(. . e) EXPR.
EXPR , .
:
N
, N. :
sub log10 {
my $n = shift;
return log($n)/log(10);
}
lstat
lstat EXPR
lstat
, Perl stat (
_),
, stat
, , . (
, stat.)
m//
/PATTERN/
m/PATTERN/
, PATTERN
.
, . . 5 .
map
map BLOCK LIST
map EXPR, LIST
802
29.
.
:
@chars = map chr, @nums;
.
:
%hash = map { genkey($_) => $_ } @array;
:
%hash = ();
foreach $_ (@array) {
$hash{genkey($_)} = $_;
}
$_ ( )
,
. ,
, LIST .
foreach . .
grep; map grep , map ,
EXPR, grep
, LIST, EXPR
.
mkdir
mkdir FILENAME, MASK
mkdir FILENAME
, FILENAME,
, MASK
umask.
, .
MASK , 0777, ,
. MASK
( 0777)
umask ,
.
, (,
). . umask.
msgsnd
803
mkdir(2) C, Perl
, mkdir(1) .
mkdir ,
.
msgctl
msgctl ID, CMD, ARG
msgget
msgget KEY, FLAGS
msgrcv
msgrcv ID, VAR, SIZE, TYPE, FLAGS
msgrcv(2)
ID VAR
SIZE. . msgrcv(2).
VAR , VAR
SIZE .
.
use IPC::SysV.
, System V IPC.
msgsnd
msgsnd ID, MSG, FLAGS
msgsnd(2)
MSG ID. . msgsnd(2). MSG
804
29.
, .
:
$msg = pack "L a*", $type, $text_of_message;
. use IPC::SysV.
, System V IPC.
my
my
my
my
my
,
,
, eval . ,
,
, .
.
,
,
. , local,
, ,
(. . , ).
.
,
.
,
.
, ,
,
, , ,
.
, .
(lexical scoping),
. C
,
.
EXPR ,
. ( ,
805
my
,
). ,
(
), .
. ,
:
my ($friends, $romans, $countrymen) = @_;
, ,
, :
my $country = @_; # ?
(. .
), .
, shift. ,
,
, :
sub simple_as {
my $self = shift; #
my ($a,$b,$c) = @_; #
...
}
my sub, Perl , .
(, , .)
TYPE ATTRIBUTES , ,
.
:
my Dog $spot :ears(short) :tail(long);
TYPE, , ,
EXPR, ,
. TYPE ,
,
, . ,
. , , Collie Dog,
:
my Dog $lassie = new Collie;
, $lassie
, , Dog. ,
Collie, ,
806
29.
, Dog.
Dog Collie,
, .
.
, 5.6.0 , Perl
TYPE,
use fields.
, ,
,
.
, ,
. .
8 .
TYPE .
TYPE ,
. TYPE
. Perl,
,
,
, int, num, str ref.
; ,
, ,
, .
, ,
str int,
, .
, int, Perl
,
. int ,
, C. ,
.
vec ,
:
my bit @bitstring;
ATTRIBUTES .
,
; . use attributes
31. , , , , con
stant:
my num $PI : constant = atan2(1,1) * 4;
, ,
807
next
. ,
,
.
, Perl 10 .
, , .
. local, our 4.
new
new CLASSNAME LIST
new CLASSNAME
new.
(. . , ),
CLASSNAME (. . ),
CLASSNAME. new,
, C++,
, .
, , ; ,
, Tk,
Listbox(). . 12.
next
next LABEL
next
next continue C:
, LABEL:
LINE: while (<STDIN>) {
next LINE if /^#/;
...
}
continue,
next. LABEL ,
.
, .
next (
continue, ).
next ,
, eval {}, sub {} do {}, ,
grep map.
, Perl , next
,
, , . .
4.
808
29.
no
no MODULE LIST
use,
no.
, , , no
. . MODULE
, .
oct
oct EXPR
oct
EXPR
. EXPR "0x",
, "0b",
.
, , ,
C C++:
$val = oct $val if $val =~ /^0/;
sprintf
:
$perms = (stat("filename"))[2] & 07777;
$oct_perms = sprintf "%lo", $perms;
oct , , "644",
, , . Perl
,
10.
open
open FILEHANDLE, MODE, LIST
open FILEHANDLE, EXPR
open FILEHANDLE
open FILEHANDLE
, EXPR LIST.
, ( ,
Perl 5.6.1).
, MODE,
, (LIST)
,
. ,
, , system exec, ,
open
809
.
(),
, . (
, :
.) MODE , open .
, ,
/ . (
, , ,
, .)
,
, FILEHANDLE, :
$LOG = ">logfile";
# $LOG my!
open LOG or die " logfile: $!";
. . , .
open undef
. open ,
ID .
, , open,
. C Java,
if, or.
||, open
. ,
, , or die,
|| die, || ,
open, || , open :
open LOG, ">logfile" || die " logfile: $!"; #
open LOG, ">logfile" or die " logfile: $!"; # ok
,
, , :
open LOG, ">logfile"
or die " logfile: $!";
, FILEHANDLE
( ),
,
. (
,
.) *
(indirect filehandle), , FILEHANDLE
,
, . open
,
Perl , . . ,
.
810
29.
,
, , ,
:
{
my $fh;
# ( )
open($fh, ">logfile")
# $fh
or die " logfile: $!";
...
# $fh
# $fh
my $fh open:
open my $fh, ">logfile" or die ...
>, ,
. open .
. ,
, >.
, >,
, EXPR, ,
, , :
open(LOG, ">", "logfile") or die "Can't create logfile: $!";
.
,
. ,
.
, , , ,
.
.
, , ,
.
. 29.1. ( ,
, sysopen.)
29.1. open
< PATH
> PATH
>> PATH
+< PATH
+> PATH
+>> PATH
| COMMAND
COMMAND |
n/a
n/a
n/a
n/a
n/a
n/a
811
open
< ,
. > ,
,
>>
( .
> >>,
, umask
, .
:
open(INFO,
"datafile")
open(INFO,
"< datafile")
open(RESULTS, "> runstats")
open(LOG,
">> logfile ")
||
||
||
||
die("
die("
die("
die("
datafile:
datafile:
runstats:
logfile:
$!");
$!");
$!");
$!");
, ,
:
open
open
open
open
INFO,
"datafile"
INFO,
"< datafile"
RESULTS, "> runstats"
LOG,
">> logfile "
or
or
or
or
die
die
die
die
"
"
"
"
datafile:
datafile:
runstats:
logfile:
$!";
$!";
$!";
$!";
"" STDIN.
STDOUT.
< > .
open(INPUT, "" ) or die;
open(INPUT, "<") or die;
open(OUTPUT, ">") or die;
#
# ,
#
,
,
, .
+ ,
. ,
, ,
. , +<
/ ,
+> ,
. ( ,
, .)
open(DBASE, "+< database")
or die " : $!";
, ,
seek,
812
29.
.
i 19.
EXPR (), open
,
. ,
, , .
:
open(PRINTER, "| lpr Plp1")
print PRINTER "stuff\n";
close(PRINTER)
EXPR , open
,
. STDOUT
. :
open(NET, "netstat i n |")
while (<NET>) { ... }
close(NET)
, ,
$? ($CHILD_ERROR). , close $! ($OS_ERROR).
. close system, .
, , ,
,
(/bin/sh Unix),
. ,
Perl , .
.
:
open(PRINTER, "|", "lpr Plp1")
open(NET, "|", "netstat i n")
. ,
, ( Perl)
:
open(PRINTER, "|", "lpr", "Plp1")
or die " : $!";
open(NET, "|", "netstat", "i", "n") or die " : $!";
"",1 fork. (
1
.
open
813
, fork, .
Microsoft fork Perl 5.6.)
, .
open ID
, 0
undef
fork ( ). :
defined($pid = open(FROM_CHILD, "|"))
or die " : $!";
if ($pid) {
@parent_lines = <FROM_CHILD>; #
}
else {
print STDOUT @child_lines;
#
}
,
( )
( ) STDOUT ( STDIN) .
. (
PID, 0.)
open ,
, (, setuid),
.
:
open FH,
"| tr 'az' 'AZ'";
#
open FH, "|",
'tr', 'az', 'AZ';
#
open FH, "|" or exec 'tr', 'az', 'AZ' or die; #
:
open FH,
"cat
n 'file' |";
#
open FH, "|",
'cat', 'n', 'file';
#
open FH, "|" or exec 'cat', 'n', 'file' or die; #
.
16 23
.
open :
"cmd|" "|cmd" . open
, , ,
, ( ) "|cmd|".
IPC::Open2 IPC::Open3
. .
16.
, , EXPR, >&;
814
29.
( , ),
dup2(2).1 &
>, >>, <, +>, +>> +<. (
.)
,
,
.
open(SAVEOUT, ">&SAVEERR") or die " SAVEERR: $!";
open(MHCONTEXT, "<&4")
or die " fd4: $!";
, ,
, ,
.
,
:
somefunction("&main::LOGFILE");
,
, ,
. ,
, STDOUT STDERR:
#!/usr/bin/perl
open SAVEOUT, ">&STDOUT";
open SAVEERR, ">&STDERR";
open STDOUT, ">foo.out" or die " stdout";
open STDERR, ">&STDOUT" or die " stdout";
select STDERR; $| = 1;
select STDOUT; $| = 1;
#
#
#
#
system("some command");
# stdout/stderr
close STDOUT;
close STDERR;
open STDOUT, ">&SAVEOUT";
open STDERR, ">&SAVEERR";
print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
() typeglob
, fileno
.
open
815
&=, &,
Perl FILEHANDLE
C fdopen(3).
, .
$fd = $ENV{"MHCONTEXTFD"};
open(MHCONTEXT, "<&=$fdnum")
or die "couldn't fdopen descriptor $fdnum: $!";
.
open ,
() :
open(FH, "<", $path) or die " $path: $!";
, open(2)
C ,
sysopen:
use Fcntl;
sysopen(FH, $path, O_RDONLY) or die " $path: $!";
,
(
), .
.
816
29.
, binmode ,
, . ,
, ,
, C , \n.
Unix Mac OS. VMS, MVS, MS S&M
, binmode.
. Perl 5.6
open binmode. MODE (
)
. binmode,
open :raw
MODE:
open(FH, "<:raw", $path) or die " $path: $!";
, , ,
, , ,
. , ,
, , ,
. 29.2.
29.2.
:raw
:text
:def
, "use open"
:latin1
ISO88591
:ctype
LC_CTYPE
:utf8
UTF8
:utf16
UTF16
:utf32
UTF32
:uni
Unicode (UTF*)
:any
Unicode/Latin1/LC_CTYPE
:xml
:crlf
:para
:slurp
(
), , , , :
open(FH, "<:para:crlf:uni", $path) or die " $path: $!";
while ($para = <FH>) { ... }
817
our
Unicode
Perl UTF8;
, ,
\n;
, , ,
$/ = "".
(:def)
:text,
open:
use open IN => ":any", OUT => ":utf8";
,
:text. : ,
, , .
opendir
opendir DIRHANDLE, EXPR
EXPR read
dir, telldir, seekdir, rewinddir closedir.
. ,
.
ord
ord EXPR
ord
our
our
our
our
our
our
, eval . , our
, my,
;
818
29.
.
, .
our
use strict "vars";
my,
, . ,
my,
our. use vars,
,
.
our my , TYPE
ATTRIBUTES. :
our Dog $spot :ears(short) :tail(long);
, .
$spot.
, my,
$spot,
. ,
, $spot Dog, ,
$spot Cat, .
,
, ,
. ( , ,
TYPE, , .
12.)
our my .
our ,
, . ,
,
, . ,
, :
package Foo;
our $bar;
$bar = 582;
package Bar;
print $bar;
# $bar $Foo::bar
, , my
, our
, .
our, ,
, , our .
local:
819
pack
($x, $y) = ("", "");
print " , x $x, y $y\n";
{
our $x = 10;
local our $y = 20;
print " , x $x, y $y\n";
}
print " , x $x, y $y\n";
:
, x , y
, x 10, y 20
, x 10, y
our
, .
, Perl , .
use warnings;
package Foo;
our $bar;
$bar = 20;
# $Foo::bar
package Bar;
our $bar = 30;
print $bar;
# $Bar::bar
# 30
our $bar;
. local, my 4.
pack
pack TEMPLATE, LIST
LIST Perl,
TEMPLATE .
, . .
, TEMPLATE, pack
, , TEMPLATE,
.
TEMPLATE .
.
,
. , N
(bigendian) .
, . ,
, :
$string = pack("Cf", 244, 3.14);
820
29.
244.
3,14 .
.
:
(, , , );
(, ,
, , , ;
8 Unicode);
(, (littleendian)
(bigendian) ).
. 29.3 . (
, .)
29.3. pack/unpack
( vec)
, 32
, 32
16 (bigendian)
32 (bigendian)
64 (quad)
64 (quad)
(short) , 16
(short) , 16
821
pack
uuencode
Unicode
BER c
( )
, ( )
. #
( ) TEMPLATE.
, (count),
,
. , a, A, b, B, h, H, P Z, count
, pack LIST
. * .
a, A Z ,
count, .
A , Z
, a , .
a Z .
b B count .
1 ,
(. . ord($byte) % 2). ,
0 1 0 1.
.
8, , .
. ,
, . *
.
, 0 1.
h H count (4 ,
).
p , .
,
( ,
). P
, count. p
P undef, .
822
29.
/ ,
, .
: lengthitem/stringitem. lengthitem
pack ,
. , n (
Java), w ( ASN.1 SNMP) N ( Sun XDR). stringitem
( ) A*, a* Z*. unpack
lengthitem, *, .
unpack 'C/a', "\04Gurusamy";
# 'Guru'
unpack 'a3/A* A*', '007 Bond J '; # (' Bond','J')
pack 'n/a* w/a*','hello,','world'; # "\000\006hello,\005world"
"\n";
"\n";
"\n";
"\n";
823
pack
Perl
,
. ,
unpack("f", pack("f", $foo)) $foo.
,
, , ,
C ,
C .
x. ,
C:
struct foo {
unsigned char c;
float f;
};
# $out eq "ABCD"
#
Unicode,
:
$foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
, :
$out = pack "CCxxCC", 65, 66, 67, 68;
# $out eq "AB\0\0CD"
, :
$out = pack "s2", 1, 2;
# "\1\0\2\0"
# "\0\1\0\2"
count
, :
$out = pack "B32", "01010000011001010111001001101100";
$out = pack "H8", "5065726c";
# "Perl"
a :
$out = pack "a4", "abcd", "x", "y", "z";
# "abcd"
824
29.
, :
$out = pack "aaaa", "abcd", "x", "y", "z";
$out = pack "a" x 4, "abcd", "x", "y", "z";
# "axyz"
# "axyz"
a :
$out = pack "a14", "abcdefg";
# "abcdefg\0\0\0\0\0\0\0"
C struct tm ( ,
):
$out = pack "i9pl", gmtime(), $tz, $toff;
unpack,
, a, A Z.
,
pack TEMPLATE A a:
$string = pack("A10" x 10, @data);
join:
$string = join(" and ", @data);
$string = join("", @data);
, ,
.
. ( , ,
.)
package
package NAMESPACE
package
, , ,
. (
package, , , my our.)
.
package
, local, ,
my.
, ,
,
package
825
. , our,
,
, my.
our
, ,
.
package ,
require use,
, .
, ,
. (
, ,
, .)
.
,
. ( package
, .)
, pac
kage main.
, ,
,
: $Package::Variable. ,
main. , $::sail $main::sail, $main'
sail, .
:
package main;
$sail = "hale and hearty";
package Mizzen;
$sail = "tattered";
package Whatever;
print "My main sail is $main::sail.\n";
print "My mizzen sail is $Mizzen::sail.\n";
:
My main sail is hale and hearty.
My mizzen sail is tattered.
,
. , main
%main::. *main::sail
$main::{"sail"}.
NAMESPACE ,
. , use strict,
.
. 10 . .
my .
826
29.
pipe
pipe READHANDLE, WRITEHANDLE
,
. pipe(2).
fork, WRI
TEHANDLE, READHANDLE. ( EOF
, .)
,
, . ,
, Perl ,
$| ($OUTPUT_AUTOFLUSH) WRITE
HANDLE,
. select ( ).
( open, ,
.)
:
pipe(README, WRITEME);
unless ($pid = fork) { #
,
, .
.
socketpair. . 16.
pop
pop ARRAY
pop
(
) ,
. ARRAY , @_
; @ARGV
( )
, eval STRING, BEGIN {},
CHECK {}, INIT {} END {}. , :
827
:
$tmp = splice @ARRAY, 1;
, pop undef. (
, , ,
undef!) . push shift.
, splice.
pop , ,
. ,
(LIST )[1]
pos
pos SCALAR
pos
SCALAR, m//g
.
. ( length($`) + length($&).) ,
m//g . ,
0. :
$graffito = "fee fie foe foo";
while ($graffito =~ m/e/g) {
print pos $graffito, "\n";
}
2, 3, 7 11, . . , "e".
pos , ,
m//g:
$graffito = "fee fie foe foo";
pos $graffito = 4; # fee, fie
while ($graffito =~ m/e/g) {
print pos $graffito, "\n";
}
7 11. \G
, pos
, . . 5.
print
print FILEHANDLE LIST
print LIST
print
828
29.
, .
$\ ($OUTPUT_RECORD_SEPARATOR),
.
. FILEHANDLE
( ),
,
. , FILEHANDLE
, :
print { $OK ? "STDOUT" : "STDERR" } "stuff\n";
print { $iohandle[$i] } "stuff\n";
FILEHANDLE , ,
, +
, :
print $a 2; # $a 2 ( STDOUT)
print $a ( 2); # 2 , $a
print $a 2;
# 2 ( :)
FILEHANDLE , ,
, STDOUT.
STDOUT, se
lect FILEHANDLE.1 LIST , $_.
print LIST, LIST .
, :
print OUT <STDIN>;
,
,
<STDIN> .
, :
print OUT scalar <STDIN>;
, : , ,
, , print
, ,
print, +
:
print (1+2)*3, "\n";
print +(1+2)*3, "\n";
print ((1+2)*3, "\n");
#
# ok
# ok
STDOUT
print.
.
prototype
829
printf
printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST
FILEHANDLE ,
, ,
STDOUT. LIST , ,
.
printf (3) fprintf (3) C.
:
print FILEHANDLE sprintf FORMAT, LIST
, $\ ($OUTPUT_RECORD_SEPARATOR) .
use locale, ,
,
LC_NUMERIC.
, FI
LEHANDLE .
.
, .
. print sprintf . sprintf
.
,
.
FORMAT, LIST, $_
print. ,
printf , print. print
.
prototype
prototype FUNCTION
( undef,
). FUNCTION
, .
FUNCTION , CORE::,
Perl,
, .
(, qw//)
(, system), undef,
, Perl.
,
.
830
29.
push
push ARRAY, LIST
ARRAY
LIST ARRAY. ARRAY LIST.
. push , :
foreach $value (listfunc()) {
$array[++$#array] = $value;
}
:
splice @array, @array, 0, listfunc();
( ).
push shift,
:
for (;;) {
push @array, shift @array;
...
}
. pop unshift.
q/STRING/
q/STRING/
qq/STRING/
qr/STRING/
qw/STRING/
qx/STRING/
. . 2.
qx// . readpipe, qr// m//.
. 5.
quotemeta
quotemeta EXPR
quotemeta
EXPR, ,
, . (
, /[AZaz_09]/,
.)
, escape \Q
( ,
).
831
read
rand
rand EXPR
rand
,
0 EXPR. (EXPR
.) EXPR ,
0 1 ( 0, 1). rand
srand, . . srand.
, ,
, int, :
$roll = int(rand 6) + 1;
# $roll 1 6
Perl
C, random(3) drand48(3),
.
, , ,
random (4) ( /dev/random
/dev/urandom), Math::TrulyRandom CPAN
,
.1
read
read FILEHANDLE, SCALAR, LENGTH, OFFSET
read FILEHANDLE, SCALAR, LENGTH
LENGTH SCALAR
FILEHANDLE.
0, .
undef. SCALAR
. OFFSET, , ,
,
.
FROM TO,
:
while (read(FROM, $buf, 16384)) {
print TO $buf;
}
read print,
.
write, .
1
. . , . 2, 3 .,
Williams, 2001.
832
29.
Perl read
fread(3), read(2)
, LENGTH, ,
fread(3) read(2),
. ,
sysread. read sysread
,
( ). ,
, , Unicode
, .
readdir
readdir DIRHANDLE
( )
, opendir.
, ,
undef.
, , . :
opendir(THISDIR, ".") or die "serious dainbramage: $!";
@allfiles = readdir THISDIR;
closedir THISDIR;
print "@allfiles\n";
.
, "." "..", (,
):
@allfiles
@allfiles
@allfiles
@allfiles
=
=
=
=
grep
grep
grep
grep
.* ( ls):
@allfiles = grep !/^\./, readdir THISDIR;
:
@textfiles = grep T, readdir THISDIR;
, readdir
, ,
, :
opendir(THATDIR, $path) or die "can't opendir $path: $!";
@dotfiles = grep { /^\./ && f } map { "$path/$_" } readdir(THATDIR);
closedir THATDIR;
833
readlink
readline
readline FILEHANDLE
, <FILEHANDLE>,
. FILE
HANDLE,
,
, ,
typeglob. ( Perl 5.6 typeglob.)
,
, un
def. readline ,
, .
, $/ ($INPUT_RECORD_SEPARATOR)
, ,
. ,
:para ( ), ,
. :slurp (
$/) ( ).
readline "",
undef.
ARGV (
""), , ,
undef.
<FILEHANDLE>
2.
$line
$line
$line
$line
=
=
=
=
<STDIN>;
readline(STDIN);
readline(*STDIN);
readline(\*STDIN);
#
#
#
readlink
readlink EXPR
readlink
,
. EXPR ,
.
, , undef,
, $!.
834
29.
,
. , :
readlink "/usr/local/src/express/yourself.h"
readlink :
../express.1.23/includes/yourself.h
,
/usr/local/src/express.
readpipe
readpipe scalar EXPR
readpipe LIST ()
, qx// (
). ,
EXPR ,
. ,
,
exec, ,
EXPR. scalar
LIST. , , , .
recv
recv SOCKET, SCALAR, LEN, FLAGS
. LENGTH
SCALAR SOCKET.
undef, . SCALAR
.
, recv(2). .
16.
redo
redo LABEL
redo
redo ,
. continue, , .
LABEL ,
. ,
:
# , .
while (<STDIN>) {
835
ref
if (s/\\\n$// && defined($nextline = <STDIN>)) {
$_ .= $nextline;
redo;
}
print; # ...
}
redo ,
, eval {}, sub {} do {},
grep map.
, Perl redo ,
.
, .
redo
. . 4.
ref
ref EXPR
ref
ref , EXPR ,
. ,
. :
SCALAR
ARRAY
HASH
CODE
GLOB
REF
LVALUE
IO::Handle
, .
ref typeof.
if (ref($r) eq "HASH") {
print "r .\n";
}
elsif (ref($r) eq "Hump") {
# . .
print "r Hump.\n";
}
elsif (not ref $r) {
print "r .\n";
}
,
, .
isa UNIVERSAL :
836
29.
if ($r>isa("Hump") }
print "r Hump .\n";
}
, . .
, ,
. . 8 12. .
reftype use attributes 31.
rename
rename OLDNAME, NEWNAME
.
. ()
, Unix
mv. NEWNAME , .
, Unix, .
.
File::Copy.
require
require VERSION
require EXPR
require
.
, require Perl,
, .
do, , require
, ,
. (
,
.) do use, require
, @INC,
%INC . . 28.
,
,
1;, ,
.
require 5.6.2, require
, Perl
. (Perl ,
5.005_03, Perl,
,
837
reset
.) , Perl
5.6, :
require 5.6.0;
# require v5.6.0
Perl . , require,
.
use 5.6.0. . $PERL_VERSION 28.
require (. package),
require .pm,
. use,
, , ,
import. , Socket.pm,
, :
require Socket;
# "use Socket;"
,
, Soc*
ket.pm :
use Socket ();
require ::
, /. ,
:
require Foo::Bar;
require Foo/Bar.pm ,
@INC. :
$class = 'Foo::Bar';
require $class;
# $class
:
require "Foo::Bar";
require Foo::Bar ,
@INC, , .
:
eval "require $class";
reset
reset EXPR
reset
838
29.
( )
continue
??, .
(
). , ,
,
. , (?PAT
TERN?) .
.
.
"X", :
reset 'X';
,
:
reset 'az';
, ??, :
reset;
"AZ" main ,
ARGV, INC, ENV SIG.
( my) .
reset ,
,
?? .
. delete_package() Symbol
23.
return
return EXPR
return
( eval, do FILE)
.
return
. , eval return
, eval.
EXPR ,
,
( ). ,
.
, EXPR
. , EXPR
rindex
839
. return
undef ,
() ()
. ,
, ( )
wantarray.
reverse
reverse LIST
,
LIST .
:
for (reverse 1 .. 10) { ... }
LIST, reverse
, ,
:
%barfoo = reverse %foobar;
LIST
.
: ,
, ,
.
rewinddir
rewinddir DIRHANDLE
readdir DIRHANDLE
. ,
readdir, rewinddir , .
.
rindex
rindex STR, SUBSTR, POSITION
rindex STR, SUBSTR
, index, ,
SUBSTR STR ( index).
$[1, SUBSTR . $[
0, 1.
POSITION, , ,
. :
840
29.
rmdir
rmdir FILENAME
rmdir
, FILENAME, .
,
. . File::Path,
rm r. (,
rm, PPT.)
s///
s///
. . 5.
scalar
scalar EXPR
LIST,
EXPR ,
. :
my ($nextvar) = scalar <STDIN>;
<STDIN>
, ( my)
. ( scalar <STDIN>
$nextvar,
, , ,
.)
,
, :
my $nextvar = <STDIN>;
print ,
:
print "Length is ", scalar(@ARRAY), "\n";
@ARRAY.
seek
841
"list", scalar,
. , , LIST,
.
scalar ,
EXPR , ,
, ,
, ,
. , .
:
print uc(scalar(&foo,$bar)),$baz;
() :
&foo;
print(uc($bar),$baz);
. 2. .
6 .
seek
seek FILEHANDLE, OFFSET, WHENCE
FILEHANDLE,
, fseek(3) .
0, 1. ,
, . ,
, ,
, ,
, ,
. (
:
, , .)
FILEHANDLE ,
,
.
.
. WHENCE
, OFFSET : 0 ;
1 2 . OFFSET
WHENCE, 1 2. WHEN
CE , SEEK_SET, SEEK_CUR SEEK_END
IO::Seekable POSIX , Perl 5.6, Fcntl.
sysread syswrite,
seek;
842
29.
.
sysseek.
ANSI C
seek . ,
clearerr (3). WHENCE 1 (SEEK_CUR) OFFSET 0,
:
seek(TEST,0,1);
, :
for (;;) {
while (<LOG>) {
grok($_);
}
sleep 15;
seek LOG,0,1;
# .
# .
seek ,
. ,
C,
, :
for (;;) {
for ($curpos = tell FILE; <FILE>; $curpos = tell FILE) {
grok($_);
# .
}
sleep $for_a_while;
seek FILE, $curpos, 0; # .
}
seek
.
seekdir
seekdir DIRHANDLE, POS
readdir DIRHANDLE. POS , telldir.
,
. , readdir.
, readdir.
843
select,
. .
select
, FILEHANDLE,
. : , write
print FILEHANDLE.
, , ,
. ,
, :
select REPORT1;
$^ = 'MyTop';
select REPORT2;
$^ = 'MyTop';
, REPORT2
.
, print write
.
, .
, FILEHANDLE ,
.
:
my $oldfh = select STDERR; $| = 1; select $oldfh;
, :
select((select(STDERR), $| = 1)[0])
, , ,
select(STDERR) ( STDERR),
$| = 1 ( 1),
STDERR .
( )
select. , ?
, , Lisp ,
.
SelectSaver,
select .
, ,
select,
844
29.
,
, ,
. $| :
use IO::Handle;
STDOUT>autoflush(1);
# , ** .
:
use IO::Handle;
REPORT1>format_top_name("MyTop");
REPORT2>format_top_name("MyTop");
select ( )
select RBITS, WBITS, EBITS, TIMEOUT
select
select. ,
, ( )
,
. (
.) select(2)
,
fileno vec, :
$rin = $win = $ein = "";
vec($rin, fileno(STDIN), 1) = 1;
vec($win, fileno(STDOUT), 1) = 1;
$ein = $rin | $win;
select ,
:
sub fhbits {
my @fhlist = @_;
my $bits;
for (@fhlist) {
vec($bits, fileno($_), 1) = 1;
}
return $bits;
}
$rin = fhbits(qw(STDIN TTY MYSOCK));
(
), :
($nfound, $timeleft) =
select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
semctl
845
,
:
$nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
, select $nfound
.
$wout=$win ,
; $wout , select,
$win .
undef,
. TIMEOUT, undef,
. ( 0 .)
$timeleft. $time
left, $timeout.
IO::Select
select, ,
.
select ,
sleep. undef .
( ) 4,75 :
select undef, undef, undef, 4.75;
( , Unix, undef ,
, .)
( read
<HANDLE>) select, , POSIX,
POSIX.
sysread.
semctl
semctl ID, SEMNUM, CMD, ARG
846
29.
semget
semget KEY, NSEMS, FLAGS
semop
semop KEY, OPSTRING
1 1.
. System V IPC 16. . IPC::Semaphore.
, System V IPC.
send
send SOCKET, MSG, FLAGS, TO
send SOCKET, MSG, FLAGS
.
, , . send(2).
TO,
Perl send sendto(2). C sendmsg(2)
Perl. send
undef, .
( , Unix,
,
send recv
.)
847
setsockopt
, ,
, send Perl send C :
send SOCK, $buffer, length $buffer
FLAGS, . .
16.
setpgrp
setpgrp PID, PGRP
(PGRP) PID
( PID, 0, ). setpgrp
, ,
setpgrp(2). :
setpgrp(0, $$). ,
. ,
0,0. BSD 4.2 setpgrp
, BSD 4.4 setpgid.
( )
setpgid POSIX .
,
POSIX::setsid(). , POSIX
setpgrp , setpgrp(0,0)
.
setpriority
setpriority WHICH, WHO, PRIORITY
PRIORITY ,
WHICH WHO. . setpriori*
ty(2). setpriority ,
, setpriority(2).
( ,
nice(1)), :
setpriority 0, 0, getpriority(0, 0) + 4;
.
.
. BSD::Resource CPAN.
setsockopt
setsockopt SOCKET, LEVEL, OPTNAME, OPTVAL
848
29.
.
undef . LEVEL ,
, SOL_SOCKET . OPTVAL
undef, .
SO_REUSEADDR, ,
,
TCP .
:
use Socket;
socket(SOCK, ...) or die " : $!\n";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1)
or warn " setsockopt: $!\n";
. setsockopt(2) .
shift
shift ARRAY
shift
,
. ( , ,
. .)
, undef.
ARRAY , @_
; @ARGV
( ) ,
eval STRING, BEGIN {}, CHECK {}, INIT {} END {}.
,
, shift:
sub marine {
my $fathoms = shift; #
my $fishies = shift; #
my $o2
= shift; #
# ...
}
shift :
while (defined($_ = shift)) {
/^[^]/
&& do { unshift @ARGV, $_; last };
/^w/
&& do { $WARN = 1;
next };
/^r/
&& do { $RECURSE = 1;
next };
die " $_\n";
}
Ge
topt::Std Getopt::Long.
shmwrite
849
shmctl
shmctl ID, CMD, ARG
shmget
shmget KEY, SIZE, FLAGS
shmread
shmread ID, VAR, POS, SIZE
ID
POS SIZE ( ,
). VAR ,
.
.
, System V IPC.
shmwrite
shmwrite ID, STRING, POS, SIZE
ID POS SIZE ( ,
). STRING
, SIZE ; STRING ,
, SIZE .
.
850
29.
, System V IPC.
(, , , .)
shutdown
shutdown SOCKET, HOW
, HOW.
HOW 0, . HOW
1, . HOW 2,
.
shutdown(SOCK, 0);
shutdown(SOCK, 1);
shutdown(SOCK, 2);
#
#
#
,
, , , .
,
, .
,
, . close,
,
. shutdown
:
print SERVER "my request\n";
shutdown(SERVER, 1);
$answer = <SERVER>;
#
# eof;
#
( , ,
. . system.)
sin
sin EXPR
sin
, .
EXPR ( ).
, , asin
Math::Trig POSIX :
sub asin { atan2($_[0], sqrt(1 $_[0] * $_[0])) }
sleep
sleep EXPR
sleep
socketpair
851
( ) EXPR
, EXPR, .
, SIGALRM.
,
, .
, . ,
,
.
select ( ),
. Unix
syscall getitimer (2) setiti*
mer (2). alarm sleep ,
sleep alarm.
. sigpause POSIX.
socket
socket SOCKET, DOMAIN, TYPE, PROTOCOL
SOCKET. DOMAIN, TYPE PROTOCOL , socket(2).
SOCKET , .
:
use Socket;
.
. . 16.
,
,
$^F. . $^F ($SYSTEM_FD_MAX) 28.
socketpair
socketpair SOCKET1, SOCKET2, DOMAIN, TYPE, PROTOCOL
. DOMAIN, TYPE PROTOCOL , socketpair(2).
, .
. , socketpair(2) ,
.
fork.
SOCKET1, SOC
KET2. ,
, pipe.
pipe socketpair, pipe(Rdr, Wtr) ,
, :
852
29.
use Socket;
socketpair(Rdr, Wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
shutdown(Rdr, 1);
#
shutdown(Wtr, 0);
#
,
,
$^F. . $^F ($SYSTEM_FD_MAX)
28. . 16.
sort
sort USERSUB LIST
sort BLOCK LIST
sort LIST
LIST
.
( ,
). use locale,
sort LIST LIST
.
USERSUB, , ,
, , 0,
, . (
<=> cmp.) USERSUB , , sort
.
, :
(
),
@_,
$a $b , sort (.
). $a $b
, .
.
(, , , $x[1]
$x[2], ), .
( , $a $b.)
USERSUB ( ),
,
. ( ,
, use strict 'refs'.)
USERSUB (inline)
.
sort
853
, :
sub numerically { $a <=> $b }
@sortedbynumber = sort numerically 53,29,11,32,7;
,
reverse $a $b
:
@descending = reverse sort numerically 53,29,11,32,7;
sub reverse_numerically { $b <=> $a }
@descending = sort reverse_numerically 53,29,11,32,7;
,
$a $b lc:
@unsorted = qw/sparrow Ostrich LARK catbird blueJAY/;
@sorted = sort { lc($a) cmp lc($b) } @unsorted;
( Unicode lc
uc,
. ASCII,
Unicode,
, lc.)
sort .
, %sales_amount
,
:
#
sub bysales { $sales_amount{$b} <=> $sales_amount{$a} }
for $dept (sort bysales keys %sale_amount) {
print "$dept => $sales_amount{$dept}\n";
}
,
|| or.
, , ,
0 ,
.
,
( ,
):
sub by_sales_then_dept {
$sales_amount{$b} <=> $sales_amount{$a}
||
$a cmp $b
}
for $dept (sort by_sales_then_dept keys %sale_amount) {
854
29.
print "$dept => $sales_amount{$dept}\n";
, @recs ,
FIRSTNAME, LASTNAME, AGE, HEIGHT SALARY.
, , ,
, , :
sub prospects {
$b>{SALARY}
||
$b>{HEIGHT}
||
$a>{AGE}
||
$a>{LASTNAME}
||
$a>{FIRSTNAME}
}
<=> $a>{SALARY}
<=> $a>{HEIGHT}
<=> $b>{AGE}
cmp $b>{LASTNAME}
cmp $b>{FIRSTNAME}
, $a $b,
. ,
,
split.
@sorted_lines = sort {
@a_fields = split /:/, $a;
@b_fields = split /:/, $b;
$a_fields[3] <=> $b_fields[3]
||
$a_fields[0] cmp $b_fields[0]
||
$b_fields[2] <=> $a_fields[2]
||
...
} @lines;
# ,
# 4 ,
# 1 ,
#
# 3
# ..
sort
$a $b,
, .
,
,
.
:
@temp = map { [$_, split /:/] } @lines;
855
sort
:
@temp = sort {
@a_fields = @$a[1..$#$a];
@b_fields = @$b[1..$#$b];
$a_fields[3] <=> $b_fields[3] # 4 ,
||
$a_fields[0] cmp $b_fields[0] # 1 ,
||
$b_fields[2] <=> $a_fields[2] #
# 3
||
...
# . .
} @temp;
, ,
:
@sorted_lines = map { $_>[0] } @temp;
, mapsortmap,
(Schwartzian Transform)1,
:
@sorted_lines = map { $_>[0] }
sort {
@a_fields = @$a[1..#$a];
@b_fields = @$b[1..#$b];
$a_fields[3] <=> $b_fields[3]
||
$a_fields[0] cmp $b_fields[0]
||
$b_fields[2] <=> $a_fields[2]
||
...
}
map { [$_, split /:/] } @lines;
$a $b ( my).
(
use
strict). ,
, $a $b
.
, 5.6
(, ,
1
. , , http://
members.home.net/andrew*johnson/perl/archit/msg00041.html. . .
856
29.
XS ),
, ($$).
, $a $b
:
sub numerically ($$) {
my ($a, $b) = @_;
$a <=> $b;
}
, ,
:
sub numerically ($a, $b) { $a <=> $b }
, . .
splice
splice
splice
splice
splice
splice
splice(@a, @a, 0, $x, $y)
pop(@a)
splice(@a, 1)
shift(@a)
splice(@a, 0, 1)
$a[$x] = $y
(@a, @a = ())
splice(@a)
splice
, . , ,
:
split
857
sub list_eq {
#
my @a = splice(@_, 0, shift);
my @b = splice(@_, 0, shift);
return 0 unless @a == @b;
# ?
while (@a) {
return 0 if pop(@a) ne pop(@b);
}
return 1;
}
if (list_eq($len, @foo[1..$len], scalar(@bar), @bar)) { ... }
split
split /PATTERN/, EXPR, LIMIT
split /PATTERN/, EXPR
split /PATTERN/
split
, EXPR,
,
.1
, PATTERN,
. ( ;
.) , split
.
. .
, , /PATTERN/i,
/PATTERN/x . . /^/
//m .
LIMIT ,
( ,
). LIMIT , ,
LIMIT.
, (
, pop). EXPR,
$_. PATTERN
, " ", ,
/\s+/, .
:
split @_,
.
858
@chars
@fields
@words
@lines
29.
=
=
=
=
split
split
split
split
//,
/:/,
" ",
/^/,
$word;
$line;
$paragraph;
$buffer;
,
, (, ,
, * ?), EXPR
, ;
. ( ,
, .) :
print join ':', split / */, 'hi there';
h:i:t:h:e:r:e. ,
. //
, . (
//
, split
.)
LIMIT :
($login, $passwd, $remainder) = split /:/, $_, 3;
,
. ( , $re
mainder , .)
LIMIT, Perl LIMIT
, ,
. LIMIT
4, $remainder ,
. , ,
, . (
, .)
, , PATTERN
, ,
,
. :
split /([,])/, "110,20";
, :
(1, '', 10, ',', 20)
, ,
,
. , :
split /()|(,)/, "110,20";
split
859
:
(1, '', undef, 10, undef, ',', 20)
/PATTERN/ ,
, .
,
, /$variable/o.
(" ");
, split . split(" ")
awk . , split(/ /)
, . (
:
.)
:
$string = join(' ', split(' ', $string));
RFC 822 ,
$head{Date}, $head{Subject} . .
, ,
.
,
. split
, . . ,
, /,
. ( ,
,
, ReceivedBy. )
$header =~ s/\n\s+/ /g;
# .
%head = ('FRONTSTUFF', split /^(\S*?):\s*/m, $header);
passwd(5) Unix.
chomp, $shell .
open PASSWD, '/etc/passwd';
while (<PASSWD>) {
chomp;
#
($login, $passwd, $uid, $gid, $gcos, $home, $shell) =
split /:/;
...
}
, :
860
29.
while (<>) {
foreach $word (split) {
$count{$word}++;
}
}
sprintf
sprintf FORMAT, LIST
,
printf sprintf C.
. sprintf (3) printf (3) .
FORMAT , ,
LIST, .
Perl sprintf: C
sprintf, .1
, sprintf (3), Perl.
Perl sprintf ,
. 29.4.
29.4. sprintf
%%
%c
%s
%d
%u
%o
%x
%e
%f
%g
, %e %f
, Perl
:
1
,
.
861
sprintf
%X
%x,
%E
%e, E
%G
%g, E ( )
%b
%p
( )
%n
, ( )
Perl ,
:
%i
%d
%D
%ld
%U
%lu
%O
%lo
%F
%f
Perl %
:
"0",
"0x"
:
, ,
Perl:
862
29.
Perl
, ,
,
, *
#
#
#
#
Perl
IPv4
IPv6
sqrt
sqrt EXPR
sqrt
EXPR. ,
, **,
.
, . .
( ).
, :
use Math::Complex;
print sqrt(2);
# 1.4142135623731i
srand
srand EXPR
srand
stat
863
rand.
EXPR , ,
( /dev/urandom),
, , ID .
, srand,
,
rand. Perl 5.004 ,
Perl, srand.
(, CGI),
time ^ $$,
, a^b == (a+1)^(b+1) .
. :
srand( time() ^ ($$ + ($$ << 15)) );
, .
/dev/random.
. :
srand (time ^ $$ ^ unpack "%32L*", `ps wwaxl | gzip`);
, Math::TrulyRandom
CPAN.
srand , , ,
, , .
rand,
.
, rand !
stat
stat FILEHANDLE
stat EXPR
stat
,
, .
13 , ,
FILEHANDLE EXPR. :
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat $filename;
;
0. . 29.5.
864
29.
29.5. , stat
$dev
$ino
inode
$mode
( )
$nlink
()
$uid
ID
$gid
ID
$rdev
( )
$size
$atime
$mtime
10
$ctime
inode ( !)
11
$blksize
12
$blocks
$dev $ino
. $blksize $blocks , ,
, BSD. $blocks ( )
512 . $blocks*512 $si
ze , , ,
$blocks.
stat ,
, stat(2) ,
stat,
, stat, lstat
stat (, r, w x).
,
, , ,
printf sprintf %o:
$mode = (stat($filename))[2];
printf "Permissions are %04o\n", $mode & 07777;
File::stat :
use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
865
study
$filename, $sb>size, $sb>mode & 07777,
scalar localtime $sb>mtime;
Fcntl.
.
: ,
s, .
, .
study
study SCALAR
study
, SCALAR,
,
. , ,
,
, , ,
study , , .
study ,
(
). ,
, study ,
. study;
, .
study .
, , , ,
k.
, C
, , .
, .
, , , , ,
, :
while (<>) {
study;
print ".IX foo\n"
print ".IX bar\n"
print ".IX blurfl\n"
...
print;
}
if /\bfoo\b/;
if /\bbar\b/;
if /\bblurfl\b/;
/\bfoo\b/ $_,
f, . . f , o.
, .
866
29.
, ,
.
, ,
eval,
. $/
, ,
, fgrep(1).
(@files)
(@words) ,
:
$search = 'while (<>) { study;';
foreach $word (@words) {
$search .= "++\$seen{\$ARGV} if /\\b$word\\b/i;\n";
}
$search .= "}";
@ARGV = @files;
undef $/;
#
eval $search;
#
die $@ if $@;
# eval
$/ = "\n";
#
foreach $file (sort keys(%seen)) {
print "$file\n";
}
, qr//, eval ,
, .
:
@pats = ();
foreach $word (@words) {
push @pats, qr/\b${word}\b/i;
}
@ARGV = @files;
undef $/;
#
while (<>) {
for $pat (@pats) {
$seen{$ARGV}++ if /$pat/;
}
}
$/ = "\n";
#
foreach $file (sort keys(%seen)) {
print "$file\n";
}
sub
:
sub NAME PROTO ATTRS
sub NAME ATTRS
867
sub
sub NAME PROTO
sub NAME
:
sub
sub
sub
sub
NAME
NAME
NAME
NAME
:
sub
sub
sub
sub
,
. :
sub NAME PROTO ATTRS BLOCK
; ,
, ,
NAME BLOCK.
PROTO ATTRS;
. NAME BLOCK
, :
NAME BLOCK, (
,
, NAME BLOCK). ,
, , , .
, .
(forward declarations).
BLOCK NAME, , . .
. ,
, ,
.
868
29.
, , ,
, sub
. ,
sub ,
, ,
.
PROTO ATTRS ( )
NAME / BLOCK.
, ,
.
.
, :
sub numstrcmp ($$) : locked {
my ($a, $b) = @_;
return $a <=> $b || $a cmp $b;
}
attributes 31. . 6
8.
substr
substr EXPR, OFFSET, LENGTH, REPLACEMENT
substr EXPR, OFFSET, LENGTH
substr EXPR, OFFSET
, EXPR,
. , OFFSET
. (: $[,
0, ( ?),
.) OFFSET ,
. LENGTH ,
. LENGTH ,
, .
LENGTH , ,
, .
substr l (,
), EXPR l.
, , ,
, .
sprintf x.
substr
.
syscall
869
"Larry" $_ :
substr($var, 0, 0) = "Larry";
$_ "Moe":
substr($var, 0, 1) = "Moe";
, $var "Curly"
:
substr($var, 1) = "Curly";
substr
REPLACEMENT .
EXPR , ,
, splice.
$var "Curly" $oldstr:
$oldstr = substr($var, 1, 1, "Curly");
substr
. ,
10 :
substr($var, 10) =~ s/ /./g;
symlink
symlink OLDNAME, NEWNAME
,
.
. ,
, .
, eval :
$can_symlink = eval { symlink("",""); 1 };
Config.
,
,
.
. link readlink .
syscall
syscall LIST
(. . syscall, ),
,
870
29.
. (
, POSIX.)
, syscall(2) .
:
, C. ,
. ,
,
; .
( ,
) syscall, Perl ,
.
, 0,
.
syscall ,
. C,
, syscall 1
$! ( ).
1.
$!=0;
$!, syscall 1.
.
, Perl 14 ,
.
, . syscall(&SYS_pipe):
.
.
pipe.
, XSUB ( ,
C) .
CPAN .
, , time. (
, gettimeofday(2).)
sub finetime() {
package main; # require
require 'syscall.ph';
# 32 long...
my $tv = pack("LL", ());
syscall(&SYS_gettimeofday, $tv, undef) >= 0
or die "gettimeofday: $!";
my($seconds, $microseconds) = unpack("LL", $tv);
return $seconds + ($microseconds / 1_000_000);
}
871
sysopen
, Perl setgroups(2),1
. , :
require 'syscall.ph';
syscall(&SYS_setgroups, scalar @newgids, pack("i*", @newgids))
or die "setgroups: $!";
Perl, syscall.ph
, h2ph.
pack "II". , sy
scall C int, long char*.
syscall
.
Time::HiRes CPAN.
sysopen
sysopen FILEHANDLE, FILENAME, MODE, MASK
sysopen FILEHANDLE, FILENAME, MODE
sysopen , FILENAME,
FILEHANDLE. FILEHANDLE
,
. FILEHANDLE , ,
. ,
, .
open(2), fdopen(3).
C
. MODE
Fcntl.
, , .
open(2)
. ,
C:
O_RDONLY
O_WRONLY
O_RDWR
O_CREAT
O_EXCL
$( .
872
29.
()
O_APPEND
O_TRUNC
O_NONBLOCK
. ,
:
O_NDELAY
O_NONBLOCK
O_SYNC
,
. O_ASYNC, O_DSYNC O_RSYNC
O_EXLOCK
flock LOCK_EX ( )
O_SHLOCK
flock LOCK_SH ( )
O_DIRECTORY
O_NOFOLLOW
O_BINARY
binmode Microsoft.
O_TEXT
O_LARGEFILE
O_NOCTTY
, .
O_EXCL :
, , sysopen
.
, FILENAME, , MODE O_CREAT,
sysopen ,
MASK ( 0666, ),
umask .
: . umask.
, open sysopen,
. sysre
ad , sys
open, open.
, . open
, , ,
.
.
sysopen , open.
sysopen
873
.
or die $!,
.
, .
, ,
| MODE.
:
open(FH, "<", $path);
sysopen(FH, $path, O_RDONLY);
,
:
open(FH, ">", $path);
sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT);
, :
open(FH, ">>", $path);
sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT);
, :
open(FH, "+<", $path);
sysopen(FH, $path, O_RDWR);
sysopen ( open):
,
:
sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT);
, :
sysopen(FH, $path, O_WRONLY | O_APPEND);
, :
sysopen(FH, $path, O_RDWR | O_CREAT);
,
:
sysopen(FH, $path, O_RDWR | O_EXCL | O_CREAT);
, ,
:
sysopen(FH, $path, O_WRONLY | O_NONBLOCK);
FileHandle, 32,
( )
. File
874
29.
sysread
sysread FILEHANDLE, SCALAR, LENGTH, OFFSET
sysread FILEHANDLE, SCALAR, LENGTH
LENGTH SCALAR
FILEHANDLE read(2).
0 EOF.2
sysread undef . SCALAR
. OFF
SET, , ,
, ,
. OFFSET . sys
write. LENGTH OFFSET
, .
(,
),
. ,
sysread , print, printf, write, seek, tell eof
,
(/ ). , ,
, Unicode
, .
sysseek
sysseek FILEHANDLE, POSITION, WHENCE
FILEHANDLE
lseek(2). ,
( sysread), print, write, seek,
tell eof . FILEHANDLE
, . WHENCE
0 POSITION, 1
POSITION 2 EOF POSITION (
). WHENCE SEEK_SET, SEEK_CUR
SEEK_END IO::Seekable POSIX ,
5.6, Fcntl, .
1
2
IO::File IO::Handle.
syseof , , eof
(, ). sys
read 0, .
875
system
( undef ).
"0 but true",
, .
system
system PATHNAME LIST
system LIST
, .
qx//. system ,
exec, , system fork, ,
exec, . ,
,
exec ,
.
;
exec,
PATHNAME.
system SIGINT SIGQUIT,
(, <Ctrl<+<C>)
. ,
, . ,
system, , .
@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"
, wait(2).
256 ( 8
). , . (
.) ,
( ), ,
. ,
, $? ($CHILD_ERROR):
$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;
,1 ,
,
. ,
876
29.
,
.
syswrite
syswrite FILEHANDLE, SCALAR, LENGTH, OFFSET
syswrite FILEHANDLE, SCALAR, LENGTH
syswrite FILEHANDLE, SCALAR
LENGTH SCALAR
FILEHANDLE write(2).
( undef ). OFFSET,
, , . (
.) OFFSET ,
, .
SCALAR , OFFSET, 0. LENGTH
OFFSET ,
.
FROM TO,
:
use Errno qw/EINTR/;
$blksize = (stat FROM)[11] || 16384; # ?
while ($len = sysread FROM, $buf, $blksize) {
if (!defined $len) {
next if $! == EINTR;
die "System read error: $!\n";
}
$offset = 0;
while ($len) {
# .
$written = syswrite TO, $buf, $len, $offset;
die "System write error: $!\n" unless defined $written;
$offset += $written;
$len
= $written;
}
}
, ,
. syswrite
C, ( sysread), (,
print, printf write) ,
seek, tell eof,
.1
1
/bin/sh , ,
, .
.
877
tie
tell
tell FILEHANDLE
tell
( , )
FILEHANDLE.
seek, . FILEHANDLE
, ,
. FILEHANDLE ,
, .
. ,
.
systell . sysseek(FH, 0, 1). . seek
tell.
telldir
telldir DIRHANDLE
readdir
DIRHANDLE. seekdir
.
,
. ,
readdir. ,
. ,
seekdir.
tie
tie VARIABLE, CLASSNAME, LIST
,
. VARIABLE
(, ) typeglob ( ),
. CLASSNAME ,
.
TIESCALAR, TIEARRAY, TIEHASH TIEHANDLE.
( ,
.) , C
dbm_open(3), . ,
, tie,
, CLASSNAME. (
tied.)
ISAM
878
29.
(S,
ISAM, sequential), DBM
.
, keys values, ,
, DBM.
each.
:
use NDBM_File;
tie %ALIASES, "NDBM_File", "/etc/aliases", 1, 0
or die " : $!\n";
while (($key,$val) = each %ALIASES) {
print $key, ' = ', $val, "\n";
}
untie %ALIASES;
, , :
TIEHASH CLASS, LIST
FETCH SELF, KEY
STORE SELF, KEY, VALUE
DELETE SELF, KEY
CLEAR SELF
EXISTS SELF, KEY
FIRSTKEY SELF
NEXTKEY SELF, LASTKEY
DESTROY SELF
, ,
:
TIEARRAY CLASS, LIST
FETCH SELF, SUBSCRIPT
STORE SELF, SUBSCRIPT, VALUE
FETCHSIZE SELF
STORESIZE SELF, COUNT
CLEAR SELF
PUSH SELF, LIST
POP SELF
SHIFT SELF
UNSHIFT SELF, LIST
SPLICE SELF, OFFSET, LENGTH, LIST
EXTEND SELF, COUNT
DESTROY SELF
, , :
TIESCALAR CLASS, LIST
FETCH SELF,
STORE SELF, VALUE
DESTROY SELF
time
879
, ,
:
TIEHANDLE CLASS, LIST
READ SELF, SCALAR, LENGTH, OFFSET
READLINE SELF
GETC SELF
WRITE SELF, SCALAR, LENGTH, OFFSET
PRINT SELF, LIST
PRINTF SELF, FORMAT, LIST
CLOSE SELF
DESTROY SELF
: Tie::Hash,
Tie::Array, Tie::Scalar Tie::Handle
. .
14 . dbmopen, tie
use require
. tie DB_File
Config.
tied
tied VARIABLE
, ,
, typeglob, VARIABLE ( ,
tie,
). , VARIABLE
. , , :
ref tied %hash
, . ( .)
time
time
, 00:00:00 1 1970 , UTC (Universal Coordi
nated Time).1 gmtime lo
caltime ,
stat, utime.
$start = time();
system(" ");
1
, Unix. (
, .)
880
29.
$end = time();
if ($end $start > 1) {
print " : ", scalar localtime($start), "\n";
print " : ", scalar localtime($end), "\n";
}
times
times
, (, ),
CPU,
.
($user, $system, $cuser, $csystem) = times();
printf " pid %.3f \n",
$user + $system + $cuser + $csystem;
. , Perl:
$start = times();
...
$end = times();
printf " %.2f CPU\n",
$end $start;
tr///
tr///
y///
( ),
y/// Unix sed, ,
. . 5.
truncate
truncate FILEHANDLE, LENGTH
truncate EXPR, LENGTH
, FILEHANDLE
EXPR, . ,
ftruncate(2) .
( , ,
.)
undef .
881
umask
uc
uc EXPR
uc
EXPR .
, escape \U ,
. Perl ,
,
Unicode. .
perllocalle. Perl
Unicode uc , .
. ucfirst .
ucfirst
ucfirst EXPR
ucfirst
EXPR,
( ),
. , escape
\u , .
LC_CTYPE , use locale
Unicode, .
,
:
ucfirst lc $word
"\u\L$word".
umask
umask EXPR
umask
,
umask(2). ,
, ,
. EXPR ,
. ,
, :
umask((umask() & 077) | 7);
, ,
, .
882
29.
, oct. ,
.
Unix rwxrx
: 0750 ( 0 ,
, ).
, .
( ), mkdir sysopen,
, sysopen
0777, 0022,
0755. 0027 ( ;
, ), sysopen MASK,
0666, 0640 ( 0666 & ~0027 0640).
: 0666
( sysopen) 0777 ( mkdir) .
: ,
022, 027
077.
.
, ,
: , cookies web, .rhosts . .
umask(2) *
(. . EXPR & 0700) > 0),
. umask(2)
, undef.
undef
undef EXPR
undef
undef , ,
. ,
.
.
undef
, .
EXPR, , .
, ,
( &) typeglob. ,
, (
). un
def , ,
.
$1 .
883
unlink
undef , ,
.
undef :
undef
undef
undef
undef
undef
undef
$foo;
$bar{'blurfl'};
@ary;
%hash;
&mysub;
*xyz;
# , delete $bar{'blurfl'};
, undef :
select(undef, undef, undef, $naptime);
return (wantarray ? () : undef) if $they_blew_it;
return if $they_blew_it; #
undef
,
. undef .
($a, $b, undef, $c) = &foo;
, undef ,
. 0 . ,
, defined .
unlink
unlink LIST
unlink
.1 ,
. :
$count = unlink 'a', 'b', 'c';
unlink @goners;
unlink glob("*.orig");
unlink ,
U Perl.
, ,
.
rmdir.
POSIX (
), .
, ,
.
884
29.
rm :
#!/usr/bin/perl
@cannot = grep {not unlink} @ARGV;
die "$0: could not unlink @cannot\n" if @cannot;
unpack
unpack TEMPLATE, EXPR
, pack:
(EXPR), ,
TEMPLATE .
. TEMPLATE ,
, pack, ,
. TEMPLATE
pack. TEMPLATE
x, X @ .
, TEMPLATE.
.
pack, C.
,
, . (
*.)
, TEMPLATE, .
unpack
, . ,
:
1986
1985
1984
1983
1982
1981
Ender's Game
Neuromancer
Startide Rising
Foundation's Edge
Downbelow Station
The Snow Queen
split ,
, . ,
,
unpack:
while (<>) {
($year, $title, $author) = unpack("A4 x A23 A*", $_);
print "$author won ${year}'s Hugo for $title.\n";
}
885
unshift
uudecode:
#!/usr/bin/perl
$_ = <> until ($mode,$file) = /^begin\s*(\d*)\s*(\S*)/;
open(OUT,"> $file") if $file ne "";
while (<>) {
last if /^end/;
next if /[az]/;
next unless int((((ord() 32) & 077) + 2) / 3) ==
int(length() / 4);
print OUT unpack "u", $_;
}
chmod oct($mode), $file;
, pack, %,
, . 16 .
( ord($char),
). ,
, SysV sum(1):
undef $/;
$checksum = unpack ("%32C*", <>) % 65535;
:
$setbits = unpack "%32b*", $selectmask;
BASE64:
while (<>) {
tr#AZaz09+/##cd;
tr#AZaz09+/# _#;
$len = pack("c", 32 + 0.75*length);
print unpack("u", $len . $_);
}
#
#
#
#
base64
uuencode
uudecode
unshift
unshift ARRAY, LIST
, shift. (
push, , .)
LIST :
unshift @ARGV, 'e', $cmd unless $ARGV[0] =~ /^/;
, LIST , ,
.
reverse.
886
29.
untie
untie VARIABLE
typeglob, VARI
ABLE, , . . tie 14,
.
use
use
use
use
use
use
use
use
use , ,
.
( ,
,
.)
use :
use MODULE LIST;
:
BEGIN { require MODULE; import MODULE LIST; }
:
BEGIN { require MODULE; }
use , 5.6.2,
Perl .
887
use
Perl VERSION,
Perl.
Perl ,
,
Perl. ( ,
. ,
.)
, , , Perl
:
use 5.005_03;
Perl 5.6
( ) :
use 5.6.0;
# 5, 6, 0.
. ( ,
,
,
.
... , , 11.)
no, ,
use, :
no integer;
no strict 'refs';
888
29.
no utf8;
no warnings "unsafe";
. 31.
utime
utime LIST
, .
. .
(inode)
. touch,
( , )
:
#!/usr/bin/perl
# montouch : + 1
$day = 24 * 60 * 60;
# 24
$later = time() + 30 * $day;
# 30 , ,
utime $later, $later, @ARGV;
touch :
#!/usr/bin/perl
# montouch : + 1
$later = time() + 30 * 24 * 60 * 60;
@cannot = grep {not utime $later, $later, $_} @ARGV;
die "$0: Could not touch @cannot.\n" if @cannot;
, stat,
localtime gmtime .
values
values HASH
,
HASH. , ,
, keys each .
,
keys, keys.
,
, .
( .)
for (@hash{keys %hash}) { s/foo/bar/g }
for (values %hash)
{ s/foo/bar/g }
#
#
889
vec
values , DBM,
, .
each,
,
.
vec
vec EXPR, OFFSET, BITS
vec
. , ,
Perl. EXPR ,
, .
OFFSET , .
, vec
,
.
BITS :
1, 2, 4, 8, 16 32 ( 64). (
.)
0..(2**BITS)1.
,
. BITS 1, 8 . BITS 2
. BITS 4, (
(nybbles)). . ,
, .
, vec. ( EXPR
, .)
4 :
$bitstring = "";
$offset = 0;
foreach $num (0, 5, 5, 6, 2, 7, 12, 6) {
vec($bitstring, $offset++, 4) = $num;
}
, Perl
.
, ,
, OFFSET.
$num_elements = length($bitstring)*2; # 2
foreach $offset (0 .. $num_elements1) {
print vec($bitstring, $offset, 4), "\n";
}
890
29.
,
0.
, vec,
|, &, ^ ~. ,
, . .
3
.
BITS == 1,
. , vec($bitstring,0,1)
.
@bits = (0,0,1,0, 1,0,1,0, 1,1,0,0, 0,0,1,0);
$bitstring = "";
$offset = 0;
foreach $bit (@bits) {
vec($bitstring, $offset++, 1) = $bit;
}
print "$bitstring\n";
# "TC", .. '0x54', '0x43'
1 0
"b*" pack unpack. pack
"b*" , 1 0.
, vec.
$bitstring = pack "b*", join('', @bits);
print "$bitstring\n"; # "TC", ,
unpack 0 1
.
@bits = split(//, unpack("b*", $bitstring));
print "@bits\n";
# 0 0 1 0 1 0 1 0 1 1 0 0 0 0 1 0
, "*".
,
vec select. . pack unpack
.
wait
wait
PID
1, (
).
$?, system.
, waitpid.
891
wantarray
wait,
, , system,
fork wait. wait(2)
. waitpid.
waitpid
waitpid PID, FLAGS
PID 1, , 0,
FLAGS , .
$?, system.
,
":sys_wait_h" POSIX. ,
.
use POSIX ":sys_wait_h";
do {
$kid = waitpid(1,&WNOHANG);
} until $kid == 1;
, waitpid(2) wa*
it4(2), 0. ,
PID, .
1 ,
, . . $SIG{CHLD} =
'IGNORE'.
wantarray
wantarray
,
,
. (""),
, (un
def), ; . .
.
:
return unless defined wantarray;
my @a = complex_calculation();
return wantarray ? @a : \@a;
. caller.
"wantlist", ,
.
892
29.
warn
warn LIST
warn
LIST , $@ (
eval), $@ STDERR \t...caught. (
, die , , warn
( ) .)
, "Warning: Something's
wrong" ( ).
die,
, .
warn Perl w,
, ,
:
warn "Something wicked\n" if $^W;
,
, ,
warn .
, _ _WARN_ _
_ _WARN_ _.
$SIG{_ _DIE_ _} ( ,
die, ).
_ _WARN_ _
, .
BEGIN{} :
# **
BEGIN { $SIG{_ _WARN_ _} = sub { warn $_[0] if $DOWARN } }
my $foo = 10;
my $foo = 20;
# my $foo,
write
893
# !
#
$DOWARN = 1;
# **
#
warn "\$foo is alive and $foo!";
#
. use warnings
.
carp cluck Carp.
write
write FILEHANDLE
write
(,
) , ,
, . 7.
, , , ,
. ,
$~ select :
$old_fh = select(HANDLE);
$~ = "NEWNAME";
select($old_fh);
:
use IO::Handle;
HANDLE>format_name("NEWNAME");
,
, format
:
$~ = "OtherPack::NEWNAME";
:
,
,
, . ,
, $, 0,
write. (
select .)
"_TOP",
$^ select ,
:
use IO::Handle;
HANDLE>format_top_name("NEWNAME_TOP");
894
29.
FILEHANDLE ,
, STDOUT,
select . FILEHANDLE
,
FILEHANDLE.
,
.
write read. .
print.
,
syswrite.
y//
y///
( ),
tr///. . 5.
30
Perl
Perl ,
perl, .
, .
, ,
, , Perl,
.
,
, .
namespace
(namespace) ,
, , ,
.
.
:
. Perl :
,
.
( ),
,
(), .
896
30. Perl
package
(package) Perl
.
. ,
() fred,
Perl $fred
&fred. ,
, ,
, .
, 11
12 .
library
(library), ,
Perl.
Perl, .
Perl ,
, .
.pl, perl library.
Perl,
do FILE require. ,
,
.
; .pl ,
.
.
module
Perl ,
,
, ,
use .
.pm,
use. use
:: ,
, Perl
. 11 ,
Perl.
class
,
, . ,
, 12.
pragma
(pragma) ,
Perl. . 31 .
Perl
897
extension
(extension) Perl,
.pm ,
C C++.
program
(program) Perl ,
; *
(script), ,
, (application) , *
(executable), ,
, (enter*
prise solution) . Perl
,
. , ,
.
Perl
,
Perl,
. , ,
.
,
. , , 100 Perl,
, C C++
.
Perl
, @INC.
, Perl ,
do, require
use. , Perl
V ( verbose) :
% perl le "print foreach @INC"
/usr/libdata/perl5/sparcopenbsd/5.00503
/usr/local/libdata/perl5/sparcopenbsd/5.00503
/usr/libdata/perl5
/usr/local/libdata/perl5
/usr/local/libdata/perl5/site_perl/sparcopenbsd
/usr/libdata/perl5/site_perl/sparcopenbsd
/usr/local/libdata/perl5/site_perl
/usr/libdata/perl5/site_perl
.
. Perl
. , ,
898
30. Perl
,
, ,
Perl. ,
, %INC.
, Perl , :
% perldoc l MODULE
@INC ,
.
.pm, .pl,.ph,.al .so.
, .pm
, Perl.
.
, .pl,
Perl, .
Perl 80 90 .
Perl, , , 1990 ,
, ,
Perl. ,
Perl, .pm
.pl. , ,
.pl.
.pl:
Perl, Perl. .pl
Perl ,
,
Perl .plx.
( ,
, .)
.al
.pm.
h2xs, Perl (
Perl A), make install AutoLoader,
.al .
.ph h2ph,
, ,
C Perl. .ph
, ,
ioctl, fcntl syscall. (
, POSIX, Errno, Fcntl Socket.) . perlinstall,
, .
Perl
899
, ,
, .so ( ,
). .so
.
C C++,
. , ,
, .
require Module use Module, Perl
Module.pm ,
, Module.so
.al. ,
582 . CPAN,
, , freshmeat.net .
Perl . ,
, .
, .
, ,
, , SPITBOL.
,
. Perl
, Perl.
.
31
(pragma) ,
. ( )
.
.
, use
no, , require do,
.
, . .
Perl.
, .
,
,
. ,
, my.
,
no:
use strict;
use integer;
{
no strict 'refs';
no integer;
# ....
}
#
#
use attributes
901
, Perl,
Perl.
, , ,
, .
,
, .
,
use attrs,
. use vars
our. use subs
override .
, ,
.
use attributes
sub afunc : method;
my $closure = sub : method { ... };
use attributes;
@attrlist = attributes::get(\&afunc);
attributes . ,
(attribute
lists), ,
( )
. ( ,
.)
attributes::get. attributes
, .
Perl
.
, Packagespe
cific Attribute Handling attributes(3).
;
. (
eval, eval.)
: locked, method lvalue. . 6
7 .
, ,
, constant.
attributes .
.
902
31.
get
(, )
,
.
Carp::croak, .
reftype
ref ,
Perl
, .
,
,
Perl.
use autouse
use autouse 'Carp' => qw(carp croak);
carp "this carp was predeclared and autoused";
, . . ,
.
, .
, AutoLoader SelfLoader.
, ,
Perl ( )
, .
autouse , . ,
Module , :
use autouse 'Module' => qw(func1 func2($;$) Module::func3);
:
use Module qw(func1 func2);
use base
903
Module,
, .
autouse
, .
,
, ,
.
,
.
, , autouse, ,
(
, ).
:
use Chase;
use autouse Chase => qw(hue($) cry(&$));
cry "this cry was predeclared and autoused";
.
, Chase
.
.
use base
use base qw(Mother Father);
.
:
BEGIN {
require Mother;
require Father;
push @ISA, qw(Mother Father);
}
904
31.
. use base
,
.)
require. require
%INC,
$VERSION . Perl
( ) ,
( , ,
). $VERSION
, use base $VERSION
, 1, defined by base.pm.
use blib
:
% perl Mblib program [args...]
% perl Mblib=DIR program [args...]
Perl:
use blib;
use blib 'DIR';
Perl
Perl M. ,
ExtUtils::MakeMaker.
blib, DIR (
, ), blib
, ..,
.
use bytes
use bytes;
no bytes;
use bytes
, .
use bytes
no bytes.
Perl
( ,
).
use charnames
905
,
15 Unicode. .
use charnames
use charnames HOW;
print "\N{CHARSPEC} is a funny character";
\N{CHARSPEC}:
use charnames ':full';
print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
use charnames ':short';
print "\N{greek:Sigma} is an uppercase sigma.\n";
use charnames qw(cyrillic greek);
print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
CHARNAME ( N{sigma}),
CAPITAL . SMALL.
, , charna
mes, .
,
.
Perl.
, , , . , script
, Unicode Con
sortium .
906
31.
use constant
use
use
use
use
use
use
use
constant
constant
constant
constant
constant
constant
constant
BUFFER_SIZE
ONE_YEAR
PI
DEBUGGING
ORACLE
USERNAME
USERINFO
=>
=>
=>
=>
=>
=>
=>
4096;
365.2425 * 24 * 60 * 60;
4 * atan2 1, 1;
0;
'oracle@cs.indiana.edu';
scalar getpwuid($<);
getpwuid($<);
CHASH>{foo};
CARRAY>[$i];
CPSEUDOHASH>{foo};
CCODE>("me");
CHASH>[10];
1
.
.
. scalar, .
$,
, ,
:
print "The value of PI is @{[ PI ]}.\n";
,
,
,
:
$homedir = USERINFO[7];
$homedir = (USERINFO)[7];
#
# ok
,
, .
, .
1
,
.
907
use constant
,
.
, .
CONST Other Other:: CONST. .
6.
use, use constant
.
constant , if
($foo) { use constant ... }.
, undef
() .
:
use constant CAMELIDS
use constant CAMEL_HOME
=> ();
=> undef;
use constant
,
.
, . , , .
:
use constant FOO => 4, BAR => 5;
FOO,
(4, "BAR", 5). :
use constant FOO => 4
use constant BAR => 5;
,
. (
, .) ,
$hash{CONSTANT}, CONSTANT
. $hash{CONSTANT ()} $hash{+CONSTANT},
. ,
=> ,
, CONSTANT () => 'value', CONSTANT =>
'value'.
constant
:
my $PI : constant = 4 * atan2(1,1);
908
31.
. , ,
.
use diagnostics
use diagnostics;
use diagnostics verbose;
enable diagnostics;
disable diagnostics;
#
#
.
,
33 . ,
, .
use diagnostics ,
Perl w $^W 1.
.
STDERR.
no diagnostics
, .
disable enable.
( use,
.)
verbose perldiag
. $diagnos
tics::PRETTY ( use), escape
less(1) more(1):
BEGIN { $diagnostics::PRETTY = 1 }
use diagnostics;
, Perl ,
. , ,
(, ).
, , warn
carp, .
diagnostics.
,
:
use diagnostics;
print NOWHERE "nothing\n";
print STDERR "\n\tThis message should be unadorned.\n";
use diagnostics
909
:
Parentheses missing around "my" list at diagtest line 6 (#1)
(W parenthesis) You said something like
my $foo, $bar = @_;
when you meant
my ($foo, $bar) = @_;
Remember that "my", "our", and "local" bind tighter than comma.
Name "main::NOWHERE" used only once: possible typo at diagtest line 2 (#2)
(W once) Typographical errors often show up as unique variable
names. If you had a good reason for having a unique name,
then just mention it again somehow to suppress the message.
The our declaration is provided for this purpose.
Name "main::b" used only once: possible typo at diagtest line 6 (#2)
Name "main::x" used only once: possible typo at diagtest line 8 (#2)
Name "main::y" used only once: possible typo at diagtest line 8 (#2)
Filehandle main::NOWHERE never opened at diagtest line 2 (#3)
(W unopened) An I/O operation was attempted on a filehandle that
was never initialized. You need to do an open() or a socket()
call, or call a constructor from the FileHandle package.
This message should be unadorned.
This is a user warning at diagtest line 4.
DIAGNOSTIC TESTER: Please enter a <CR> here:
Use of uninitialized value in division (/) at diagtest line 8 (#4)
(W uninitialized) An undefined value was used as if it were
already defined. It was interpreted as a "" or a 0, but maybe
it was a mistake. To suppress this warning assign a defined
value to your variables.
Illegal division by zero at diagtest line 8 (#5)
(F) You tried to divide a number by 0. Either something was
wrong in your logic, or you need to put a conditional in to
guard against meaningless input.
Uncaught exception from user code:
Illegal division by zero at diagtest line 8.
perldiag.pod.
$SIG{__WARN__}, ,
diagnostics::splainthis ( $SIG{__WARN__}
910
31.
) .
Perl , ,
. $diagnostics::DEBUG,
, ,
:
BEGIN { $diagnostics::DEBUG = 1 }
use diagnostics;
use fields
Pet:
package Pet;
use strict;
use fields qw(name weight _Pet_pid);
my $PID = 0;
sub new {
my Pet $self = shift;
unless (ref $self) {
$self = fields::new($self);
$self>{_Pet_pid} = "this is Pet's secret ID";
}
$self>{name} = "Hey, you!";
$self>{weight} = 20;
return $self;
}
1;
demopet:
use Pet;
my Pet $rock = new Pet;
$rock>{name}
= "quartz";
$rock>{weight} = "2kg";
$rock>{_Pet_pid} = 1233;
$rock>{color}
= "blue";
Dog:
package Dog;
use strict;
use base 'Pet';
use fields qw(name pedigree);
#
#
#
use fields qw(wag _Dog_private); #
sub new {
my $class = shift;
my $self = fields::new($class);
$self>SUPER::new();
#
Pet
Pet name,
pedigree
Pet
911
use fields
$self>{pedigree} = "none";
return $self;
demodog:
use Dog;
my Dog $spot = new Dog;
$spot>{name}
= "Theloneus"; #
$spot>{weight} = "30lbs";
#
$spot>{pedigree} = "mutt";
#
$spot>{color}
= "brown";
$spot>{_Pet_pid} = 3324;
#
#
fields , ,
.
, :
(my Pet $rock) ( Pet)
($rock>{name})
, ,
fields,
, , .
base ,
fields.
.
, ,
.
, ,
.
,
, ,
. ,
.
, ,
, . .
, .
:
new
fields::new
( ).
,
fields. :
package Critter::Sounds;
use fields qw(cat dog bird);
912
31.
sub new {
my Critter::Sounds $self = shift;
$self = fields::new($self) unless ref $self;
$self>{cat} = 'meow';
@$self{'dog','bird'} = ('bark','tweet');
return $self;
}
#
#
phash
fields::phash (
) .
, ,
.
phash ,
. ,
,
. ,
,
.
:
sub dogtag {
my $tag = fields::phash([qw(name rank ser_num)], [@_]);
}
/,
:
my $tag = fields::phash(name => "Joe",
rank => "captain",
ser_num => 42);
my $pseudohash = fields::phash(%args);
. 8 .
%FIELDS
, ,
.
use filetest
$can_perhaps_read = r "file";
{
use filetest 'access';
$can_really_read = r "file";
}
$can_perhaps_read = r "file";
#
#
use integer
913
r, w, x, R, W X, 3
.
, stat.
, , ACL (
). , AFS, ,
use filetest
, .
,
.
:
.
, . . ,
. , ,
, ,
.
, , (
). . 23
.
use integer
use integer;
$x = 10/3;
# $x is now 3, not 3.33333333333333333
,
.
, ,
,
.
914
31.
,
, . , :
use integer;
$x = 1.8;
$y = $x + 1;
$z = 1.8;
: $x == 1.8, $y == 2 $z == 1. $z
, , 1.8
1, . ,
, sqrt
,
use integer. sqrt(1.44) 1.2,
0 + sqrt(1.44) 1.
,
C. , Perl
.
. Perl
, :
% perl le 'print (4 % 3)'
2
% perl Minteger le 'print (4 % 3)'
1
use less
use less;
# !
use
use
use
use
use
less
less
less
less
less
'CPU';
'memory';
'time';
'disk';
'fat';
,
,
.
Perl (less),
.
use lib
use lib "$ENV{HOME}/libperl";
no lib ".";
# ~/libperl
# cwd
@INC .
Perl ,
915
use lib
# ?
# libs
FindBin ,
.
, ,
, . ,
, . $FindBin::Bin (
), ,
. lib,
916
31.
@INC, ,
.
bin
, lib,
, bin. , /usr/local/apa*
che/bin /opt/perl/bin, /usr/local/apache/lib /opt/perl/
lib. :
use FindBin qw($Bin);
use lib "$Bin/../lib";
, use
lib, , PERL5LIB. .
PERL5LIB 19
.
# sh, bash, ksh zsh
$ PERL5LIB=$HOME/perllib; export PERL5LIB
# csh tcsh
% setenv PERL5LIB ~/perllib
, , I:
% perl I ~/perllib programpath args
I . 19.
use locale
@x = sort @y;
{
use locale;
@x = sort @y;
}
@x = sort @y;
# ASCII
#
# ASCII
,
( , no locale)
POSIX .
Perl
,
. . , C
POSIX, Perl LC_CTY
PE LC_COLLATE ,
sort.
use overload
917
,
, Unicode
. () .
15.
use open
use open IN => ":crlf", OUT => ":raw";
open .
open readpipe (. . qx// ),
,
.
open sysopen .
, :raw :crlf
( :utf8).
,
, :raw , :crlf
. (
, binmode,
; . open 29
,
.)
. ,
.
, ,
:DEFAULT
, .
.
open (FH, "<:para :DEFAULT", $file) or die "can't open $file: $!";
,
.
use overload
Number:
package Number;
use overload "+" => \&myadd,
"" => \&mysub,
"*=" => "multiply_by";
918
31.
:
use Number;
$a = new Number 57;
$b = $a + 5;
,
(, C
C++, Perl ). overload
.
Number:
Number::myadd, Number::mysub,
multiply_by
Number ( ).
, ,
( , ,
).
13 .
use re
.
: 'taint' 'eval',
, 'debug' 'debugcolor', .
use re 'taint';
# $match , $dirty .
($match) = ($dirty =~ /^(.*)$/s);
# :
use re 'eval';
$pat = '(?{ $var = 1 })';
#
/alpha${pat}omega/;
# T
# $pat
use re 'debug';
/^(.*)$/s;
# "perl Dr"
#
#
use re 'debugcolor';
# , 'debug'
#
use re 'taint'
,
, m// ,
. ,
,
. .
23.
919
use sigtrap
use re 'eval',
, Perl (?{ ... }),
.
: ,
, ,
CGI,
,
. ;
( ).
. 5 23.
( qr//)
. , qr//, ,
use re 'eval',
. :
$code = '(?{ $n++ })';
#
$str = '\b\w+\b' . $code; #
$line =~ /$str/;
# use re 'eval'
$pat = qr/$str/;
$line =~ /$pat/;
# use re 'eval'
# use re 'eval'
use sigtrap
use sigtrap;
use sigtrap qw(stacktrace oldinterfacesignals); #
use
use
use
use
use
sigtrap
sigtrap
sigtrap
sigtrap
sigtrap
920
31.
sigtrap ,
. ,
,
, END {},
,
.
sigtrap .
Perl,
die.
.
.
, .
, use sigtrap, .
sigtrap,
. ,
.
,
, :
stacktrace
, ,
Perl STDERR, .
die
, , die Carp::cro
ak , .
handler YOURHANDLER
YOURHANDLER ,
. YOURHANDLER ,
%SIG. ,
C (
). , ,
C Perl . (
, , sigtrap,
, , ,
, .)
use sigtrap
921
sigtrap ,
:
normalsignals
. ,
.
HUP, INT, PIPE TERM.
errorsignals
. ,
Perl .
ABRT, BUS, EMT, FPE, ILL, QUIT, SEGV, SYS TRAP.
oldinterfacesignals
.
sigtrap. ABRT, BUS, EMT, FPE, ILL,
PIPE, QUIT, SEGV, SYS, TERM TRAP. use sigtrap
, .
,
, .
( , .)
sigtrap
untrapped
,
, .
any
,
. , .
signal
, (. .
/^[AZ][AZ09]*$/),
sigtrap.
number
, sigtrap
number. , ,
$VERSION:
% perl Msigtrap le 'print $sigtrap::VERSION'
1.02
922
31.
sigtrap
:
use sigtrap;
, :
use sigtrap qw(stacktrace oldinterfacesignals);
:
use sigtrap qw(BUS SEGV PIPE ABRT);
die
normalsignals; ,
Perl errorsignals:
use sigtrap qw(die untrapped normalsignals
stacktrace any errorsignals);
my_handler normalsig
nals:
use sigtrap 'handler' => \&my_handler, 'normalsignals';
my_handler normalsignals;
Perl errorsignals:
use sigtrap qw(handler my_handler normalsignals
stacktrace errorsignals);
use strict
use strict;
# .
923
use strict
use strict;
no strict "vars";
# ...
# ... .
, , Perl .
, ,
. ,
.
:
subs, vars refs. ,
.
strict 'refs'
, . .
8.
use strict 'refs';
$ref = \$foo;
print $$ref;
# "" () .
# .
$ref = "foo";
print $$ref;
# () .
# , strict refs.
.
; strict 'refs' .
,
. .
:
.
, .
,
. , @EXPORT
@ISA , .
,
. ,
, ,
use strict, "refs":
#
for my $methname (qw/name rank serno/) {
no strict 'refs';
*$methname = sub { $_[0]>{ __PACKAGE__ . $methname };
}
924
31.
strict 'vars'
,
:
our ( ) my ( ).
. ( use vars ,
our.)
.
, Perl,
.
( main, @ARGV $_)
, $a $b, sort.
, Exporter,
our:
our @EXPORT_OK = qw(name rank serno);
strict 'subs'
Perl
. (bareword , ,
bearword) ,
, . (
.)
.
.
, .
,
.
,
925
use vars
=>,
.
use strict 'subs';
$x = whatever;
$x = whatever();
# : " "!
# .
sub whatever;
$x = whatever;
# .
# ok.
# , => :
%hash = (red => 1, blue => 2, green => 3);
$rednum = $hash{red};
# But not
@coolnums
@coolnums
@coolnums
# Ok, .
this one:
= @hash{blue, green};
# : " ".
= @hash{"blue", "green"}; # Ok, .
= @hash{qw/blue green/}; # .
use subs
use subs qw/winken blinken nod/;
@x = winken 3..10;
@x = nod blinken @x
. ,
,
.
, ,
, :
sub winken(@);
sub blinken(\@) : locked;
sub nod($) : lvalue;
, use subs
, . ,
, ,
. no subs.
use vars
use vars qw($frobbed @munge %seen);
,
, our.
:
our($frobbed, @munge, %seen);
926
31.
:
our $frobbed = "F";
our @munge = "A" .. $frobbed;
our %seen = ();
, , ,
,
.
use warnings
use warnings;
no warnings;
# , ""
# , ""
use warnings::register;
if (warnings::enabled()) {
warnings::warn("some warning");
}
if (warnings::enabled("void")) {
warnings::warn("void", "some warning");
}
Perl, ,
.
Perl
w $^W.
, . w
, ,
,
. $^W
,
, .1
,
, . ,
, ,
,
.
warnings ,
,
, , .
(. 31.1),
. (
1
, , BEGIN.
use warnings
927
. 31.1. Perl
.) , use
no :
928
31.
warnings, :
use warnings "void"; # "void".
...
use warnings "io"; # "void" "io".
...
no warnings "void"; # "io".
,
warnings, , FATAL .
, , ,
, . , ,
(
0) ,
. ,
,
,
:
{
use warnings FATAL => qw(numeric uninitialized);
$x = $y + $z;
}
, $y $z (. .
undef) ,
, , ,
, , w,
. ( Perl,
Python.) ,
. ,
.
warnings w
$^W; .
W ,
, , do, req
uire use. , W Perl ,
use warnings 'all'.
lint(1) Perl. (, ,
B::Lint.)
X . ,
no warnings 'all'.
,
,
use warnings
929
(
,
,
):
warnings::register
,
.
warnings::enabled(CATEGORY )
, CATEGORY
.
. CATEGORY ,
.
warnings::warn(CATEGORY, MESSAGE)
CATEGORY "FATAL",
MESSAGE STDERR. CATEGORY
"FATAL", MESSAGE STDERR, . CATEGO
RY , .
32
, Perl.
,
, ,
.
,
. , CGI,
.
Perl ActiveState, .
,
, ,
.
, ,
, .
, , ,
.
Perl,
perldoc,
man(1) . .
. ,
, ()
* . , pod
,
(.pm), pod
, . ( , , HTML.)
931
.
, ,
31 .
, ( )
Perl, .
Class::Struct
Perl struct
Constant
Integer
Perl ,
Math::BigFloat
Math::BigInt
Math::Complex
Math::Trig
Overload
Perl
Symbol
Perl ty
peglob
Time::Local
localtime
gmtime
, ,
.
Search::Dict
Text::Abbrev
Text::ParseWords
Text::Soundex
Soundex
Text::Tabs
expand(1) unexpand(1)
Text::Wrap
932
32.
, ,
.
Getopt::Long
(xxx)
Getopt::Std
(xyz)
.
Cwd
File::Basename
File::CheckTree
File::Compare
File::Copy
File::DosGlob
Microsoft
File::Find
, find(1)
File::Glob
Unix
File::Path
File::Spec
( )
File::Spec::Functions
( )
File::Spec::Mac
Mac OS
File::Spec::OS2
OS/2
File::Spec::Unix
Unix
File::Spec::VMS
VMS
File::Spec::Win32
Microsoft
File::stat
stat lstat
933
,
,
.
DirHandle
FileCache
FileHandle
io
io::Dir
io::File
io::Handle
io::Pipe
io::Poll
poll(2)
io::Seekable
io::Select
OO select(2)
SelectSaver
Bytes
Charnames
escape \N{na
me}
I18N::Collate
8
( )
Locale
POSIX
utf8
UTF8 Unicode
934
32.
Fcntl
C fcntl.h Perl
filetest
(r, w . .)
open
open
POSIX
Shell
Perl
sigtrap
Sys::Hostname
Sys::Syslog
C syslog(3)
Time::gmtime
gmtime
Time::localtime
localtime
Time::tm
, Time::gmtime Time::lo
caltime
User::grent
getgr*
User::pwent
getpw*
, 16
.
io::Socket
io::Socket::INET
io::Socket::UNIX Unix (
)
IPC::Msg
IPC::Open2
IPC::Open3
IPC::Semaphore
System V
IPC::SysV
IPC System V
Net::hostent
gethost*
935
Net::netent
getnet*
Net::Ping
Net::protoent
getproto*
Net::servent
getserv*
Socket
C socket.h
CGI
CGI HTML
CGI::Apache
CGI::Carp
CGI httpd(8) ( )
CGI::Cookie
cookie HTTP
CGI::Fast
Fast CGI
CGI::Pretty
HTML
CGI::Push
CGI push
DBM
.
AnyDBM_File
DBM
DB_File
GDBM_File
gdbm(3)
NDBM_File
ndbm(3)
SDBM_File
SDBM ( DBM)
OS CLI I/O API, ASAP.
Term::Cap
termcap(3)
Term::Complete
Term::ReadLine
readline
936
32.
,
(sandboxes).
Opcode
Perl Safe
Ops
Safe
Perl:
(extentions & internals)
c (Intentions & Externals).
attributes
attrs
()
base
Data::Dumper Perl
DB
API Perl
Devel::DProf Perl
Devel::Peek
XS
diagnostics
Dumpvalue
English
Env
%ENV,
Errno
C errno.h
%!
Fatal
,
fields
less
Perl (
)
re
strict
subs
vars
( . our
29
937
Tie::Array
Tie::Handle
Tie::Hash
Tie::RefHash
Tie::Scalar
Tie::SubstrHash
UNIVERSAL
( )
, .
Carp
, warn die (
)
warnings
, , .
Pod::Checker
pod
( podchecker(1))
Pod::Functions
Pod::Html
Pod::InputObjects
Pod::Man
Pod::Parser
pod
Pod::Select
pod ( podse*
lect(1))
Pod::Text
pod ASCII
( pod2text(1))
938
32.
()
Pod::Text::Color
Pod::Text::Termcap
pod ASCII c
escape
Pod::Usage
,
pod
AutoLoader
AutoSplit
autouse
blib
MakeMaker
Config
Perl
CPAN
, Perl CPAN
Devel::SelfStubber
SelfLoader
DynaLoader
C C++
Perl
Exporter
ExtUtils::Command
Makefile
ExtUtils::Embed
Perl C C++
ExtUtils::Install
Perl
ExtUtils::Installed
ExtUtils::Liblist
MANIFEST
ExtUtils::Miniperl
C perlmain.c
ExtUtils::Mkboots
trap
DynaLoader
ExtU
tils::Mksymlists
939
Unix ExtUtils::MakeMaker
ExtUtils::MM_Unix
ExtUtils::MakeMaker
ExtUtils::MM_VMS
Unix ExtUtils::MakeMaker
ExtUtils::MM_Win32
Unix ExtUtils::MakeMaker
ExtUtils::Packlist
.packlist
ExtUtils::testlib
blib/* @INC
FindBin
,
Perl
lib
@INC
SelfLoader
XSLoader
C C++ Perl
, .
Benchmark
Test
Test::Harness
Perl
Perl.
B::Asmdata
Perl
B::Assembler
Perl
B::Bblock
B::Bytecode
Perl
B::C
C Perl
B::CC
C
Perl
B::Debug
Perl
940
32.
()
B::Deparse
Perl
Perl
B::Disassembler
Perl
B::Lint
B::Showlex
B::Stash
B::Terse
Perl
B::Xref
Perl
ByteLoader
Perl
Perl
Microsoft
Perl Microsoft ActiveState
, Microsoft.
(,
Unix Cygwin),
Intel Windows, CPAN.
Win32::ChangeNotify ,
Win32::Console
Win32
Win32::Event
Win32 Perl
Win32::EventLog
Win32 Perl
Win32::File
Perl
Win32
Win32::Internet
WININET.DLL
Win32::Mutex
Win32 Perl
Win32::NetAdmin
Perl
Win32::NetResource
Perl
Win32::ODBC
ODBC Win32
Win32::OLE
OLE
941
Benchmark
Win32::OLE::Const
TypeLib
Win32::OLE::Enum
Collection OLE
Win32::OLE::NLS
OLE
Windows NT
Win32::Process
Win32::Semaphore
Win32
Win32::Service
Win32::Sound
Windows
Win32::TieRegistry
( ?)
Win32API::File
API Win32
Win32API::Net
Windows NT
Win32API::Registry
API Win32
WINREG.H
Benchmark
use Benchmark qw(timethese cmpthese timeit countit timestr);
# :
timethese $count, {
'Name1' => '...code1...',
'Name2' => '...code2...',
};
# :
timethese $count, {
'Name1' => sub { ...code1... },
'Name2' => sub { ...code2... },
};
cmpthese $count, {
'Name1' => '...code1...',
'Name2' => '...code2...',
};
$t = timeit $count, '...code...';
print "$count loops of code took:", timestr($t), "\n";
$t = countit $time, '...code...';
$count = $t>iters;
print "$count loops of code took:", timestr($t), "\n";
942
32.
Benchmark ,
. timethese
,
. cmpthese,
.
( ,
)
, , , ,
. ,
, .
,
(timeit)
(countit). Benchmark
(. ). countit ,
, ,
, .
Benchmark ,
.
,
,
. ,
.
, , ,
. ,
(, , ): chop,
substr
. 2, 200 20_000:
use Benchmark qw/countit cmpthese/;
sub run($) { countit(5, @_) }
for $size (2, 200, 20_000) {
$s = "." x $len;
print "\nDATASIZE = $size\n";
cmpthese {
chop2 => run q{
$t = $s; chop $t; chop $t;
},
subs
=> run q{
($t = $s) =~ s/..\Z//s;
},
lsubstr => run q{
$t = $s; substr($t, 2) = '';
},
rsubstr => run q{
$t = substr($s, 0, length($s)2);
},
};
}
943
Carp
:
DATASIZE = 2
Rate
181399/s
214655/s
338477/s
384487/s
DATASIZE = 200
Rate
subs
200967/s
lsubstr 246468/s
rsubstr 264428/s
chop2 304818/s
subs
lsubstr
chop2
rsubstr
DATASIZE = 20000
Rate rsubstr
rsubstr 5271/s
subs
9087/s
72%
lsubstr 9260/s
76%
chop2 9660/s
83%
"rsubstr" 14% ,
"chop2", 45%
. ( )
.
, , . .
C,
Perl. ,
. ,
. ,
.
Carp
use Carp;
croak "We're outta here!";
use Carp qw(:DEFAULT cluck);
cluck "This is how we got here!";
Carp ,
,
. Carp ,
warn die
, ,
, , . , Carp
.
944
32.
. carp , warn,
,
(). croak , die,
, , , , .
, , cluck confess
carp croak , ,
. cluck,
.
.
CGI
use CGI qw(:standard);
$who = param("Name");
$phone = param("Number");
@picks = param("Choices");
CGI HTML, ,
.
, , ,
(radio buttons), ,
, , "MULTIPLE".
, ,
cookies,
HTML, ,
.
Perl Apache mod_perl.
OReilly Writing Apache Modules with Perl and C
(Lincoln Stein) (Doug MacEachern).
CGI::Carp
use CGI::Carp;
warn "This is a complaint";
# .
die "But this one is serious"; # Server 500.
use CGI::Carp qw(carpout);
# .
open(LOG, ">>/var/tmp/mycgilog")
or die "Can't append to mycgilog: $!\n";
carpout(*LOG);
# .
use CGI::Carp qw(fatalsToBrowser);
die " ";
945
Class::Struct
. ,
, ,
,
,
.
, . .
CGI "Server 500",
HTTP ,
, ,
. carpout
. fatalsToBrowser
.
CGI.
Class::Struct
use Class::Struct;
struct Manager
name
=>
salary =>
started =>
};
=> {
'$',
'$',
'$',
#
#
#
#
Manager>new().
name() .
salary().
started().
#
#
#
#
#
Shoppe>new().
owner() .
addrs() .
stock() .
Manager>new().
$store = Shoppe>new();
$store>owner('Abdul Alhazred');
$store>addrs(0, 'Miskatonic University');
$store>addrs(1, 'Innsmouth, Mass.');
$store>stock("books", 208);
$store>stock("charms", 3);
$store>stock("potions", "none");
$store>boss>name('Prof L. P. Haitch');
$store>boss>salary('madness');
$store>boss>started(scalar localtime);
Class::Struct ,
. , ,
struct.
Perl, , ,
,
,
(wrappers). struct Class::Struct
946
32.
, . ,
, . ,
, new,
, ,
.
"$", "@", "%" "&" .
.
Class::Struct
, Net::hostent User::pwent,
. , CPAN
Tie::SecureHash Class::Multimethods
. .
12 .
Config
use Config;
if ($Config{cc} =~ /gcc/) {
print "This perl was built by GNU C.\n";
}
use Config qw(myconfig config_sh config_vars);
print myconfig();
# perl V
print config_sh(); #
config_vars qw/osname osvers archname/;
, Perl,
. Config
%Config,
900 . (
Perl V: PATTERN.) Config
, ,
, . ,
:
osname='openbsd';
osvers='2.6';
archname='OpenBSD.sparcopenbsd';
. ,
perl , , ,
; ,
, Linux Solaris, BSD.
947
Cwd
CPAN
# CPAN.
% perl MCPAN e shell
# .
% perl MCPAN e 'CPAN::Shell>r'
# .
% perl MCPAN e "install Class::Multimethods"
CPAN ,
Perl CPAN, 22.
,
.
CPAN,
, .
,
, ,
, .
Cwd
use Cwd;
$dir = getcwd();
# ?
# $ENV{PWD}.
Cwd
. ,
pwd(1), , POSIX,
, Perl POSIX.
getcwd, ,
,
. chdir,
$ENV{PWD}; ,
, . real
path ,
,
, realpath(3).
948
32.
Data::Dumper
use Data::Dumper;
print Dumper($store);
Class::Struct :
$VAR1 = bless( {
'Shoppe::owner' => 'Abdul Alhazred',
'Shoppe::stock' => {
'charms' => 3,
'books' => 208,
'potions' => 'none'
},
'Shoppe::boss' => bless( {
'Manager::name' =>
'Prof L. P. Haitch',
'Manager::salary' =>
'madness',
'Manager::started' =>
'Sat Apr 1 16:18:13 2000'
}, 'Manager' ),
'Shoppe::addrs' => [
'Miskatonic University',
'Innsmouth, Mass.'
]
}, 'Shoppe' );
Dumper Data::Dumper (
, ) ,
eval,
.
DBM
. MLDBM CPAN DB_File
DBM,
, .
( (serialization),
(marshalling)) Storable FreezeThaw, CPAN.
DB_File
use DB_File;
DBM:
tie(%hash, "DB_File", $filename)
# .
or die "Can't open $filename: $!";
$v = $hash{"key"};
# .
949
DB_File
$hash{"key"} = "value";
untie %hash;
# .
,
DBM:
tie(%hash, "DB_File", "mytree", O_RDWR|O_CREAT, 0666, $DB_BTREE)
or die "Cannot open file `mytree': $!";
while (($k, $v) = each %hash) {
print "$k => $v\n";
}
# .
:
tie(@lines, "DB_File", $textfile, O_RDWR|O_CREAT, 0666, $DB_RECNO)
or die "Cannot open textfile $textfile: $!";
# , .
$lines[0] = "first line";
$lines[1] = "second line";
$lines[2] = "third line";
push @lines, "penult", "last"; # .
$wc = scalar @lines;
# .
$last = pop @lines;
# .
, . ,
.
950
32.
Dumpvalue
use Dumpvalue;
Dumpvalue>new>dumpValue($store);
Class::Struct :
'Shoppe::addrs' => ARRAY(0x9c1d4)
0 'Miskatonic University'
1 'Innsmouth, Mass.'
'Shoppe::boss' => Manager=HASH(0xa5754)
'Manager::name' => 'Prof L. P. Haitch'
'Manager::salary' => 'madness'
'Manager::started' => 'Sat Apr 1 16:18:13 2000'
'Shoppe::owner' => 'Abdul Alhazred'
'Shoppe::stock' => HASH(0xfdbb4)
'books' => 208
'charms' => 3
'potions' => 'none'
, .
, .
Perl x.
.
Perl
.
English
use English;
# awk.
$RS = '';
# $/
while (<>) {
next if $NR < 10;
# $.
...
}
# , .
$INPUT_RECORD_SEPARATOR = '';
while (<>) {
next if $INPUT_LINE_NUMBER < 10;
...
}
English
,
(
<Caps Lock>). ,
951
Exporter
.
. ,
, $$ $PROGRAM_NAME, $0,
$PID. .
English
. 28 .
Errno
use Errno;
unless (open(FH, $pathname))
if ($!{ENOENT}) { #
warn "$pathname does
}
else {
warn "open failed on
}
}
{
!
not exist\n";
`$pathname': $!";
Errno
,
. , ":POSIX",
, POSIX 1003.1.
%!, tie.
%!
, POSIX, ,
.
Exporter
MyModule.pm:
package MyModule;
use strict;
use Exporter;
our
our
our
our
our
);
$VERSioN = 1.00;
# ...
@ISA = qw(Exporter);
@EXPORT
= qw(f1 %h); # , .
@EXPORT_OK = qw(f2 f3); # ,
%EXPORT_TAGS = (
# :.
a => [qw(f1 f2 f3)],
b => [qw(f2 %h)],
952
32.
# .
1;
, :
use
use
use
use
use
MyModule;
# @EXPORT.
MyModule ();
# , .
MyModule "f1", "f2", "%h"; # .
MyModule qw(:DEFAULT f3); # @EXPORT + .
MyModule "f4";
# , f4 .
use ,
import
. (, )
import ,
Exporter. ,
.
Exporter ,
. ,
Exporter,
( ).
Exporter
@ISA, .
use , import
: MyModule>import(LIST). import
, Exporter::import
.
@EXPORT (
),
use. @EXPORT_OK ,
, .
$VERSION, use .
. . 11 ,
Exporter.
Fatal
Fatal .
, , ,
,
.
, .
, , ,
, ,
.
953
File::Basename
Fcntl
use
use
use
use
use
Fcntl;
Fcntl ":flock";
Fcntl ":seek";
Fcntl ":mode";
Fcntl ":Fcompat";
#
#
#
#
#
fcntl.h.
LOCK_*.
SEEK_CUR, SEEK_SET, SEEK_END.
S_* stat .
F*.
Fcntl ,
Perl.
F_GETFL F_SETFL fcntl, SEEK_SET SEEK_END seek sysseek,
O_CREAT O_EXCL sysopen.
":flock" LOCK_EX, LOCK_NB, LOCK_SH LOCK_UN
flock; ":mode" sys/stat.h, S_IRUSR
S_ISFIFO; ":seek" seek sysseek; ":Fcompat"
"F", "F_" , FAPPEND,
FASYNC FNONBLOCK. . Fcntl
fcntl(2), lseek(2), open(2) stat(2).
File::Basename
use File::Basename;
$fullname = "/usr/local/src/perl5.6.1.tar.gz";
$file = basename($fullname);
# file="perl5.6.1.tar.gz"
$dir = dirname($fullname);
# dir="/usr/local/src"
# dir="/usr/local/src/" file="perl5" ext=".6.1.tar.gz"
954
32.
File::Basename
. dirname ,
, basename , .
fileparse ,
; ,
.
, .
,
. fileparse_set_fstype
, , fileparse_set_fstype("VMS")
VMS, ,
VMS.
File::Compare
use File::Compare;
printf "fileA and fileB are %s.\n",
compare("fileA","fileB") ? "different" : "identical";
use File::Compare 'cmp';
sub munge($) {
my $line = $_[0];
for ($line) {
s/^\s+//; # .
s/\s+$//; # .
}
return uc($line);
}
if (not cmp("fileA", "fileB", sub {munge $_[0] eq munge $_[1]} ) {
print "fileA and fileB are kinda the same.\n";
}
File::Compare compare,
. 0,
File::Find
955
, 1, ,
1, .
,
. cmp(1)
cmp. (
cmp.)
File::Copy
use File::Copy;
copy("/tmp/fileA", "/tmp/fileA.orig") or die "copy failed: $!";
copy("/etc/motd", *STDOUT)
or die "copy failed: $!";
move("/tmp/fileA", "/tmp/fileB")
or die "move failed: $!";
use File::Copy qw/cp mv/;
# Get normal Unix names.
cp "/tmp/fileA", "/tmp/fileA.orig"
or die "copy failed: $!";
"/tmp/fileA", "/tmp/fileB"
or die "move failed: $!";
File::Find
use File::Find;
# .
find sub { print "$File::Find::name\n" if d }, ".";
1
956
32.
# ,
.
@dirs = @ARGV ? @ARGV : ('.');
my $sum = 0;
find sub { $sum += s }, @dirs;
print "@dirs contained $sum bytes\n";
#
#
find { wanted => \&myfunc, follow => 1, bydepth => 1 }, ".";
find File::Find .
,
.
. $_
,
.
$File::Find::name .
, , "wanted",
"bydepth", "follow", "follow_fast", "follow_skip", "no_chdir", "untaint", "unta
int_pattern" "untaint_skip",
.
find2perl(1), Perl.
File::Glob
use File::Glob ':glob';
@list = <*.[Cchy]>;
# glob.
# glob POSIX, csh.
File::Glob
957
:glob,
bsd_glob,
csh_glob. ,
bsd_glob ,
, csh_glob .
<*.c *.h> . ,
, .
bsd_glob ,
( )
. , ,
.
, :1
GLOB_BRACE
{pat,pat,...},
csh(1). {} ,
find(1).
GLOB_CSH
GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE.
GLOB_ERR
, bsd_glob ,
. bsd_glob
.
GLOB_MARK
, , .
GLOB_NOCASE
;
bsd_glob . (.
MSDOS).
GLOB_NOCHECK
, bsd_glob
, , /bin/sh.
GLOB_QUOTE, .
GLOB_NOMAGIC
, GLOB_NOCHECK, ,
*, ? [. NOMAGIC
1
glob
bsd_glob.
958
32.
csh(1) , ,
.
GLOB_NOSORT
(
).
.
GLOB_QUOTE
\ :
, ,
. (.
MSDOS.)
GLOB_TILDE
, ~USER.
USER , (,
) .
bsd_glob (, )
, (tainted),
. GLOB_ERROR ,
$! ($OS_ERROR) .
GLOB_ERROR ,
GLOB_ABEND GLOB_NOSPACE. (GLOB_ABEND , bsd_glob
, GLOB_NOSPACE ,
.) bsd_glob ,
, ,
, GLOB_ERROR. ,
bsd_glob ,
ENOENT ENOTDIR .
, , GLOB_ERR.
, ,
,
, VMS, OS/2,
Mac OS ( Mac OS X) Microsoft ( , Perl
Cygwin). , ,
, GLOB_NOCASE.
,
:case :nocase.
MSDOS
.1
1
,
;
.
959
File::Spec
( GLOB_QUOTE)
. (, )
, ,
.
, ( GLOB_QUO
TE) [, ], {, }, , ~
.
,
Perl.
, . ,
MSDOS
.
, File::DosGlob, . .
, Unix.
File::Spec
use File::Spec;
# OO
$path
#
#
#
= File::Spec>catfile("subdir", "filename");
'subdir/filename' Unix, OS2 Mac OS X
'subdir:filename' () Apple Macs
'subdir\filename' Microsoft
$path
#
#
#
use File::Spec::Unix;
$path = File::Spec::Unix>catfile("subdir", "filename");
# 'subdir/filename' ( , Unix)
use File::Spec::Mac;
$path = File::Spec::Mac>catfile("subdir", "filename");
# 'subdir:filename'
use File::Spec::Win32;
$path = File::Spec::Win32>catfile("subdir", "filename";)
# 'subdir\filename'
# .
use File::Spec::Functions;
$path = catfile("subdir", "filename");
File::Spec
,
. Unix,
VMS, Mac Win32. catfile,
,
960
32.
. File::Spec
. ,
. File::Spec::Functions
.
File::stat
use File::stat;
$st = stat($file)
or die "Can't stat $file: $!";
if ($st>mode & 0111 and $st>nlink > 1)) {
print "$file is executable with many links\n";
}
use File::stat ":FIELDS";
stat($file)
or die "Can't stat $file: $!";
if ($st_mode & 0111 and $st_nlink > 1) ) {
print "$file is executable with many links\n";
}
@statinfo = CORE::stat($file);
# .
File::stat
Perl stat lstat, , Fi
le::stat ( undef ). ,
stat(2),
dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize
blocks.
":FIELDS". ( stat lstat.)
, "st_"
. $st_dev $st>dev.
File::Temp
use File::Temp qw(tempfile tempdir);
$dir = tempdir(CLEANUP => 1);
($fh, $filename) = tempfile(DIR => $dir);
($fh, $filename) = tempfile($template, DIR => $dir);
($fh, $filename) = tempfile($template, SUFFIX => ".data");
$fh = tempfile();
use File::Temp ':mktemp';
($fh, $filename) = mkstemp("tmpfileXXXXX");
($fh, $filename) = mkstemps("tmpfileXXXXXX", $suffix);
$tmpdir = mkdtemp($template);
$unopened_file = mktemp($template);
961
FileHandle
FileHandle
use FileHandle;
$fh = new FileHandle;
if ($fh>open("< file")) {
print $line while defined($line = $fh>getline);
$fh>close;
}
$pos = $fh>getpos;
$fh>setpos($pos);
# tell()
# seek()
FileHandle ,
Perl , .
,
,
io::Handle io::File.1
C (clea*
rerr(3), fgetpos(3), fsetpos(3) setvbuf(3)).
$|
autoflush
$,
output_field_separator
$\
output_record_separator
, .
962
32.
()
$/
input_record_separator
$.
input_line_number
$%
format_page_number
$=
format_lines_per_page
$
format_lines_left
$~
format_name
$^
format_top_name
$:
format_line_break_characters
$^L
format_formfeed
:
$ofh = select(HANDLE);
$~ = 'SomeFormat';
$| = 1;
select($ofh);
:
use FileHandle;
HANDLE>format_name('SomeFormat');
HANDLE>autoflush(1);
(output_field_separator, output_record_separa
tor input_record_separator) ,
:
.
, . , .
,
:
open my $fh, "< somefile"
or die "can't open somefile: $!";
:
use FileHandle;
my $fh = FileHandle>new("< somefile")
or die "can't open somefile: $!";
FileHandle
963
HANDLE>blocking(EXPR)
,
, (. .
), .
(
, ). blocking
$! undef.
fcntl, FileHandle .
HANDLE>clearerr
C clearerr(3)
.
HANDLE>error
C ferror(3)
. ,
.
clearerr. (
seek.)
HANDLE>formline(PICTURE, LIST)
, ($^A),
formline PICTURE LIST
,
. , ,
72:
use FileHandle;
STDOUT>formline("^" . ("<" x 72) . "~~\n", $long_text);
HANDLE>getpos
C fgetpos(3),
tell . (UNIX)
, getpos setpos
.
FileHandle>new_tmpfile
C tmpfile(3),
, ,
.
. . unlink,
. POSIX::tmpnam,
POSIX, ,
, ,
. Perl 5.6.1
File::Temp.
964
32.
HANDLE>setbuf(BUFFER)
C setbuf(3) BUFFER.
, undef.
, setbuf setvbuf,
,
setbuf setvbuf.
, .
HANDLE>setpos(EXPR)
C fsetpos(3),
seek . ,
getpos, .
HANDLE>setvbuf(BUFFER, TYPE, SIZE)
C setvbuf(3) BUFFER.
TYPE C _ioNBF
( ), _ioLBF ( ) _ioFBF (
), . . ,
setbuf.
HANDLE>sync
C fsync(3)
. , sync
, ,
, .
HANDLE>untaint
(untainted) . (.
23 ) , ,
. ,
: Perl .
Getopt::Long
:
use Getopt::Long;
GetOptions("verbose" => \$verbose,
"debug"
=> \$debug,
"output=s" => \$output);
:
%
%
%
%
%
prog
prog
prog
prog
prog
verbose
debug
v d
output=somefile
o somefile
Getopt::Std
965
Getopt::Long GetOptions
.
, , , ,
, , ,
, POSIX
FSF, , ,
. ,
Getopt::Std, .
, CPAN
Getopt::Declare,
.
Getopt::Std
use Getopt::Std;
getopt getopts :
our ($opt_o, $opt_i, $opt_f);
getopt('oif');
# o, i f .
# $opt_*.
getopts('oif:');
# o & i ; f .
# $opt_* .
:
my %opts;
# .
getopt('oif', \%opts);
# .
getopts('oif:', \%opts); # o i
# f .
966
32.
% prog iofTMPFILE
% prog oifTMPFILE
IO::Socket
use io::Socket;
:
$socket = new io::Socket::INET (PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto
=> "tcp",
Type
=> SOCK_STREAM)
or die "Can't connect to $remote_host:$remote_port : $!\n";
$socket = io::Socket::INET>new("$remote_host:$remote_port");
# "localhost:80", for example.
print $socket "data\n";
$line = <$socket>;
:
$server = io::Socket::INET>new(LocalPort
Type
Reuse
Listen
or die "Can't be a TCP server on port
=> $server_port,
=> SOCK_STREAM,
=> 1,
=> 10 ) # or SOMAXCONN
$server_port : $!\n";
io::Socket ,
Socket.
, ,
, tcp_connect.
io::Handle io::Socket::INET io::So
IPC::Open3
967
cket::UNIX. . FileHandle.
16.
IPC::Open2
use IPC::Open2;
local(*HIS_OUT, *HIS_IN); # , , .
$childpid = open2(*HIS_OUT, *HIS_IN, $program, @args)
or die "can't open pipe to $program: $!";
print HIS_IN "here's your input\n";
$his_output = <HIS_IN>;
close(HIS_OUT);
close(README);
waitpid($childpid, 0);
IPC::Open2 open2
.
( ,
).
, , ,
. .
,
, .
waitpid $pid, 0
.
().
, C
.
, ,
. ,
.
:
, .
. 16.
IPC::Open3
use IPC::Open3;
local(*HIS_IN, *HIS_OUT, *HIS_ERR);
$childpid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd, @args);
print HIS_IN "stuff\n";
close(HIS_IN);
# .
968
32.
@outlines = <HIS_OUT>;
# EOF.
@errlines = <HIS_ERR>;
# XXX:
print "STDOUT:\n", @outlines, "\n";
print "STDERR:\n", @errlines, "\n";
close HIS_OUT;
close HIS_ERR;
waitpid($childpid, 0);
if ($?) {
print "That child exited with wait status of $?\n";
}
IPC::Open3 IPC::Open2 (
), , open3
,
.
, open2 (. ),
. open3
open2.
. , open3
.
,
,
. , select
io::Select.
. 16.
Math::BigInt
use Math::BigInt;
$i = Math::BigInt>new($string);
use Math::BigInt ':constant';
print 2**200;
:
+1606938044258990275541962092341162602522202993782792835301376
Math::BigInt ,
.
new ,
, ":constant",
Math::BigInt.
, Perl 5.6 .
, ,
. ( ,
2**200.)
969
Net::hostent
Math::Complex
use Math::Complex;
$z = Math::Complex>make(5, 6);
$z = cplx(5, 6);
# ,
$t = 4 3*i + $z;
#
print "$t\n";
# 9+3i
print sqrt(9), "\n";
# 3i
Math::Complex
. ,
, , ,
, .
( abs, log, sqrt,
sin, cos atan2) , .
Re Im,
,
, tan, asin, acos, sinh, cosh tanh.
i, , ,
i; . . 1.
Math::Trig
use Math::Trig;
$x = tan(0.9);
$y = acos(3.7);
$z = asin(2.4);
$halfpi = pi/2;
$rad = deg2rad(120);
Net::hostent
use Socket;
use Net::hostent;
970
32.
# localhost
gethostbyna
me gethostbyaddr, ,
Net::hostent ( undef ).
, C
struct hostent netdb.h: name, aliases, addrtype, length addr_list. ali
ases addr_list ;
. addr ,
addr_list. gethost ,
gethostbyaddr Socket::inet_aton,
gethostbyname.
, , ,
:FIELDS
, ,
h_. , ,
.
POSIX
use POSIX;
# .
$n = ceil($n);
#
$n = floor($n);
#
# "20000401" .
$datestr = strftime("%Y%m%d", localtime);
# "Saturday 04/01/00" .
$datestr = strftime("%A %D", localtime);
# , ,
# ; . File::Temp
# CPAN, v5.6.1.
do {
$name = tmpnam();
} until sysopen(FH, $name, O_CREAT|O_EXCL|O_RDWR, 0666);
POSIX
971
# chown .
if (sysconf(_PC_CHOWN_RESTRICTED)) {
print "Hurray only the superuser may call chown\n";
}
# uname.
my($kernel, $hostname, $release, $version, $hardware) = uname();
use POSIX ":sys_wait_h";
while (($dead_pid = waitpid(1, &WNOHANG)) > 0) {
# , $dead_pid.
}
# / ( ,
# ).
setsid(0)
or die "setsid failed: $!";
Perl POSIX ( )
POSIX 1003.1, ANSI C,
, . ,
. . POSIX
Programmers Guide (Donald Lewine, OReilly, 1991).
, C #define ,
EINTR O_NDELAY,
. ,
Perl (, floor, ceil, strftime, uname, setsid, setlocale sysconf),
. ,
Perl, open,
, , ,
, POSIX::open CO
RE::open.
, C.
, ,
Perl, .
, setjmp "setjmp()
is Cspecific: use eval {} instead" ( eval), tmpfile
"Use method io::File::new_tmpfile()". ( Perl 5.6.1 Fi
le::Temp.)
POSIX
( POSIX),
C.
, ,
. ,
C. ,
,
POSIX
:
972
32.
#!/usr/bin/perl w
use strict;
$| = 1;
for (1..4) {
my $got;
print "gimme: ";
$got = getone();
print " > $got\n";
}
exit;
BEGIN {
use POSIX qw(:termios_h);
my ($term, $oterm, $echo, $noecho, $fd_stdin);
$fd_stdin = fileno(STDIN);
$term
= POSIX::Termios>new();
$term>getattr($fd_stdin);
$oterm
= $term>getlflag();
$echo
= ECHO | ECHOK | ICANON;
$noecho = $oterm & ~$echo;
sub cbreak {
$term>setlflag($noecho);
$term>setcc(VTIME, 1);
$term>setattr($fd_stdin, TCSANOW);
}
$term>setlflag($oterm);
$term>setcc(VTIME, 0);
$term>setattr($fd_stdin, TCSANOW);
}
sub getone {
my $key = "";
cbreak();
sysread(STDIN, $key, 1);
cooked();
return $key;
}
}
END { cooked() }
POSIX
. ,
, ":sys_wait_h", ":sys_stat_h" ":termi
os_h". POSIX 16.
Safe
use Safe;
$sandbox = Safe>new();
$sandbox = Safe>new("PackName");
#
#
973
Socket
# .
$sandbox>permit(qw(:base_core));
$sandbox>permit_only(qw(:base_core :base_loop :base_mem));
$sandbox>deny("die");
# do(),
$ok = $sandbox>rdo($filename);
# eval(),
$ok = $sandbox>reval($code);
$ok = $sandbox>reval($code, 1);
# 'use strict'
# 'use strict'
Safe
.
. , FTP
chroot(2) ,
Safe>new("PackName")
.
(main::) ,
PackName::. ,
Frobnitz:: ,
PackName::Frobnitz::. ,
.
, Safe,
, , eval.
(
Perl), Safe.
Safe: rdo ( do) reval
( eval) . do eval
,
.
, ,
use strict.
(
),
Safe
. . 23.
Socket
use Socket;
$proto = getprotobyname('udp');
socket(SOCK, PF_INET, SOCK_DGRAM, $proto)
or die "socket: $!";
$iaddr = gethostbyname('hishost.com');
$port = getservbyname('time', 'udp');
$sin = sockaddr_in($port, $iaddr);
974
32.
send(SOCK, 0, 0, $sin)
or die "send: $!";
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
$port = getservbyname('smtp', 'tcp');
$sin = sockaddr_in($port,inet_aton("127.1"));
$sin = sockaddr_in(7,inet_aton("localhost"));
$sin = sockaddr_in(7,INADDR_LOOPBACK);
connect(SOCK,$sin)
or die "connect: $!";
($port, $iaddr) = sockaddr_in(getpeername(SOCK));
$peer_host = gethostbyaddr($iaddr, AF_INET);
$peer_addr = inet_ntoa($iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_UNIX, SOCK_STREAM, $proto)
or die "connect: $!";
unlink('/tmp/usock'); # XXX:
$sun = sockaddr_un('/tmp/usock');
connect(SOCK,$sun)
or die "connect: $!";
use Socket qw(:DEFAULT :crlf);
# CR(), LF() CRLF()
# $CR, $LF $CRLF.
Socket
C sys/socket.h, Perl
. ,
inet_aton inet_ntoa, IP ASCII (
127.0.0.1) ,
, sockaddr_in sockaddr_un,
, .
:crlf
, \r \n
. CRLF,
LF. io::Socket
TCP. . 16.
Symbol
use Symbol "delete_package";
delete_package("Foo::Bar");
print "deleted\n" unless exists $Foo::{"Bar::"};
use Symbol "gensym";
$sym1 = getsym();
$sym2 = getsym();
# typeglob.
# typeglob.
975
Sys::Hostname
package Demo;
use Symbol "qualify";
$sym = qualify("x");
$sym = qualify("x", "Foo");
$sym = qualify("Bar::x");
$sym = qualify("Bar::x", "Foo");
#
#
#
#
"Demo::x"
"Foo::x"
"Bar::x"
"Bar::x"
Symbol ,
: typeglob, , ,
,
. delete_package
(
,
). gensym
typeglob. ( ,
open, pi
pe, socket ).
qualify ,
, .
, (
, ). qualify_to_ref
, typeglob,
. ,
, .
, , typeglob,
,
. qualify_to_ref,
strict refs.
, .
Sys::Hostname
use Sys::Hostname;
$hostname = hostname();
Sys::Hostname hostname,
, ,
, . ,
gethostname(2),
976
32.
.1 ,
hostname(1). ,
uname(3) C,
Perl POSIX::uname. ,
. , , Perl
.
;
, Net::Domain CPAN.
, hostname ,
, ,
,
, .
, , ifconfig(8)
, .
Sys::Syslog
use Sys::Syslog;
# setlogsock.
use Sys::Syslog qw(:DEFAULT setlogsock); # setlogsock.
openlog($program, 'cons,pid', 'user');
syslog('info', 'this is another test');
syslog('mail|warning', 'this is a better test: %d', time());
closelog();
syslog('debug', 'this is the last test');
setlogsock('unix');
openlog("$program $$", 'ndelay', 'user');
syslog('info', 'problem was %m'); # %m == $! syslog
syslog('notice', 'fooprogram: this is really done');
setlogsock("unix");
# "inet" "unix"
openlog("myprogname", $logopt, $facility);
syslog($priority, $format, @args);
$oldmask = setlogmask($mask_priority);
closelog();
Sys::Syslog syslog(3) C,
syslogd(8).
,
, ,
( ) .
:
Sys::Host
name::ghname, .
Term::Cap
977
Term::Cap
use Term::Cap;
$ospeed = eval {
require POSIX;
my $termios = POSIX::Termios>new();
$termios>getattr;
$termios>getospeed;
} || 9600;
$terminal = Term::Cap>Tgetent({ TERM => undef, OSPEED => $ospeed });
$terminal>Tputs('cl', 1, STDOUT);
# .
$terminal>Tgoto('cm', $col, $row, STDOUT); # .
Term::Cap
termcap(3). . .
, terminfo(5) termcap(5). ( terminfo
978
32.
Text::Wrap
use Text::Wrap; # wrap().
@lines = (<<"EO_G&S" =~ /\S.*\S/g);
This particularly rapid,
unintelligible
patter isn't generally
heard,
and if
it is, it
doesn't matter.
EO_G&S
$Text::Wrap::columns = 50;
print wrap(" " x 8, " " x 3, @lines), "\n";
:
This particularly rapid, unintelligible
patter isn't generally heard if it is, it
doesn't matter.
Text::Wrap .
wrap ,
. .
, .
,
.
, , , , $Text::Wrap::co
lumns. ioctl
TioCGWINSZ, , C,
CPAN Term::ReadKey GetTer
minalSize.
Time::Local
use Time::Local;
$time = timelocal($sec,$min,$hours,$mday,$mon,$year);
$time = timegm($sec,$min,$hours,$mday,$mon,$year);
Time::localtime
979
:
Scalar localtime gives: Thu Jan 18 03:45:50 1973
Twentyeight years of seconds later, it's now
Wed Jan 17 22:43:26 2001
Time::localtime
use Time::localtime;
printf "Year is %d\n", localtime>year() + 1900;
$now = ctime();
use Time::localtime;
use File::stat;
$date_string = ctime(stat($file)>mtime);
localtime, ,
Time::tm ( undef ). Time::gmtime
, gmtime.
struct tm
C time.h, sec, min, hour, mday, mon, year, wday, yday
isdst. ctime ( )
CORE::localtime. ,
struct tm ,
. ,
, . . POSIX::strftime
.
980
32.
User::grent
use User::grent;
$gr = getgrgid(0)
or die "No group zero";
if ($gr>name eq "wheel" && @{$gr>members} > 1) {
print "gid zero name wheel, with other members";
}
$gr = getgr($whoever); # .
use User::grent ':FIELDS';
getgrgid(0)
or die "No group zero";
if ($gr_name eq "wheel" && @gr_members > 1) {
print "gid zero name wheel, with other members";
}
getgrent, get
gruid getgrnam, , User::grent
( undef ).
struct group C grp.h,
name, passwd, gid members ( mem, C!). ,
.
":FIELDS",
. ( )
"gr_". getgr ,
getgrgid, getgrnam.
User::pwent
use User::pwent;
# .
$pw = getpwnam("daemon") or die "No daemon user";
if ($pw>uid == 1 && $pw>dir =~ m#^/(bin|tmp)?$# ) {
print "gid 1 on root dir";
}
$pw = getpw($whoever);
# .
$real_shell = $pw>shell || '/bin/sh';
for (($fullname, $office, $workphone, $homephone) =
split /\s*,\s*/, $pw>gecos)
{
s/&/ucfirst(lc($pw>name))/ge;
}
use User::pwent qw(:FIELDS);
#
.
getpwnam("daemon")
or die "No daemon user";
if ($pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
print "gid 1 on root dir";
}
User::pwent
981
getpwent, get
pwuid getpwnam, , User::pwent
( undef ). ,
,
.
C passwd pwd.h, "pw_",
name, passwd, uid, gid, change, age, quota, comment, class, gecos, dir, shell ex
pire. passwd, gecos shell .
":FIELDS",
.
, "pw_" . getpw
,
getpwuid, getpwnam.
Perl , ,
change, age quota, comment class.
expire, gecos , , passwd.
, ,
undef. . passwd(5) getpwent(3).
, ,
pw_has. ,
,
, ,
, ,
. ,
, C .
gecos ,
, ,
, . & gecos
. shell, , /bin/
sh, Perl . passwd
, ,
, , .
, DES. ,
, Perl
, ,
C , ,
.
33
(
):
(W)
Warning ()
(D)
Deprecation ()
(S)
Severe warning ()
(F)
Fatal error ()
(P)
Panic , (
)
(X)
( )
(A)
Alien ( Perl)
(W, D S)
warnings w W.
warnings,
; (W misc)
. warnings 31
.
, ,
$SIG{__WARN__} ,
. ,
983
, $SIG{__DIE__}
, die ,
. ,
.
eval.
,
, warnings X.
%s ,
. ( %d
printf,
%d .) ,
%s,
. ,
, , .
"%?@ , [ \ .
, Perl, ,
perlbug, Perl.
"%s" variable %s masks earlier declaration in same %s
(W misc) my our
,
. . ,
.
"my sub" not yet implemented
(F)
. .
"my" variable %s can't be in a package
(F)
,
. local,
.
"no" not allowed in expression
(F) no ,
.
"our" variable %s redeclared
(W misc) ,
.
"use" not allowed in expression
(F) use
.
984
33.
985
, :
@foo[$bar, $baz, $xyzzy]
@{$ref>[12]}{"susie", "queue"}
986
33.
987
988
33.
\1 better written as $1
(W syntax)
. ,
,
Perl, ,
.
accept() on closed socket %s
(W closed) accept .
, socket?
Allocation too large: %lx
(X) 64 K MSDOS.
Applying %s to %s will act on scalar(%s)
(W misc) (//), (s///)
(tr///) .
,
,
, , , .
Arg too short for msgsnd
(F) msgsnd sizeof(long).
Ambiguous use of %s resolved as %s
(W ambiguous | S) ,
, .
, , .
Ambiguous call resolved as CORE:: %s (), qualify as such or use &
(W ambiguous) ,
Perl,
. Perl ,
.
.
(
use subs).
Perl
, CORE:: (, CORE::log($x))
.
Args must match #! line
(F) setuid , ,
Perl, #!.
#! ,
, , w U wU.
989
990
33.
991
, :
$var = 'myvar';
$sym = "mypack::$var";
, ,
:
use constant TYPO => 1;
if (TYOP) { print "foo" }
, strict.
992
33.
993
,
, .
Can't "next" outside a loop block
(F) next ,
. , if else
, sort, map
grep. ,
, .
Can't read CRTL environ
(S) VMS . Perl
%ENV CRTL ,
. , CRTL
, PERL_ENV_TABLES (. perlvms(1)) ,
.
Can't "redo" outside a loop block
(F) redo ,
. , if el
se , sort, map
grep. ,
,
.
Can't bless nonreference value
(F) . Perl
.
Can't break at that line
(S internal)
,
, .
Can't call method "%s" in empty package "%s"
(F) , ,
, ,
.
Can't call method "%s" on unblessed reference
(F) ,
. ,
.
, .
Can't call method "%s" without a package or object reference
(F) ,
,
, ,
. :
994
33.
$BADREF = 42;
process $BADREF 1,2,3;
$BADREF>process(1,2,3);
Can't chdir to %s
() perl x/foo/bar, /foo/bar ,
chdir, , , .
Can't check filesystem of script "%s" for nosuid
(P) nosuid
.
Can't coerce %s to integer in %s
(F) SV, (typeglob),
.
:
*foo += 1;
:
$foo = *foo;
$foo += 1;
$foo glob.
Can't coerce %s to number in %s
(F) SV, (typeglob),
.
Can't coerce %s to string in %s
(F) SV, (typeglob),
.
Can't coerce array into hash
(F) , ,
, .
, 0 .
Can't create pipe mailbox
(P) VMS . ,
, .
995
996
33.
, ,
.
.
Can't fork
(F) fork.
997
998
33.
999
1000
33.
, ,
. ,
sort { &func } @x, sort func @x.
Can't remove %s: %s, skipping file
(S inplace)
. Perl ,
. .
Can't rename %s to %s: %s, skipping file
(S inplace) i
, , .
Can't reopen input pipe (name: %s) in binary mode
(P) , VMS. Perl , STDIN ,
. ,
.
Can't reswap uid and euid
(P) setreuid
setuid suidperl.
Can't return outside a subroutine
(F) return , . .
, .
Can't return %s from lvalue subroutine
(F) Perl (
, ) ,
. .
Can't stat script "%s"
(P) fstat ,
. .
Can't swap uid and euid
(P) setreuid
setuid suidperl.
Can't take log of %g
(F)
.
, Perl
Math::Complex.
Can't take sqrt of %g
(F)
.
, Perl
Math::Complex.
1001
1002
33.
.
Perl ,
SCALAR(0xdecaf). $1.
Can't use bareword ("%s") as %s ref while "strict refs" in use
(F) strict
refs. .
Can't use string ("%s") as %s ref while "strict refs" in use
(F) strict
refs. .
Can't use an undefined value as %s reference
(F) ,
, .
.
Can't use global %s in "my"
(F)
. ,
( )
,
, .
Can't use subscript on %s
(F)
. ,
,
.
Can't weaken a nonreference
(F) , .
.
Can't x= to readonly value
(F) ( )
, .
,
.
Can't find an opnumber for "%s"
(F) CORE:: word prototype,
word .
Can't resolve method `%s' overloading `%s' in package `%s'
(F|P) ,
( ):
. ???,
.
1003
, 777 ,
01411. Perl
0, C.
Close on unopened file <%s>
(W unopened) , .
Compilation failed in require
(F) Perl , require. Perl
,
, .
Complex regular subexpression recursion limit (%d) exceeded
(W regexp)
, .
32 766 ,
. (
, .)
, Perl (
, while), ,
,
.
1004
33.
1005
defined(%hash) is deprecated
(D deprecated) defined ,
.
, , if (%hash) { # }.
Delimiter for here document is too long
(F) <<FOO FOO
Perl. , ,
.
Did not produce a valid header
. Server error.
(Did you mean &%s instead?)
(W) , &FOO $FOO
.
(Did you mean "local" instead of "our"?)
(W misc) , our
.
, .
(Did you mean $ or @ instead of %?)
(W) , %hash{$key}, $hash{$key}
@hash{@keys}. , , ,
%hash .
Died
(F) die ( die "")
, $@ $_ .
(Do you need to predeclare %s?)
(S) , %s fo
und where operator expected.
, .
sub, package, require
use. , ,
.
sub foo; package FOO;.
Document contains no data
. Server error.
Don't know how to handle magic of type '%s'
(P) .
do_study: out of memory
(P) safemalloc.
Duplicate free() ignored
(S malloc) free , .
1006
33.
1007
1008
33.
:
if ($foo == 123)
( ).
1009
1010
33.
2**321
1011
1012
33.
Insecure directory in %s
(F) system, exec setu
id setgid, $ENV{PATH} ,
.
Insecure $ENV{%s} while running %s
(F) system, exec setu
id setgid, $ENV{PATH}, $ENV{IFS}, $ENV{CDPATH}, $ENV{ENV}
$ENV{BASH_ENV} ( ) ,
.
, .
Integer overflow in %s number
(W overflow) , ,
hex oct,
. 32
, ,
, 0xFFFFFFFF, 037777777777
0b11111111111111111111111111111111 .
, Perl
,
.
Internal inconsistency in tracking vforks
(S) VMS . Perl ,
fork exec, ,
exec (. exec
LIST perlvms(1)). , Perl
exec
Perl .
internal disaster in regexp
(P)
.
internal urp in regexp at /%s/
(P) .
Invalid %s attribute: %s
(F) Perl
.
Invalid %s attributes: %s
(F) Perl
.
invalid [] range "%s" in regexp
(F) , ,
.
1013
1014
33.
Malformed PERLLIB_PREFIX
(F) OS/2. PERLLIB_PREFIX :
prefix1; prefix2
:
prefix1 prefix2
1015
substr, .
Modification of noncreatable array value attempted, subscript %d
(F) , ,
, , .
Modification of noncreatable hash value attempted, subscript "%s"
(P) ,
.
Module name must be constant
(F) use
.
msg%s not implemented
(F) IPC System V.
Multidimensional syntax %s not supported
(W syntax) $foo[1,2,3] .
$foo[1][2][3], C.
Missing name in "my sub"
(F)
, ,
.
Name "%s:: %s" used only once: possible typo
(W once) ()
.
, ,
. our.
Negative length
(F) read/write/send/recv
0. .
1016
33.
1017
1018
33.
1019
1020
33.
1021
Out of memory!
(X) Perl malloc 0, ,
( )
. Perl , .
Out of memory for yacc stack
(F) yacc ,
, realloc , .
Out of memory during request for %s
(X|F) malloc 0, ,
( ) .
,
Perl. .
Perl $^M
die .
.
Out of memory during "large" request for %s
(F) Perl malloc 0, ,
( )
. (
64 K),
.
Out of memory during ridiculously large request
() 2**31+ .
, , Perl (
, $arr[time] $arr[$time]).
page overflow
(W io) write ,
.
panic: ck_grep
(P)
grep.
panic: ck_split
(P)
split.
panic: corrupt saved stack index
(P)
, .
panic: del_backref
(P)
.
1022
33.
panic: die %s
(P) eval ,
eval.
panic: do_match
(P) pp_match
.
panic: do_split
(P) split.
panic: do_subst
(P) pp_subst
.
panic: do_trans
(P) do_trans
.
panic: frexp
(P) frexp,
printf("%f").
panic: goto
(P)
, , goto.
panic: INTERPCASEMOD
(P) .
panic: INTERPCONCAT
(P)
.
panic: kid popen errno read
(F)
.
panic: last
(P) ,
.
panic: leave_scope clearsv
(P)
.
panic: leave_scope inconsistency
(P) , , .
enum.
panic: malloc
(P) malloc .
1023
panic: magic_killbackrefs
(P)
.
panic: mapstart
(P) map.
panic: null array
(P)
AV.
panic: pad_alloc
(P) ,
.
panic: pad_free curpad
(P) ,
.
panic: pad_free po
(P) .
panic: pad_reset curpad
(P) ,
.
panic: pad_sv po
(P) .
panic: pad_swipe curpad
(P) ,
.
panic: pad_swipe po
(P) .
panic: pp_iter
(P) foreach .
panic: realloc
(P) realloc.
panic: restartop
(P) goto ( ),
.
panic: return
(P) eval
, eval.
1024
33.
panic: scan_num
(P) Perl scan_num ,
.
panic: sv_insert
(P) sv_insert ,
.
panic: top_env
(P) goto .
panic: yylex
(P)
.
panic: %s
(P) .
Parentheses missing around "%s" list
(W parenthesis) :
my $foo, $bar = @_;
when you meant:
my ($foo, $bar) = @_;
1025
:
@list = qw(
a
b
);
,
, :
@list = (
'a',
'b',
);
#
#
. ,
:
qw( a b c );
1026
33.
sub doit
{
use attrs qw(locked);
}
:
sub doit : locked
{
...
use attrs
.
Precedence problem: open %s should be open(%s)
(S precedence) :
open FOO || die;
:
open(FOO || die);
Perl 5
. ( open .)
or ||.
Premature end of script headers
. Server error.
print() on closed filehandle %s
(W closed) , ,
. .
printf() on closed filehandle %s
(W closed) , ,
. .
Process terminated by SIG%s
(W) OS/2,
Unix .
OS/2.
. . Process terminated by SIGTERM/SIGINT
README.os2 Perl.
Prototype mismatch: %s vs %s
(S unsafe)
.
Range iterator outside integer range
(F) ..
,
1027
.
, 0 .
readline() on closed filehandle %s
(W closed) , ,
. .
realloc() of freed memory ignored
(S malloc) realloc ,
.
Reallocation too large: %lx
(F) 64 K MSDOS.
Recompile perl with DDEBUGGING to use D switch
(F debugging) D,
Perl,
, .
Recursive inheritance detected in package '%s'
(F) 100 .
.
Recursive inheritance detected while looking for method '%s' in package '%s'
(F) 100
.
.
Reference found where evensized list expected
(W misc) , Perl
( ).
, ,
.
/:
%hash
%hash
%hash
%hash
=
=
=
=
#
#
#
#
1028
33.
1029
1030
33.
1031
1032
33.
. .
, abc, xyz,
/abc(?=(?:xyz){3})/, /abc(?=xyz){3}/.
Stub found while resolving method `%s' overloading `%s' in package `%s'
(P) @ISA
.
, can .
Subroutine %s redefined
(W redefine) .
:
{
no warnings;
eval "sub name { ... }";
}
Substitution loop
(P) . (,
, ,
.)
Substitution pattern not terminated
(F)
s/// s{}{}. ,
. ,
$ $s.
Substitution replacement not terminated
(F)
s/// s{}{}. ,
. ,
$ $s.
substr outside of string
(W substr|F) substr,
. ,
, . ,
substr (
, ).
suidperl is no longer needed since %s
(F) Perl DSETUID_SCRIPTS_ARE_SECURE_NOW,
setuid .
switching effective %s is not implemented
(F) use filetest
UID GID.
1033
syntax error
(F) .
:
.
.
. ( w.)
, Perl
.
, Perl .
,
, ,
perl c,
, , . ,
.
syntax error at line %d: `%s' unexpected
(A) , Perl.
#! Perl.
System V %s is not implemented on this machine
(F) ,
sem, shm msg, System V IPC . (
,
.)
syswrite() on closed filehandle %s
(W closed) , ,
. .
Target of goto is too deeply nested
(F) goto ,
Perl. Perl .
tell() on unopened file
(W unopened) tell ,
.
Test on unopened file %s
(W unopened)
, . .
1034
33.
.
The %s function is unimplemented
Configure.
The crypt() function is unimplemented due to excessive paranoia
(F) Configure crypt, , ,
, , , ,
, ,
, .
The stat preceding l _ wasn't an lstat
(F) , stat
, stat, ,
, .
.
This Perl can't reset CRTL environ elements (%s)
This Perl can't set CRTL environ elements (%s=%s)
(W internal) VMS.
CRTL,
Perl CRTL,
setenv. Perl CRTL,
, PERL_ENV_TABLES (. perlvms(1)),
%ENV,
.
times not implemented
(F) C, , times.
, Unix.
Too few args to syscall
(F) syscall ,
.
1035
1036
33.
1037
1038
33.
unmatched [] in regexp
(F) .
,
.
Unquoted string "%s" may clash with future reserved word
(W reserved) ,
.
,
. , , .
Unrecognized character %s
(F) Perl ,
, Perl ( eval). ,
,
Perl.
Unrecognized escape \\%c passed through
(W misc) Perl
.
Unrecognized signal name "%s"
(F) kill , .
kill l , .
Unrecognized switch: %s (h will show valid options)
(F) Perl. . ( ,
, #!,
.)
Unsuccessful %s on filename containing newline
(W newline) ,
,
, , chop
chomp.
Unsupported directory function "%s" called
(F) opendir readdir.
Unsupported function fork
(F)
.
, , OS/2,
Perl,
fork, . ,
Perl, perl_, perl__ . .
Unsupported function %s
(F) , .
, Configure.
1039
1040
33.
1041
$one, $two = 1, 2;
:
($one, $two) = (1, 2);
,
. , :
$array = (1,2);
:
$array = [1,2];
,
. ,
, C,
, , .
untie attempted while %d inner references still exist
(W untie) , tie ( tied),
, untie.
Value of %s can be "0"; test with defined()
(W misc) <HANDLE>, <*> (glob), each
readdir .
"0",
, , .
defined.
Value of CLI symbol "%s" too long
(W misc) VMS . Perl
%ENV CLI ,
1024 .
1024 .
Variable "%s" is not imported%s
(F) use strict
, , , ,
( )
. ,
.
Variable "%s" may be unavailable
(W closure) ()
,
; ( )
1042
33.
,
, :
sub outermost { my $a; sub middle { sub { $a } } }
(
) ,
, .
,
,
,
, , , .
, ,
sub {}. Perl
;
.
Variable "%s" will not stay shared
(W closure) ()
, .
, ,
,
; ,
,
.
, .
,
, *
.
,
sub {}.
,
, ,
.
Variable syntax
(A) csh Perl. #!
Perl.
Version number must be a constant number
(P) use Module n.n LIST
BEGIN
.
perl: warning: Setting locale failed.
(S) ( .)
:
1043
( .)
, Perl
(variable system), Perl
. , :
, C,
Perl , .
,
Perl.
, perllocale(1) Locale
Problems.
Warning: something's wrong
(W) warn ( warn "")
, $_ .
Warning: unable to close filehandle %s properly
(S) close, open,
close. , .
Warning: Use of "%s" without parentheses is ambiguous
(S ambiguous) ,
,
. , ,
rand 1.0 :
rand + 5;
,
rand() + 5;
:
rand(+5);
, .
write() on closed filehandle %s
(W closed) , ,
. .
X outside of string
(F) pack
.
1044
33.
x outside of string
(F) pack
.
Xsub "%s" called in sort
(F)
.
Xsub called in sort
(F)
.
You can't use l on a filehandle
(F) ,
, , ,
, . .
YOU HAVEN'T DISABLED SCRIPTS IN THE KERNEL YET!
(F) ( SETID !) , ,
, , , ,
, , , .
C setuid
wrapsuid eg Perl.
You need to quote "%s"
(W syntax) .
, ,
Perl 5
, , , . ( ,
, & .)
, ,
.
.
ARGV
, *
.
<>, ARGV
,
, ,
.
ASCII
American Standard Code for Information
Interchange
(
7 ,
).
128
ISO8859X
8 ,
ASCII. . Unicode.
AV
array value
,
Perl,
. AV
SV.
awk
,
, awkward ().
,
Perl
.
BLOCK
,
Perl, .
if while, ,
.
, . .
, ,
, eval ,
.
BSD
,
80 ,
, ,
.
System V,
. (,
, .)
Berkeley Standard Distri
bution.
C
,
*
, *
. ( ,
C ,
,
,
.) Perl C,
1046
,
.
) dweomer.
CODE
, ref
. . CV.
escape) escape
sequence
. .
CPAN
Comprehensive Perl Archive Network
Perl ( .
22 CPAN).
CV
, .
CV SV.
DBM
Data Base Ma
nagement
, *
.
. DBM
Perl
.
DBM . AnyDBM_File(3)
DB_File 32
.
dweomer
, , ,
. , Perl
,
dwimmer, ,
,
. [
]
dwimmer
DWIM Do What
I Mean , ,
,
, ,
. ,
.
, (
exec
,
(
).
FIFO
First In, First Out ,
. . LIFO.
, *
.
fileglob
(wildcard). .
glob.
FMTEYEWTK
Far More Than Everything You Ever
Wanted To Know ,
,
.
,
FAQ.
.
GID
ID Unix ID ,
.
glob
, * ,
,
. ,
. . fileglob typeglob.
grep
Unix Globally search for a Regular
Expression and Print it (
).
1047
,
. Perl
grep, ,
, grep(1)
,
.
LIFO
Last In, First Out ,
. . FIFO. LIFO
.
GV
glob, typeglob. GV
SV.
Makefile
,
. Perl
Makefile, . . Perl
.
HV
hash
value,
Perl. HV
SV.
lvaluable
.
man
Unix,
(
).
I/O
*
.
monger
Perl Monger,
Perl.
IO
/.
*
.
NFS
Network File System
,
,
.
IP
Internet Protocol, Intellectual Pro
perty ( ).
IPC
(Inter
process Communication).
IV
, six,
. IV
,
, NV.
JAPH
Just Another Perl Hacker (
Perl). ,
Perl,
.
Perl,
Perl Usenix.
NV
,
. NV
Numeric Value (
)
,
;
IV.
pad
scratchpad
.
PATH
,
, *
.
,
Perl $ENV{PATH}.
pod
,
Perl. . 26.
1048
POSIX
(Portable Ope
rating System Interface).
pp
push
pop, . . C,
Perl.
pumpking
.
PV
pointer value,
Perl char*.
regex
. .
RFC
Request For Comment (
), ,
,
.
root
(UID == 0).
,
.
RTFM
, ,
Read The Fine
Manual.
RV
RV
(Reference Value) ,
.
, . IV NV.
script kiddie
, ,
, .
(cargocult)
.
sed
Stream Editor,
Perl .
setgid
, setuid,
.
setuid
,
, ,
( ).
(
),
.
,
,
, ,
.
shebang
Perl ,
sharp () bang (
),
#!,
, .
slurp
.
STDERR
. .
STDIN
. .
STDIO
. *.
STDOUT
. .
struct
,
.
SV
scalar value
.
Perl
,
SV,
. Perl
C SV*.
struct SV
,
(),
.
1049
TCP
Transmission Control
Protocol
. ,
Internet Protocol,
. (.)
TMTOWTDI
(There's More Than One Way To Do It)1,
Perl. ,
. ( ,
,
.)
UID
ID .
*
.
umask
,
,
,
. .
umask.
Unicode
,
. . http://www.unicode.org.
typemap
, C
Perl
, XS.
Unix
,
, . ,
, ,
,
,
,
.
,
Unix
.
,
Perl.
Unix
,
Bell Labs ,
PDP7,
.
UDP
User Datagram Protocol,
.
v) v)string
,
v
,
troff
,
Perl $%
Camel.
typedef
C.
typeglob
*. , *name
$name, @name, %name,
&name name.
,
. .
2.
. . .
1050
v1.20.300.4000.
(
, v ).
WYSIWYG
What You See Is What You Get
, .
, ,
, ,
,
format Perl.
, ,
, ,
open .
XS
(eXternal Sub
routine), C C++.
. 21
.
XSUB
,
XS.
yacc
Yet Another Compiler Compiler
.
,
Perl , ,
. . perly.y
Perl.
autoload
.
. (
(lazy)
.) ,
AUTOLOAD ,
.
autoincrement
, ++
.
,
.
autogeneration
,
.
,
, .
. 13 .
autosplit
, (switch) a
p n
awk. (. AutoSplit,
a,
.)
algorithm
,
,
.
alternatives
,
,
:
A, B C?
: |.
Perl
:
||.
|| or.
anonymous
,
*
.
.
.
argument
, *
, , *
1051
,
.
.
command)line arguments
,
,
,
.
Perl @ARGV.
arithmetical operator
, + /,
Perl
,
.
architecture
,
, ,
.
Perl ()
,
, Perl
,
,
, C,
. .
.
asynchronous
,
, . .
.
,
,
.
associativity
,
, A
B C
.
+ ,
**
. . 3
.
associative
array
. .
atom
,
*
,
,
. (
,
, *
.)
atomic operation
,
,
: a () + tomos ().
, , ,
.
attribute
,
, sub foo :
locked method. ,
*
.
base class
;
,
,
. ,
,
.
byte
.
) bytecode
,
,
(. ).
, ( )
1052
XX .
,
,
.
freely available
, ,
(, ).
freely
redistributable
,
,
,
. ,
.
freeware
, ,
.
*
(open source soft*
ware).
freeware
open source software,
,
(Free Software Foundation
General Public License GPL),
.
library
.
.pl.
Perl .
binary operator
, .
binary
. .
execute bit
,
,
.
Unix ,
,
, ,
.
bit
0 1
.
.
. (
piece
of eight, . .
,
,
. 25
two
bits.)
bit string
( .)
,
.
permission bits
,
,
.
,
stat
. Unix
ls(1).
, block
, ,
:
.
,
,
, *
( ,
512 8192).
,
.
, bucket
*, (
) ,
. ( ,
,
.)
block buffering
/
. Perl
. . .
Boolean context
,
(
) ,
.
. . .
Boolean
,
(true) (false).
buffer
. ,
,
.
, ,
. *
,
print
( ).
,
,
.
.
command
buffering
,
Perl,
c
.
$| ($AUTOF
LUSH) .
, ,
,
, ,
. . *
.
1053
line buffering
*
/,
.
/
,
.
variadic
,
.
vector
.
fork
,
,
,
. (thread)
.
cracker
,
.
script kiddie.
virtual
, ,
, ,
.
(. .)
(transparent),
, ,
Perl
UTF8.
dangling statement
,
,
if while. C
. Perl .
owner
(
),
1054
.
,
,
. . .
return value
, *
.
Perl
.
, magic
,
,
, $!, $0, %ENV %SIG,
.
,
(
) .
octal
8.
0 7.
Perl 0,
013. . oct.
scratchpad
,
,
.
embedding
,
:
Perl
!
built)in
,
.
,
,
CORE::.
here documents
, ,
, *
, ,
,
. Perl
.
call by value
,
*
,
,
. . *
.
call by reference
,
,
,
.
,
. . .
invocation
,
, , , ,
,
, ,
, .
call
, invoke ,
.
) invocant
,
.
() .
.
execute
*
. (
kill,
.)
expression
, ,
.
, , *
, ,
.
1055
hubris
, ,
. ,
( )
,
.
.
. .
.
code generator
,
, ,
,
. . .
core dump
,
,
.
program
generator
,
. . .
datagram
,
UDP, (
)
. (
IP, ,
TCP,
.)
global
destruction
(
),
*
Perl.
, ,
.
global
, ;
,
. Perl
( )
.
our. .
4
.
() bareword
,
, use
strict 'subs'.
,
group
,
.
(, Unix)
.
binary
2.
, , ,
, 0 1.
,
, ,
. Unicode
, ,
.
decrement
,
, $x (
1 )
$x 3.
parse tree
. .
file descriptor
, *
, ,
. Perl
*,
1056
.
descriptor
. .
destructor
, ,
.
DESTROY
; Perl
,
, .
dynamic scoping
,
, *
,
.
(
)
local. (
.)
, ,
.
directive
pod. . 26
pod.
distribution
,
.
. ,
(bi
naryonly).
accessor methods
( ).
compilation unit
( eval),
.
greedy
,
.
hard reference
,
,
. (
, ,
typeglob
.)
.
lookahead
,
.
lookbehind
,
.
header file
, ,
,
.
C .h.
Perl
,
.h .ph. . require
29 . (
.)
overriding
.
( ,
,
.)
,
: ,
(.
11 ),
,
, *
(. 12).
cloister
,
*
.
closure
, ,
,
*
,
.
,
.
record
,
.
Unix
,
().
/etc/passwd
,
,
.
reserved
words
, if de
lete. ( Perl)
.
( ,
.) Perl
*
.
.
capturing
*
*
(backreference). (
1057
.)
granularity
, ,
.
default
, ,
.
value
,
, , , ,
,
.
zombie
, (),
wait waitpid.
fork
,
.
identifier
,
,
. (
Perl) ,
. Perl
. ( Perl
,
.)
named pipe
, *
,
.
import
,
. . use
29.
command name
,
,
1058
. C
. Perl
$0.
,
.
filename
open,
,
, *
,
, .
interpreter
, ,
,
,
,
,
. Perl
,
,
( *
) perl,
Perl.
subscript
,
.
indexing
(
, ),
,
() .
, index
Perl ()
.
encapsulation
,
(
), ,
.
increment
1
( , ).
variable
interpolation
.
interpolation
,
, .
Perl
,
interface
,
, *
, ,
.
infix
,
,
: 24 * 7.
exception
,
. . .
executable file
, ,
,
.
true
,
0 "".
home directory
,
.
Unix
$ENV{HOME} $ENV{LOGDIR}
,
(getpwuid($<))[7]. (
.)
1059
iterator
,
,
, ,
. foreach Perl
, ,
each.
iteration
.
pipe
,
.
,
, ,
.
canonical
.
directory
,
.
, . .
qualified
.
$Ent::moot ; $moot
.
.
quantifier
,
.
character class
,
*
,
.
,
.
class
,
Perl ,
(
) *
(. . ) *
( ).
. .
switch cluster
(, a b c)
(. . abc).
.
cluster
*
,
.
client
,
, ,
, .
key
,
,
.
switch
,
,
.
*
.
keyword
. .
loop
control statement
,
. ,
.
command
.
,
1060
(
), .
Perl,
.
concatenation
,
,
.
statement
, ,
:
, .
,
, .
construct
*
.
comment
,
. Perl
#
.
compiler
, ,
,
,
. perl,
,
,
,
( *
) perl,
*
.
, Perl
. . 18
.
pipeline
, ,
.
EOF
.
.
endian
. .
constructor
, *
, ,
,
.
.
construct
,
.
array context
,
,
.
context
. ,
; ,
*
, ,
.
: , *
.
, *
,
.
( , ,
2 ).
breakpoint
,
,
,
.
watch
expression
,
Perl.
1061
indirection
, ,
, .
*
.
indirect object
, ,
(
). Perl print STDOUT "$foo\n";
, STDOUT
print, "$foo"
.
:
$gollum = new Pathetic::Creature "Smeagol";
give $gollum "Fisssssh!";
give $gollum "Precious!";
indirect
filehandle
,
: ( ),
typeglob, typeglob
* .
lvalue
,
,
,
, .
l left,
,
.
lvaluable ,
,
, , pos($x) = 10.
lexeme
(token).
token
,
.
lexical scoping
Oxford English Dicti*
onary . (
,
.)
,
( ),
,
,
. *
. *
.
lexical
variable
, *
,
my.
lexical. ( our
,
.)
lexical
analysis
*
(tokenizing).
lexer
tokener.
tokener
,
.
laziness
,
,
.
,
,
, ,
.
.
. . *
.
1062
literal
,
, ,
,
,
.
logical operator
,
, , .
false
Perl ,
"" "0",
.
"",
,
.
local
.
Perl
*
local.
magical
variables
,
.
, Perl
%ENV
,
.
$!
.
magical increment
,
,
.
array
*
, ,
,
.
metasymbol
, *
, ,
.
,
.
metacharacter
,
.
,
.
,
, Perl
, *
.
loop label
,
,
,
.
label
, ,
.
class method
,
,
. ,
.
method
,
,
. . 12 .
tainted
,
,
. Perl
,
1063
setuid ( setgid)
T.
minimalism
, .
,
,
,
, .
.
multidimensional array
. Perl
.
9 .
multiple inheritance
,
,
. (. *
.)
( Perl) ,
,
.
statement
modifier
,
, ,
, .
lvalue
modifier
(
) ,
.
: my, our
local.
regular expression
modifier
,
/i,
. .
cloister.
modifier
. , *
*
,
.
module
,
() ,
,
.
( .pm
.) .
use.
modulus
,
, .
soft reference
. .
en passant
. [
,
]
superclass
. .
inheritance
,
.
,
*
, *
. . *
.
impatience
, ,
.
,
, .
.
. . *
.
1064
errno
,
. Perl
$! ( $OS_ERROR,
English).
line number
,
, 1. Perl
.
__LINE__.
( ,
<FH>)
$. ($IN
PUT_LINE_NUMBER).
,
.
porter
,
. ,
,
C, ,
, Perl,
.
null string
, ;
,
,
.
zero width
,
.
null character
ASCII .
C ,
Perl ,
null.
null list
,
, Perl
().
scope
.
Perl .
*
local ,
,
,
,
. *
my ,
, ,
,
.
shell
.
,
,
,
.
,
. Unix
(/bin/sh), C (/bin/csh)
(/bin/ksh).
Perl ,
(
Perl ).
wrapper
,
,
.
exception handling
.
Perl
eval.
signal handler
, ,
,
,
,
. Perl
,
kill. . %SIG 28
16
.
handler
,
Perl,
, ,
, .
. .
backward
compatibility
,
,
, .
callback
,
,
, .
public domain
,
. Perl
.
referent
, ,
( ).
, , .
object
. , ,
()
.
, ,
.
, .
declaration
,
; ,
,
,
1065
.
, :
, ,
....
.
,
.
,
.
delimiter
,
,
(separator) *
(terminator).
(
).
single
inheritance
,
, ,
. (.
.)
,
,
,
. Perl
, , ,
Perl
, .
one)liner
,
.
environment
,
. %ENV.
operand
, ,
. .
.
relational
operator
, ,
1066
.
Perl
. . *
.
address operator
,
. Perl
. Perl
,
, ,
.
file test
operator
,
;
o $filename ,
.
operator
,
,
.
, *
()
.
operating
system
,
*
.
,
.
.
,
Unix Unix
( ,
).
,
. Perl
,
. .
.
defined
. Perl ,
,
, ,
, ,
,
, . ,
Perl
.
. false defined
29 .
options
. *
.
bless
, :
WebCruncher. , Perl
,
,
WebCruncher. .
bless 29 .
little)endian
: ,
.
,
,
. ,
. . *
.
dispatch
.
,
1067
,
,
,
,
.
circumfix
operator
,
,
,
.
flush
, *
, .
error
. *
.
package
,
, ,
.
, . .
,
,
.
.
shared
memory
,
,
.
memory
, . ,
, . .
, ,
,
.
, ,
,
.
,
, ,
, ,
.
,
,
.
parameter
. .
patch
.
,
patch(1)
.
newline
,
, ASCII
012 Unix ( 015
Mac) \n
Perl. Windows,
,
C
,
.
operator
overloading
,
,
,
,
,
.
overload . 13.
overloading
.
1068
,
.
switch statement
,
, ,
,
. case
structure,
Pascal.
Perl for.
. Case 4.
environment
variable
,
,
, ,
( ,
. .).
/
, .
instance
variable
; ,
,
.
variable
,
,
.
portable
C,
BSD SysV. , ,
,
,
.
,
. . *
.
floating point
,
,
(
). Perl
,
.
.
platform
,
. ,
, ,
,
, ,
,
. perl
,
C, ,
Perl,
.
side effects
,
.
. ,
. ( ,
!)
$| ($AUTOFLUSH)
,
write print
.
subclass
. .
subroutine
,
,
.
,
,
.
, .
substitution
s///. (
.)
substring
,
()
.
toolbox approach
,
,
,
, . ,
,
1,
,
. Perl
.
code subpattern
,
Perl, (?{...})
(??{...}).
subpattern
*
.
indirect
object slot
1069
. (
.) STDERR
:
print STDERR "Awake! Awake! Fear, Fire,
Foes! Awake!\n";
pattern matching
,
,
, .
,
.
backtracking
:
,
.
. Perl
,
. . ,
/() ?/ 5
.
field
,
.
(
split),
1070
polymorphism
,
,
.
[ ]
thread
,
fork .
, ,
. .
, ,
,
. . 17
.
nybble
,
.
rvalue
,
. .
.
bit shift
,
.
() pragma
,
(, , )
.
.
(
unpack). *
.
port
TCP UDP,
,
,
,
,
. ,
,
.
progressive
matching
,
,
.
warning
, STDERR
, ,
,
. . warn 29
use warnings 31 .
stringification
.
postfix
,
, $x++.
preprocessing
,
,
.
. .
C.
stream
. *
,
.
C C preprocessor
C, ,
#,
,
.
cpp(1).
1071
prefix
, *
, ++$x.
type casting
. C . Perl
. .
bind
*
.
precedence
, ,
,
, .
,
.
assignment
,
.
assignment
operator
,
,
,
, . .
.
$a += 2 2 $a.
whitespace
, ,
.
:
, , ,
, .
open source software
,
,
.
http://www.opensource.org/osd.html.
continuation
. Makefile
.
RFC 822
.
Perl
, *
(
) .
.
derived class
,
, .
,
:
, , , .
namespace
.
,
.
. .
protocol
,
.
prototype
*
,
Perl,
*
,
,
,
. (
, .)
procedure
.
process
.
Unix
1072
fork.
threads, tasks,
jobs (, , ),
.
pseudoliteral
,
,
`command`.
alias
,
,
.
fore
ach, $_ map
grep, $a $b
sort
@_ *
.
*
type*
glob.
our.
pseudofunction
,
,
.
,
my, ,
scalar,
q//, qq//, qx//,
qw//, qr//, m//, s///, y/// tr///.
pseudohash
,
.
, .
void context
,
,
.
, pathname
, /usr/bin/perl.
PATH.
working directory
,
.
,
chdir, ,
*
.
tokenizing
*
. lexing.
separator
,
.
split.
.
.
destroy
( DESTROY,
).
dereference
,
, .
,
.
funny
character
,
(funny cha
racter ).
,
Perl
.
extension
Perl,
C C++.
1073
,
Perl
, .
implementation
,
.
,
,
.
regular
expression
,
, .
,
, .
,
,
, .
Perl
,
.
: /Oh s.*t./.
, "Oh say can you see
by the dawn's early light" "Oh sit! ".
. 5.
regular file
, ,
, ,
.
Perl
f.
(plain) .
mode
stat(2)
,
.
recursion
(
), .
,
, ,
,
, .
parent class
. .
leftmost
longest
*
; ,
, ,
(
).
. 5.
autovivification
(autovivication,
). Perl (
)
,
,
. $a[5][5][5][5][5] =
= "quintet"
(
),
(
).
,
.
garbage collection
,
, ,
.
, Perl ,
,
.
,
. ( , ,
, ,
.)
character property
,
\p.
1074
Unicode
.
property
. *
.
bundle
CPAN. (
,
.)
glue language
, , Perl,
,
.
serialization
*
,
.
(marshalling).
network address
,
. IP.
. .
signal
; ,
,
.
right shift
,
2.
character
,
.
(
,
), UTF8
,
. Perl, ,
.
semaphore
,
.
symbol
.
,
.
server
,
,
.
symbolic link
,
,
, ,
. *
,
,
.
tie
. .
tie 29 14
.
left shift
,
2.
service
,
, ,
, (
, ).
getser
vent.
symbolic
reference
,
.
.
use
strict 'refs'.
symbolic
debugger
,
,
,
,
, , .
,
,
,
.
syntax
. ,
( ,
) .
parsing
,
,
,
.
syntax tree
,
.
syntactic sugar
; .
synchronous
,
; . .
,
.
syscall
,
.
,
,
1075
,
, .
, Perl
. ,
, Perl
, ,
$!
($ERRNO) . ,
Perl system,
.
,
,
,
Perl syscall,
,
Perl system.
scalar
; ,
.
scalar variable
$,
.
scalar value
, ,
.
scalar context
, (
) ,
,
. .
.
. *
.
,
,
,
.
scalar literal
,
.
1076
word
,
, 32
.
Perl
(
) *
, ,
.
mortal
,
,
.
offset
,
,
.
, ,
,
.
connection
.
*
.
socket
,
.
*
( ).
:
, .
matching
. .
collating sequence
,
. ,
, ,
,
.
list value
,
,
,
,
.
list context
, (
) ,
,
. ,
,
,
. .
.
list operator
,
, join
grep.
(,
print, unlink system),
*
.
LIST
,
,
.
LIST
.
list
.
slice
,
, .
backreference
,
*
.
(\1, \2 . .)
1077
.
($1, $2 . .)
,
.
reference
,
, .
(. .)
:
.
; link
,
.
.
.
() .
,
.
standard input
, , ,
,
.
Perl STDIN.
standard output
, , ,
,
.
Perl
STDOUT.
standard error
,
. Perl
STDERR.
,
die warn
.
/ standard I/O
C
. (
/
.)
Perl
/,
,
Perl
.
,
.
/
,
,
$|
.
standard
Perl ,
,
Perl.
static
scoping
. . *
.
static
variable
. *
,
.
static method
. . .
static
. ( ,
,
1078
,
.)
,
,
,
,
.
Perl .
exit status
. .
status
,
,
.
$?.
,
( ), .
Unix
, ,
wait(2). . system 29.
stack
,
,
. . LIFO.
manpage
,
man(1).
SYNOPSIS, DESCRIPTION,
BUGS . .,
. ,
,
, ,
, ,
.
Perl (
, perlop perldelta),
,
.
string
,
He said !@#*&%@#*?!.
.
string context
, (
) ,
. .
.
line
Unix
,
,
. Unix
C,
.
structure
. .
stat stat structure
Perl,
,
.
data structure
,
, ,
.
superuser
,
.
,
. Unix
root. Windows
Administrator.
script
,
,
,
.
, Unicode
,
, Klingon
(. 15).
symbol table
,
. Perl
1079
,
,
. ,
,
Perl *.
,
.
text
, ,
, .
currently selected output channel
,
select(FILE
HANDLE); STDOUT,
.
current package
,
.
*
,
.
.
current
working directory
. .
term
, . . *
. ,
.
terminator
,
. $/
,
readline, chomp
.
.
.
ternary
, *
. trinary.
empty subclass
test
,
,
.
data type
,
,
. ,
,
,
,
,
, , .
,
. ,
,
,
, . *
,
, ,
.
, ,
_ ().
type
. .
typed lexical
,
: my Pony $bill.
transliterate
. . tr/// 5.
trigger
,
.
trinary
, ,
.
ternary.
1080
big)endian
: ,
.
,
,
.
. .
.
pumpkin
,
Perl
.
reaping
,
,
. . wa
it waitpid.
directory handle
,
. . open
dir.
filehandle
(
),
,
.
,
,
.
pointer
C,
. Perl
,
.
,
(
).
unary operator
,
! chdir.
, . .
.
++
. (
.)
truncating
truncate.
conditional
iffy (). .
.
device
( ,
,
),
, *
( ). Unix
/dev.
assertion
,
,
.
.
run phase
,
Perl
. . .
*
( ) (run time),
(
) (compile time),
require,
do FILE eval STRING
/ee.
compile phase
,
Perl
. . *
.
,
*
, BE
GIN, use
.
use
.
file
,
*
.
,
.
.
, , .
filesystem
,
.
(partition) .
,
, Unix.
actual
arguments
, *
.
, power("puff")
"puff"
. .
.
fatal error
*
, *
. ,
eval,
. eval
,
$@ ($EVAL_ERROR).
die (
),
1081
eval.
die
.
source filter
,
, *
.
filter
, ,
.
flag
.
,
(
, Perl n p) , ,
(,
O_CREAT O_EXCL sysopen).
formal
arguments
, *
.
, Perl
.
Perl
$ARGV[0], $ARGV[1] . .
Perl $_[0], $_[1] . .
, my.
. .
format
,
, , ,
,
.
composer
,
, ,
. ,
,
1082
. .
8 .
function
.
, .
(
).
hacker
,
, ,
.
.
script kiddies.
,
, .
hash
/
,
.
,
,
.
,
.
) hash table
,
Perl
().
. bucket.
host
,
.
integer
() .
, , 1, 2, 3
. ., 0
.
loop
,
,
.
FAQ
Frequently Asked Question
(
,
Perl FAQ,
Perl).
numeric context
, (
) ,
. . *
.
readable
,
,
.
, ,
,
.
) member data
. .
run)time
pattern
,
,
,
,
.
, .
pattern
,
.
hexadecimal
16, hex.
10 16
a f.
Perl 0x. .
hex 29.
1083
broadcast
.
instance
,
.
eclectic
.
.
export
.
element
.
,
, .
run time
, Perl
, ,
, ,
, ,
.
compile time
, Perl
,
, ,
, ,
, ,
, ,
, .
is)a
,
,
:
.
,
*
*
. ,
. *
.
( ) ( ), 43, 125, 126, 641,
659
(...) , 201
(?:PATTERN) ,
, 228
Deparse, 532
, 264, 278
, 111
, 108
, 74,
226
, 737
, 182, 187,
194
, 181, 200
, 259
, , 110
,
, 143
[] ( )
, 201, 204, 208
, 40
Unicode , 457, 458
, 181, 200
, 126
, 292
{ } ( ), 311, 641, 658
{ , 181, 200
, 63, 84
, 566
, 338
, 281
, , 162
Unicode, 206, 456
, 151
, 44
, 98
, 71, 201
, 42
, 121
, 126
, 301
, 292
,
, 101
& (), 134
&& ( ) , 59, 140,
362
& ( ) , 139
&&= ( )
, 144
&= ( )
, 144
Perl 5,
, 48
, 37, 88, 89,
166, 264
, 813
( C), 148
, 272
| ( )
|| , 567
| , 480
| , 479
|| ( ) , 140,
233, 362
chdir, 126
| ( ) , 139
||= ( )
, 144
|= ( )
, 144
...|... , 182, 201, 230
, 810
, 285
, 567
, 181, 200
? ( ), 134
? , 71, 201, 219
?? , 202, 219
?: () , 142, 164
, 181, 200
? ( )
(?<=), 249
(?!), 248
(?=), 248
(?>), 249
! ( )
! ( ), 566
!! , 567
! ( )
, 129
, 401
!= ( ) , 60, 138, 640
!~ () , 130
, 184
,
, 217
(), 134, 246
() , 58, 128
, 543
* emacs, 539
= ( )
, 144
> () , 127, 299
, 359
, 127
, 129
, 208
, 565
, 216
, 132
# ()
#! (shebang) , 50, 538
, 623
Unix
, 540
, 84
/x ,
, 189
, 281
, , 100
1085
, (), 42
, 95
print, 729
, 155
, 43
, 146
, 647
print, 640
/ , 42, 113
, 126, 170
, 108,
111
* (), 134
** ( )
, 129
*? , 201
**= (
) , 144
*= ( )
, 144
typeglob, 37, 89, 115
, 121
, 71, 201, 219
, 181, 200
, 131
, 126
( C), 148
, 272
@ ( at)
@_ (@ARG) , 265, 717
@+ (@LAST_MATCH_END)
, 228, 726
@ (@LAST_MATCH_START)
, 228, 726
, 36, 88
, 559
, 281
,
, 97
Perl, 645
, 892
, 272
$ ( ), 42
$< ($REAL_USER_ID), 732
$;($SUBSCRIPT_ SEPARA
TOR), 115, 734
$#
, 113
1086
, 716
$*, 716
$_ ($ARG) , 69, 72, 716
glob , 121
grep , 791
map , 801
foreach, 156
, 117
$? ($CHILD_ERROR), 90, 718
close , 753
, 117
$) ($EFFECTIVE_GROUP_ID), 719
$> ($EFFECTIVE_USER_ID), 719
$! ($ERRNO, $OS_ERROR), 60, 683
$@ ($EVAL_ERROR), 720
$ ($FORMAT_LINES_LEFT), 283,
286, 722
$= ($FORMAT_LINES_PER_
PAGE), 284, 722
$~ ($FORMAT_NAME), 284, 641,
722
$%($FORMAT_PAGE_NUMBER), 2
84, 722
$^ ($FORMAT_TOP_NAME), 284,
286, 641, 723
$.($INPUT_LINE_NUMBER), 141
close, 753
$/($INPUT_RECORD_ SEPARA
TOR), 543, 724
$+ ($LAST_PAREN_MATCH), 228,
727
$& ($MATCH), 187, 228, 649, 728
$| ($OUTPUT_AUTOFLUSH), 284,
641, 729
$,($OUTPUT_FIELD_SEPARATOR),
729
$] ($PERL_VERSION), 728
$ ($POSTMATCH), 187, 228, 649,
730
$ ($PREMATCH), 187, 228, 649,
731
$$ ($PROCESS_ID), 90, 731
$( ($REAL_GROUP_ID), 731
${^ (
), 90
$:($FORMAT_LINE_BREAK_ CHA
RACTERS), 282, 722
$0 ($PROGRAM_NAME), 731
$1 . ., 715
$^A ($ACCUMULATOR), 286, 716
formline, 778
$a $b (
), 716
$^C ($COMPILING), 718
$^D ($DEBUGGING), 544, 719
$^E ($EXTENDED_OS_ERROR), 721
$^F ($SYSTEM_FD_MAX), 474, 734
$^H ( Perl), 723
$^I ($INPLACE_EDIT), 724
$^L ($FORMAT_FORMFEED), 284
$ ($LIST_SEPARATOR), 727
$^M ( ), 727
$^O ($OSNAME), 678, 728
$ ($OUTPUT_RECORD_SEPARA
TOR), 548, 729
$^P ($PERLDB), 730
$^S ($EXCEPTIONS_BEING_CAU
GHT), 720
$^T ($BASETIME), 137, 718
$^V ($PERL_VERSION), 730
$^W ($WARNING), 177, 735
${^WARNING_BITS}, 735
${^WIDE_SYSTEM_CALLS}, 454
$^X ($EXECUTABLE_NAME), 721
$[ (
), 716
,
36, 40, 87
, 559
, 97
, 181, 200
,
, 192, 222
, 299
, 272
+ ( ), 134
++ () , 58, 128
+? , 202
+= ( )
, 144
+ , 126, 130
, 132
, 810
, 70, 201, 219
, 181, 200
% ( ), 134
%= (
) , 144
, 42, 88
, 885
, 131
, 37
, 272
= ( )
= => , 559
= = () , 192,, 138, 640
=~ ( ) ,
69, 130
, 184
= () , 37
=> () , 42,
114, 146
pod, 687
, 568
/ ( ), 134
//, 801
/= ( )
, 144
, 131
, 565
, 752
, , 98
( ), 96
,
338
escape
, 37, 99,
236
, 189
,
, 248
( ), 193, 183, 311
formline, 778
, 193
, 37
, 108
escape
, 236
, 399
, , 39
,
, 194
_ ()
, 95
, 83, 90
, 339, 660
, 715
1087
Perl, 645
, 911
, 614
, 681
, 104
< ( )
< () , 53, 60, 138
<< , 102,
<> 120,
<< ( ) 133, 1074
<<= ( )
, 144
<=> () , 60, 138,
852
,
285
, 566,
, 810
, 53, 117
> ( )
>= ( ) , 60,
138
> () , 53, 60, 138
>>= ( )
, 144
>> ( ) , 133
, 810
, 566
, 285
, 120
, 117
; ( )
,
, 619
, 560
, 150
, , 640
, , 83
,
, 640
^ ( )
^= ( XOR)
, 144
, , 281
,
208
, 217
, 208
, 181, 200
1088
, 72, 192
, 139
, 246
73, 222
~ ()
, 282
,
129
, 788
. (), 134
.. ( ), 141
... ( ), 141
.= ( )
, 144
@INC, 915
, 217
, 245
(IP), 781
, 206
, 71
() , 55, 132,
401, 652
, 181, 200
, 788
v, 104
, 70
\ ( ), 37
quotemeta , 830
escape, 96,
181
, 558
, 560
, 201
, 181
, 200
, 235
MSDOS, 958
, 181, 678
, 97
, 130, 291, 304
, ,
, 99
, 74
: ()
:: , 101,
338, 645
:: ,
, 347
::
, 90
XSUB, 586
, 230
, 154
, 277,
278
, 721
$, 722
` ( /
), 37, 98, 478
qx , 830
, 620
, 108, 117, 644, 660
, 683
, 38
, 660
0 , 539, 543
0+ ( ) ,
399
32 64 , 680
A
a (autosplit) ,
544, 721
A ( )
, 137, 718
abs ( ) ,
742
, 969
, 403
accept , 742
preforked , 472
connect, 754
FileHandle, 873
, 492, 493, 966
, 467
$ACCUMULATOR, 716
ActiveState Perl
PPM (Perl Package Manager), 604
Microsoft, 940
1089
Windows, 541
.al , 345
alarm , 743
, 638
sleep, 851
ALRM , 467
, 463
American Standard Code for Information
Interchange, 1045
and (&&) , 147, 661
, and &&, 59, 362
AnyDBM_file , 935
Apache , 591
CGI::Apache , 935
mod_perl , 527
API
Perl
, 544
arctangent , 743
$ARG ( $_), 117, 716
@ARGV , 717, 1050
eof , 765
pop , 826
s , 551
shift , 848
, 612
, 65, 161, 198,
253, 486, 866
$ARGV , 714, 1045, 1081
C Perl, 644
ARGV , 714
ARGVOUT , 718
ASCII, 82, 817, 1045
, 817
Unicode, 456
, 752
async ( Thread), 502
atan2 ()
, 743
, 969
$atime , 863
attributes , 277, 901
attrs (), 936
AUTOLOAD , 166, 344,
376
goto, 790
AutoLoader , 345, 938
AutoSplit , 345, 938
, 681
autouse , 902, 938
AV ( ),
583, 1045
awk, 68, 1045
Perl, 115
B
b, 181, 203, 209, 223, 246
b ( )
, 135
B ()
, 137
B , 939
B::Asmdata , 939
B::Assemble , 939
B::Bblock , 939
B::Bytecode , 528, 939
B::C B::CC , 529, 939
B::Debug , 939
B::Deparse , 531, 940
B::Disassembler , 940
B::Lint , 530, 940
B::Showlex , 940
b::Stash , 940
B::Terse , 940
B::Xref , 531, 940
backspace, 96
base , 371, 903, 936
, 384
$BASETIME, 718
BASIC ,
, 59
BEGIN , 516
, 534
, 269
@inc, 347
, 533
Benchmark , 939
, 941
bind , 744
SO_REUSEADDR , 848
, 492
binmode , 744, 815
sysopen , 872
, 438
seek tell DOS, 679
/bin/sh, 540
bless , 294, 305, 364, 746, 1066
1090
typeglob , 295
tie, 412
, , 365
, 290
, 662,
746, 833
blib , 589
blib , 904, 938
$blksize , 863
$blocks , 863
break ( C), 643
break , 160
BSD (Berkeley Standard Distribution),
1045
BSD::Resource ,
, 638
ByteLoader , 529, 940
bytes , 455, 933
C
C (
)
, 136, 137, 718
c ( )
, 544, 718
Perl , 520
C ( )
, 454, 544, 735
c ( )
, 135
/c , 198
, 224
C , 1045
fcntl.h ,
Perl, 934
Perl, , 591
XSUB, 589
Perl,
592
Perl, , 594
Perl, 592
Perl, , 593
Perl, , 595
Perl, , 582,
584, 586
C,
, 590
syslog ,
Perl, 934
, ,
, 464
, 738
C, 529
Perl , 527
, 1056
Perl,
611
, 140
Perl, 148
, 125
, 583
, 457
, 550, 1070
, 623
,
Perl, 643
Perl C,
Perl, 585
, 268
C, C, 526
, 823
caller , 746
, 525
Perl,
647
goto, 166, 790
Perl , 575
, 273, 389,
429, 700, 975
can ( UNIVERSAL), 374
Carp , 416, 937, 943
case , 163
cat (Unix), 430
cbreak , 779
/cg , 192
CGI, 935
cgi.pm , 944
mod_perl , 26
, 494
, 630, 633
Perl , 935
, 611
mod_perl,
527
, ,
777
, 28
CGI::Apache , 935
CGI::Carp , 935, 944
CGI::Cookie , 935
CGI::Fast , 935
CGI::Pretty , 935
CGI::Push , 935
CGI push, 935
charnames , 456, 933
chdir , 748
Cwd, 947
, 555
, 353
, 152, 620,
681, 761
, 126
CHECK , 516
, 533
$CHILD_ERROR, 718
chmod , 51, 748
, 121, 737,
885
chomp , 54, 749
$/ , 724
chop, 750
, 458
$, 548
, 644, 666,
755, 859
chop , 54, 750
, 458
, 57, 942
, 650
chown , 751
POSIX, 971
chr , 207, 752
CRLF , 494
Unicode , 214, 458
, 104, 403,
802
chroot , 630, 752
Class::Contract , 390
Class::Multimethods , 373
Class::Struct , 931, 945
close , 479, 753
, 471
, 724
, 754
1091
, 682
, 53, 152, 334,
472
,
477, 478, 812
closedir , 754, 817
, 682
cmp , 60, 138, 852
, 403
cmp ( File::Compare), 954
comp.lang.perl , 27
COMSPEC , 556
Config , 938
%config , 678, 592
, 464
connect , 754
IO::Socket ,
491
Socket , 973
, 491
constant , 342
continue , 154, 159, 160
continue ( C), 643
ControlA, 194
ControlD , 105, 674
ControlZ , 105, 674
cookie (HTTP), ,
935
CORE , 372
CORE::GLOBAL , 353
cos () , 754
Math::Trig , 969
, 969
, 523, 744
CPAN (Comprehensive Perl Archive Net
work), 20, 46, 336, 346, 599, 1046
CPAN , 938, 947
, 684
,
604
modules , 601
, 604
, 601
, 605
, 447
, 606
Perl, 605
, 604
1092
, 599
CPU
,
880
, 622
, 505
,
, 511
CRLF, 678
, 494
crypt , 755
, 112
$ctime , 863
<Ctrl>+<C>, 96
, , 463
CV ( ), 1046
Cwd , 932
,
, 947
D
^D (ControlD) EOF, 674
d () ,
61, 135
/d , 198
d () ,
544, 557, 578
_ _DATA_ _ (), 105, 718
DATA , 718
D ( )
, 544
Data::Dumper , 334, 936, 948
date , 800
DB , 936
caller , 747
DB_File , 935
Data::Dumper ,
, 948
dbmclose , 412
dbmopen , 412
, 473
, 413
DBM, 1046
dbmclose dbmopen , 756
, 473
, 935
, 684
, 759
, 447,
948
$DEBUGGING, 719
DEFAULT , 464
defined , 757, 882, 1066
wantarray , 267
exists , 770
, 108
, 118
, 264
, 340, 474,
476, 748, 756, 813
DEL , 96
delete , 759
undef, 883
, 302, 556
, 303
,
, 647
dequeue (Thread::Queue ),
513
detach , 504
$dev , 863
Devel::DProf , 936
, 578
Devel::Peek , 936
Devel::SelfStubber , 938
Devel::SmallProf , 581
diagnostics , 936
_ _DIE_ _ , 733
die , 733, 760
alarm , 468
END , 534
eval , 766
sigtrap , 920
, 463
warn, 892
exit, 772
, 464
, 126, 140,
152, 657, 659
, 1081
, 728
dieLevel , 571
DNS (Domain Name Server), 489
do, 127
block, 762
FILE, 762
SUBROUTINE, 763
, 151
, 162
, 162
require, 762
, 641
, 151
, 151
, 162
doc (CPAN),
Perl, 600
dosish.h , 540
dot,
, 428
down (Thread::Semaphore ),
514
dprof, 578
dprofpp , 578
dump , 763
Dumpvalue , 936
dweomer, 1046
dwimmer, 1046
DynaLoader , 345, 588, 938
E
e (execute) ,
538, 546
e (exists) , 61,
135
/e ( )
, 194, 254
each , 329, 764
keys , 794
, 878
, 42, 432
, 654
else , 63
elsif , 63
emacs
* , 539
, 569
,
, 68
END , 516
, 535
, 533
, 534
_ _END_ _ (), 104, 654,
674, 718
endgrent , 779
endhostent , 782
endnetent , 741
endprotoent , 785
endpwent , 785
1093
endservent , 787
English , 284, 936
enqueue (Thread::Queue ),
513
Env , 936
env , Perl , 539
%ENV , 719
, 682
, 759
EOF ( ), 1060
eof , 65, 653, 764
sysread , 874
, 141, 160, 698
,
, 438
eq , 60, 640
, 63, 67
equal (Thread ), 505
errno, 1063
Errno , 936
escape
, 96, 236
,
236
/etc/group , 653, 779
/etc/hosts , 782
/etc/networks , 783
/etc/passwd , 653, 785
/etc/protocols , 785
/etc/services , 787
/etc/utmp , 782
eval exec, 51, 539, 551, 673
eval (Safe ), 632
eval , 503
eval , 92, 127, 288, 649, 766
AUTOLOAD, , 344
caller , 747
die , 674, 760
do , 762
qr , 866
return , 838
s///ee , 195, 252
, 159, 162
, 516
, 720
, 668
, 334, 948
, 636
, 733
, , 504
1094
,
, 613, 633
, 339
,
, 410
, 273
, 172
, 199
, , 285
, 649
, 518
$EVAL_ERROR, 720
eval_sv eval_pv , 594
$EXCEPTIONS_BEING_CAUGHT, 720
exec , 768, 875, 1046
END , 518, 534
fork , 770
,
, 952
, 683
, 613, 620
,
, 475,
734, 815
, 635
$EXECUTABLE_NAME, 721
execv ( C), 769
execvp ( C), 768
exists , 664, 770
delete , 759
, 432
, 386, 389,
556
, 303
, 424
exit , 34, 540, 771, 776
END , 535
, 657
, 502, 504
, 474
exp , 772
, 48
expat XML, 669
Expect , 483
@EXPORT , 721
Exporter , 588, 938
, 349
export_fail , 352
@EXPORT_OK , 721
%EXPORT_TAGS , 721
export_to_level , 351
$EXTENDED_OS_ERROR, 721
ExtUtils::Command , 938
ExtUtils::Embed , 592, 938
ExtUtils::Install , 938
ExtUtils::Installed , 938
ExtUtils::Liblist , 938
ExtUtils::MakeMaker , 607, 938
ExtUtils::Manifest , 938
ExtUtils::Mkbootstrap , 938
ExtUtils::Mksymlists , 938
ExtUtils::MM_Cygwin , 939
ExtUtils::MM_OS2 , 939
ExtUtils::MM_Unix , 939
ExtUtils::MM_VMS , 939
ExtUtils::MM_Win32 , 939
ExtUtils::Packlist , 939
ExtUtils::Testlib , 939
F
f (
) , 136
@F , 721
f ( )
, 61, 135
F ( )
, 544, 546
fallback , 406
false , 38
FAQ, Perl,
639
Fast CGI , 935
Fatal , 936
Fcntl , 934, 953
fcntl , 772
Fcntl , 953
flock , 775
h2ph , 898
FileHandle,
963
, 793
,
, 629
exec, 629
, 475, 815
fields , 371, 382, 936
, 382
%FIELDS , 722
FIFO, 484, 1046
1095
_ _FILE_ _ , 104, 674
File::Basename , 681, 932
File::CheckTree , 932
File::Chmod , 749
File::Compare , 932
File::Copy , 932
File::DosGlob , 932
File::Find , 932
File::Glob , 932
File::Path , 932
File::Spec , 681, 932
File::Spec::Functions , 932
File::Spec::Mac , 932
File::Spec::OS2 , 932
File::Spec::Unix , 932
File::Spec::VMS , 932
File::Spec::Win32 , 932
File::Stat , 932, 960
, 960
File::Temp , ,
, 628
fileglob , 120
FileHandle , 284, 287, 933
fileno , 438, 773
filetest , 136
find , 69
, 932
FindBin , 939
findstr , 68, 69
flock , 417, 470, 774
alarms , 468
for, 66, 150, 155
foreach, 156
, 196
foreach , 66, 150, 156, 649
$_ , 164
Perl, 647
,
Perl
, 644
, 196
fork , 479, 776, 1053
$$ , 731
exec , 770
perlfork , 462
shutdown , 850
socketpair , 851
wait , 891
,
, 775
, 499
, 483
,
,
473, 476
, 657
, 477, 826
Windows, 498, 527
, , 621
, 479, 480, 813
, 683
, 474
, 466
,
, 493
format , 777
$FORMAT_FORMFEED, 722
$FORMAT_LINE_BREAK_CHARAC
TERS, 722
$FORMAT_LINES_LEFT, 722
$FORMAT_LINES_PER_PAGE, 722
$FORMAT_NAME, 722
$FORMAT_PAGE_NUMBER, 722
$FORMAT_TOP_NAME, 723
formline , 286, 778
$^a , 716
FORTRAN, 83
freeware, 1052
G
g (setgid) ,
135
/g () ,
192, 194
GDBM (GNU DBM)
GDBM_File , 935
, 473
ge , 60
getc , 651, 779
,
436
getenv, 554
getgrent , 779
, 980
getgrgid , 780
getgrnam , 780
1096
gethost* ,
, 934
gethostbyaddr , 780
getpeername , 783
, 970
, 653
Socket , 973
, 453
, 493, 496,
787
gethostbyname , 781
, 970
Socket , 973
, 493
gethostent , 782
, 782
getlogin , 782
getnet* ,
, 935
getnetbyaddr , 783
getnetbyname , 783
getnetent , 783
Getopt::Long , 932
Getopt::Std , 932
getopts::* , 161
getpeername , 493, 783
, 974
getpgrp , 784
getppid , 784
, 796
getpriority , 784
setpriority,
, 847
getproto* ,
, 935
getprotobyname , 784
Socket , 973
, 495
getprotobynumber , 785
getprotoent , 785
getpwent , 785
, 981
, 112, 758
getpwnam , 786
, 981
, 429, 751,
752
getpwuid , 782, 786
, 981
, 140
, 906
, 429, 503,
755
getserv* ,
, 935
getservbyname , 786
Socket , 973
, 495
getservbyport , 787
getservent , 787
getsockname , 787
getsockopt , 788
GID (ID ), 480, 731, 751, 1046
, 780
, 719
$gid , 863
glob , 120, 121, 788
, 354
, 788, 883
globbing filenames, 1046
gmtime , 789, 931
utime , 888
, 934, 979
time, 879
, 979
, 824
goto , 165, 344, 649, 790
dump , 763
Perl, 646
,
, 646
, 523
Greenwich Mean Time (GMT), 789
grep , 69, 236, 1046
grep , 660, 791
$_ , 664
, , 832
, 270, 354,
429, 442, 478, 493, 748
, 660
, 274
gt , 60
GUI, Perl/Tk, 657
GV ( glob),
583, 1047
H
h (help) , 546
h2xs , 585, 586, 607
here , 102
hex , 791
HOME , 555
hosts , 783
HTML ( ), 935
pod, , 937
CGI,
935
, , 935
HTTP ( )
cookie, , 935
, , 69
HV ( ),
583, 1047
I
I (include path)
, 539, 548
i (inplace editing)
, 546
/i ( )
, 188, 192, 194
I18N::Collate , 933
ID , 751
ID (PID)
waitpid , 891
, 784
fork, 776
, 847
, 784
if , 60, 63, 150, 153
C , Perl, 643
case, 163
, 58
IGNORE , 464
import , 351, 792, 1057
, 407
@INC , 347, 723, 939
lib , 914
,
548
%INC , 347, 723
index , 792, 1058
$, 716
, 458
, 488
inet_ntoa ( Socket), 781
INIT , 516
, 269
1097
, 536
, 533
init , 784
$ino , 863
$INPUT_LINE_NUMBER, 724
$INPUT_RECORD_SEPARATOR, 543,
724
int , 792, 822
, 831
integer , 131, 913
, 900
I/O (Input/Output), 1047
IO ( ),
1047
IO , 933
IO::Dir , 933
IO::File , 933
IO::Handle , 933
IO::Pipe , 933
IO::Poll , 933
IO::Pty , 483
IO::Seekable , 933
IO::Select , 933
IO::Socket , 490
IO::Socket , 934, 966
IO::Socket::Inet , 491
IO::Socket::INET , 934
IO::Socket::UNIX , 934
ioctl , 793
h2ph , 898
, 793
, 978
, 793
IP (Internet Protocol), 489, 1047
v, 781
UTF8, 453
, 783
IPC ( ),
461, 485
, 461
, 477
, 482
, 933
, 462
, 683
, 462
, 489
, 495
1098
, 491
, 492
, 469
IPC::Msg , 934
IPC::Open2 , 591, 934, 967
IPC::Open3 , 591, 934, 967
IPC::Semaphore , 934
IPC::Shareable , 486
IPC::SysV , 934
@ISA , 369, 726, 998, 1032, 1040
use base , 903
, 370
isa ( UNIVERSAL), 374
itimer , 743
IV (
), 583, 1047
J
JAPH (Just Another Perl Hacker), 1047
Java, Perl , 601
join (Thread ), 502
, 503
join , 196, 794
split, 860
pack, 824
, 101, 196,
305, 858
, 653
K
k ( )
, 135
keys , 42, 76, 115, 764, 794, 888
, 797
, 270, 324,
888
, 432
,
646
kill , 462, 795
, 464
, 535
, 465
Microsoft , 465
L
l (automatic line ending)
, 548
l ( )
, 135
last , 67, 156, 159, 160, 164,
652, 660, 796
do while, 162
, 120, 158,
665
, 162
$LAST_PAREN_MATCH, 727
lc , 797
, 76, 154,
165, 186
, 853
lcfirst , 797
le , 60
length , 797
pos , 827
, 455
, 458
, 135
less , 936
lib , 347, 939
LIFO (Last In, First Out), 1047
#line , 674
_ _LINE_ _ , 104, 674
LineInfo , 572
link , 798
lint ( C), 530
list
, 559
listen , 798
, 492
$LIST_SEPARATOR, 727
local, 1062
, 115
, 169
,
464
local , 169, 171, 798
my, 643
, 175
, 641
,
525
locale , 933
localtime , 789, 800, 931
File::stat , 865
strftime , 970
, 934, 979
, 979
, 111, 378,
945
time , 879
lock , 506, 800
, 290
, 622
locked (), 277,
391, 509, 510
log () , 801
, 969
LOGDIR , 555
login , 741
long, 822
long long, 822
lstat , 801
_ , 137, 715
, 932
lt , 60
l
, ,
, 390
, 170
, 666
M
M (use module)
blib , 904
m (use module)
, 549
M ( )
, 718
m ( )
, 136, 137
/m , 188, 192, 194
m// () , 108, 180, 191,
565, 641, 801
=~ () ,
, 185
pos , 827
,
, 183
, 188, 191
m?? ( ) , 193,
565
reset , 838
Macintosh
File::Spec:, 932
, 50
1099
, 542
Perl, 540
magic, 1054
mail, , , 683
main , 92, 337
Makefile, 1047
Perl, 938
Makefile.PL, 586, 604, 606
MakeMaker, 607
malloc
PERL_DEBUG_MSTATS
, , 556
, 727
man , 21, 346, 568, 1047
MANIFEST , 587, 938
map , 76, 660, 791, 801
, 435
, 855
, 76, 238, 334
, 660
$MATCH, 728
Math::BigFloat , 931
Math::BigInt , 395, 931
Math::Complex , 931
Math::Trig , 931
method (), 277,
391
Method ( overload), 409
m//g, Perl,
647
Microsoft Windows
File::Spec:, 932
Win32::Pipe , 484
, 498
Perl ActiveState,
, 541
NT, 542
CPAN, , 604
Perl , 940
,
Perl, 556
(perlwin32),
462
File::DosGlob, 932
, , 448
0 , 465
,
API , 454
1100
mkdir , 802
mkdir, 653
, 661
mkfifo , 484
MLDBM , 447
Data::Dumper ,
, 948
mmap , 485
$mode , 863
mod_perl, 527, 591
modules (CPAN), 600
MSDOS ,
Perl, 540
msgctl , 803
msgget , 803
msgrcv , 803
msgsnd , 803
$mtime , 863
my , 91, 149, 157, 169, 171, 804
local, 643
, 804
, 306
, 337
, 154
, 106
, 268
, 112
local, 176
our, 91
,
641
, 652
N
n ( )
, 550
names
, , 780
, 464
NDBM_File , 935
ne , 60, 640
Net::hostent , 782, 934
Class:, 946
Net::netent , 783, 935
Net::Ping , 935
Net::proto , 785
Net::protoent , 935
Net::servent , 935
networks , 783
new , 502, 807
next , 67, 154, 159, 160, 807
next if , 650
do { } while, 162, 643
, 162
NFS ( ), 1047
, 489
$nlink , 863
nn , 206
no , 166, 176, 348, 420, 808
nomethod , 406
null
, 119
, 38
null device ( ),
Unix, 444
NV (
), 583
O
O , 528, 530, 940
/o ()
, 188, 189, 192, 194, 234
O ( UID)
, 135
o ( UID)
, 135
oct , 95, 748, 808
chmod, , 748
, 95
Opcode , 936
open , 917
open , 52, 479, 808, 871
$. , 753
$^F , 734
fileno , 773
open , 917
open2 open3 , 482
setid , 619
, 471
binmode, 744
sysopen, 871
FileHandle,
962
, 626
, 477
,
812
, 612
, 485
, 618
, 635
, 479, 813
, 474
, 681, 683
, 476
, 120
,
810
,
434
, 493
, 295
Open2, Open3 ,
482
opendir , 817, 832
ops , 936
or , 139, 140, 141, 147, 661
| (), 139
, 167
, or ||, 59, 362
ord , 752, 817
Unicode , 458
, 685
OS/2
File::Spec:, 932
Perl, 540
$OS_ERROR, 728
$OSNAME ($^O ), 728
our , 91, 149, 169, 171
my, 804
, 173
, 178
, 106
subs vars , 901
strict , 924
vars , 925
$OUTPUT_AUTOFLUSH, 729
$OUTPUT_FIELD_SEPARATOR, 729
$OUTPUT_RECORD_SEPARATOR, 729
overload , 396
P
p ( )
, 550
1101
p ( )
, 135, 484
P ( C)
, 550
pack , 680, 819, 884
UTF8 , 458
vec , 890
, 458
, 680
, 104, 804,
846, 870
, 820
package , 824
pads, 88
PalmPilot, Perl , 601
paragraph mode, 543
PATH , 51, 551,
555
, 618
PAUSE, 606
Perl
C, , 584
XSUB,
589
,
CPAN , 605
, 728, 730
, 524, 526
C, 591
Perl,
C, 592
Perl, C,
594
, 592
Perl, C,
593
Perl, C, 595
Unicode,
, 459
C
XSUB XS, 585
C, 590
, 586
, 36
, 703
,
542
, , 516
1102
, 501
, 639
,
, 936
, 706
Perl Data Language (PDL) , 319
Perl Package Manager (PPM), 604
Perl , 50, 516
s , 557
(sticky) , 654
PERL5DB , 555
, ,
570
PERL5LIB , 555
PERL5OPT ,
555
PERL5SHELL ,
556
perlapi , 594
perlcall , 595
perlcc , 528, 718
pbyscript, 529
$PERLDB, 730
PERLDB_OPTS
AutoTrace ,
, 560
, 569
PERL_DEBUG_MSTATS
, 556
PERL_DESTRUCT_LEVEL
, 379, 556
perldoc , 21, 346
, 568
perlembed , 591
eval_sv , 595
perlfork , 462
perllocale , 797
perlmodinstall , 604
perlport , 462
, 742
Perl/Tk, 657
$PERL_VERSION, 728, 730
perlwin32 , 462
PID (ID ), 731, 784, 815
, 784
pipe , 826
socketpair , 483
Win32 , 484
socketpair, 851
, 683
.pm , 336
, 348
pod, 686
Perl, 84
, 689, 1056
, 695
Perl , 601
, 687, 695
Pod::Checker , 937
Pod::Functions , 937
Pod::Html , 937
Pod::InputObjects , 937
Pod::Man , 937
Pod::Parser , 937
Pod::Select , 937
Pod::Text , 937
Pod::Text::Termcap , 938
Pod::Usage , 938
pop , 315, 826, 849, 933
splice , 856
split , 857
, 111, 270,
669
, 426
popen ( C), 591
pos , 224, 827
@+ , 726
, 458
POSIX , 934
getattr , 779
strftime , 800
,
468
, 348
POSIX , 208,
217
$POSTMATCH, 730
PPM (Perl Package Manager), 604
$PREMATCH, 731
print , 53, 76, 652, 828, 829
select , 843
read, 831
write, 283, 894
CGI, 673
$\,
729
1103
,
, 952
, 134
, 400
,
361
$|, 729
, 640
$?@,
729
, 435
, 649, 652
printf , 143, 829, 860
$# , 716
,
, 952
, 754, 781,
864, 880
, 437
, 672
, 649
PrintRet , 572
$PROCESS_ID, 731
$PROGRAM_NAME, 731
prototype ,
, 952
push , 86, 315, 830, 849
pop , 87
splice , 856
, 270, 317
, 425
, 298
, 41
, 271
pushpop (pp) , 524
PV ( ),
583
pwd (Unix), 654
Python, 83
Perl, 640
Q
q// ()
, 108, 185, 237, 408
, 188
quotemeta , 830
, 259
qw ( )
, 100, 111
qx// ( )
, 108, 117
open , 917
readpipe , 834
R
R (
uid/gid) ,
135
r ( uid/
gid) , 61, 135
rand , 126, 831, 863
, 126, 166,
403, 636, 755
$rdev , 863
re , 936
read , 831, 832
print, 831
, 437
, 467
readdir , 832
ReadLine , 574
readline , 117, 833
readlink , 833
readpipe , 834
recallCommand , 571
record mode, 725
records
output separator, 729
recv , 834, 846
, 496
, 846
redo , 159, 160, 834
ref , 305, 365, 835
attributes , 902
, 366, 386,
397, 401, 417
reftype (attributes ),
365
regex, 68
regexp, 68
rename , 836
, 682
, 547, 767
require , 168, 348, 723, 836,
886
base , 903
caller , 747
__data__ , 105
@INC , 897
package , 825
tie , 413
1104
Perl , 896
do, 762
use, 886
, 269
, 338
, 172
lib , 914
res, 68
reset , 838
return , 159, 265, 838
eval , 162, 766
, 502
, 267
reval ,
, 631
reverse , 76, 839
scalar , 459
sort, 647
, 76, 156, 402
rewinddir , 817, 839
rindex , 839
, 458
rmdir , 840
root , 1078
rot13 , 198
RS ( awk), 724
rstart ( awk), 726
RV ( ),
583
S
s (switch) , 551,
557
/s , 188, 192, 194,
198
s ( )
, 135
s/// () , 108, 180,
193, 641, 1069
=~ !~ , 185
,
, 183
, 188, 194
S ( )
, 551
S () , 135
Safe , 630
, 630
safe , 936
scalar
, 559
scalar , 106, 840
reverse , 459
,
, 797
, 305
, 271
scripts
CPAN, 600
SDBM_File , 935
Search::Dict , 931
sed, 68, 197
seek , 841, 877
Fcntl , 953
File::Temp , 629
x , 554
, 472
, 437
DOS , 679
seekdir , 817, 842, 877
telldir , 877
select ( )
, 773, 843, 844
fileno , 774
IPC::Open3 , 968
, 845,
851
select ,
/
, 493
select ( )
, 828, 843, 933
IPC::Open3 , 482
FileHandle , 962
, 485
, 284
, 712
, 284, 495,
548, 814, 893
SelectSaver , 933
self (Thread ), 504
SelfLoader , 345, 939
Devel::SelfStubber ,
, 938
semctl , 845
semget , 846
semop , 846
send , 846
setgid, 480, 610, 619
setgrent , 779
sethostent , 782
setid , 610
, , 619
Unix,
, 623
setnetent , 741, 783
setpgrp , 465, 847
setpriority , 847
setprotoent , 785
setpwent , 785
setservent , 787
setsockopt , 848
, 492, 495
setuid, 480, 610, 619
sh ( ), 554, 1064
#! , 540
eval exec , 51, 551
exec , 768
system , 612
User::pwent , 981
x Perl, 545
, 812
, 875
, 477
shebang, , 50
Shell , 934
shift , 65, 315, 848, 849, 885
@_ , 664
splice , 856
, 830
, 805
, 649
shmctl , 849
, 488
ShMem , 487
shmget , 849
, 487
shmread , 849
, 488
shmwrite , 849
, 488
short, 861
shutdown , 850
, 754
, 851
%SIG , 462, 732
sigaction , 467
SIGALRM , 743
signalLevel , 571
sigprocmask , 468
sigtrap , 464
sin () , 850
1105
, 969
, 523, 744
$size , 863
sleep , 851
alarm , 743
select , 845
, 156
, 845
slurping , 543
Socket , 490, 935
inet_ntoa , 781
,
, 491
socket
accept , 742
, 799
socketpair , 483, 851
sort , 42, 76, 852
reverse , 647
, 458
$a $b, 712
, 916
, 522
, 716
, 60
, 327
spaceship ( )
(), 139
splice , 856
delete, 759
Perl, 646
, 403, 420,
827, 830
, 426
, 649
split , 68, 185, 196, 229, 857
join , 196
reverse , 402
,
, 952
,
, 1069
, 49, 112, 151,
323, 325, 663, 802
, 68
, 650, 653
sprintf , 437, 791, 860
, 789
, 458
, 781
, 792
1106
, 195, 414,
808
, 433
, 437
sqrt ( ) , 862
, 969
, 850
srand , 863
src (CPAN), 600
stat , 801, 863, 960
_ , 137, 715
File::Stat , 616
, 136
, 626
, 932
, 808
, 111
STDERR , 52, 662,
733, 774
,
448
, 474
, 494
STDIN , 52, 733, 774
i , 548
, 474
, 494
, 779
STDOUT , 52, 53, 733,
774, 1078
i , 548
, 828
, 474
, 494
strict , 47, 91, 178, 310, 922, 936
, 642
struct , 384
Struct::Class , 381
StrVal ( overload), 409
study , 865
sub , 262, 272, 294, 867
subs , 377, 936
, 353
$SUBSCRIPT_SEPARATOR (.
S;), 115, 734
substr , 868
chop , 750
, 458
,
222
, 549
, 401, 488,
699, 750
, 726
, 651, 654
SUPER , 372
SV ( ),
583
SWIG, ,
XSUB, 585
switch , 163, 652, 1059
Perl
C, 643
symlink , 869
, 681
Sys::Hostname , 934
Sys::Syslog , 934
syscall , 743, 869
fileno , 774
h2ph , 898
setitimer , 851
, 738
syslog, ,
448
sysopen , 472, 815, 871
Fcntl , 953
open , 815, 917
, 471
, 619
, 625
, 485
, 619, 627,
970
, 882
sysread , 832, 874
seek , 841
read, 832
, 876, 972
, 437
System V IPC, 485
IPC::SysV , 934
, 683
, 462
, 467
,
, 849
,
, 803
, 796
, 740
, 845
system , 768, 875
$? , 535, 718, 760
exit , 776
setid , 619
wait , 891
exec, 768
, 660
,
, 952
, 613
Windows , 556
, 683
syscalls, 24
, 635
, 654
$SYSTEM_FD_MAX, 734
syswrite , 433, 876
seek , 841
, 732
, 418
, 433, 438
T
t (tty) , 135
T ( )
, 490, 610, 552
, 494
T () , 61,
137
taintperl , 647
tan () , 743, 969
Math::Trig , 969
TCP, 495, 934, 1049
IO::Socket:, 491, 492
Socket , 491
UDP, 495
/, 491
, 489
, 489
tee , Unix, 444
tell , 877
fgetpos , 963
, 472, 842
, 437
1107
DOS , 679
telldir , 817, 877
seekdir , 877
Tengwar , 457
Term::Cap , 935
Term::Complete , 935
Term::ReadKey , 569, 779
Term::ReadLine , 569, 935
, 574
Term::Rendezvous , 574
test (Unix), 51
Test , 939
Test::Harness , 939
test.pl, 586, 590
Text::Abbrev , 931
Text::ParseWords , 931
Text::Wrap , 931
Thread , 501
detach ,
, 504
join ,
, 503
, 504
,
505
, 502
, 502
Thread::Queue , 512
Thread::Semaphore , 501, 511,
513
Thread::Signal , 514
tid (Thread ), 504
tie , 411, 877, 879
DBM , 473
use require , 413
bless, 412
, 444
CPAN , 447
, 682
, 313, 948
Tie::Array , 421, 937
splice , ,
426
Tie::Counter , 419
Tie::DBI , 447
Tie::DevNull , 443
Tie::DevRandom , 443
Tie::Hash , 427, 937
Tie::Persistent , 447
Tie::Refhash , 937
1108
Tie::Scalar , 414, 937
Tie::SecureHash , 390, 448
Tie::StdArray , 421
Tie::STDERR , 448
Tie::StdHash , 427
Tie::StdScalar , 414
Tie::SubstrHash , 937
Tie::Syslog , 448
Tie::Tee , 443
Tie::TextDir , 448
Tie::TransactHash , 448
Tie::VecArray , 448
Tie::Watch , 448
tied , 879
, 431
time , 789, 879, 931
, 684
, 789, 800
Time::gmtime , 934
Time::Hires , 743
Time::localtime , 934
Time::tm , 934
timelocal , 800
times , 880
Tk, 657
tk , 571
tr/// () , 184,
197, 880, 894
=~ !~ , 185
, 188
,
, 458
troff, 1049
true , 38
truncate , 880
, 472
try , 766
tty, 483
typedef (C ), 1049
typeglob, 36, 89, 115, 288
local , , 799
, 295
Symbol,
931
typeglob,
295
, 340
, 573
,
, 412
, , 359
, 435
(*), 37
, 342
,
, 295
typeglobs, 305
IO::Handle , 296
typemap , 586
U
u (setuid) ,
135
U (unsafe) , 552
u ( )
, 552
uc , 458, 881
, 389, 435,
954
, 853
ucfirst , 458, 881
, 380, 981
UDP, 1049
TCP, 495
, 489
, 495
, 495
, 489
UID (ID ), 480, 751
passwd, 786
, 719
$uid , 863
umask , 881, 1049
mkdir , 802
open , 811
sysopen , 872
, 620
undef (), 62, 107, 111, 757
select , 485, 845
undef , 62, 882
, , 113
, 760, 771
$/ , 334,
725
, 795
, 653, 654
Underscore , 420
Unicode, 82, 451
, 459
,
, 199
, 90
,
, 207
Perl,
, 459
, 797
, , 933
, 752
, 456
, 211
, 208, 210
, 214
, 213
, 213
,
, 214
Perl , 209
Perl, 211
, 212
, , 459
, 83
, 83
() , 70
, 96, 752
Unicode, 3.0, 459
, 458
UNIVERSAL , 374, 937
, , 376
Unix, 37, 1049
#! , 50
File::Spec:, 932
fork , 777
IPC , 462
test ,
, 51
Perl , 537
, 879
, 774
,
353
, 541
, 29
, 674
CPAN, , 604
, 37
, 623
, 678, 815
,
932
, 882
1109
Perl , 13
, 681
, 552
, 465, 467
, 745
, 489, 934
#!, 539
Perl, 20
, 311
Perl, 738, 742
Windows, 940
unless , 64, 150
if, 153
unlink , 883
Socket , 974
, 682
, 107, 431
unpack , 680, 820, 884
UTF8 , 458
vec , 890
, 458
, 1069
, 656
, 680
, 756, 793,
817, 863, 870, 885
, 651
unshift , 315, 849, 885
splice , 856
, 699, 737,
799
unsigned short, 861
untie , 411, 446, 756, 886
,
, 414
, 473, 878
until , 150, 154
up (Thread::Semaphore ),
514
use, 46, 166, 168, 176, 336, 348, 886
tie , 413
, 169
usenet
Perl, , 27
User::grent , 780, 934
User::pwent , 786, 934
Class:, 946
UTF8, 82, 207, 210, 453
1110
binmode , 744
/ , 816,
917
, 656, 680
, 669
/ ,
454
, 458
, 104
CGI , 633
, 651
utf8 , 207, 210, 455, 933
, 735
, 218
, 216
utime , 879
utmp , 783
UV (
), 583
V
V () ,
553
v () ,
553
values , 764, 888
, 427, 432
vars (), 936
vec , 889
, 458
, 496, 844
, 654
VERSION , 375
vi
, 569
,
, 68
VMS
File::Spec:, 932
Perl, 540
void
, 559
v ( ), 104, 1049
in $^V, 730
W
W (
uid/gid) ,
135
w ( uid/
gid) , 61, 135
w , 171, 177,
553, 721, 982
, 51
wait , 890
$? , 718
die , 753
fork , 777
, 466
waitpid , 890, 891
close , 753
IPC::Open2 , 482, 967
IPC::Open3 , 967
, 474, 483,
826, 971
, 466, 890
wantarray , 106, 267, 891, 266
, 481, 883
_ _WARN_ _ , 732
diagnostics , 909
, 892
warn , 892, 943
, 674
, 161
CGI , 944
$WARNING, 735
${^WARNING_BITS}, 735
warnings , 177, 557, 658, 926, 937
_ _WARN_ _ , 733
warnLevel , 571
WeakRef , 379
while , 60, 64, 150, 154
eof , 764
, , 107
$_, 117
, 112
${^WIDE_SYSTEM_CALLS}, 735
Win32::ChangeNotify , 940
Win32::Console , 940
Win32::Event , 940
Win32::EventLog , 940
Win32::File , 940
Win32::FileSecurity , 940
Win32::Internet , 940
Win32::IPC , 940
Win32::Mutex , 940
Win32::NetAdmin , 940
Win32::NetResource , 940
Win32::ODBC , 940
1111
win32::ODBC , 940
Win32::OLE , 940
Win32::OLE::Const , 941
Win32::OLE::Enum , 941
Win32::OLE::NLS , 941
Win32::OLE::Variant , 941
Win32::PerfLib , 941
Win32::Pipe , 484
Win32::Process , 941
Win32::Semaphore , 941
Win32::Service , 941
Win32::Sound , 941
Win32::TieRegistry , 448, 941
Win32API::File , 941
Win32API::Net , 941
Win32API::Registry , 941
World Wide Web, 935
wrapper, 1064
write , 280, 282, 777, 893
$% , 722
$ , 722
$= , 722
$| , 729
$^A , 778
$^a , 716
formline , 778
i , 546
$^L , 722
select , 843
, 458
, 286
, 777
, 777
X
x (extract program)
, 538
x (
uid/gid) ,
135
x (
uid/gid) ,
135
x ( )
, 554
/x , 188, 189,
192, 194
X (
)
, 554
x () , 55, 132
x= ( )
, 144
xor , 59, 139, 147
XS
,
584
, , 683
, 936
, C
Perl, 585
XSLoader , 939
XSUB, 585
, 589
Perl,
584
xsubpp , 585
Y
y/// () , 184,
197, 894
yield (Thread ), 505
Z
^Z (ControlZ) EOF, 674
z ( )
, 135
, 931
, , 724
, 335
, 358
( ) ,
128
, 402
, 1050
, 385
, 376
(++) ,
128, 403, 1050
, 128
, , 1050
, 548
, 729, 1053
, 132
1112
, 744
,
, 573
, 783
, 781
, 780, 783
, 787
C, Perl, 644
, 1066
, 1061
( C), 148, 1066
, 1050
, 1050
, 477
, 319
, 292
, , 323
, 290
, 262
, 342
, 294
, 327
,
, 325
, 365
, 292
, 1080
, 1050
, 1050
, Perl, 645
, 1081
, 717, 1081
, 129, 1051
, 54, 129
, 397, 400
, 55
, 123
, 1051
, 1051
, 757
, 123,
138, 1051
, 244, 1051
, 245
, 626
, 1051
attributes , 936
locked method,
, 391
locked, , 510
lvalue, , 390
, 367
, 818
, 806
, 392
, , 45
,
, 381
, ,
, 277
, , 60
, Perl , 936
, 82, 1051
, 1066
, 133
, 452, 739
, 831, 874
, 582, 1051
,
, 209
, 412
Perl , 179
, 1046
, 356, 937, 1051
,
375
pod,
937
, 372
, 368
UNIVERSAL, 937
, Perl, 601
($^T), 718
, 156
, 1052
C/C++,
Perl, 938, 939
1113
ExtUtils::Liblist , 938
Perl, CPAN
, 605
Perl,
, 555
C, 590
MakeMaker, 938
, 896
, 54, 123, 124, 1052
, 396
, 1052
, 844
, 63, 84, 150, 151, 160, 299
continue , 154
, 162
, ,
338
, 1052
, 796
, 1052
, 468
, 469
DBM, 473, 757
flock , 774
, 471
, 470
,
, 507
, 729
(>) , 138
(>=) , 138
, 1053
, 107, 1060
bool ,
400
m// () , 191
, 38
, 61
, 62
, 558
, 1053
IPC::Open2 , 967
seek , 841
, 729
, 1052
, 779
,
, 482
, 729, 1053
, 491
Perl,
776
,
971
pipe, 826
exec, 768
, 729, 1053
Perl, 265
, 774
XSUB, 589
, 52
ioctl , 793
, 477
, 482
, 478
, 482
,
, 779
, 116
,
844
, 729
,
eof, 548
, 740
,
, 448
, , 862, 889
,
, 62
, 705
, 35
, Perl, 728
, , 352
$^V, 730
Perl 4 5, 645
, 543
#!, 539
, , 97,
193
, 508
, 1053
(%) , 131
( TCP),
489
1114
, Perl, 524, 526
, 1053
, 751
, 316
, 309
, 316
, 44, 563
, (
), 40
,
, 683
, Perl, 90
, 595
(**) , 129
, 203, 223
, 96, 678
POSIX, 218
, 209, 246
,
, 230
, 95, 808
$/, 543
, , 206, 453
, 86
, 960
File::Temp , 628
POSIX , 970
, 626
, 963
, 879
Time::Local , 931
, 800
/
, 863, 888
(GMT), 789
UDP
, 495
, 851
, 137
Perl , 741
Perl, 582, 591, 938, 1054
Perl C, 594
Perl
C, 592
,
592
Perl, C, 595
, , 372
, 102, 1054
, 1060
, 103
, 640
, 85
, 1054
, 353
, 937
,
, 455
, 272
, 379
, ,
811
, 52, 774
XSUB, 589
, 843
, 563
, 828
ioctl , 793
, 477
STDOUT
, 478
, 482
, 480
,
( ), 573
, 326
, 318, 323
, 327, 328, 331
, 778
,
844
, 729
typeglob, 573
, 1078
, , 280
, 740
, 324
escape , 97
uc ucfirst , 881
, 931
,
( ), 193
, , 100
, 199
1115
,
, 413
, 154
, 264
/, 660
, 1054
, 461
, 265, 1054
, 265, 269, 1054
, , 263
, 126
( ), 573
Perl, 875
, 524
, 59
, 358
Perl, 50, 517, 537
, 281
, 285
, 285
, 285
, 1054
expr list, 151
goto , 166
s///e , 252
, 155
, 1054
, 156
if unless, 162
,
162
Perl, 567, 572
, 34
, ,
811
, 216
, 230
, 397
Perl, 672
Perl, 673
, 675
, 528, 939, 1055
Bytecode , 528
C, 529
, 252
, 47
, , 46
,
, 214
, 192
, 169, 1055
local, , 175
, 798
, 166, 173, 817
, 337
, , 505
vars, 936
strict, 178
, 82
, 162
case,
163
, 101
() , 100, 1055
strict ,
, 178
, 117
(, ), 222, 223
ID, 751
, 780
, 1055
, 780
, 741
, , 779
, 465, 784, 847
, 784
, 796
, 740
, Exporter, 353
, 125
, 182, 201
, 206, 457
, 208, 209
, 1055
dump , 763
Thread::Signal ,
, 514
1116
C, 464
, 637
, 452
, 610
, 619
, ,
635
, 613
, , 618
, 936
,
, 740
, 367, 380, 1068
, 385
, 387
, Perl 5.6, 390
Class::Struct, 384
, 387
, 381
, , 684
, 489, 1055
, 1055
, 744
, pack , 819
, 815
, 61
B::Deparse, 532
, 139
, ,
316
, Unicode, 213
,
, 441
, , 482
()
, 563
,
, 565
,
, 252
, 213
CPAN, 604
( ) , 58
, 58,
1055
, 131
,
, , 611
, 517
, 516,
520
, , 527
,
, 529
, 36, 52, 773, 844,
1055
syslog, , 448
typeglob , 115
, 295
,
, 280
, 438
, 89
, 115
, 290
_TOP, 282
, 117
, 272, 476
,
, 441
, 52
, 295
,
, 474
, 378, 1056, 1072
, 379
, 488
, 556
, , 281
, 982
, , 280
1117
, , 198,
208
(.. ...), 141
, 586
C Perl, 584
, 168,
169, 171, 1056
local,
, 175
, 187, 193
,
(pod), 689, 1056
, 815
,
binmode, 744
, 221, 726
, 797
Unicode, 456, 458
, 650
, 326
, 318, 885
perl, , 21
C, 738
Perl, 686
, 28
, 346
Perl, 25
, 937
, , 568
, 84
Perl, 27
.=, 57, 144
, 830
, 372
, 331
, 326
, 328
, 319
, 324
, 423
, , 505
, 509
, 508
, 510
, 511
, 508
lock, 506
, 367
,
, 61, 135
,
, 61, 135
, 890
ID, , 776
, 477
, , 478
,
Perl , 601
, 92, 1056
, 34
,
Perl, 555
escape,
, 97
, 156
,
467
, 795
, , 463
ucfirst , 199, 458
, 89
, 348
1118
, , 938
, 247
, 247, 1056
, 247, 1056
, , 286
, 1056
, 332
, 350
, 45
,
, 377
,
, 387
, 804
exec
socket , 851
socketpair , 852
, 356
Perl , 349
, 172, 306, 308, 867,
1056, 1057
, 501, 502
, 229
,
, 309
,
, 387
, 308
, 378
glob
, 344
, 263
, 387
, 856
, 193
, 372
, 353
( ),
, 375
, 849
, , 811
, 829, 876, 893
, 282
, 729
, 331
, 332
, ,
331
, 740
, 52
, 89
,
, 100
, 226, 1057
, 228
, , 192
, 17, 609, 863
Win32::FileSecurity , 940
, 1053
, 936
, 610
, 619
, , 618
,
622
, 626
Unix, 623
, 624
, 913
, 629
, 630
,
, 635
, 630
, , 206
,
, 223
,
1063
, 101
,
170
, 1057
, 94
, 36
, 290
,
882
, , 795
, 1057
, , 494
(&&) , 59 140
(&) , 139
, 83, 90, 786, 1057
GID (ID ), 1046
PID (ID ), 465, 815
, 627
, 90
, 101
,
98
Perl, 347
(') , 96
, 338
, 339
( _ )
,
Perl, 645
, 504
, 849
, 846
Unicode, 456
uid gid
, 610
, 619
(), 70
, 457
,
129
Perl, 662
Perl, 60
, 868
, 59, 233
, 88
typeglob, 115
, 277
,
90
, 627
, 780
, 39
,
347
,
, 364
, 47, 1057
, 47
, 367
, 88
1119
, 89
, ,
413
/, 347, 660
,
348
,
170
, 728
, 351
, 339
,
, 265
, 36, 87, 660
, 89, 263
,
264
, 91
, 90
, 786
, , 786
, 731
Perl, 721
Perl, 731
,
787
/ , 784
, ,
Windows, 347
, 779
getgrent , 779
getgrgid , , 780
getgrnam , , 780
getlogin , 782
, , 783
Perl
, 89
, 744
, 340
, 783
, 104, 1057
& () , 813
globbing, 120
,
, 618
, 836
, 120, 682
, 833
1120
eval, 565
, 788
, 811
,
798
, 869
%INC, , 723
,
, 280
, 777
, 47
, 88
Sys::Hostname , 934
,
780
, 781, 783
, 349, 352
, 484, 1057
, 96
, 456
, 206
, 133
, 251
, 347, 350
, 341
, 263
, 886
, 839
, 358, 1054
, 649
, 40, 324, 716
, 85
, 1058
, 155
, 367
,
, 570
, 269
, 114
, 132
, 44, 301, 356, 1058
, 448
, 388
(++) , 58
, 108
, ,
101
, 37, 97, 1058
glob , ,
122
,
, 55, 97, 183
,
, 234
,
260
, 110
, 74
, 426
, 259
, 36
Perl, 50, 524, 526, 1058
, 527
C, 591, 592
#!
(shebang), 538
, 532
, 527
, 537
Unix, 541
, 498
, 379
, 356, 1058
, 454
, 54, 127, 1058
, 61
(xor) ,
139, 147
, 1058
die , 760
, 504
, 267
, 936
, 937
, 613
, 733, 766
, , 503
, 1081
, , 741
, 34
, 1058
, 1058
if
unless, 63
Perl, 703
, 748, 1058
CPAN, Perl, 600
, ,
, 610
,
, 559
, 530
, 529, 601, 675
, 64
for , 66
foreach , 66
until , 64
while , 64
, 403
, 159, 1059
, 37, 311
formlin, 778
, Unix, 541
, 98
, 183
, 108
, 1062
, 98
, 100
, 39
,
573
io::Pipe , 933
,
482
, 441
, 753
, 484, 1057
, 477
, 479
,
, 812
, 1059
1121
, , 457
,
, 213
, 630
DirHandle , 933
io::Dir , 933
opendir , 817
rewinddir , 839
seekdir , 842
, 754
, 748
, , 752
, 61
@inc, 548
, 932
, 802
, 932
,
, 932
, 840, 883
, 1079
, 740
, 832
, 212
, 739
, 70, 71, 182, 201,
219, 650
, 244
, 221
, 209
,
220, 202
(.) ,
, 207
, 336, 355, 1059, 1062
Perl, 208
Thread , 501
, 372
, 337
, , 391
, 394
, 369
base , 371, 936
, 376
, ,
372
1122
, 377
UNIVERSAL , 374
, , 375
, 346
, 127, 358, 877, 886
Class::Struct, 384
, 1071
, 806
,
433
, 427
, 412
, 415
, 740
, 363
, 70, 208, 1059
, 206
, ,
208
, , 204,
209
Unicode,
457
, 208
, ,
457
, 101
,
, 379
, , 228
, 228, 1056
, 1059
TCP, 491
UDP, 495
, 493
, 1059
/ , 1082
, 538, 543
, 543
0 , 539, 543
a , 544, 721
c , 544
d , 544, 557, 578
e , 50, 538, 546
f , 544, 546
h , 546
i switch, 548
i , 539, 546
l , 548
m , 549
n , 550
P , 550
p , 550
S , 551
s , 551
T , 494, 552
t , 610
U , 552
u , 552
V , 553
v , 553
w , 51, 553, 982
X , 554
x , 538, 554
,
, 932
,
, 555
, ,, 42, 383
, 564
, 629
, 630
, ,
635
Safe, 633
, 662
, 455
, 655
, 520, 524, 583
Perl, C, 594
, 729, 1053
pod, 689
, 1053
,
619
, 47
, 1062
, 537
Perl, 542
, 560
, 563
, 567
, 565
, 562
, 564
, 567
,
561
, 568
, 563
, 568
, ,
571
for, 159
, , 330
Perl , 50
, 572
, 932
, 572
, , 207,
457
, 1060
, 687
, 84
, 233
/x, 189
Perl C, 644
, 1060
, 166
, 239
, Perl, 718
, 532
, , 601, 939
, 527
, 723
, 515, 518, 582
Perl ( C), 592
Perl,
516
, 517
, 516
CPAN, 605
, 536
(|), 52
1123
open ,
, 618
, 477, 478
, 222
, 548
(.) , 55, 132
, 57, 132
, 401
, ,
408
, 652
, 1060
, 522
join, 794
, 342
Perl, fcntl.h
, 934
System V IPC, , 934
, 407
, 274
, 294, 355, 364, 1060, 1081
bless , 365
new , 807
, 367
, ,
366
, 367
, 412
, 405
, 365
, , 389
, 105, 1060
, 107
(
) , 108
, 108, 891
, 105
, 349
, 563, 1060
, 885
, 1060
, 563
, 557, 560
, 563
, 563
, 448, 562
Perl, , 545
1124
, 138
, 494
, 932
,
498
, ,
752
(shortcircuit)
, 58
, 288
, 358
, 809, 1061
, 361, 1061
, 361
, 361
, 332
, ,
613
, 93
, 863
, 313, 485, 603
, 217
, , 447
, , 392
, 371
, 430, 663
stat, 715
, 653, 780, 933
, 307
, 168
our , 817
, , 92
, 268
, 91
, 177
, 895
, 519
, 93
, 583, 1061
Perl, 519
, 1061
, 169, 283,
804, 1061
B::Showlex, 940
, 268
, 526
, , 505
,
, 377
, 311
, 268
, 1079
, 177, 937
${^WARNING_BITS}, 735
$^w , 553
, 1061
(sticky) , 654
, 565
, 180, 1061
UTF8, ,
456
, 116
, 94
, 94
, 444
, 58, 147, 1062
(!) , 129
or (||) ,
, 167
(&&) , 140
(||) , 233, 140
, 401
, 362
, 1062
, 128, 142,
402, 419
, , 71
, 201, 220
, 723
, 36, 39, 85, 87, 101,
1062, 1063
@_ , 265
my, 265
AV ( )
C, , 583
exists, 770
pop , 826
s/// () , 196
, ,
, 382
use fields ,
, 382
, , 292
, 757
, 316
,
, 316
, 830, 885
/ , 856
, 88
x, 132
, 612
, 44, 316
, , 740
,
, 647
, 716
, 269
, 101
, 653
, 931
, 115
, 112
, delete , 647
, 421
Tie::Array , 937
, 422
, 426
, 108
1125
, 290
,
, 573
, 848, 856
, 325
, 325
, 326
, 323
,
, 799
, 1060
( C), 590
,
, 403
, 54, 123
, 461
(<) , 138
(<=) , 138
, 181, 200, 1062
, 199, 203, 206
, 202
, 181, 654, 1062
exec, 768
, 199
, , 812
quotemeta, 830
(\), 200
, 182
, 151, 159, 1062
goto , 165, 790, 996
, 89
, 338
, 154, 159, 660, 1013, 1059
, 301, 355, 1062
Thread , 501
UNIVERSAL , ,
376
, 376
( ), 510
, 413
, 358
, 366
, 359
,
361
, 359
1126
, 393
, 421
,
433
, 427
, 373
,
, 377
, 372
, 661
UNIVERSAL, 374
Perl, 380
,
l, 390
, 359
, 358
, 370
@ISA, 370
, 308
, 422
, 415
, 428
, 1056
, 393
, 385
, 383
, 380
, 387
, 355
, 358
, 365
, 355
, 358
, 72, 201, 220
() , 129
, 685
Perl , 933
,
Unicode, 451
, , 270
, 39
, 369, 1063
SUPER , , 373
,
, 375
AUTOLOAD , 377
, 183
, 150
, 188
m// () , 191
s/// () , 194
tr/// () ,
198
, 229
, , 863, 888
, 16, 45, 336, 344, 346
CPAN, 46, 601
, 604
, 601
, 605
, 604
,
683
, 684
, 606
, 336
Perl, 605
, 500
, 337
, ,
353
, 938
CPAN, 447
, 660
, 938
,
, 377
, 512
, 352
, 90
,
345
, 528
, 371
, 349
Exporter, 349
, 352
, 930
DBM,
, 935
Perl
, 939
, 937
, 936
, 933
, , 934
,
IPC, 933
,
932
, 937, 938, 939
,
, 935
, 937
Perl, 936
, 934
, , 931
, 931
,
, 932
, 586, 590, 607
, 740
, 792
,
, 471
, 497
, 622
, 499
, 55, 131
, , 405
ASCII, 685
Unicode, 451
, 685
, 198
1127
, 356
, 356, 1063
,
, 936
, 337, 369
, 369
, 365
,
393
, Exporter, 350
, 390
, 409
, 384
@ISA, 370
, 569
, 570
, 571
, 569
, 577
locale
POSIX, 218
Unicode , 797
, 281
Perl , 209
, 933
, ,
138
, 588
, 726
, , 723
, , 222
(
), 241
( ) , 223
(!=) , 138
( ), 457
, 552
,
38
,
, 574
, ,
91
,
, 613
,
, 344
, 94
, 138
1128
() , 204
, 41
, 290
, 359
, , 97
, 286
,
, 787
, 785
, 722
, 104, 1063
_ _LINE_ _ , 674
__line__ , 104
, 674, 724
, 1064
, 110, 113
, 181, 201
, 183
, 715
, , 546
, 1059
, 477
, 812
, 567
, 477
|, 480
, 635
, 554, 916
, 554
, 618
/, , 52
,
620
, 571
, 843,893
, 538
, 282
, Perl , 932
, 1064
, 396, 397
as_string, 400
, 406
, 463
, 339
(swatches), 217
, 867, 1065
Pod::Parser, 699
XML::Parser, 670
,
, 227
, 1065
print printf (
), 433
, , 863, 888
, 749
, , 61
, 44, 355, 1079
, 933
, 385
,
, 387
, Perl 5.6, 390
Class::Struct, 384
, 387
, 380, 382
, 378
, 379
, 336
, 391
, 294, 364
, 367
, 365
, 1061
, 301
, 510
, 501
, 308
,
412
, 369
@ISA, 370
, 746
, 305
, 379
, 127
, 446
Perl, 357
, , 38
, 289, 357
(), 556
, 740
, 336, 355
, 358
, 346, 933
,
932
Perl, , 337
, 149, 1065
our, 817
package, 824
structs, 384
, 166
, 173
, ,
l, 390
, 45, 169, 337
, 149, 169
1129
1130
,
, 400
( ), 400
, 56, 646
, 150
,
, 38
, 151
, 161
, 60, 138
, 138
, 55
, 642, 546
argv , 717
glob
, 121
for, 159
, 153
C, Perl, 148
, , 105
, 13, 1066
#! ,
Unix, 540
, 500
#!,
538
,
, 347
Perl, , 50
, 537
, , 934
Perl , 601
, 467
GUI, 682
, , 678
Perl, 600,
677
flock , 470
fork, 477
,
464
, , 490
, 1066
(
), 346
, 264
, 168
AUTOLOAD, 344
, 167
( C), 583
Perl, 519
, 648
, 188
, 313
($^T), 718
, , 932
, 680, 821, 1066
, 823
, 851
, 871
, 544, 557, 612
DB , 936
Devel::Peek
XS, 936
Perl, ddebug
ging C, 370
, 573
, 441
, , 556
C, 643
, 644
Perl 4, 645
,
, 555
Perl, 560
, 320
, 528
, 569
, 410
Perl, 575
, 1074
, 28
, 560
,
, 393
, 719
, 639
, ,
383
, 129
, () , 129
, (!) , 129, 401
, 72, 208, 209
POSIX, 218
, (~), 129
, 85,
647
,
, 244, 248
,
, 224
, , 280
, 1067
, 315
Thread::Queue , 512
, 729, 1067
, 431
, 774
$@ , 720
$? , 718
$! , 728
Carp , 943
CGI::Carp , , 944
warn ,
, 892
Perl, , 28, 983
, 267
httpd
CGI, 935
, 613
, 352
, 727
(errno), 1063
1131
, 877
, 938
, 886
, 340
, 1078
, 363
, 91, 92, 169
, , 506
, 1067
ID
, 849
,
556
, 727
, 313
, System V
IPC, 485
, 486
, , 378
, 654
, 717
, ,
266
, 265
, ,
38
, 863
crypt , 755
passwd , 785
, 851
, Perl
, 601
, 241
, 241
, 249
lc lcfirst, 797
, 33, 83, 678
,
, 619
, 97
,
, 65
, ,
, 188
(.) , ,
207
,
, 261
1132
, 125, 395, 1067
nomethod fallback (
), 406
overload , 396, 931
Overloaded ( overload),
409
, 410
, 407
(=), 405
, 403
, 410
, 409
, 397
,
Perl, 106
, 398
, 439
, 401
, 403
, 409
, ,
405
, 1054
@_
my, 265
, 265, 1054
, 290
, 269
,
, 625
( ), 563
, 85, 87, 126, 288, 297
$[, , 716
B::Xref ,
C, 531
local, 643
my, 305
our, 337
Perl C, , 644
, 291
, ,
936
V ,
341
, 1055
, 804
, , 57
, 36, 660
,
91
, 269
, 37
, 391
, ,
448
,
804
, 798
, 612
, , 350
, 38
, 169, 149
, 173
, 171
, 554, 1068, 719
Env , 936
PERLDB_OPTS,
, 574
perl_destruct_level, 379
Perl, 645
Perl , 556
, 682
, 611
, , 570
, 91, 337
, 564
, 573
, 817
, 393
, 392
, 511
,
, 196
,
, 631
,
Perl, 644
,
English, 936
, 838
, 411, 877
, 444
, 421
, 886
, 413
, 433
, 427
, 114
, 714
, 36
( C/C++), 268
,
, 235
, 114
, 114
, 199
, foreach, 156
, 511
,
strict, 47
, 284
, 282
, 453
, 287, 931
gethostent , 782
, 656
Perl, 677
, 462
, 682
, 681
, , 742
, , 948
, 270
, 875
, 481
, 766
, 727
, 464
, 463
, 550
, 378
,
, 500
(x) , 55, 132
,
, 573
, 662
,
, 198
, 722
, ,
601
1133
, 356
, ,
381
, 37, 48, 262, 358
B::Xref ,
C, 531
do SUBROUTINE , 763
Perl, C, 593
, 344, 376, 938
, 262
, 294
, 717
, ,
936
, 509
locked , 510
, 309
, 584
, 683
, 294
, 265, 838
l, 390
, 267
, 366
, 561
, 263
, 1054
, , 264
goto, 790
, 88, 89, 263
,
/, 532
, 263
, 363
, 565
, 342
, 359
, 167, 867
AUTOLOAD, 377
, 264
,
, 168
, , 267
, 269
, 275
goto, 166
exit, 771
, 650
1134
, , 645
, 271
, 276
, , 370
,
804
, , 265
, 341
, 38
, 746
, 162
,
89
, 254
Perl, 644
, 108
,
, 275
, 274
, 1069
s///,
193
, 249
,
255, 1069
(slurping) , 543, 1048
, 222
substr ,
, 222
, 458
, 223
, 222
, 242
grep , 791
, , 194
, ,
648
, 723
C,
, 206, 457
(??), 838
, 244
(m//) , 801
, 1069
, , 682
, 120,
932
, 193, 792, 839
, 92
, 68
, 551
,
, 935
, Perl ,
603
CPU, 880
, 1055
, 741
,
610
, 356, 1069
, 244, 248
, 139, 147
(&) ,
, 971
(~) , 129
, 971
, 401
, 133,
144, 1069
, 1069
/, 786
Perl,
, 600
, 243
,
,
, 463
Perl, 591
,
tie, 447
, 58, 124
, 497, 800
locked method , 391
, 800
,
, 527
, 499
, 505
,
, 512
, 498
, 498
Perl API , 527
, 561
Perl, 706
(rvalues), 86
, 872, 881
,
, 136
,
, 610
, 47, 168, 176, 336, 549, 886, 900
charnames, 96
, ,
90
, 348
,
177
Perl, 674
, 377, 936
,
653
, 553, 892, 982, 1070
${^WARNING_BITS}, 735
w , , 51
, 937
, 149
, 446
, 554
, 732
, 936
, 399
, 948
, 439
, 318, 320
, 334
,
808
, 789, 800
,
, 860
ASCII , 752
Unicode , 752
UTF8, 458
, 675
ASCII, 817
, 794,
819
1135
, 857, 884
C Perl, 595
/ ,
195, 791
( C), 550, 1070
Perl, 1070
, ,
463
, 54, 124
, 58
, 200
, 94, 1070
( C), 148
, 73, 313, 1070
(=~, !~), 69, 184
, 411, 877
DBM , 756
, 126, 737, 1070
defined, 124
my , 170
, 982
,
, 278
(and) &&, (or)
||, 141
Perl,
646
, 59, 147,
362
, 170
, 22
, 241
, 784, 847
, , 124
(
), 133
?, 144
, 56, 86, 144, 1070
, 57
, 402, 405
, 418
, 111
, 317
, 423
, @inc,
548
1136
, 70, 83, 658, 737,
1071
/x ,
189
, 282
,
, 103
, ,
646
, 196
, ,
99
( ),
, 637
,
, 389
, 494, 613,
552, 609, 610, 611, 1062
d , 554
File::Glob , 958
PERL5LIB , 555
T, 611
, 633
, 637
, 964
CGI , 633
, , 610
, 610
,
, 741
, 258, 918
, 60, 625, 135, 1066
, 262
, 875, 1054
, 34, 771
, 763
, 1065
, 561
, 49
, 560
, Perl, 639, 662
, 672
Perl, 673
, 675
, 639
, 677
,
, 681
, 251, 658
, 648
, 1071
(),
strict, 47
, 16
, 880
, 578
, 356, 1071
, 372
, 1079
, 88, 336, 1071
, 45
,
Safe, 630
, , 448
, 349
()
Perl,
645
, 642
, /, 784
, 271, 1071
& (),
, 271
, ,
272
, 309
, 290
,
276
, 274
, Perl, 578
Devel::DProf, 936
, 47
, 251
fork , 776
STDIN, STDOUT STDERR
, 52
umask , 881
, 795
, 466
(PID), 815
,
461, 469
System V IPC, 485
, 461
, 462
, 489
,
CPU, 622
, 890
, 465
, 720
, 847
, 498
, , 784
, , 847
, 465
, 740
, , 466
, , 479
, 116
, 1071
for/foreach, 157
, 196
, 568
, 91, 116
, 205
typeglob, 341
, , 399
, 302
use fields ,
, 383
overload, 404
, 806
, 384
, ,
65
, 138
, 411,
1072
, 290, 297, 298, 299,
1072
1137
typeglob, 340
(*), C , 148
, 330
, 299, 404
, 318
, 498
, ,
544, 1069
,
split, 68
, 112
, 740
, 811
CPAN, 604
Perl, 584
XSUB XS,
Perl, 585
XSUB, 589
C, 590
, 586, 936,
1072
, 788
, 112
, Perl ,
601
, API
, , 454
, 755
1138
(Microsoft Windows)
.pl , 541
, 448
, 941
( a), 544, 1050
, , 810
,
/o, 189
, 259
, 526
, 267
, 448
, 447, 448
, 356
, 776
, , 401
, 406
,
221, 1073
, 127, 300, 771, 1073
typeglob, 296, 435
, 809, 962
, 313
, 1073
,
, 418
DESTROY, 379
CPAN, 447
(=~, !~), 130
, 411
, 444
, 421
, 886
, 413
, 433
, 427
, 412
, 601, 1073
, Perl , 52, 1073
(<<) , 133
(>>) , 133
, ,
886
, 1074
IPC::Semaphore , 934
System V IPC, 485
Thread::Semaphore , 513
Win32::Semaphore , 941
, 472
, 486
, 845
, 492
TCP, 491
, 493
, 527
, 528
, 940
,
, 611
, CPAN ,
490
Perl,
936
,
, 741
, 491
, 490, 934
, 492
, ,
780, 783
Win32, 940
, 1047
/, 1076
, 461, 462
%SIG , 462, 732
Perl C, 644
Thread::Signal , 514
, 468
,
C, 464
, 467
,
795
, 463
, 465
, 682
sigtrap,
464, 934
, , 500
, 466
, 82, 246, 1074
, 452, 739
, 272
, 820
, 180
, 779
, 95
(^), 139
, 206
, 213
, 797
, 197
, 181
, , 54, 749
ASCII, 817
ASCII, 752
Unicode, 752
8
UTF8, 453
, 205
, 83
, 411, 1072
, 881
, 216, 1073
, 216
, 749
, 95
, 454
, 455
, 1074
, 289, 310, 801,
869, 1074
lstat , 801
Unix, 623
, 833
, 178
strict,
178
, 1074
Perl, 86
, 544
, 583
Perl, 519
, 183
1139
, 1075
, 506
, , 622
, 626
Unix, 623
, , 624
, 869, 1075
Perl, C , 644
, 467
, 467
, 654
, 417, 470
, 467
, 624
Unix , 623
, 36, 37, 85, 1075
Perl, C, 594
SV ( ) C, 583
, 290
, 94
v , 104
, 102
,
, 101
, 125
, 95
, , 62
, 342
, 415
, 41
,
, 186
,
194
, 120
, 87
, 413
,
, 937
, 419
$_, 420
, , 797
,
, 44
, , 264
, , 43
1140
, 39
, 38
, 739
, 105, 1060
/g ,
, 192
l , 170
m// () , 191
, 107, 1053
, 151
(..) , 141
, 146, 647
, 840
, ,
642
, ,
114
,
, 265
, 106
, 112, 146
, 108
, 143
, 738
( ), 43
, 70
, 43
, 578
, 313
, 1075
, 192
,
, 935
() , 70, 204
, 457
, 55
(+), 397
, 44
Perl, 315
, 831, 863
, 863
, 444
, 1075
read , 831
seek , 841
m//, 827
, 723
, ,
213
, System
V IPC
, 486
, 849
, 1076
, 850
listen , 798
TCP , 489
Unix, 489
, 753, 850
, 850
, 934
,
, 740
socket, 851
, 846
, , 678
, 834
, 783
, 489
, 495
, 491
, 742
, 744
, 462
, 754
, 848
, 490, 787
IPC::Msg , 934
System V IPC, 485
, 554
, 572
, 662, 982
,
, 657
, 846
UDP, 495
, 834
, 803
, 794
, , 324
, 403
, 852
, 329
,
1076
Unicode, 211
, 394
, 391
, 334
, 714
, 39, 75, 106, 108, 109, 151, 1076
map , 801
, 736
, 316
, 839
, 110
, 110, 1064
, , 740
, 43
chop, 750
, 111
x,
132
, ,
, , 264
, 111
, 852
, 108
, 505
, 736
,
, 322
, 124, 126, 147,
1076
, , 134
, 74, 105, 1060,
1076
foreach , , 66
l , 170
m// () , 191
m//g,
, 192
, 151
,
564
(..) , 141
, 146
,
840
, 118
1141
,
, 305
, , 642
,
, 114
,
, 265
, 146
, 143
, 738
, 42
, 939
Thread, 505
, 138, 640
, 932
, 60, 138
sort , 852
, 403
, 88
local , 170, 176
x, 132
, 97, 727
, 111, 142
, 641
, 128
, 319
, 105
, 304
, 300
, , 911
, 38, 288, 833, 1076
HTTP, , 69
Perl, C, 148
, 507
, 316
, 340
, 69
, 127, 1056
, 62
, 93
, 288
, 291
, 295
, 342
(
), 462
1142
, 313
, , 405
Unicode, 212
/,
52
, 586
, 171
( C/C++), 268
, 718
, 41, 315
jumpenv, 526
Perl, C, 595
, 526
T, 559
, 86
Perl, 525
, 816
, 525
(mark stack), 526
, 525
, 525
, ,
746
, 658
, 21, 1078
Pod::Man , 937
doc CPAN, 600
(>) , 127
, 299
, 36, 88, 281
eval , 649
index , 792
rindex , 839
study , 865
vec , 889
, 729
Perl, 647
,
55
, 96
, 828
, 306
, 808
, 808
, 92
, 39
,
/e, 254
, 62
, 868
, 132, 652
join, 794
, 106
, 195
,
, 573
1143
, 55, 640
, , 142
, , 139
, 53
, 716
, 797
(s///
), 193
, 884
, 94
0x, 0b 0 ,
, 95
, 305
, 305, 312
, 160, 722, 1071
, 722
, 857
,
, 188
, 794
,
, 819
, 60, 138
, 312
, 222
, 860
, 777
, 808
, 1060
v ( ), 104
, 97
, 1078
, 102
, , 823
, 315, 1078
, 365
, 331
, 325
, 327
, 355
Perl, 322
, Perl, 288
,
tie, 447
, 334
,
, 44
,
, ,
379
, 334
, 290
, 323
, 330
, 182
, 200
, 1078
, 35
, , 46
, , 465
CGI,
, 611
, 552
, 875
, 137
, 554
exec, 768
, 731
,
Perl, 645
Perl, 662
e, 546
, 104
, 538
, 546
, 629
, 544
, 551
, 538
, 544
, 648
Perl,
50
, 851
, 658
, 939
, 639
, 753
,
, 419
, 1079
, 316
1144
, 88, 91, 340
, 90
, 339
, 116
, 340
local, 176
,
, 337
Symbol , 931
, 573
, 290
, 778
, 825
, 296
HTML XML,
, 261
, 88
Unicode
, 451
pod,
ASCII, 937
, 88
Perl , 931
, Perl, 179
, 287
,
Perl, 83
, , 50
(t
), 61, 137
GID ($EFFECTIVE_GROUP_ID),
719, 731
UID ($EFFECTIVE_USER_ID), 719
, 1078
, 27
, 786
, 86, 1079
,
, 123
, 126
/
, 574
, 1079
, 54, 124
?: , 142, 164
, 397
, 607, 608
, p, FIFO,
484
, 36, 1079
typeglob, 115
, Perl C, 583
, 85
, 931
Perl, 805
C Perl, 595
, 94
, , 94
, 94
, 222
, 184, 880
, 894
, 675
/ , 786
, 559, 563
(), , 96
Math::Trig,
931
, 1079
, 680, 1079
pack, 819
vec, 889
, 466
, 494
, 117
,
435
, 566
, 840, 883
, 563
,
, 198
, 720
, 749
, 883
, 826, 848, 856
, 759
, 1061
, 85
C, 288
, 933, 1079
, 821
,
, 937
,
, 95
, 1080
open , 808
SelectSaver , 933
, 471
, 843
, , 812
, , 814
,
933
, 753
, 799
,
625
_TOP, 723
IPC, 473
, 474
, 841
, 433
, 434
, 740
, 831
, 113
, 55
, , 55
, 54, 123, 124
, 58
, 61
, 642
, 129
, 122, 126, 133
, 396
, 126
,
276
,
, 134
detach , 504
join , 502
, 783
1145
, , 740
, 156
, 96, 193
, 206
ASCII Macintosh, 542
, 61
, , 62
, 571
, 1080
, 113
, 573
, 792
, , 913
, 59
, 107
, 153
(?:) , 142, 164
, 155
, 152
, 260
CPAN, 604
Perl, 605
, , 938
, 557
Unix, 623
(), 1080
/,
, 574
, 192
,
, 231
( ), 181, 201,
1080
, 247
, 209
, 222
, 261
, 192, 222, 223
, , 244
, , 941
, 137
, 578
1146
, 1080
do FILE , 762
open , 808
Win32API::File , 941
, , 751
, 877
, 960
, 863, 888
, 137
, 552
, 744
, 52, 474
,
619
, 753
, 876
, 836
, 1058
(EOF), 764, 1060
, 469
, 469
, 473
, , 345
Perl,
, 336
, 932
, 172
, 60
, 682
, 871
, ,
767
, 93
, 863
, 748, 872, 881
, 548
, 815
, 869
,
, 681
, 883
, 880
, 740
, 874
, 1080
, 681
, 517
, 518
, 583, 1080
, 1058, 1081
, 42, 63
8 ,
453
, 1081
, 76, 675
, 480
exec, 629, 734, 743
fcntl , 475, 772, 815
, 188, 543, 1081
, 717, 1081
, 280, 1081
B::Xref ,
C, 531
pack/unpack, 819
, 778
, 283
, 286
, 286
, 722
, 280, 777
, 282
, 284
, 820
,
, 646
, 860
, 822
pipe,
, 483
, 47, 262, 736, 1081
Perl,
, 678
Perl, C,
585
return , 159
Thread , 501
, 344, 376
,
, 642
, 455
C, 738
, 738
,
741
,
, 937
, 307
, 353
, 661
, ,
627
, 426
, , 350
, 577
, 463
, 307
, 123
, 133
, 262
, 409
, , 570
, 490
, 739
, 270
, 330
, 308
, 1081
, 36, 39, 41, 85, 113, 1082
exists , 770
HV ( )
C, 583
%sig,
, 462
,
365, 380
, 292
, 1052
, 157
, 648
, 888
, 88
1147
, 839
x, 132
,
, 165
, 42, 85, 1059
each,
740, 764
,
794
, 322
, 115
, 937
( %inc),
347
, 340
, 427
({ }),
, 42
, 325
, 325
, 323
, 323
, 324
, 428
, 612
, 327
, 327
, 328
, , 740
Perl, 322
/, 42, 85, 113
each,
764
m//g, 192
, 270
,
, 315
DBM , 756
,
, 390
, 412, 427
Tie::Hash ,
,
937
, 332
, 312
, 290
1148
,
, 573
, 340
typeglob , 340
/, 340
, 759
,
, 799
, 935, 1082
DBM, 447
,
, 743
, 319
, 196
, 1058
,
, 233
, 159, 1059
, ,
161
() , 155
, 1059
, , 225
, 313
, , 405
, 379
, 363
, 70
, 90
, 182, 1050
, 230
, 209
,
243
(...|...), 201
, 36
,
281
, 95, 808
, 39
, , 285
, 62
, 558
, 94, 822, 1068
Math::BigFloat , 931
rand , , 831
, ,
680
, 95
, ,
139
, 400
, 831, 863
, 60
, , 94
Perl, 94
int, 792
, 739
, 1082
, 90
() , 70
, 95, 195, 791
, 680
, 95
, 106, 1060
, 874
, 849
, , 811
( ),
68, 179, 801
grep , 791
re ,
, 936
split , 185, 857
study , 865
a, 546
,
203
,
255
, 254
, 252
, 252
, 71, 244
,
184
, 225
Perl,
646
, 259
, 98
, 182, 201, 219,
244
, 70, 208
,
101
, 228
Perl , 252
1149
, 199, 206
, 72
, 188
, 229
, , 565
, 222
, 453
, 680
, 1082
, 755
Perl , 936
, , , 214
1150
, 355, 1082
, 95
, 359
, 34
, 214
, 83
, 675
Books.Ru
ISBN 5932860200,
Perl, 3
Books.Ru .
,
. ,
(piracy@symbol.ru),
.