Вы находитесь на странице: 1из 386

PerI: cneuuanuum cnpanouuuk

Hepeeeu c aue.ucroeo A. Bepouuroe


Inannt peaxrop B. Vc+auoe
3aneymmn peaxnne E. Cmpoeauoea
Pyxononrent npoexra A. Hace:uur
Hayunt peaxrop H. Auorau
Hnreparypnt peaxrop C. Peeumeuro
Xyoxnnx H. Bupraroe
Koppexrop B. Hucmoea
Bepcrxa H. Bicmpoe
EEK32.973.2-018.1n22
YK 681.3.06(03)
Xonsuep C.
X71 Perl: cneuuanuuu cnpaeouuk CR: Rufep, 2001. 496 c.: un.
SBN 5-8046-0198-9
Hopont cnpanounnx no xstxy Perl, coepxamn onncanne cnnraxcnca, ononnennoe ocnonntmn cneennxmn o
ncnontsonannn xstxa (nneennem n npemer). Coepxnr cornn npnmepon n rorontx pemenn. Cnpanounax nn]opmannx or-
nocnrcx ne rontxo x cnnraxcncy xstxa, no n x pemennm ncenosmoxntx saau, nosnnxammnx y nporpammncron na Perl, or
camtx npocrtx o camtx cnoxntx.

Original English language Edition Copyright 1999 The Coriolis Group
Repeeop ua pyccku nsuk, A. Eeppuukoe, 2000
Cepun, oqoprneuue, Hspafenucku por Rufep, 2001
Rpaea ua uspauue nonyeuu no cornaueuuk c The Coriolis Group, nc.
Bce npaea saueuu. Hukakan acfu pauuo kuuru ue roef ufu eocnpouseepeua e kako u fo uu uno qopre es nucureuuoro paspeueuun
enapenuuee aefopckux npae.
Huqoprauun, copepaancn e pauuo kuure, nonyeua us ucfouukoe, paccrafpueaerux uspafenucfeor kak uapeuue. Ter ue reuee, uren e
eupy eosrouue enoeeeckue unu fexuueckue ouuku, uspafenucfeo ue roef rapaufupoeafu aconkfuyk fouocfu u nonuofy npueopurux
ceepeuu u ue uecef ofeefcfeeuuocfu sa eosrouue ouuku, censauuue c ucnonusoeauuer kuuru.
SBN 5-8046-0198-9
SBN 1-57610-426-5 (aurn.)
3AO Hn1ep Byx. 196105, Canx1-He1epypr, Bnaroa1nm yn., . 67. Annensnn H N 01940 o1 05.06.00.
Honncano n neua1t 06.12.00. mopua1 70x 100
1
/
16
. Ycn. n. n. 39,99. on. 1npaa 5000 +xs. 3axas N 2468.
Hanoronan ntro1a-omepoccnncxnn xnaccnqnxa1op npoyxnnn OK 005-93, 1ou 2; 953000 -xnnrn n pommpm.
O1neua1ano c qo1oqopu n IHH Heua1nmn nop Mnnnc1epc1na Pm no enau neua1n,
1enepanonemannn n cpec1n uacconmx xouuynnxannn.
197110, Canx1-He1epypr, Hxanoncxnn np., 15.
Crp.2
Kpa1koe copepxauue
Beepeuue ................................................................................................................. 14
-acfu . Cuufakcuc Perl
Fnaea 1. Ocuoeu Perl ............................................................................................. 20
Fnaea 2. Ckannpuue nepereuuue u cnucku........................................................... 38
Fnaea 3. Maccueu, xsuu u sanucu fanuuu cureonoe ......................................... 56
Fnaea 4. Onepafopu u npuopufefu onepafopoe.................................................... 73
Fnaea 5. Ycnoeuue onepafopu u uuknu................................................................. 90
Fnaea 6. Perynnpuue eupaeuun .......................................................................... 106
Fnaea 7. Ropnporparru ......................................................................................... 135
Fnaea 8. Ccunku e Perl ............................................................................................ 151
-acfu . Bcfpoeuuue pecypcu
Fnaea 9. Bcfpoeuuue nepereuuue......................................................................... 163
Fnaea 10. Bcfpoeuuue qyukuuu: opaofka pauuux ............................................ 178
Fnaea 11. Bcfpoeuuue qyukuuu: eeop/eueop
u renpoueccuue esauropecfeun ............................................................. 199
Fnaea 12. Bcfpoeuuue qyukuuu: paofa c qanaru............................................ 218
-acfu . Rporparrupoeauue ua Perl
Fnaea 13. Cfaupapfuue ropynu............................................................................. 235
Fnaea 14. Cfpykfypu pauuux ................................................................................. 247
Fnaea 15. Cospauue nakefoe u ropyne................................................................ 260
Fnaea 16. Cospauue knaccoe u oLekfoe............................................................... 282
Fnaea 17. Ofnapka cueuapuee Perl.
Pykoeopcfeo no cfunk nporparrupoeauun......................................................... 318
-acfu V. Cospauue cueuapuee CG
Fnaea 18. CG-nporparrupoeauue ......................................................................... 329
Fnaea 19. CG-nporparrupoeauue c ucnonusoeauuer cgi-lib.pl ........................... 343
Fnaea 20. CG: cefuku noceeuu, rocfeeue kuuru,
ofnpaeka snekfpouuo nofu u eonpocu sauufu cucferu......................... 354
Fnaea 21. CG: ruorononusoeafenuckue afu,
feueeue nocunku (cookies) u urpu................................................................ 366
PERL. Kpafkan cnpaeka........................................................................................... 378
Crp.3
Copepxauue
OB ABTOPE........................................................................................................................................................................ 14
BRAFOAPHOCTH............................................................................................................................................................. 14
BBEEHHE......................................................................................................................................................................... 14
-TO ECTL B STOH KHHFE ..................................................................................................................................................15
-TO EE BAM ROTPEEYETCH .............................................................................................................................................17
PYFHE PECYPCL...........................................................................................................................................................17
Om usamenicmea...................................................................................................................................................19
HACTL I. CHHTAKCHC PERL .......................................................................................................................................... 20
FRABA 1. OCHOBL PERL.................................................................................................................................................. 20
KOPOTKO ............................................................................................................................................................................ 20
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 21
KAK CKORHPOBATL H YCTAHOBHTL PERL ...............................................................................................................................21
KAK HARHCATL CUEHAPHH PH PERL....................................................................................................................................22
YEEHTECL, -TO CUEHAPHH CMOXET HAHTH PERL...................................................................................................................22
UNIX......................................................................................................................................................................23
MS-DOS .................................................................................................................................................................24
Windows 95/98 u Windows NT......................................................................................................................................25
KAK HARHCATL RPOFPAMMY PERL: KOMAHL H ORHCAHHH ...........................................................................................................25
BLROPHEHHE CUEHAPHEB PERL..........................................................................................................................................26
Kak eau cueuapu uoem uamu Perl cau .....................................................................................................................26
Kak ucnonisoeami kouauuyk cmpoky ..........................................................................................................................26
HHTEPAKTHBHOE BLROPHEHHE CUEHAPHEB PERL .....................................................................................................................28
KPK-H KOMAHHOH CTPOKH ..............................................................................................................................................29
KPK- -W H RPOBEPKA CHHTAKCHCA ......................................................................................................................................31
TEKCTOBLH BBO H BLBO C ROMOLK CTAHAPTHLX ECKPHRTOPOB 4AHPOB................................................................................32
BLBO TEKCTA ..............................................................................................................................................................32
RE-ATL HOMEPA TEKYEH CTPO-KH CUEHAPHH H HMEHH CUEHAPHH.............................................................................................32
ROBTOP TEKCTA RPH RE-ATH.............................................................................................................................................33
OCHOBHLE KOMAHL 4OPMATHPOBAHHH .............................................................................................................................33
BLBO HE4OPMATHPOBAHHOFO TEKCTA: BCTPOEHHLE OKYMEHTL ...............................................................................................34
KOMMEHTAPHH ..............................................................................................................................................................34
-TEHHE ROTOKA BBOA ....................................................................................................................................................35
CREUHAPLHAH REPEMEHHAH $_..........................................................................................................................................35
O-HCTKA BBEEHHOFO TEKCTA ...........................................................................................................................................36
KAK H3EEXATL HEMEPEHHOFO 3AKPLTHH OKHA MS-DOS B WNDOWS 95/98/NT .............................................................................36
FRABA 2 .CKARRPHLE REPEMEHHLE H CRHCKH........................................................................................................ 38
KOPOTKO ........................................................................................................................................................................... 38
CKAPHPHLE REPEMEHHLE................................................................................................................................................38
CRHCKH ......................................................................................................................................................................38
HEROCPECTBEHHLE PELEHHR...................................................................................................................................... 39
-TO TAKOE CKAPHPHAH REPEMEHHAH? .................................................................................................................................39
HMEHA CKAPHPHLX REPEMEHHLX.......................................................................................................................................39
RPHCBOEHHE CKAPHPHLX REPEMEHHLX...............................................................................................................................40
-TO TAKOE PEBOE 3HA-EHHE?........................................................................................................................................41
HCROPL3OBAHHE -HCEP B CKAPHPHLX REPEMEHHLX ................................................................................................................41
PAEOTA C HEORPEEPEHHLMH AHHLMH: HEHTH4HKATOP UNDEF ................................................................................................41
ORHCAHHE KOHCTAHT......................................................................................................................................................42
PAEOTA C POFH-ECKHMH AHHLMH B PERL.............................................................................................................................42
RPEOEPA3OBAHHE BOCLMEPH-HLX, ECHTH-HLX H LECTHAUATEPH-HLX -HCEP .............................................................................43
Hpeopasoeauue uecmuauamepuvuoao vucna e ecnmuvuoe ...........................................................................................43
Hpeopasoeauue ecnmuvuoao vucna e uecmuauamepuvuoe ...........................................................................................43
Hpeopasoeauue eociuepuvuoao vucna e ecnmuvuoe .....................................................................................................43
Hpeopasoeauue ecnmuvuoao vucna e eociuepuvuoe .....................................................................................................44
Crp.4
OKPYFPEHHE -HCEP ........................................................................................................................................................44
HCROPL3OBAHHE CTPOK B CKAPHPHLX REPEMEHHLX.................................................................................................................44
Cuueon uoeo cmpoku e mekcmoeix cmpokax ................................................................................................................46
ROCTAHOBKA REPEMEHHLX (HHTEPROPHUHH CTPOK) ...............................................................................................................46
CPOXHLE CPY-AH HHTEPROPHUHH......................................................................................................................................47
OEPAEOTKA KABL-EK H CPOB EE3 KABL-EK.............................................................................................................................48
-TO TAKOE CRHCOK? .......................................................................................................................................................50
CCLPKA HA SPEMEHTL CRHCKA -EPE3 HHEKC ........................................................................................................................50
RPHCBAHBAHHE CRHCKOB CRHCKAM .....................................................................................................................................51
RPEOEPA3OBAHHE CRHCKA................................................................................................................................................52
OEIEHHEHHE SPEMEHTOB CRHCKA B CTPOKY .........................................................................................................................52
RPEBPAEHHE CTPOKH B CRHCOK........................................................................................................................................52
COPTHPOBKA CRHCKOB ....................................................................................................................................................53
HHBEPTHPOBAHHE CRHCKA................................................................................................................................................53
BLEOP SPEMEHTOB H3 CRHCKA...........................................................................................................................................54
CKAPHPHLH H CRHCKOBLH KOHTEKCTL.................................................................................................................................54
4OPCHPOBAHHE CKAPHPHOFO KOHTEKCTA ..............................................................................................................................55
FRABA 3. MACCHBL, XDLH H 3ARHCH TABRHUL CHMBOROB........................................................................................ 56
KOPOTKO ............................................................................................................................................................................ 56
MACCHBL....................................................................................................................................................................56
XSLH .........................................................................................................................................................................56
3ARHCH TAEPHUL CHMBOPOB TYPEGLOB................................................................................................................................57
HEROCPECTBEHHLE PELEHHR...................................................................................................................................... 57
CO3AHHE MACCHBOB.....................................................................................................................................................57
HCROPL3OBAHHE MACCHBOB .............................................................................................................................................58
OREPAUHH PUS H POP ....................................................................................................................................................59
OREPAUHH SFT H UNSFT...............................................................................................................................................59
ORPEEPEHHE PHHL MACCHBA .........................................................................................................................................60
YBEPH-EHHE HPH CYXEHHE MACCHBA...................................................................................................................................60
CPHHHHE BYX MACCHBOB ................................................................................................................................................61
ROPY-EHHE CPE3A MACCHBA .............................................................................................................................................61
UHKPL H MACCHBL.........................................................................................................................................................61
BLBO MACCHBA............................................................................................................................................................62
CPAHBAHHE (SPLCNG) MACCHBA .....................................................................................................................................63
HHBEPTHPOBAHHE MACCHBA..............................................................................................................................................64
COPTHPOBKA MACCHBA ....................................................................................................................................................64
CO3AHHE XSLEH..........................................................................................................................................................64
HCROPL3OBAHHE XSLEH ..................................................................................................................................................66
OEABPEHHE SPEMEHTOB B XSL .........................................................................................................................................66
RPOBEPKA XSLA HA HAPH-HE SPEMEHTA ...............................................................................................................................67
YAPEHHE SPEMEHTOB XSLA .............................................................................................................................................67
UHKPL RO XSLY ............................................................................................................................................................67
BLBO XSLA ................................................................................................................................................................68
COPTHPOBKA XSLA.........................................................................................................................................................69
CPHHHHE XSLEH............................................................................................................................................................69
HCROPL3OBAHHE XSLEH H MACCHBOB B RPHCBOEHHH CRHCKOM.................................................................................................69
HCROPL3OBAHHE THRA AHHLX TYPEGLOB.............................................................................................................................70
THR AHHLX TYPEGLOB H 3ARHCH B TAEPHUE CHMBOPOB............................................................................................................71
FRABA 4. OREPATOPL H RPHOPHTETL OREPATOPOB.................................................................................................... 73
KOPOTKO ............................................................................................................................................................................ 73
RPHOPHTET OREPATOPOB.................................................................................................................................................74
HEROCPECTBEHHLE PELEHHR...................................................................................................................................... 75
HAHBLCLHH RPHOPHTET: TEPML H CRHCKH, CTOHHE CRPABA ..................................................................................................75
OREPATOP-CTPEPKA .......................................................................................................................................................76
ABTORPHPAEHHE H ABTOYMEHLLEHHE...............................................................................................................................76
BO3BEEHHE B CTEREHL ..................................................................................................................................................77
YHAPHLE OREPATOPL.....................................................................................................................................................77
OREPATOP CBH3LBAHHH..................................................................................................................................................78
Crp.5
YMHOXEHHE H EPEHHE ..................................................................................................................................................78
CPOXEHHE, BL-HTAHHE H KOHKATEHAUHH............................................................................................................................79
OREPATOP CBHFA .........................................................................................................................................................79
HMEHOBAHHLE YHAPHLE OREPATOPL .................................................................................................................................79
OREPATOPL RPOBEPKH 4AHPOB.........................................................................................................................................79
OREPATOPL CPABHEHHH..................................................................................................................................................81
OREPATOPL PABEHCTBA ..................................................................................................................................................81
ROEHTHOE H.............................................................................................................................................................82
ROEHTHOE HPH.........................................................................................................................................................83
ROEHTHOE HCKPK-AKEE HPH ....................................................................................................................................83
POFH-ECKOE H B CTHPE H3LKA C....................................................................................................................................83
POFH-ECKOE HPH B CTHPE H3LKA C................................................................................................................................84
OREPATOP HARA3OHA B CRHCKOBOM KOHTEKCTE ...................................................................................................................85
OREPATOP HARA3OHA B CKAPHPHOM KOHTEKCTE ...................................................................................................................85
TEPHAPHLH YCPOBHLH OREPATOP .....................................................................................................................................87
OREPATOP RPHCBOEHHH..................................................................................................................................................87
OREPATOP-3ARHTAH .......................................................................................................................................................88
CRHCKH, CTOHHE CPEBA ................................................................................................................................................88
POFH-ECKOE NOT .........................................................................................................................................................89
POFH-ECKOE ND .........................................................................................................................................................89
POFH-ECKOE OR ...........................................................................................................................................................89
POFH-ECKOE OR .........................................................................................................................................................89
FRABA 5. YCROBHLE OREPATOPL H UHKRL................................................................................................................... 90
KPATKOE BBEEHHE.......................................................................................................................................................... 90
YCPOBHLE OREPATOPL...................................................................................................................................................90
OREPATOPL UHKPA ........................................................................................................................................................91
HEMEREHHLE PELEHHR ................................................................................................................................................ 92
YCPOBHLH OREPATOP F ..................................................................................................................................................92
KOMAHA UNLESS ..........................................................................................................................................................93
OREPATOP UHKPA FOR ...................................................................................................................................................94
OREPATOP UHKPA FOREC ..............................................................................................................................................95
OREPATOP UHKPA WLE ..................................................................................................................................................97
OREPATOP UHKPA UNTL ...................................................................................................................................................98
MOH4HKATOPL F, UNLESS, UNTL, WLE H FOR.....................................................................................................................98
KAK CO3ATL UHKP DO WLE.............................................................................................................................................99
KOMAHA NET: KAK REPEHTH K CPEYKEH HTEPAUHH ..........................................................................................................100
KOMAHA LST: KAK RPEPBATL BLROPHEHHE UHKPA ...............................................................................................................100
KOMAHA REDO: KAK BEPHYTLCH K HA-APY HTEPAUHH.............................................................................................................101
EPOK KOMAH KAK OREPATOP UHKPA..................................................................................................................................101
CO3AHHE REPEKPK-ATEPEH SWTC................................................................................................................................102
OREPATOP EE3YCPOBHOH REPEA-H YRPABPEHHH GOTO.........................................................................................................103
BLROPHEHHE KOA PERL C ROMOLK KOMAHL EVL ...........................................................................................................104
BLXO H3 RPOFPAMML C ROMOLK KOMAHL ET...............................................................................................................105
BLXO H3 RPOFPAMML C ROMOLK KOMAHL DE ................................................................................................................105
FRABA 6. PEFYRRPHLE BLPAXEHHR......................................................................................................................... 106
KOPOTKO ......................................................................................................................................................................... 106
HCROPL3OBAHHE PEFYPHPHLX BLPAXEHHH ........................................................................................................................106
Onepamop npoeepku coenaeuu m/.../.......................................................................................................................107
Onepamop nocmauoeku s/.../.../ ...............................................................................................................................107
Onepamop saueui tr/.../.../ .......................................................................................................................................108
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 108
CO3AHHE PEFYPHPHLX BLPAXEHHH ................................................................................................................................108
OHHO-HLE CHMBOPL B PEFYPHPHLX BLPAXEHHHX .............................................................................................................109
Coenaeuue c nkiu cuueonou ...............................................................................................................................110
KPACCL CHMBOPOB B PEFYPHPHLX BLPAXEHHHX .................................................................................................................111
APLTEPHATHBHLE LAEPOHL B PEFYPHPHLX BLPAXEHHHX ......................................................................................................112
KBAHTH4HKATOPL B PEFYPHPHLX BLPAXEHHHX...................................................................................................................112
Xauocmi keaumuqukamopoe ..............................................................................................................................113
Crp.6
MHHMLE CHMBOPL B PEFYPHPHLX BLPAXEHHHX .................................................................................................................113
CCLPKH HA HAHEHHLH TEKCT ........................................................................................................................................115
OROPHHTEPLHLE KOHCTPYKUHH B PEFYPHPHLX BLPAXEHHHX ................................................................................................116
MOH4HKATOPL KOMAH M/.../ H S/.../.../ ............................................................................................................................118
OCOEEHHOCTH PAEOTL KOMAH M/.../ H S/.../.../ ...................................................................................................................118
Hpeeapumeniuan opaomka peaynnpuix eipaeuu ................................................................................................120
Paoma kouaui m/.../ e peuue ouokpamuoao noucka ...............................................................................................121
Paoma kouaui m/.../ e peuue anoaniuoao noucka .................................................................................................122
3AMEHA CTPOK C ROMOLK KOMAHL TR/.../.../ ...................................................................................................................123
MOH4HKATOPL KOMAHL TR/.../.../..................................................................................................................................125
ROHCK OTEPLHLX CPOB ...............................................................................................................................................126
RPHBH3KA K HA-APY CTPOKH ...........................................................................................................................................127
RPHBH3KA K KOHUY CTPOKH ............................................................................................................................................127
ROHCK -HCEP .............................................................................................................................................................127
RPOBEPKA HEHTH4HKATOPOB ........................................................................................................................................128
KAK HAHTH MHOXECTBEHHLE COBRAEHHH.........................................................................................................................129
ROHCK HE-YBCTBHTEPLHLX K PEFHCTPY COBRAEHHH ...........................................................................................................129
BLEPEHHE ROCTPOKH................................................................................................................................................130
BL3OB 4YHKUHH H BL-HCPEHHE BLPAXEHHH RPH ROCTAHOBKE TEKCTA ....................................................................................130
ROHCK N-FO COBRAEHHH ..............................................................................................................................................130
KAK OFPAHH-HTL XAHOCTL KBAHTH4HKATOPOB ...............................................................................................................131
KAK YAPHTL BEYHE H 3ABEPLAKHE RPOEEPL ...............................................................................................................132
YTBEPXEHHH, RPOBEPHKHE TEKCT REPE H ROCPE LAEPOHA...............................................................................................132
FRABA 7. RORPOFPAMML .......................................................................................................................................... 135
KOPOTKO ......................................................................................................................................................................... 135
PAEOTA C RORPOFPAMMAMH..........................................................................................................................................135
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 136
OEIHBPEHHE RORPOFPAMM ..........................................................................................................................................136
HCROPL3OBAHHE RPOTOTHROB........................................................................................................................................137
ORPEEPEHHE RPOTOTHROB ...........................................................................................................................................138
BL3OB RORPOFPAMM...................................................................................................................................................138
-TEHHE APFYMEHTOB, REPEAHHLX RORPOFPAMME .............................................................................................................139
HCROPL3OBAHHE REPEMEHHOFO -HCPA RAPAMETPOB ............................................................................................................139
HCROPL3OBAHHE 3HA-EHHH RO YMOP-AHHK.......................................................................................................................140
3HA-EHHH, BO3BPAAEMLE RORPOFPAMMAMH (4YHKUHHMH) .................................................................................................140
YRPABPEHHE OEPACTLK BHHMOCTH (KPK-EBLE CPOBA TY H LOCL) .........................................................................................141
TPEEOBAHHE OEH3ATEPLHOH PEKCH-ECKOH OEPACTH BHHMOCTH ............................................................................................143
CO3AHHE BPEMEHHLX REPEMEHHLX (KPK-EBOE CPOBO LOCL) ..............................................................................................143
ROCTOHHHLE (CTATH-ECKHE) REPEMEHHLE .......................................................................................................................144
PEKYPCHBHLH BL3OB RORPOFPAMM................................................................................................................................146
BPOXEHHLE RORPOFPAMML .........................................................................................................................................146
REPEA-A RAPAMETPOB RO CCLPKE..................................................................................................................................146
REPEA-A 3ARHCH TAEPHUL CHMBOPOB (THR AHHLX TYPEGLOB) .............................................................................................147
RPOBEPKA KOHTEKCTA BL3OBA 4YHKUHH: 4YHKUHH WNTRRY ...............................................................................................148
CO3AHHE BCTPAHBAEMLX 4YHKUHH ................................................................................................................................148
3AMEEHHE BCTPOEHHLX 4YHKUHH. RCEBORAKET CORE .....................................................................................................149
HEROHMEHOBAHHLE RORPOFPAMML ...............................................................................................................................149
CO3AHHE TAEPHU HCRET-EPH3AUHH RORPOFPAMM ...........................................................................................................150
FRABA 8. CCLRKH B PERL ............................................................................................................................................ 151
KOPOTKO ......................................................................................................................................................................... 151
XECTKHE CCLPKH........................................................................................................................................................151
CHMBOPH-ECKHE CCLPKH ..............................................................................................................................................152
OREPATOP-CTPEPKA .....................................................................................................................................................152
AHOHHMHLE MACCHBL, XSL-TAEPHUL, RORPOFPAMML ........................................................................................................152
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 153
CO3AHHE CCLPKH ......................................................................................................................................................153
CCLPKH HA AHOHHMHLE MACCHBL...................................................................................................................................154
CCLPKH HA AHOHHMHLE XSLH ........................................................................................................................................155
Crp.7
CCLPKH HA AHOHHMHLE RORPOFPAMML...........................................................................................................................155
KAK H3BPE-L CCLPKY H3 TAEPHUL CHMBOPOB .....................................................................................................................156
PA3LMEHOBAHHE CCLPOK..............................................................................................................................................156
PA3LMEHOBAHHE CCLPOK C ROMOLK OREPATOPA-CTPEPKH ..................................................................................................157
KOFA MOXHO ORYCKATL OREPATOP-CTPEPKY......................................................................................................................158
KAK ORPEEPHTL THR CCLPKH C ROMOLK OREPATOPA REF ....................................................................................................159
CO3AHHE CHMBOPH-ECKHX CCLPOK ................................................................................................................................159
3ARPET CHMBOPH-ECKHX CCLPOK ....................................................................................................................................160
HCROPL3OBAHHE CCLPOK HA MACCHBL KAK CCLPOK HA XSLH..................................................................................................160
KAK CO3ATL 3AMLKAHHE OEPACTH BHHMOCTH B YCTOH-HBYK 4OPMY ......................................................................................161
CO3AHHE 4YHKUHH HA OCHOBE LAEPOHOB........................................................................................................................161
HACTL II. BCTPOEHHLE PECYPCL.............................................................................................................................. 163
FRABA 9. BCTPOEHHLE REPEMEHHLE ...................................................................................................................... 163
KOPOTKO ......................................................................................................................................................................... 163
PA3BEPHYTLE HMEHA BCTPOEHHLX REPEMEHHLX.................................................................................................................164
HACTPOHKA BCTPOEHHLX REPEMEHHLX HA KOHKPETHLE ECKPHRTOPL 4AHPOB...........................................................................164
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 165
$ - CTPOKA, CPEYKAH 3A COBRAEHHEM.........................................................................................................................165
$- - -HCPO CTPOK, OCTABLHXCH HA CTPAHHUE.....................................................................................................................165
$ - TEKYAH OLHEKA ...................................................................................................................................................165
$ - PA3EPHTEPL ROPEH MACCHBOB RPH HHTEPROPHPOBAHHH.................................................................................................166
$ - 4OPMAT BLBOA -HCEP C RPABAKEH TO-KOH ..............................................................................................................166
$$ - HEHTH4HKATOP RPOUECCA PERL...............................................................................................................................166
$ - TEKYAH CTPAHHUA BLBOA ....................................................................................................................................166
$ - COBRAEHHE C LAEPOHOM ROHCKA.............................................................................................................................166
$( - PEAPLHLH HEHTH4HKATOP FPYRRL ROPL3OBATEPEH (REL GD) ........................................................................................167
$)- TEKYHH HEHTH4HKATOP FPYRRL ROPL3OBATEPEH (EFFECTVE GD) ....................................................................................167
$ - COBRAEHHE C LAEPOHOM ROHCKA .............................................................................................................................167
$, - PA3EPHTEPL ROPEH BLBOA.....................................................................................................................................167
$. - TEKYHH HOMEP CTPOKH BBOA..................................................................................................................................168
$/ - PA3EPHTEPL BXOHLX 3ARHCEH ................................................................................................................................168
$: - MAPKEP PA3EHBKH CTPOKH.........................................................................................................................................168
$ - PA3EPHTEPL HHEKCOB ...........................................................................................................................................168
$? - CTATYC ROCPEHEH CHCTEMHOH OREPAUHH ..................................................................................................................169
$ - OLHEKA BLROPHEHHH 4YHKUHH EVL .........................................................................................................................169
$ - EA3OBLH HHEKC MACCHBOB .....................................................................................................................................169
$ - PA3EPHTEPL BLXOHLX 3ARHCEH ..............................................................................................................................169
$ - BEPCHH PERL.........................................................................................................................................................169
$ - TEKYHH 4OPMAT KOPOHTHTYPA CTPAHHUL...................................................................................................................170
$A - HAKORHTEPL KOMAHL WRTE ..................................................................................................................................170
$D - TEKYHE 4PAFH OTPAKH .......................................................................................................................................170
$E - HH4OPMAUHH OE OLHEKE, CREUH4H-HAH PH OREPAUHOHHOH CHCTEML ............................................................................170
$F - MAKCHMAPLHOE KOPH-ECTBO ECKPHRTOPOB 4AHPOB ....................................................................................................170
$H - 4PAFH RPOBEPKH CHHTAKCHCA.................................................................................................................................170
$ - PACLHPEHHE 4AHPOB PH PEAKTHPOBAHHH RO MECTY ................................................................................................171
$L - CHMBOP RPOFOHA CTPAHHUL...................................................................................................................................171
$M - EY4EP RAMHTH HA KPAHHHH CPY-AH.......................................................................................................................171
$O - HMH OREPAUHOHHOH CHCTEML................................................................................................................................171
$P - ROEPXKA OTPAKH ............................................................................................................................................172
$R - PE3YPLTAT BL-HCPEHHH YTBEPXEHHH B TEPE LAEPOHA................................................................................................172
$S COCTOHHHE HHTEPRPETATOPA ...............................................................................................................................172
$T - BPEMH 3ARYCKA CUEHAPHH HA BLROPHEHHE ................................................................................................................172
$W - PEXHM BLBOA RPEYRPEXAKHX COOEEHHH........................................................................................................172
$X - HMH RPOFPAMML-HHTEPRPETATOPA...........................................................................................................................173
$_ - APFYMEHT RO YMOP-AHHK........................................................................................................................................173
$ - CTPOKA, CPEYKAH REPE COBRAEHHEM ..................................................................................................................173
$ -- YRPABPEHHE EY4EPOM BLBOA .................................................................................................................................173
$ - HMH TEKYEFO 4OPMATA OT-ETOB..............................................................................................................................174
$ - 4PAFMEHT COBRAEHHH...........................................................................................................................................174
$ - PEAPLHLH HEHTH4HKATOP ROPL3OBATEPH (REL USER D) ..............................................................................................174
Crp.8
$ - TEKYHH PA3MEP CTPAHHUL.....................................................................................................................................174
$ - TEKYHH HEHTH4HKATOP ROPL3OBATEPH (EFFECTVE USER D) .........................................................................................175
$0 - HMH RPOFPAMML ...................................................................................................................................................175
$RGV - HMH BXOHOFO 4AHPA ......................................................................................................................................175
$NN - NN-H 4PAFMEHT COBRAEHHH ..................................................................................................................................175
ENV - REPEMEHHLE OKPYXEHHH ...................................................................................................................................175
NC - ROKPK-AEMLE 4AHPL.......................................................................................................................................176
SG - OEPAEOT-HKH CHTYAUHH .....................................................................................................................................176
_ - APFYMEHTL, REPEAHHLE RORPOFPAMME ..................................................................................................................176
RGV - APFYMEHTL, REPEAHHLE B KOMAHHOH CTPOKE ....................................................................................................176
NC - RYTH ROHCKA ROKPK-AEMLX 4AHPOB ...................................................................................................................177
FRABA 10. BCTPOEHHLE OYHKUHH: OBPABOTKA AHHLX.................................................................................... 178
KOPOTKO ......................................................................................................................................................................... 178
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 178
BS - AECOPKTHOE 3HA-EHHE .........................................................................................................................................178
TN2 - APKTAHFEHC.....................................................................................................................................................179
COMP - YAPEHHE KOHUA CTPOKH....................................................................................................................................179
COP- YAPEHHE ROCPEHEFO CHMBOPA CTPOKH..................................................................................................................179
CR - RPEOEPA3OBAHHE -HCPA B CHMBOP ..........................................................................................................................180
COS - KOCHHYC ...........................................................................................................................................................180
EC - RAPA KPK-/3HA-EHHE H3 XSLA...............................................................................................................................180
EVL - KOMRHPHPOBAHHE H BLROPHEHHE KOMAH PERL .........................................................................................................180
ESTS -- RPOBEPKA KPK-A B XSLE ...................................................................................................................................181
EP - BL-HCPEHHE SKCROHEHUHAPLHOH 4YHKUHH................................................................................................................181
E - RPEOEPA3OBAHHE LECTHAUATEPH-HOFO -HCPA ..........................................................................................................181
NDE - ROPOXEHHE ROCTPOKH ......................................................................................................................................182
NT - UEPAH -ACTL -HCPA ...............................................................................................................................................182
ON - RPEOEPA3OBAHHE CRHCKA B CTPOKY .........................................................................................................................182
EYS - CRHCOK KPK-EH XSLA .........................................................................................................................................182
LC - RPEOEPA3OBAHHE EYKB K HHXHEMY PEFHCTPY ................................................................................................................183
CFRST - RPEOEPA3OBAHHE REPBOH EYKBL K HHXHEMY PEFHCTPY .............................................................................................183
LENGT - PHHA CTPOKH ................................................................................................................................................183
LOG - HATYPAPLHLH POFAPH4M .......................................................................................................................................184
MP - BLROPHHTL KOMAHY PH KAXOFO SPEMEHTA CRHCKA ..................................................................................................184
OCT - RPEOEPA3OBAHHE BOCLMEPH-HOFO -HCPA..................................................................................................................184
ORD - RPEOEPA3OBAHHE CHMBOPA B KO ............................................................................................................................184
PC - YRAKOBKA 3HA-EHHH............................................................................................................................................185
POP - H3BPE-EHHE AHHLX H3 MACCHBA ............................................................................................................................187
4YHKUHH POS.........................................................................................................................................................187
PUS - OEABPEHHE AHHLX K MACCHBY ............................................................................................................................188
RND - CPY-AHHOE -HCPO..............................................................................................................................................188
REVERSE - REPECTABHTL CRHCOK B OEPATHOM ROPHKE.........................................................................................................188
RNDE - ROPOXEHHE ROCTPOKH ....................................................................................................................................189
SCLR - 4OPCHPOBAHHE CKAPHPHOFO KOHTEKCTA ...............................................................................................................189
SFT - H3BPE-EHHE REPBOFO SPEMEHTA MACCHBA ...............................................................................................................189
SN - CHHYC................................................................................................................................................................190
SORT - COPTHPOBKA CRHCKA ...........................................................................................................................................190
SPLCE - 3AMEHA CPE3A MACCHBA .....................................................................................................................................190
SPLT - PA3EHBKA CTPOKH HA CRHCOK CTPOK ........................................................................................................................191
SPRNTF - 4OPMATHPOBAHHE CTPOKH.................................................................................................................................192
SRT - KBAPATHLH KOPEHL ...........................................................................................................................................194
SRND - HHHUHATOP FEHEPATOPA CPY-AHHLX -HCEP ............................................................................................................194
SUBSTR - ROCTPOKA TEKCTOBOH CTPOKH...........................................................................................................................194
TME - BPEMH B CEKYHAX C 1 HHBAPH 1970 FOA .................................................................................................................195
UC - RPEOEPA3OBAHHE EYKB K BEPXHEMY PEFHCTPY...............................................................................................................195
UCFRST - RPEOEPA3OBAHHE REPBOH EYKBL K BEPXHEMY PEFHCTPY ...........................................................................................195
UNPC - YRAKOBKA 3HA-EHHH ........................................................................................................................................195
UNSFT - OEABPEHHE REPBOFO SPEMEHTA B MACCHB ...........................................................................................................196
VLUES - CRHCOK 3HA-EHHH XSLA....................................................................................................................................196
VEC - BEKTOP UEPLX 3HA-EHHH EE3 3HAKA..........................................................................................................................196
Crp.9
FRABA 11. BCTPOEHHLE OYHKUHH: BBO/BLBO H MEXRPOUECCHLE B3AHMOEHCTBHR ........................ 199
KOPOTKO ......................................................................................................................................................................... 199
4OPMATL PERL...........................................................................................................................................................199
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 200
PRNT - RE-ATL CRHCKA AHHLX ......................................................................................................................................200
PRNTF - RE-ATL 4OPMATHPOBAHHOFO CRHCKA AHHLX..........................................................................................................200
-TEHHE BXOHOFO ROTOKA ........................................................................................................................................201
WRTE - BLBO 4OPMATHPOBAHHOH 3ARHCH........................................................................................................................202
4OPMATL: TEKCT, BLPOBHEHHLH RO PEBOMY KPAK..............................................................................................................202
4OPMATL: TEKCT, BLPOBHEHHLH RO RPABOMY KPAK ............................................................................................................203
4OPMATL: UEHTPHPOBAHHLH TEKCT .................................................................................................................................203
4OPMATL: RE-ATL -HCEP ..............................................................................................................................................203
4OPMATL: 4OPMATHPOBAHHLH MHOFOCTPO-HLH BLBO .......................................................................................................203
4OPMATL: 4OPMATHPOBAHHLH MHOFOCTPO-HLH BLBO C BLPE3KOH HYXHOFO TEKCTA.................................................................204
4OPMATL: HE4OPMATHPOBAHHLH MHOFOCTPO-HLH BLBO....................................................................................................205
4OPMATL: BLBO KOPOHTHTYPA (3AFOPOBKA CTPAHHUL) .......................................................................................................205
4OPMATL: BLBO HHXHEFO KOPOHTHTYPA .........................................................................................................................205
4OPMATL: CREUHAPLHLE REPEMEHHLE PERL .....................................................................................................................206
4OPMATL: 4OPMAPLHLH CHHTAKCHC ................................................................................................................................206
WRN - BLBO RPEYRPEXAKHX COOEEHHH..................................................................................................................209
PC: EEC - CHCTEMHLH BL3OB.......................................................................................................................................209
PC: SYSTEM - BETBPEHHE H BLROPHEHHE CHCTEMHOH KOMAHL .............................................................................................210
PC: KAK RPO-HTATL AHHLE, REPEAHHLE PYFOH RPOFPAMMOH ............................................................................................211
PC: KAK REPECPATL AHHLE PYFOH RPOFPAMME ................................................................................................................211
PC: BLBO AHHLX B O-EPHHH RPOUECC ........................................................................................................................212
PC: BLBO AHHLX B POHTEPLCKHH RPOUECC ..................................................................................................................212
PC: KAK REPECPATL CHFHAP PYFOMY RPOUECCY .................................................................................................................213
PC: HCROPL3OBAHHE COKETOB.......................................................................................................................................214
PC: HCROPL3OBAHHE TEXHOPOFHH WN32 OLE UTOMTON..................................................................................................215
FRABA 12. BCTPOEHHLE OYHKUHH: PABOTA C OAHRAMH..................................................................................... 218
KOPOTKO ......................................................................................................................................................................... 218
KOE--TO O PAEOTE C 4AHPAMH ........................................................................................................................................218
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 219
OPEN - OTKPLTHE 4AHPA................................................................................................................................................219
CLOSE - 3AKPLTHE 4AHPA...............................................................................................................................................220
PRNT - BLBO B 4AHP...................................................................................................................................................221
WRTE - 3ARHCL B 4AHP .................................................................................................................................................221
BNMODE - YCTAHOBKA BOH-HOFO PEXHMA ........................................................................................................................222
YRPABPEHHE EY4EPH3AUHEH BLBOA ...............................................................................................................................222
-TEHHE 4AHPOB, REPEAHHLX -EPE3 KOMAHHYK CTPOKY .....................................................................................................223
-TEHHE H3 ECKPHRTOPA 4AHPA......................................................................................................................................223
RED - -TEHHE BXOHLX AHHLX.....................................................................................................................................223
REDLNE - C-HTLBAHHE CTPOKH BBOA.............................................................................................................................224
GETC - C-HTLBAHHE OHHO-HOFO CHMBOPA .......................................................................................................................224
SEE - ROHCK 3AAHHOH RO3HUHH B 4AHPE ........................................................................................................................225
TELL - TEKYAH RO3HUHH B 4AHPE....................................................................................................................................225
STT - HH4OPMAUHH O 4AHPE..........................................................................................................................................226
4AHPOBLE 4YHKUHH MOYPH POS ................................................................................................................................227
SELECT - BLEOP ECKPHRTOPA 4AHPA PH STDOUT .............................................................................................................227
EOF - RPOBEPKA KOHUA 4AHPA.........................................................................................................................................228
3ARHCL EA3L AHHLX DBM ...........................................................................................................................................229
KAK RPO-HTATL 4AHP EA3L AHHLX 4OPMATA DBM..............................................................................................................229
FLOC - EPOKHPOBKA 4AHPA............................................................................................................................................230
CMOD - H3MEHEHHE RPAB OCTYRA K 4AHPY.......................................................................................................................230
RENME - REPEHMEHOBAHHE 4AHPOB ................................................................................................................................231
GLOB - ROHCK 4AHPOB RO LAEPOHY ..................................................................................................................................231
UNLN - YAPEHHE 4AHPOB ............................................................................................................................................231
OPENDR - OTKPLTHE KATAPOFA........................................................................................................................................232
Crp.10
CLOSEDR - 3AKPLTHE KATAPOFA.......................................................................................................................................232
REDDR - -TEHHE COEPXHMOFO KATAPOFA........................................................................................................................232
SEEDR - YCTAHOBKA TEKYEH RO3HUHH B KATAPOFE ............................................................................................................233
TELLDR - -TEHHE TEKYEH RO3HUHH B KATAPOFE .................................................................................................................233
REWNDDR - YCTAHOBKA TEKYEH RO3HUHH HA HA-APO KATAPOFA.............................................................................................233
CDR - CMEHA TEKYEFO KATAPOFA ..................................................................................................................................233
MDR - CO3AHHE HOBOFO KATAPOFA ................................................................................................................................234
RMDR - YAPHTL KATAPOF ..............................................................................................................................................234
HACTL III. RPOFPAMMHPOBAHHE HA PERL ................................................................................................................ 235
FRABA 13. CTAHAPTHLE MOYRH ............................................................................................................................ 235
KOPOTKO ......................................................................................................................................................................... 235
HCROPL3OBAHHE MOYPEH PERL .....................................................................................................................................235
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 239
TERM::CP - PAEOTA C TEPMHHAPOM.................................................................................................................................239
MT - KOMRPEKCHLE -HCPA H -HCPA EOPLLOH PA3PHHOCTH................................................................................................240
POS - 4YHKUHH PORTBLE OPERTNG SYSTEM NTERFCE ..................................................................................................240
BENCMR - TECTHPOBAHHE RPOH3BOHTEPLHOCTH............................................................................................................241
TME - H3MEPEHHE BPEMEHH H RPEOEPA3OBAHHE 4OPMATOB BPEMEHH .......................................................................................241
CRP - COOEEHHH OE OLHEKAX C YKA3AHHEM TO-KH BL3OBA.................................................................................................241
LOCLE -- HCROPL3OBAHHE POKAPLHLX HACTPOEK KOMRLKTEPA ..............................................................................................242
FLE - OREPAUHH C 4AHPAMH...........................................................................................................................................242
NET - OCTYR K CETH HHTEPHET ......................................................................................................................................242
SFE - EE3ORACHOE BLROPHEHHE KOA.............................................................................................................................243
T - CPECTBA PAEOTL C EHEPHOTEKOH T.........................................................................................................................243
T: KHORKH H TEKCTOBLE HHTEP4EHCHLE SPEMEHTL............................................................................................................244
T: KHORKH C 3ABHCHMOH H HE3ABHCHMOH 4HKCAUHEH .........................................................................................................244
T:HHTEP4EHCHLH SPEMEHT CRHCOK ............................................................................................................................245
T: HHTEP4EHCHLH SPEMEHT LKAPA .............................................................................................................................245
T: HHTEP4EHCHLE SPEMEHTL FPA4HKH (KAHBA) ..............................................................................................................245
T: HHTEP4EHCHLE SPEMEHTL MEHK ...............................................................................................................................246
T: OKHA HAPOFA .......................................................................................................................................................246
FRABA 14. CTPYKTYPL AHHLX.................................................................................................................................. 247
KOPOTKO ......................................................................................................................................................................... 247
ROCKA3KA: HCROPL3YHTE KOMAHY USE STRCT VRS ...........................................................................................................249
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 250
CPOXHLE 3ARHCH: XPAHEHHE CCLPOK H PYFHX SPEMEHTOB ..................................................................................................250
OEIHBPEHHE MACCHBA MACCHBOB ...................................................................................................................................251
CO3AHHE MACCHBA MACCHBOB HA PETY.........................................................................................................................251
OCTYR K SPEMEHTAM MACCHBA MACCHBOB ........................................................................................................................252
OEIHBPEHHE XSLA XSLEH.............................................................................................................................................253
CO3AHHE XSLA XSLEH HA PETY ..................................................................................................................................254
OCTYR K SPEMEHTAM XSLA XSLEH..................................................................................................................................254
OEIHBPEHHE MACCHBA XSLEH........................................................................................................................................255
CO3AHHE MACCHBA XSLEH HA PETY .............................................................................................................................255
OCTYR K SPEMEHTAM MACCHBA XSLEH .............................................................................................................................256
OEIHBPEHHE XSLA MACCHBOB........................................................................................................................................257
CO3AHHE XSLA MACCHBOB HA PETY .............................................................................................................................257
OCTYR K SPEMEHTAM XSLA MACCHBOB.............................................................................................................................257
CBH3HLE CRHCKH H KOPLUEBLE EY4EPL ...........................................................................................................................258
FRABA 15. CO3AHHE RAKETOB H MOYREH ............................................................................................................ 260
KOPOTKO ......................................................................................................................................................................... 260
RAKETL ....................................................................................................................................................................260
MOYPH....................................................................................................................................................................262
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 262
KAK CO3ATL RAKET......................................................................................................................................................262
Crp.11
CO3AHHE RAKETA: KOHCTPYKTOP BEGN...........................................................................................................................264
CO3AHHE RAKETA: ECTPYKTOP END ...............................................................................................................................264
KAK ORPEEPHTL TEKYHH RAKET.....................................................................................................................................265
KAK PA3EHTL RAKET HA HECKOPLKO 4AHPOB ........................................................................................................................266
CO3AHHE MOYPEH.....................................................................................................................................................267
OKYMEHTHPOBAHHE MOYPEH........................................................................................................................................268
KAK RO YMOP-AHHK SKCROPTHPOBATL HMEHA MOYPH ..........................................................................................................272
KAK PA3PELHTL SKCROPT HMEHH, HO RO YMOP-AHHK EFO HE SKCROPTHPOBATL............................................................................272
KAK OTKPK-HTL HMROPT HMEH RPH 3AFPY3KE MOYPH ...........................................................................................................273
KAK 3ARPETHTL SKCROPT HMEHH ......................................................................................................................................273
SKCROPTHPOBAHHE EE3 ROMOH METOA MPORT ................................................................................................................274
CO3AHHE BPOXEHHLX MOYPEH ....................................................................................................................................275
RPOBEPKA BEPCHH MOYPH ............................................................................................................................................276
ABTO3AFPY3KA MOYPEH................................................................................................................................................276
HCROPL3OBAHHE ABTO3AFPY3KH H CAMO3AFPY3KH RORPOFPAMM .............................................................................................277
FRABA 16. CO3AHHE KRACCOB H OBtEKTOB ......................................................................................................... 282
KOPOTKO ......................................................................................................................................................................... 282
KPACCL ....................................................................................................................................................................282
OEIEKTL ..................................................................................................................................................................283
METOL....................................................................................................................................................................283
HACPEOBAHHE ..........................................................................................................................................................284
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 284
CO3AHHE KPACCA.......................................................................................................................................................284
CO3AHHE OEIEKTA .....................................................................................................................................................285
CO3AHHE METOA KPACCA ............................................................................................................................................285
CO3AHHE METOA SK3EMRPHPA......................................................................................................................................286
BL3OB METOA ...........................................................................................................................................................287
CO3AHHE REPEMEHHOH SK3EMRPHPA ...............................................................................................................................287
CO3AHHE RPHBATHLX METOOB H AHHLX ........................................................................................................................288
CO3AHHE REPEMEHHOH KPACCA .....................................................................................................................................288
CO3AHHE ECTPYKTOPA ...............................................................................................................................................289
KAK PEAPH3OBATL HACPEOBAHHE KPACCOB........................................................................................................................290
HACPEOBAHHE KOHCTPYKTOPOB .....................................................................................................................................291
HACPEOBAHHE REPEMEHHLX SK3EMRPHPA ........................................................................................................................293
HACPEOBAHHE METOOB ..............................................................................................................................................293
HACPEOBAHHE METOOB: BHPTYAPLHLE METOL................................................................................................................296
HACPEOBAHHE METOOB: METOL HHHUHAPH3AUHH ............................................................................................................297
HACPEOBAHHE REPEMEHHLX KPACCA...............................................................................................................................299
MHOXECTBEHHOE HACPEOBAHHE....................................................................................................................................301
MHOXECTBEHHOE HACPEOBAHHE: RPOEPEML ....................................................................................................................302
Hacneoeauue auuix ............................................................................................................................................303
Hacneoeauue uemooe..........................................................................................................................................304
3aueeuue uemooe..............................................................................................................................................304
Hepeueuuie knacca ...............................................................................................................................................306
Koucmpykmopi......................................................................................................................................................306
ecmpykmopi.......................................................................................................................................................308
Hukancynnuun euecmo uacneoeauun ........................................................................................................................309
CBH3LBAHHE REPEMEHHLX ............................................................................................................................................310
CBH3LBAHHE CKAPHPHLX REPEMEHHLX.............................................................................................................................311
CBH3LBAHHE MACCHBOB ...............................................................................................................................................313
CBH3LBAHHE XSLEH ....................................................................................................................................................314
HCROPL3OBAHHE KPACCA PERL UNVERSL ......................................................................................................................315
FRABA 17. OTRAKA CUEHAPHEB PERL. PYKOBOCTBO RO CTHRK RPOFPAMMHPOBAHHR ............................ 318
KOPOTKO ......................................................................................................................................................................... 318
RPHMEP CEAHCA OTPAKH..............................................................................................................................................318
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 320
REPEXBAT OLHEOK BPEMEHH BLROPHEHHH.........................................................................................................................320
3ARYCK OTPA-HKA ......................................................................................................................................................321
Crp.12
OCTYRHLE KOMAHL OTPA-HKA....................................................................................................................................321
RPOCMOTP HCXOHOFO KOA ..........................................................................................................................................322
ROLAFOBOE BLROPHEHHE .............................................................................................................................................322
ROLAFOBOE BLROPHEHHE EE3 3AXOA B RORPOFPAMML ......................................................................................................323
YCTAHOBKA TO-EK RPEPLBAHHH ......................................................................................................................................323
YAPEHHE TO-EK RPEPLBAHHH........................................................................................................................................324
3ARYCK RPOFPAMML O CPEYKEH TO-KH OCTAHOBKH.........................................................................................................324
RE-ATL BLPAXEHHH ....................................................................................................................................................325
BL-HCPEHHE BLPAXEHHH .............................................................................................................................................325
H3MEHEHHE 3HA-EHHH REPEMEHHLX ................................................................................................................................325
YCTAHOBKA FPOEAPLHLX YCPOBHH ...................................................................................................................................326
YCTAHOBKA OTPAO-HLX EHCTBHH .................................................................................................................................326
BLXO H3 OTPA-HKA...................................................................................................................................................327
PYKOBOCTBO RO CTHPK RPOFPAMMHPOBAHHH HA PERL ........................................................................................................327
HACTL IV. CO3AHHE CUEHAPHEB CGI ...................................................................................................................... 329
FRABA 18. CGI-RPOFPAMMHPOBAHHE ........................................................................................................................ 329
KOPOTKO ......................................................................................................................................................................... 329
HCROPL3OBAHHE CG.PM...............................................................................................................................................330
CO3AHHE H HCROPL3OBAHHE SPEMEHTOB YRPABPEHHH TML ................................................................................................331
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 335
HCROPL3OBAHHE H3LKA PERLSCRPT................................................................................................................................335
HA-HHAEM TML-OKYMEHT ..........................................................................................................................................335
CO3AEM 3AFOPOBKH TML ...........................................................................................................................................336
UEHTPHPYEM SPEMEHTL................................................................................................................................................336
CO3AEM MAPKHPOBAHHLH CRHCOK .................................................................................................................................336
CO3AEM FHREPCCLPKY ................................................................................................................................................336
CO3AEM FOPH3OHTAPLHYK ROPOCY ................................................................................................................................337
CO3AEM TML-4OPMY ................................................................................................................................................337
PAEOTAEM C TEKCTOBLMH ROPHMH ..................................................................................................................................337
-TEHHE AHHLX H3 SPEMEHTOB YRPABPEHHH TML .............................................................................................................338
PAEOTAEM C TEKCTOBLMH OEPACTHMH..............................................................................................................................338
PAEOTAEM C KHORKAMH C HE3ABHCHMOH 4HKCAUHEH ............................................................................................................338
PAEOTAEM CO CRHCKAMH...............................................................................................................................................339
PAEOTAEM C KHORKAMH C 3ABHCHMOH 4HKCAUHEH ...............................................................................................................339
PAEOTAEM C PACKPLBAKHMCH CRHCKOM .........................................................................................................................340
PAEOTAEM CO CKPLTLMH ROPHMH AHHLX.........................................................................................................................340
CO3AEM KHORKH OTMEHL H ROTBEPXEHHH ....................................................................................................................340
3AKPLBAEM TML-4OPMY .............................................................................................................................................341
3AKPLBAEM TML-OKYMEHT .........................................................................................................................................341
4YHKUHOHAPLHO-OPHEHTHPOBAHHOE CG-RPOFPAMMHPOBAHHE..............................................................................................341
FRABA 19. CGI-RPOFPAMMHPOBAHHE C HCRORL3OBAHHEM CGI-LIB.PL.............................................................. 343
KOPOTKO ......................................................................................................................................................................... 343
HCROPL3OBAHHE CG-LB.PL............................................................................................................................................344
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 347
KAKHE RORPOFPAMML BXOHT B COCTAB CG-LB.PL?............................................................................................................347
HA-HHAEM OKYMEHT TML...........................................................................................................................................347
CO3AEM 3AFOPOBKH TML ...........................................................................................................................................348
UEHTPHPYEM SPEMEHTL TML .......................................................................................................................................348
CO3AEM MAPKHPOBAHHLH CRHCOK .................................................................................................................................348
CO3AEM FHREPCCLPKY ................................................................................................................................................348
CO3AEM FOPH3OHTAPLHYK PHHHK .................................................................................................................................349
CO3AEM 4OPMY TML.................................................................................................................................................349
PAEOTAEM C TEKCTOBLMH ROPHMH ..................................................................................................................................349
-HTAEM AHHLE H3 SPEMEHTOB YRPABPEHHH TML .............................................................................................................349
PAEOTAEM C TEKCTOBLMH OEPACTHMH..............................................................................................................................350
PAEOTAEM C KHORKAMH C HE3ABHCHMOH 4HKCAUHEH ............................................................................................................350
PAEOTAEM CO CRHCKAMH...............................................................................................................................................351
Crp.13
PAEOTAEM C KHORKAMH C 3ABHCHMOH 4HKCAUHEH ...............................................................................................................351
PAEOTAEM C PACKPLBAKHMHCH CRHCKAMH ......................................................................................................................351
PAEOTAEM CO CKPLTLMH ROPHMH AHHLX.........................................................................................................................352
CO3AHHE KHOROK SUBMT H RESET .................................................................................................................................352
3AKPLBAEM 4OPMY TML..............................................................................................................................................352
3ABEPLAEM OKYMEHT TML .........................................................................................................................................353
BLBOHM BCE REPEMEHHLE...........................................................................................................................................353
FRABA 20. CGI: CHETHHKH ROCEEHHH, FOCTEBLE KHHFH, OTRPABKA DREKTPOHHOH ROHTL H BORPOCL
3ALHTL CHCTEML....................................................................................................................................................... 354
KOPOTKO ......................................................................................................................................................................... 354
3AHTA CG ..............................................................................................................................................................354
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 355
CEPLE3HO EEPEMCH 3A 3AHTY.......................................................................................................................................355
PAEOTAEM C ME-EHLMH AHHLMH ...................................................................................................................................356
O-HCTKA AHHLX ........................................................................................................................................................357
RPEOCTABPHEM CG-CUEHAPHK EOPLLHE RPHBHPEFHH B CHCTEME UN ..................................................................................358
CO3AEM C-ET-HK ROCEEHHH.......................................................................................................................................358
CO3AEM FOCTEBYK KHHFY ............................................................................................................................................359
OTRPABKA RO-TOBLX COOEEHHH H3 CG-CUEHAPHH ...........................................................................................................362
FRABA 21. CGI: MHOFORORL3OBATERLCKHE HATL, TEHEBLE ROCLRKH (COOKIES) H HFPL......................... 366
KOPOTKO ......................................................................................................................................................................... 366
RPHPOXEHHE PH ROEPXKH MHOFOROPL3OBATEPLCKOH EECEL (CT) .................................................................................366
TEHEBLE ROCLPKH (COOES) .........................................................................................................................................366
HFPA........................................................................................................................................................................367
HEROCPECTBEHHLE PELEHHR................................................................................................................................ 367
CO3AEM RPHPOXEHHE PH MHOFOROPL3OBATEPLCKOH EECEL..............................................................................................367
Hponeui saumi e cucmeue co uuoauuu nonisoeamennuu........................................................................................368
Opaomka amak muna omkas om ocnyueauun .....................................................................................................368
Bonmaeu us paysepa.............................................................................................................................................369
Ycmauaenueaeu nepuo ouoeneuun HTML ................................................................................................................369
Ovuaeu ouoeneuuie aneueumi HTML...................................................................................................................369
-HTAEM H 3ARHCLBAEM TEHEBLE ROCLPKH (COOES)...........................................................................................................372
Hcnonisoeauue meueeix nocinok ............................................................................................................................372
Kak sanucami meueeyk nocinky ..............................................................................................................................373
Kak npovumami meueeyk nocinky............................................................................................................................373
CO3AEM HFPY............................................................................................................................................................374
Xpaueuue auuix uey eisoeauu cueuapun e Web-cmpauuuax ....................................................................................374
Hacmpaueaeu uapy.................................................................................................................................................374
PERL. KPATKAR CRPABKA............................................................................................................................................. 378
POFH-ECKHE BEPH-HHL ................................................................................................................................................378
PA3LMEHOBLBAKHE RPE4HKCL....................................................................................................................................378
CKAPHPHLE REPEMEHHLE..............................................................................................................................................378
CRHCKH ....................................................................................................................................................................379
CKAPHPHLH H CRHCKOBLH KOHTEKCT................................................................................................................................379
MACCHBL..................................................................................................................................................................379
XSLH .......................................................................................................................................................................380
TYPEGLOB .................................................................................................................................................................380
OREPATOPL...............................................................................................................................................................380
RPHCBOEHHE AHHLX...................................................................................................................................................381
OREPATOPL CPABHEHHH................................................................................................................................................381
OREPATOPL PABEHCTBA ................................................................................................................................................381
OREPATOP F ..............................................................................................................................................................381
OREPATOP UNLESS.......................................................................................................................................................382
OREPATOP FOR ...........................................................................................................................................................382
OREPATOP FOREC.....................................................................................................................................................382
OREPATOP WLE.........................................................................................................................................................382
OREPATOP UNTL .........................................................................................................................................................383
Crp.14
MOH4HKATOPL F, UNLESS, UNTL H WLE .........................................................................................................................383
KOMAHL YRPABPEHHH UHKPOM.......................................................................................................................................383
OREPATOP GOTO .........................................................................................................................................................383
RORPOFPAMML..........................................................................................................................................................384
-TEHHE APFYMEHTOB, REPEAHHLX RORPOFPAMME .............................................................................................................384
BO3BPAT 3HA-EHHH RORPOFPAMMAMH .............................................................................................................................385
CCLPKH ....................................................................................................................................................................385
PA3LMEHOBAHHE CCLPOK..............................................................................................................................................385
CREUHAPLHLE (BCTPOEHHLE) REPEMEHHLE .......................................................................................................................385
RAKETL ....................................................................................................................................................................386

O an1ope
C1nnen Xonsnep nauan sannmartcx xstxom Perl saonro o roro, xax y nero noxnnnact
mtcnt o nanncannn xnnr o nem. On onapyxnn, uro or nporpammnponannx na Perl moxno
nonyunrt maccy yonontcrnnx. Crnn nporpammnponan Hnrepner-npnnoxennx axe pant-
me, uem Cert crann nastnart Hnrepnerom, n on namen Perl neantnoe cpecrno nx CGI-
npnnoxenn. On nncan xnnrn n crartn npo mnorne pasent nporpammnponannx (n na-
croxmn momenr on paoraer na cnoe copox noctmo xnnro). On raxxe tn peaxro-
pom xononxn xypnana PC Maga:ine.
Crnn nonyunn oxropcxym crenent no ]nsnxe n Kopnenoncxom ynnnepcnrere (Cornell
University), n xoropom on n reuenne ecxrn ner npoxonn craxnponxy n oyuenne. Kpome
roro, on saxonunn Maccauycercxn rexnnuecxoro nncrnryr.
B nacroxmee npemx Crnn n ero xena Hencn xnnyr nno n Ancrpncxnx Antnax, Tenrnye,
nno n neontmom xnnonncnom ropoxe na noepexte Hono Anrnnn.
Bnaropapuoc1u
3ra xnnra xnnxercx pesyntrarom rpya mnornx nme. t xoren ocoenno nonaroa-
pnrt Cre]annm Vonn (Stephanie Wall), peaxropa no oropy pyxonnce, sa rpynym pao-
ry; Tonn 3axxapnnn (Toni Zuccarini), peaxropa npoexra, xoropt na nporxxennn ncero
npemenn ero ntnonnennx conepman rnranrcxym paory no oennennm noenno uacre
npoexra n xonrponm na nx cosannem; Benn Hnrnn (Wendy Littley), xoopnnaropa nt-
nycxa xnnr, xoropax orcnexnnana nect nponecc; Fonn Cmnr (Bonie Smith), peaxropa-
xoppexropa, xoropax ycnemno oonena nect rexcr n npnnena ero no nnonne unraemt nn;
3npen Hnntcen (April Nielsen) n xon Vnnxnep (Jody Winkler) sa nsan xnnrn. Oco-
te naroapnocrn enny Vnntxmcy (David Williams) sa rexnnuecxoe peaxrnponanne
xnnrn. Cnacno ncem, +ro tna ornnunax paora!
Bnepeuue
Hent +ro xnnrn art nam nect marepnan, neoxonmt nx roro, urot crart npo-
rpammncrom na xstxe Perl. H +ro ronopnr o mnorom. Perl +ro ne npocro otunt xstx
nporpammnponannx. On nosyxaer npeannocrt, crpacrt, +xsantrnponannocrt, +xcnen-
rpnunocrt (ne ronopx yxe o paspaxennn n paccrpocrne). Perl ontme, uem xstx, nx
no+ron n ]anaron nporpammnponannx, on ncrounnx rnopuecxoro noxnonennx n cpecrno
nx ero peannsannn. On moxer trt cnoxntm n saraountm, nnoxmnm n sanyxenne n
Crp.15
npornnopeunntm. Ho nx ncrnnnoro paspaorunxa pyroro nyrn ner. Bt nomere, uro
nmenno x nmem n nny, xora npourere +ry xnnry.
Ha nenanart ro cnoero rocnocrna Hparmu:ecru Bsir o. Hse.e:euu mercmoe u
Ieuepauu om:emoe (Practical Extraction and Reporting Language) raxxe nastnaemt
nexoroptmn Hamo.oeu:ecru Jr.ermu:ui Bsir o. Pacne:amor Uenvxu (Pathologically
Eclectic Rubbish Listing), cran nceomnm nmnmnem. Horpxcammee xonnuecrno nme
nocnxrnno nneuarnxmmee xonnuecrno cnoonoro npemenn paore c nnm, ero conepmencr-
nonannm n noncemecrnomy pacnpocrpanennm. C momenra nauana moe paort c Perl o
noxnnennx nen cosannx xnnrn o nem npomnn mnorne rot. Bosmoxno, ynnen, xax pa-
oraer Perl, nt raxxe cranere ero noxnonnnxom.
-fo ecfu e sfo kuure
3ra xnnra ne rontxo oxcnxer cnnraxcnc xstxa Perl, ona raxxe aer peanncrnuecxoe
npecrannenne o rom, uro ns cex npecrannxer Perl ceronx n xax on ncnontsyercx. Ha-
npnmep, xora nt sarnxtnaere n mnp Hnrepnera, Perl yxnantno noncmy no+romy n
xnnre yenxercx ocrarouno mnoro nnnmannx CGI-nporpammnponannm. Taxxe nonynxp-
nt ceronx nonpoc o cnxsn mexy Perl n Tk, xoropt nosnonxer c nomomtm Perl ntno-
nrt na +xpan oxna, xnonxn, menm n pyrne +nemenrt rpa]nuecxoro nnrep]eca, cran
ononnnrentno remo anno xnnrn. 3arparnnamrcx raxxe n pyrne nnonne npaxrnue-
cxne nonpoct rnna noxnmuennx Perl x asam anntx, cepnepam, paorammnm no rexno-
nornn Windows OLE Automation, x npounm nponeccam n r. . Bce +ro nt naere n +ro
xnnre. B nenom, ona nanncana rax, urot art nam nanonee nonnoe npecrannenne o rom,
uro nponcxonr c xstxom Perl ceronx.
Kpome nn]opmannn nenocpecrnenno o xstxe Perl nepcnn 5 n +ro xnnre nt raxxe nae-
re mnoxecrno ononnnrentntx cneenn +ro n cosanne onee unraemoro Perl-xoa, n
noxn BEGIN n END nx naxeron, conmecrnmocrt c onepannonntmn cncremamn xnacca
POSIX, oexrno-opnenrnponannoe nporpammnponanne, nponsnontno nnoxennte crpyx-
rypt anntx, nexcnuecxne onacrn nnnmocrn, pacmnpennte nosmoxnocrn no ncnontso-
nannm moyne, a raxxe pyrne remt. Bepcnx 5 crana necrxme peannsanne nosmoxno-
cre xstxa Perl, no+romy xnnra nanncana nmenno na ocnone +ro nepcnn.
Knnra pasnra na orentnte, nerxo unraemte orptnxn (npnmepno 500 rem), xaxt ns
xoroptx ornocnrcx x onpeenennomy paseny nporpammnponannx. Bor nexoropte ns nnx:
Cnnraxcnc xstxa Perl nepcnn 5: xomant n onncannx.
Hnrepaxrnnnt sanycx Perl-cnenapnen.
Texcront nno n ntno.
Cosanne cxanxpntx nepemenntx.
Cxanxpnt xonrexcr n xonrexcr cnncxa.
Cosanne maccnnon n x+me.
Hnxnt n ycnonnte oneparopt.
Tannnt cnmnonon n rnn anntx typeglob.
Oneparopt xstxa Perl.
Perynxpnte ntpaxennx n paora co crpoxamn.
Cosanne nonporpamm.
Hepemennte c nexcnuecxo onacrtm nnnmocrn.
Bpemennte nepemennte.
Vcrounnte (persistent), nnn crarnuecxne nepemennte.
Crp.16
Pexypcnnnte nonporpammt.
Anonnmnte maccnnt, x+mn n nonporpammt.
Cctnxn n xstxe Perl.
Cnmnonnuecxne cctnxn.
Vcrounnte (persistent) orpannunrenn onacrn nnnmocrn.
Banont ]ynxnn.
Cnennantnte nepemennte Perl.
Bcrpoennte ]ynxnnn Perl.
uynxnnn POSIX.
uopmart xstxa Perl.
Bsanmoecrnne mexy nponeccamn.
Texnonornn Win32 OLE Automation.
Paora c ]anamn.
uant as anntx DBM.
Fnoxnponxa ]anon.
Oneparopt Perl nx paort c ornannennxmn.
Tecrt tcrpoecrnnx.
Oneparopt, uyncrnnrentnte x noxantntm nacrpoxam.
Fesonacnoe nsonnponanne xoa.
Perl/Tk: ncnontsonanne nnnorexn Tk.
Cnoxnte sanncn.
Maccnnt maccnnon, x+mn x+me, maccnnt x+me n x+mn maccnnon.
Cnxsannte cnncxn n xontnente y]ept.
Haxert Perl.
Koncrpyxropt n ecrpyxropt naxeron.
Pasnnxa naxera na necxontxo ]anon.
Moynn Perl.
3xcnopr cnmnonon ns moyne.
Anrosarpysxa nonporpamm moyne.
Knacct Perl.
Koncrpyxropt xnaccon.
Oexrt Perl.
Merot xnaccon.
Hepemennte +xsemnnxpa xnacca n nepemennte xnacca.
Hacneonanne xnaccon.
Mnoxecrnennoe nacneonanne.
Cnxstnanne cxanxpon, maccnnon n x+me.
Hepexnar omnox npemenn ntnonnennx.
Ornaunx Perl.
stx PerlScript.
Hporpammnponanne Common Gatewav Interface (CGI-nporpammnponanne).
Cosanne n ncnontsonanne ]opm HTML n CGI.
3amnra anntx nx CGI.
Meuente annte n noccranonnenne anntx.
Kax art nontmennt npnopnrer n npana CGI-cnenapnm.
Cosanne cnoero Web-cuerunxa.
Cosanne rocreno xnnrn.
Kax nocnart nnctmo uepes CGI-cnenapn.
Omenne n peantnom npemenn (cosanne uar-npnnoxenn).
Bonpoct cexpernocrn nx mnoronontsonarentcxoro pexnma.
Orpaxenne arax na cepnep.
Ouncrxa ononnenntx +nemenron ynpannennx HTML.
Crp.17
Cosanne renentx noctnox (cookies).
3annct anntx na Web-crpannny nepe ntsonom cnenapnx.

Hmemrcx ono nnn na cornamennx, xoropte x ncnontsym n +ro xnnre n o xoroptx nam
nao snart. Kora mne nao yxasart na xonxpernym crpouxy xoa, x ntenxm ee cneym-
mnm opasom:
$fexf $fexf $fexf $fexf = = = = "he11o "he11o "he11o "he11o\ \\ \n" n" n" n"
p1nf $fexf
A urot orennrt pesyntrar paort cnenapnx or cocrnenno cnenapnx, x ntenxm ero rax:
$fexf = "he11o\n"
p1nf $fexf
he11o
-fo ee ear nofpeyefcn
B +ro xnnre x ncnontsym nnrepnperarop xstxa Perl nepcnn 5.005. Perl xnnxercx cnoono
pacnpocrpanxemtm nporpammntm npoyxrom. Bce, uro nam nao cenart, +ro sarpy-
snrt ero ns Hnrepnera n ycranonnrt (cm. pasen Kax cxonnponart n ycranonnrt Perl n
rnane 1). Ecnn nt paoraere n mnoronontsonarentcxo cncreme, n ne yxe moxer trt yc-
ranonnen Perl. urot nponepnrt +ro, nonpoyre ntnonnnrt xomany
pe1 -v
xoropax ntneer nepcnm namero Perl-nnrepnperaropa.
Hocuasua. Eue napa sa+e:auu nepeo me+, rar ei ua:ueme ca+ocmome.iuo paoomami c Perl. B coeemvm, :mo-
oi ei ucno.isoea.u r.m: -w e ro+auouo cmpore npu sanvcre uumepnpemamopa. B omo+ c.v:ae e npoecce oopa-
oomru ceuapu Perl npu ueooxoou+ocmu ovoem eieooumi npeovnperoamuue coooueuu (roeoa-uuovoi omo cma-
uem noeeoeuue+ uumepnpemamopa no v+o.:auum). Bmopo coeem. saoaeame e ceuapuu npae+v use strict e
omo+ c.v:ae Perl mpeovem, :mooi ece nepe+euuie oi.u onucaui e euo+ euoe. Bino.ueuue omux oevx npocmix
coeemoe corouo+um ea+ voueume.iuo +uoeo epe+euu o. om.aoru.
Bam raxxe norpeyercx nncrpymenr nx cosannx Perl-cnenapnen. Cnenapnn +ro npo-
cro rexcronte ]ant, coepxamne xomant n onncannx xstxa Perl. urot cosart cne-
napn nx Perl, nam nyxen rexcront peaxrop, xoropt coxpanxer peaxrnpyemte ]a-
nt n ]opmare npocroro rexcra. (Ornocnrentno noponocre cm. pasen Kax nanncart
cnenapn nx Perl n rnane 1.)
Ormeuy, uro nam ue norpeyercx rnyoxoe snanne onepannonno cncremt Unix, nx xoro-
po ncxono cosanancx xstx Perl. Xorx mnorne xnnrn no Perl cunramr xax annoe, uro nt
xnnxerect nporpammncrom n cpee Unix, nx anno xnnrn +ro ne rax. Perl anexo ntmen
sa pamxn Unix, n nacrano npemx, urot pyxonocrna no Perl npnsnann +ror ]axr
1
.
pyrue pecypcu
Cymecrnymr n pyrne pecypct, xoropte moryr nomout npn paore c Perl. K nn-
repnperaropy npnnaraercx omnpnax n nonesnax oxymenrannx. B cncremax rnna Win-
dows +ra oxymenrannx npeocrannxercx n nne cnxsanntx HTML-crpannn. nx mnoro-

1
B +rom ornomennn xnnra ycrapena npaxrnuecxn nce conpemennte pyxonocrna no Perl ne nonepramr comnennm
cymecrnonanne nnar]opm, ornnuntx or Unix (npexe ncero, MS Windows). Hecomnenno, onaxo, uro npannntno Perl
paoraer rontxo nx Unix-noontx onepannonntx cncrem. Hpu+e:. peo.
Crp.18
nontsonarentcxnx cncrem nt, xax npannno, nonyuaere ocryn x +ro oxymenrannn c no-
momtm cncremntx xoman (noono xomane man onepannonno cncremt Unix).
nx nporpammncron na Perl nmeercx raxxe px renexon]epennn (rpynn nonocre USENET):
comp.lang.perl.announce rpynna c nnsxnm noroxom coomenn.
comp.lang.perl.misc rpynna c nnrencnnntm noroxom coomenn (cma, n uacrnocrn,
pacctnaercx FAQ-]an no Perl).
comp.lang.perl.modules nce, nmemmee ornomenne x cosannm moyne n mno-
roxparno ncnontsyemoro xoa.
comp.lang.perl.tk o cnxsxx Perl n oxonno-rpa]nuecxo nnnorexn Tk. Onn
noepxnnamr ontmoe xonnuecrno nnsyantntx nnrep]ecntx +nemenron (xnonxn, menm
n r. .). Bt moxere ncnontsonart nx n Perl, uro cranonnrcx onontno nonynxpntm.
Ecnn nt nnrepecyerect CGI-nporpammnponannem, nsrnxnnre na rpynny nonocre:
comp.infosvstems.www.authoring.cgi +ra rpynna ne coepxnr manona perl n
cnoem nmenn, onaxo +ro xopomee mecro nx ocyxennx c pyrnmn paspaorunxamn
ocoennocre CGI-nporpammnponannx na Perl.
B Cern nmemrcx raxxe mnorouncnennte Web-crpannnt, nocnxmennte Perl (cnyuant noncx
no Bcemnpno nayrnne nosnpamaer onee 1 527 903 crpannn, na xoroptx ynomnnaercx Perl):
omamnxx crpannna Perl www.perl.com, sect nt cmoxere narn ncxont
xo nnrepnperaropa Perl n roronte nporpammt no pasnnunte onepannonnte
cncremt, oxymenrannm, moynn, coomennx o omnxax, a raxxe FAQ cnncox or-
neron na uacro saanaemte nonpoct (on naxonrcx na www.perl.com/perl/Iaq).
urot sarpysnrt cam Perl, ero moynn, pacmnpennx n ronnt pyrnx nmemmnx
ornomenne x nemy neme, sarnxnnre n apxnn CPAN (Comprehensive Perl Archive Net-
work) na www.cpan.org nnn www.perl.com/CPAN-local/CPEN.html. 3ro orpomnt, o-
ennxmmn necxontxo ysnon ncrounnx nourn uro ncero, uro nmeer ornomenne x Perl.
Ecnn nt nporynxerect no apxnny CPAN, ro rapanrnponanno naere ram nyxnt nam
xo or pacmnpenn xstxa Perl o opaorxn nsopaxenn, or moyne nx paort n
Hnrepnere o nnrep]econ x asam anntx.
Hncrnryr Perl na www.perl.com +ro nexommepuecxax oprannsannx, ute
nentm xnnxercx, no ee cocrnenntm cnonam, noepxxa Perl ocrynntm, pa-
orocnocontm n ecnnarntm nx ncex. Hncrnryr, copan no cnonm xptnom nner
copyxecrna nmnrene Perl, oecneunnaer ceptesnym noepxxy omena nn]opmann-
e mexy nporpammncramn na Perl.
Crpannna, nocnxmennax camomy xstxy Perl, naxonrcx na www.perl.com/perl/
(cma xe nac npnneer cctnxa language.perl.com). 3ect naxoxrcx osopt,
nonocrn, cnncxn pecypcon, nporpammnoe oecneuenne. 3ect xe pacnonoxen
xaranor cnncxon pacctnxn (mailing lists), nocnxmenntx Perl.
Mnorne ysnt, cnennannsnpymmnecx na raxnx nonpocax, xax oecneuenne cexpernocrn,
CGI-nporpammnponanne n r. ., coepxar pasent, ornocxmnecx x Perl, ecnn ne on-
rect yronyrt n noroxe noono nn]opmannn, npocro nponenre Web-noncx.
Taxxe uertpe pasa n ro nsaercx xypnan no xstxy Perl. Fontme ysnart o nem moxno, sa-
Crp.19
rnxnyn na crpannuxy orwant.www.media.mit.edu/theperlournal/
1
.
Haxonen, mnoxecrno nerpnnnantntx pemenn nponem, penenron n coneron nt nae-
re n xnnre T. Kpncrnancena n H. Topxnnrrona Perl: nnnorexa nporpammncra, ntny-
menno nsarentcrnom Hnrep n 2000 r. (opnrnnantnoe nasnanne Perl Cookbook, ns-
arentcrno OReilly, 199 r.)
Hrax, ncx neoxonmax nnonax nn]opmannx nonyuena camoe npemx npncrynnrt x nsy-
uennm Perl. Haunem, xax nonrcx, c rnant 1.
Of uspafenucfea
Bamn sameuannx, npenoxennx, nonpoct ornpannxre no apecy +nexrponno nourt
comppiter-press.ru(nsarentcrno Hnrep, xomntmrepnax peaxnnx).
Mt yem pat ysnart name mnenne!
Hoponym nn]opmannm o namnx xnnrax nt naere na Web-care nsarentcrna
http://www.piter-press.ru.

1
Hnn xe na www.tp.com. Hpu+e:. peo.
Crp.20
Hac1u I
Cuu1akcuc PerI






Fnana 1
Ocuonm PerI

Kopo1ko
B +ro rnane paccxastnaercx o ocnone ocnon Perl cosannn n sanycxe cnenapnen Perl.
Hantxn, nonyuennte n +ro rnane, oecneuar unrarenm, ne snaxomomy c Perl, ]ynamenr,
xoropt norpeyercx nx nocnpnxrnx ocrantntx uacre xnnrn. Kpome cosannx n sanycxa
cnenapnen mt paccmorpnm ntno rexcra na +xpan n nno rexcra c xnannarypt. Bosmoxno,
unrarent yxe snaxom c ontme uacrtm marepnana +ro rnant, n +rom cnyuae ona moxer
cnyxnrt osopno. Onaxo uacrt marepnana, no-nnnmomy, yer nono. Hanpnmep, ne-
mnorne nmn nancxnxy cnocont nepeuncnnrt, uro nmenno enaer raroi ns xnmue,
saanaemtx n xomanno crpoxe npn sanycxe nnrepnperaropa.
stx Perl tn cosan n 19 roy xax nncrpymenr nx amnnncrpnponannx n xon]nrypn-
ponannx cncremntx pecypcon n cern, cocroxme ns Unix-xomntmrepon. Hocrenenno Perl
(utx apennarypa pacmn]pontnaercx xax Hparmu:ecru Bsir o. Hse.e:euu mercmoe
u Ieuepauu om:emoe (Practical Extraction and Reporting Language) nnn xe nexno n
nacxono xax Hamo.oeu:ecru Jr.ermu:ui Bsir o. Pacne:amor Uenvxu (Pathologi-
cally Eclectic Rubbish Listing)) +nonmnnonnponan n mexnnar]opmennt xstx n oxasancx n
nenrpe nnnmannx nponnerammero xnepnernuecxoro coomecrna.
(Bt moxere cnpocnrt; nouemy Perl, a ne Pearl, ro ecrt xemuyxnna eno n rom, uro
rpa]nuecxn xstx c nmenem Pearl x momenry cosannx Perl yxe cymecrnonan. Kpome roro,
oparnre nnnmanne, uro rounax apennarypa cnon Practical Extraction and Reporting Lan-
guage, npn nxnmuennn nepntx yxn ecex cnon, yer npecrannxrt coo nmenno Pearl.)
Perl xnnxercx nnrepnpernpyemtm xstxom, npenasnauenntm nx cxannponannx rexcrontx
]anon, nsnneuennx ns nnx nn]opmannn n ntnoa, na ocnone nonyuenntx raxnm opasom
anntx, rexcrontx orueron. To ecrt nporpamma perl (oparnre nnnmanne na orcyrcrnne
sarnanno yxnt) ncnontsyercx nx ntnonnennx cnenapnen Perl. (Xorx xomnnnxropt Perl
Crp.21
roxe cymecrnymr.) B +ro rnane mt naunem nsyuenne cnenapnen Perl.
Hexoropte nmn ynnnxmrcx nonynxpnocrn Perl (xstxa, opnenrnponannoro rexcront
nno n ntno n sanycxaemoro ns xomanno crpoxn) n mnpe rpa]nuecxnx nnrep]econ
rnna Windows. Honynxpnocrt Perl npoonxaer pacrn no pxy npnunn.
Mnorne onepannonnte cncremt ocramrcx rexcrono-opnenrnponanntmn.
Perl xnnxercx mexnnar]opmenntm xstxom, maxcnmantno nenrnuno noepxnnaemtm
n pasntx onepannonntx cncremax, n ornnuaercx rontxo n necxontxnx nensexntx e-
ranxx (raxnx, xax uncno aron, ncnontsyemtx nx npecrannennx nnnnoro nenoro).
Ha camom ene Perl oo.aoaem onpeenenntmn rpa]nuecxnmn nosmoxnocrxmn sa cuer
nsanmoecrnnx c nonynxpntm moynem Tk.pm (mt paccmorpnm ero n rnane 13). an-
nt moynt nosnonxer ncnontsonart cranaprnte rpa]nuecxne nnrep]ecnte +nemen-
rt (widgets) c nomomtm ncnomorarentnoro cpecrna nnnorexn Tk xstxa Tcl. 3ro
nosnonxer cosanart ns Perl oxna c xnonxamn, menm n pyrnmn oexramn.
Onaxo c rouxn spennx xnnoro ontmnncrna nporpammncron rexymax nonynxpnocrt Perl
nonnrtnaercx nporpammnponannem Common Gateway InterIace (CGI- nporpammnponann-
em), npnmenxemoro nx onepann no nsanmoecrnnm xnnenr/cepnep n cpee Web. Kora
peut ner o cosannn Web-crpannn, rexcronax opnenrnponannocrt xstxa nepecraer trt
neocrarxom, rax xax onn raxxe xnnxmrcx uncro rexcrontmn oexramn. CGI-
nporpammnponanne na Perl npecrannxer coo ouent momnt nncrpymenr, n, coornercr-
nenno, +ro ona ns ocnonntx rem, xoropte mt yem paccmarpnnart.
A renept nepeem x sanycxy necxontxnx cnenapnen.

Henocpepc1neuume pemeuun
Kak ckonupoeafu u ycfauoeufu Perl
Perl xnnxercx cnoontm nporpammntm npoyxrom. ocrarouno cxonnponart n ycranonnrt
ero. Ecnn nt paoraere na xomntmrepe c necxontxnmn nontsonarenxmn nnn n mnoronontso-
narentcxo cncreme, ro Perl, nosmoxno, yxe ycranonnen. Honpoyre ntart xomany
xpe1 -v
n xomanno crpoxe.
Hocuasua. Ha npomreuuu omo ruueu suar npoeuma (") e ua:a.e cmporu osua:aem npue.aueuue ro-
+auouo cmporu o. onepauouuo cucme+i Unix u eeooumi eeo ue uaoo.
Ecnn Perl ycranonnen n naxonrcx n onom ns nyre noncxa, +ra xomana ntneer nomep
nepcnn n nomep narua namero nnrepnperaropa (narun Perl ntnycxamrcx perynxpno nx
ncnpannennx orentntx omnox).
Oparnre nnnmanne, uro nx nexoroptx cncrem nnrepnperarop Perl, ncnontsyemt no
ymonuannm, ornocnrcx x onee panne nepcnn nanpnmep, x nepcnn . urot ncnontso-
nart Perl nepcnn 5, nam norpeyercx xomana rnna perl5 (nonpoyre ee, ecnn perl -v nt-
aer nepcnm, ornnunym or 5):
xpe15 -v
Crp.22
Ecnn Perl ne ycranonnen, nt moxere narn ero na www.perl.com nnn www.cpan.org.
(CPAN Comprehensive Perl Archive Network camt nonnt +nexrponnt apxnn ma-
repnanon, nmemmnx ornomenne x xstxy Perl. Ho mepe urennx +ro xnnrn nt ysnaere o
+rom pecypce ontme.) Ha +rnx ysnax nt moxere narn n sarpysnrt nce, uro nam nyxno.
namepenno ne conpamct onnctnart nponecct nncrannxnnn, xoropte nyxno ntnon-
nnrt nx pasntx onepannonntx cncrem, urot ycranonnrt Perl. Bo-nepntx, +rn nponey-
pt rmarentno erannsnponant n onncant na yxasanntx ysnax (nanpnmep, pyxonocrno
no nncrannxnnn Perl nx Unix naxonrcx na www.perl.com/CPAN-
local/20doc/relinIo/INSTALLhtml). Bo-nroptx, onn nonepxent cnonranntm nsmenenn-
xm, xoropte ne moryr trt orpaxent n xnnre. (Mnorne xnnrn ycrapenn sa cuer onncannx
erantntx nncrpyxnn no nncrannxnnn nanpnmep, xnnrn no xstxy Java, nocxontxy
+rn nncrpyxnnn nsmenxnnct, uyrt nn ne momenrantno c noxnnennem nono nepcnn.)
Camax nocnenxx nepcnx Perl moxer trt nonyuena, ecnn ntpart cctnxy Get the latest ver-
sion oI Perl na ysne www.perl.com. 3ra cctnxa npnneer nac x crpannuxe, na xoropo nepe-
uncnent nepcnn Perl nx nanonee nonynxpntx onepannonntx cncrem (nanpnmep, ActiveState
Perl nx Win32). Venrect, uro nt nonyunnn nepcnm 5.005 nnn onee nosnmm, nocxontxy
npetymne nepcnn Perl nx Win32 ne nnonne conmecrnmt c Perl nx Unix n ero moynxmn.
Kak uanucafu cueuapu pnn Perl
Cnenapnn Perl npecrannxmr coo npocrte rexcronte ]ant, n xoroptx naxoxrcx onncannx
n xomant. (Kax nt ynnnre nosnee, nce, uro nam nao cenart, +ro onncart ]opmart n
nonporpammt.) urot cosart cnenapn Perl, nao nmert rexcront peaxrop nnn rexcront
nponeccop, xoropt moxer coxpanxrt peaxrnpyemte ]ant n ]opmare npocroro rexcra.
Coxpanenne ]ana n ]opmare npocroro rexcra +nemenrapnoe ecrnne, xoropoe moxer
oxasartcx sa npeenamn nosmoxnocre xnrpoymnoro rexcronoro nponeccopa. Hanpnmep,
nerxo cronxnyrtcx c rpynocrxmn npn paore c nporpammo MicrosoIt Word, xorx n ram
moxno coxpannrt pesyntrar peaxrnponannx xax npocro rexcr, ecnn nt ncnontsyere ox-
no nanora File[Save As. Omee npannno rnacnr: ecnn npn ntnoe ]ana na +xpan ns xo-
manno crpoxn (nanpnmep, c nomomtm xomant type) ne noxnnxercx crpanntx neyx-
nenntx cnmnonon, - +ro ]opmar npocroro rexcra. Hcrnnnax nponepxa, ecrecrnenno, yer
n rom, cmoxer nn Perl npounrart n cxomnnnnponart nam cnenapn.
B +ro xnnre nx cranaprntx cnenapnen Perl ncnontsyercx pacmnpenne .pl. Hanpnmep,
cnenapn, xoropt yer npnneen uyrt nosxe, nonyunr nmx hello.pl. Cnenapnn Perl ne
rpeymr raxoro pacmnpennx (nanpnmep, pyroe rnnnunoe pacmnpenne +ro .p) n, onee
roro, noome moryr es nnx oxonrtcx. Tem ne menee, nx Perl-cnenapnen rpannnonno
ncnontsymr pacmnpenne .pl. B uacrnocrn, nonynxpnt nnrepnperarop ActiveState Perl
nx Win32 acconnnpyer pacmnpenne .pl c nnrepnperaropom Perl, rax uro nt moxere sa-
nycxart cnenapnn anromarnuecxn, naxt menxnyn no nmenn ]ana. Ecrecrnenno, nnxro
ne sacrannxer nx cnenapnen ncnontsonart pacmnpenne .pl, panno xax n noome npncoe-
nnxrt pacmnpenne x nmenn ]ana.
Yepufecu, fo cueuapu croef uafu Perl
Kax nt ysnaere ns pasena Btnonnenne cnenapnen Perl, cymecrnymr na ocnonntx cno-
coa sanycxa Perl-cnenapnen. Bo-nepntx, moxno sanycrnrt nnrepnperarop Perl n xnnom
Crp.23
nne ns xomanno crpoxn:
xpe1 he11o.p1
Moxno raxxe nacrponrt cncremy rax, urot cnenapn cam sanycrnn nnrepnperarop Perl.
B +rom cnyuae cnenapn ntnonnxercx xomano rnna:
xhe11o.p1
nnn n cnyuae, xora rexymn xaranor ne nxnmuaercx n nyrt noncxa no coopaxennxm
esonacnocrn, xomano rnna
1
:
x./he11o.p1
Cncrema nacrpoex n pasnnuntx onepannonntx cncremax ycrpoena no-pasnomy.
UN
Onepannonno cncreme Unix moxno oxcnnrt, uro nx sanycxa cnenapnx nao ntsnart
nnrepnperarop Perl, ecnn n nepno crpoxe ]ana naxonrcx cneymmn rexcr (yurnre, uro
raxo crpoxn ne rpeyercx, ecnn nt sanycxaere cnenapn na ntnonnenne otuntm cno-
coom):
#!/us/1oca1/b1n/pe1 # use pe1
Crpouxa c raxo cnenn]nuecxo cnnraxcnuecxo xoncrpyxnne, xax #!, oxsarentno
onxna croxrt n ]ane nepno. 3ra crpoxa coepxnr cctnxy na xaranor, n xoropom na
ontmnncrne xomntmrepon c onepannonno cncremo Unix pacnonaraercx nnrepnperarop
Perl. Perl raxxe moxer pacnonarartcx n pyrom mecre - nanpnmep, /usr/bin/perl (oparnre
nnnmanne, uro na mnornx mamnnax oa nyrn cctnamrcx na onn n ror xe ]an). urot
ntxcnnrt, re naxonrcx Perl, ncnontsyre xomany which perl.
urot yxasart, uro nam rpeyercx Perl 5, nx ontmnncrna cncrem moxno ncnontsonart
crpoxy
#!/us/1oca1/b1n/pe15 # use Pe1 5
Hpn sanycxe nnrepnperaropa, urot rapanrnponart ntno npeynpexammnx coomenn
no mepe opaorxn nnrepnperaropom namero xoa, pexomenyercx ncnontsonart xnmu -w.
(Ha camom ene nnrepnperarop Perl xomnnnnpyer xo nennxom npn ero sarpysxe. Ho+romy
npeynpexammne coomennx noxnxrcx cpasy xe, ecnn rontxo nt ne sarpyxaere orxom-
nnnnponannt xo nosxe. 3ro moxno cenart c nomomtm xomant Perl require, xoropax
sarpyxaer xo no npemx ntnonnennx cnenapnx. Mt ynnnm, xax +ro enaercx, n rnane 15.)
#!/us/1oca1/b1n/pe15 -W # use Pe1 5 W1fh Wan1ngs
Hocxontxy no mnornx cncremax Unix crpoxa c manonom #! opesaercx nocne 32 snaxon,
npn nontrxe nnecrn nx Perl nnnnt nyrt nt ncrpernrect c nponemo:
#!/us/1oca1/b1n/uses/sfandad/bu11d36/pe15
B noontx cnyuaxx, a raxxe ecnn nama cncrema ne noepxnnaer cnnraxcnuecxne xon-

1
Hanpnmep, n Unix no yxasanntm coopaxennxm rexymn xaranor, xax npannno, ne nxnmuaercx n cnncox nyre noncxa.
Ho+romy ecnn nt npocro nneere nmx cnenapnx n xomanno crpoxe n nne hello.pl, ro nam cnenapn moxer trt
ntnonnen nnmt n rom cnyuae, ecnn on naxonrcx n xaranorax, xya otunomy nontsonarenm uro-nno sannctnart sa-
npemeno (Unix nonpocry ne cmoxer narn xomany hello.pl, ecnn ona ne naxonrcx n cncremnom xaranore). B cnny
+roro annt anropcxn npnmep tn namn cnerxa noxoppexrnponan. Hpu+e:. peo.

Crp.24
crpyxnnn nna !#, moxno ncnontsonart ntson xomannoro nnrepnperaropa sh c rem, uro-
t sanycrnrt Perl otuntm nyrem:
#!/b1n/sh
eva1 /us/1oca1/b1n/pe15 -W5 $0 ${1+"$0"} 1f 0
3ect ncnontsyercx xomana eval xomannoro nnrepnperaropa c rem, urot ntnonnnrt
sanycx Perl. Knmu -w oecneunnaer ntno npeynpexammnx coomenn. Hapamerp $0
onxen nxnmuart n cex nonnt nyrt, onaxo nnora +ror mexannsm ne paoraer. Ho+ro-
my xnmu -S sacrannxer Perl npn neoxonmocrn camocroxrentno ncxart cnenapn. Crpan-
nax xoncrpyxnnx $1+"$"] opaartnaer nmena c nnyrpennnmn npoenamn. Oparnre
raxxe nnnmanne, uro nam npnmep sanycxaer nnrepnperarop Perl, no ne nosnpamaer snaue-
nnx (xoa sanepmennx), rax xax oneparop if 0 nnxora ne xnnxercx ncrnnntm.
Bt moxere ncnontsonart crpoxy rnna
#!/us/1oca1/b1n/pe15 -W
n nauane ncex cnenapnen. Onaxo samertre, uro nx xparxocrn (n noromy, uro ontmnncr-
no onepannonntx cncrem raxym xoncrpyxnnm ne noepxnnamr) n cnenapnxx, xoropte
mt yem paspaartnart n +ro xnnre, raxax crpoxa yer onycxartcx. Ecnn nx namnx
cnenapnen ona rpeyercx, oantre ee camn.
Hocuasua. Hepeo me+ rar sanvcrami ceuapu Perl noo Unix e ra:ecmee ro+auoi (mo ecmi, npocmo vra-
siea eeo u+ e ro+auouo cmpore, uanpu+ep ./script.pl, a ue perl script.pl), ea+ uvruo npuceoumi
e+v cmamvc ucno.ue+oeo qa.a. . omoeo npoc+ompume pasoe. Bino.ueuue ceuapuee Perl, romo-
pi +oruo uamu e omo re e.aee ue+uoeo oa.ee.
MS-DOS
B onepannonno cncreme MS-DOS nt moxere rapanrnponart, uro cnenapn naer nn-
repnperarop Perl, ecnn npeopasyere ero n .bat-]an c nomomtm yrnnnrt pl2bat.bat. (Ona
nxonr n xomnnexr naxera ActiveState Perl.)
Hanpnmep, ecnn y nac ecrt cnenapn hello.pl:
p1nf "he11o!\n"
p1nf "Pess <Lnfe> fo conf1nue..."
<51DlN>
ro c nomomtm xomant
C:\>p12baf he11o.p1
nt npeopasyere ero n ]an hello.bat. Pesyntrnpymmn ]an yer ntrnxert cneym-
mnm opasom:
0em = --"-Pe1-"--
0echo off
1f "xO5x" == "W1ndoWsN1" gofo W1nN1
pe1 -x -5 "x0" x1 x2 x3 x4 x5 x6 x7 x x9
gofo endofpe1

:W1nN1
pe1 -x -5 "x0" x"
1f NO1 "xCOM5PLCx"=="x5ysfemkoofx\sysfem32\cmd.exe" gofo endofpe1
0em
#!pe1
#11ne 14
p1nf "he11o!\n"
Crp.25
p1nf "Pess <Lnfe> fo conf1nue..."
<51DlN>
LND

:endofpe1
Windos 95/98 u Windos NT
Haxer ActiveState Perl nx Windows 95/9 n Windows NT mon]nnnpyer peecrp Windows
rax, uro ]ant c pacmnpennem .pl acconnnpymrcx c nnrepnperaropom Perl. nono men-
uox mtmtm na ]ane cnenapnx sanycxaer ero na ntnonnenne. Onaxo xora +ro nponcxo-
nr, orxptnaercx oxomxo MS-DOS, n nem sanycxaercx nnrepnperarop Perl, a sarem oxno
MS-DOS nemenenno saxptnaercx (o roro, xax nam yaercx npounrart ntneennt
rexcr). nx roro urot nsexart +roro +]]exra, nocmorpnre pasen Kax nsexart ne-
menennoro saxptrnx oxna MS-DOS n Windows 95/9/NT, xoropt moxno narn n +ro
xe rnane nemnoro nnxe.
Macintosh
Ha xomntmrepax Macintosh cnenapnn Perl ntnonnxmrcx anromarnuecxn - nx ntsona Perl
naxt menxnnre na cnenapnn mtmtm.
Kak uanucafu nporparry Perl: koraupu u onucauun
Hporpamma na Perl cocronr ns xoman n onncann. Onncannx yxastnamr Perl, xax nt co-
npaerect ncnontsonart onpeenennte nporpammnte xoncrpyxnnn o roro, xax +ro npo-
nsoer na camom ene. Onn nyxnt rontxo nx ]opmaron n nonporpamm, xorx moxno
raxxe onnctnart n pyrne +nemenrt rnna nepemenntx. Fonee nopono o onncannxx
peut noer n +ro xnnre antme.
Komant ncrpeuamrcx n nyx ]opmax: npocro n cocranno. Hpocma ro+auoa +ro ntpa-
xenne, ntnonnxmmee nexoropoe xonxpernoe ecrnne. B nporpamme npocrte xomant sa-
xanunnamrcx rouxo c sanxro (;), xax nponcxonr n cneymmem npnmepe, re ]ynxnnx print
ncnontsyercx nx ntnoa na +xpan crpoxn Hello!, sanepmaemo cnmnonom nepenoa crpoxn \n
(cm. pasen Ocnonnte xomant ]opmarnponannx anee n +ro rnane):
p1nf "he11o!\n"
Cocrannte xomant cocroxr ns ntpaxenn n noxon. Fnoxn n xstxe Perl orpannunnamrcx ]n-
rypntmn cxoxamn () n (]) n moryr coepxart necxontxo npocrtx xoman. Onn raxxe nmemr
cnon onacrn nnnmocrn (onacrt nnnmocrn +nemenron rnna nepemenntx +ro cermenr npo-
rpammt, n xoropom moxno ncnontsonart nepemennym; onee nopono +ror nonpoc paccmar-
pnnaercx anee). Hocne saxptnamme ]nrypno cxoxn ne nao crannrt rouxy c sanxro.
anee cneyer npnmep noxa, c nomomtm xoroporo cosaercx cocranno oneparop nnxna for
(+ro ]ynamenrantnt oneparop nnxna n Perl, mt yem nopono ero nccneonart n rnane 5):
fo {$1oop1ndex = 1 $1oop1ndex <= 5 $1oop1ndex++} {
p1nf "he11o!"
p1nf "fhee!\n"
}
Crp.26
Bunonueuue cueuapuee Perl
Hpenonoxnm, uro nmeercx ]an hello.pl co cneymmnm cnenapnem na Perl:
#!/us/1oca1/b1n/pe15 -W # use Pe15 W1fh Wan1ngs
p1nf "he11o\n"
xax ero ntnonnnrt 3ro ocnonnax onepannx npn paore c Perl. Onaxo, nocxontxy nmeercx ne-
cxontxo napnann na ocnonnym remy, mt cenaem xparxn osop nmemmnxcx nosmoxnocre.
Kak eau cueuapu roef uafu Perl car
Ecnn nam cnenapn moxer sanycrnrt nnrepnperarop Perl camocroxrentno (cm. pasen
Venrect, uro cnenapn cmoxer narn Perl panee n +ro rnane), nam nerxo ero ntnon-
nnrt. nx Unix +ro snaunr, uro nepnax crpoxa ]ana coepxnr rexcr rnna
#!/usr/local/perl5 -w. Kpome roro, cnenapn nao cenart ncnonnxemtm ]anom. 3ro
ocymecrnnxercx c nomomtm xomant
chmod +x he11o.p1
Taxxe cneyer yenrtcx, uro cnenapn pacnonoxen n onom ns nyre noncxa.
Hanpnmep, nx +roro cneyer nponepnrt nam ]an .login n nponecrn noncx xoman set
path, ecnn n xauecrne xomanno oonouxn ncnontsyercx csh nnn ona ns ero nponsno-
ntx. Ecnn xe nx +ro nenn ncnontsyercx sh nnn ananornunt nnrepnperarop, nponeptre
xomany PATH. B cnyuae npnmenennx pyroro rnna oonouxn ncnontsyre ee cnennant-
nte xomant (n xpanem cnyuae, cneptrect co cnpanounnxom). Hocne +roro sanycrnre
cnenapn na ntnonnenne, nnex n xomanno crpoxe xomany rnna:
xhe11o.p1
B onepannonntx cncremax Windows nnn Macintosh, urot sanycrnrt cnenapn, nyxno
naxt menxnyrt na ero nmenn. Venrect, uro n cnyuae Windows ]an nmeer pacmnpe-
nne .pl, nocxontxy naxer ActiveState Perl ncnontsyer nmenno +ro pacmnpenne nx acco-
nnnponannx ]anon cxpnnron c nnrepnperaropom Perl.
Ecnn nt paoraere n onepannonno cncreme MS-DOS, ro, npeopasonan c nomomtm yrnnnrt
pl2bat.bat Perl-cnenapn x ]opme xomannoro ]ana (cm. pasen Venrect, uro cnenapn
cmoxer narn Perl panee n +ro rnane), npocro sanycrnre +ror ]an ns xomanno crpoxn
C:\>he11o.baf
Kak ucnonusoeafu koraupuyk cfpoky
urot sanycrnrt cnenapn na ntnonnenne c nomomtm ntsnannoro n xnnom nne nnrepnpe-
raropa, yenrect, uro nporpamma c nmenem perl naxonrcx n onom ns nyre noncxa. 3arem
nnenre n xomanno crpoxe xomany perl. Ona moxer nmert cneymmn cnnraxcnc:
pe1 |-s1uu] |-hv] |-v|conf1gva]] |-cW] |-d|:debugge]] |-D|numbe/11sf]] |-pna]
|-Ipaffen] |-1|ocfa1]] |-0|ocfa1]] |-ld1] |-m|-]modu1e] |-M|-]modu1e...] |-P] |-5]
|-x|d1]] |-1|exfens1on]] |-e command] |--] |pogamf11e] |agumenfs]
(Knmun n xnaparntx cxoxax xnnxmrcx neoxsarentntmn. O nx nasnauennn peut no-
er anee n pasene Knmun xomanno crpoxn +ro rnant.)
Hpn sanycxe nnrepnperaropa Perl cnenapn nmercx cneymmnm opasom:
Crp.27
Ecnn saan xnmu -e, ro xomant nx Perl yxastnamrcx n xomanno crpoxe cneom sa
+rnm xnmuom.
Cnenapn epercx ns ]ana, xoropt cronr nepntm n cnncxe napamerpon xomanno
crpoxn (n namem npnmepe +ro programfile]).
Ecnn n xauecrne nmenn ]ana saan e]nc (-), ro cnenapn cunrtnaercx nocrpouno ns
cranaprnoro noroxa nnoa.
Paccmorpnm xaxt ns +rnx cnocoon.
Hcnontsonanne xnmua -e nosnonxer saanart xomant Perl n sanycxart nnrepnperarop ns
ono n ro xe xomanno crpoxn. (B nexoroptx cncremax nt moxere ncnontsonart ne-
cxontxo xnmue -e, urot nepeart necxontxo noxon xoman.)
Hanpnmep:
xpe1 -e p1nf "he11o!\n
he11o!
Onaxo c xantuxamn nao trt ocropoxnee, rax xax n pasntx onepannonntx cncremax
onn paoramr no-pasnomy. Bor, nanpnmep, xax ntrnxnr ra xe camax xomana n MS-DOS:
C:\>pe1 -e "p1nf \" he11o!\n\""
(Oparnre nnnmanne na escape-nocneonarentnocrn \", samenxmmne nonte xantuxn n
rene xomant Perl. Fonee nopono +rn xoncrpyxnnn paccmarpnnamrcx n pasene Ocno-
nt ]opmarnponannx rexcra anee n +ro rnane.)
Koneuno, moxno nomecrnrt cnenapn n rexcront ]an n nepeart nnrepnperaropy nmx
]ana. Hanpnmep, ecnn coepxnmtm ]ana hello.pl xnnxercx
p1nf "he11o!\n"
(marnuecxax crpouxa c manonom !# onymena, rax xax nnrepnperarop sanycxaercx n xnnom
nne), ro +ror cnenapn sanycxaercx xax
xpe1 he11o.p1 he11o!
Moxno raxxe nnonrt xomant Perl nocrpouno c xnannarypt, ecnn nmecro nmenn ]ana
yxasan e]nc:
xpe1 -
(3ect axe e]nc moxno onycrnrt, nocxontxy raxo mero npenonaraercx no ymonua-
nnm.) Tora nnrepnperarop yer xart, noxa nt ne nneere cnenapn:
xpe1 -
p1nf "he11o!\n"
Hnrepecnt nonpoc: a xax nnrepnperarop onpeennr, uro cnenapn nneen n nopa npn-
crynnrt x ero ntnonnennm 3ro nponsoer, xora na +xpane noxnnrcx nncrpyxnnx
__END__, osnauammax xonen rexcra:
xpe1 -
p1nf "he11o!\n"
LND
he11o!
Taxo mero osnauaer ntnonnenne ncero cnenapnx nennxom. Hpn recrnponannn +]]exrnnnee
nnonrt xomant no ouepen n ntnonnxrt nx n nnrepaxrnnnom pexnme. nx +roro neoxo-
nmo cosart mnnn-nnrepnperarop Perl (na Perl J), uem mt n samemcx n cneymmem pasene.
Crp.28
Hufepakfueuoe eunonueuue cueuapuee Perl
Bo npemx recrnponannx moxer norpeonartcx nocneonarentno ntnonnxrt xomant Perl
no mepe nx nnoa n nanmart na +xpane pesyntrar nx ecrnnx. nx +roro neoxonmo
nanncart Perl-oonouxy. Bor neontmo paorammn npnmep:
#!/us/1oca1/b1n/pe15 -W # use Pe1 5 W1fh Wan1ngs
use sf1cf # kequ1e va1ab1e dec1aaf1ons, efc.
my $counf = 0 # $counf used fo mafch {}, {}, efc.
my $sfafemenf = "" # sfafemenf ho1ds mu1f1-11ne commands
1oca1 $5lG{WAkN} = sub{} # suppess eo epof1ng
Wh11e {<>} { # Accepf 1npuf fom fhe keyboad
chomp # C1ean-up 1npuf
Wh11e {/{|\{1\|/g} {$counf++} # Wafch fo {, {, efc.
Wh11e {/}|\}1\]/g} {$counf--} # Wafch fo }, }, efc.
$sfafemenf .= $ # Append 1npuf fo cuenf sfafemenf
1f {!$counf} { # Lva1uafe on1y 1f {,{ mafches }, }
eva1 $sfafemenf # Lva1uafe Pe1 sfafemenf
1f {$0} {p1nf "5ynfax eo.\n"} # lf eo ...
$sfafemenf = "" # C1ea fhe cuenf sfafemenf
$counf = 0 # C1ea fhe counfe
}
}
3ror cnenapn npecrannxer coo npocremym Perl-oonouxy, xoropax moxer opaart-
nart cocrannte xomant, n rom uncne n re, uro oxnartnamr necxontxo crpox nnoa. On pao-
raer sa cuer ntsona ]ynxnnn Perl eval, xoropax ntuncnxer ntpaxenne, nepeannoe e n xaue-
crne aprymenra. 3ra nporpamma raxxe sanomnnaer nnonmt rexcr (pasnrt na necxontxo
crpox nnoa) o rex nop, noxa uncno orxptnammnx cxoox ne yer coornercrnonart uncny
saxptnammnx cxoox, n nnmt sarem opamaercx x ]ynxnnn eval. (o nexoropo crenenn +ro
rapanrnpyer, uro pasnroe na necxontxo crpox ntpaxenne oecmeume.iuo nneeno nonno-
crtm. Onaxo nnnmarentnt unrarent, esycnonno, samernr, uro raxax oonouxa ne enaer
pasnnunx mexy pasnnuntmn rnnamn cxoox n ne nponepxer npannntnym nx nnoxennocrt.)
Hanpnmep, moxno sanycrnrt +ror cnenapn n nnecrn xomany, xoropax sanocnr rexcr n nepe-
mennym Perl (ornocnrentno nepemenntx cm. cneymmym rnany):
$fexf = "he11o!\n"
a sarem pacneuarart ee:
$fexf = "he11o!\n"
p1nf $fexf
Pesyntrar noxnnrcx na +xpane nemenenno:
$fexf = "he11o!\n"
p1nf $fexf
he11o!
Touno rax xe moxno nponepnrt, xax ntnonnxmrcx mnorocrpounte xomant (rax xax xa-
xax xomana ntnonnxercx, xora ona nonnocrtm nneena, pesyntrar paort xomant
print noxnnxercx na +xpane mrnonenno):
$va1ab1e1 = 1
$va1ab1e2 = 2
p1nf $va1ab1e1 + $va1ab1e2
4
Ananornuntm opasom opaartnamrcx cocrannte xomant, sannmammne onee ono crpouxn:
Crp.29
fo {$1oop1ndex = 1 $1oop1ndex <= 5 $1oop1ndex++} {
p1nf "he11o!\n"
}
he11o!
he11o!
he11o!
he11o!
he11o!
urot ntrn ns oonouxn, nnenre xomany exit.
Cranaprnte oonouxn Perl (noonte anno) tnamr nonesnt npn recrnponannn xo-
porxnx cnenapnen, no ne rpeymr cosannx orentntx ]anon n nx sarpysxn n nnrep-
nperarop. (Oparnre nnnmanne, uro annax oonouxa - nnmt npnmep, a nnxonm opasom
ne saxonuennax oonouxa Perl. B uacrnocrn, ecnn recront cnenapn coepxnr xomany
eval, nt ncrpernrect c ceptesntmn nponemamn, rax xax oonouxa cama ncnontsyer xo-
many eval nx nomaronoro ntnonnennx xoman Perl.)
Knku koraupuo cfpoku
Hnrepnperarop Perl moxer ncnontsonart nneuarnxmmee xonnuecrno ononnnrentntx
xnmue, ynpannxmmnx ero paoro:
pe1 |-s1uu] |-hv] |-v|:conf1gva]] |-cW] |-d|:debugge]] |-D|numbe/11sf]] |-pna]
|-Ipaffen] |-1|ocfa1]] |-0|ocfa1]] |-ld1] |-m|-]modu1e] |-M|-] modu1e...]
|-P] |-5] |-x|d1]] |-1|exfens1on]] |-e command] |--] |pogamf11e] |agumenfs] ...
(xax otuno, xnaparnte cxoxn yxastnamr na neoxsarentnocrt coornercrnymme xon-
crpyxnnn).
Hrax, uro xe enamr nce +rn xnmun Onn nepeuncnent sect n an]annrnom nopxxe (mno-
rne, onaxo, npeocrannxmr nosmoxnocrn, xoropte mt ocynm nosnee):
-0 uqpi] saaer pasennrent nxontx sannce xax cnmnon, saannt noctmepnuntm
npecrannennem (+ror pasennrent raxxe coepxnrcx n cnennantno nepemenno Perl $/). Ho
ymonuannm n xauecrne pasennrenx ntcrynaer cnmnon \n. Ecnn xnmu -0 saan es nocne-
ymmnx nn]p, ro ncnontsyercx cnmnon \0.
-a nxnmuaer pexnm anropasnnxn (ecrnyer rontxo npn nannunn xnmue -p n -n). Hpn
+rom pexnme nxonte crpoxn pasnnamrcx na ]parmenrt no rpannne cnona n nomemamrcx n
cnennantnt maccnn F (ro ecrt nmecro cxanxpno nepemenno $_ nao yer ncnontsonart
ero).
-c sacrannxer Perl nponepxrt cnnraxcnc nxonoro cnenapnx n sanepmart paory es ero
ntnonnennx. Bnpouem, noxn BEGIN n END (xoncrpyxropt n ecrpyxropt moyne) yyr
ntnonnent n n +rom cnyuae.
-d sanycxaer cnenapn no ynpannennem ncrpoennoro ornaunxa Perl.
-d:u+ sanycxaer cnenapn no ynpannennem ornaunxa nnn rpaccnponmnxa, nncrannn-
ponannoro xax Devel::nun (ro ecrt ornaunx cymecrnyer xax nomoynt moynx Devel,
nmemmn yxasannoe nmx cm. rnany 15).
-D:uc.o/cnucor] ycranannnnaer ]narn pexnma ornaxn (aprymenr nonunax macxa
nnn cnncox ]naron). Fonee nopono ]narn ornaxn paccmarpnnamrcx n rnane 17.
-e ro+auoa nnn -e ro+auoi... ncnontsyercx nx nenocpecrnennoro ntnonnennx xo-
Crp.30
man Perl, saanaemtx npxmo n xomanno crpoxe. B nexoroptx cncremax moxno ncnontso-
nart ononpemenno necxontxo xnmue -e, pasnnax cnncox xoman Perl na noxn.
-Fuao.ou nnn -F/uao.ou/ saaer manon nx pasnnxn nxontx crpox na noxn, xora
saant xnmun -n nnn -p, a raxxe xnmu -a. Cnmnont naxnonno ueprt, oxpyxammne manon
pasnnxn, neoxsarentnt.
-h ntnonr xparxn cnncox xnmue c noxcnennxmn.
-i nnn -ipacuupeuue paspemaer peaxrnponart na mecre ]ant, orxptrte ns cran-
aprnoro noroxa nnoa (xoncrpyxnnx <> cm. anee pasen urenne noroxa nnoa). Pexnm
peaxrnponannx na mecre osnauaer, uro nsmenennx nnocxrcx nenocpecrnenno n orxptrt
]an, xora nt ntnonre annte n norox nnoa STDIN. Ecnn nocne xnmua yxasano pacmn-
penne, ro Perl oecneunnaer pesepnnym xonnm peaxrnpyemoro ]ana, cosanax ]an c sa-
anntm pacmnpennem n rem xe camtm nmenem. Ecnn xe pacmnpenne ne yxasano, ro npemen-
nax xonnx ]ana, cosannax nnrepnperaropom, yanxercx nocne sanepmennx paort.
-Irama.oe - saaer xaranor, n xoropom Perl nmer moynn. B xomanno crpoxe moxer trt
saano necxontxo napamerpon -I.
-l ynpannxer opaorxo cnmnonon nauana nono crpoxn. Ecnn +ror xnmu saan nme-
cre c -p nnn -n, Perl npn nnoe anromarnuecxn yanxer cnmnon nauana nono crpoxn (sa-
annt n cnennantno nepemenno $/). Ononpemenno npn ntnoe oneparopom print ror
xe cnmnon oannxercx n xonen crpoxn.
-luqpi n ononnenne x ]ynxnnxm npetymero xnmua nosnonxer saart n xnnom
nne cnmnon, ntcrynammn n xauecrne nauana nono crpoxn (n sanecrn ero n nepemennym
$/) nx +roro ocrarouno saart noctmepnunoe uncno, onpeenxmmee xo cnmnona. Ho
ymonuannm n xauecrne pasennrenx crpox ncnontsyercx cnmnon \n.
-m-]+oov., -M-]+oov. nnn -M-]+oov.u... es snaxa e]nca +ror xnmu noxnm-
uaer yxasannte moynn. Ecnn xnmu ncnontsyercx co snaxom e]nca, yxasannte moynn
ncxnmuamrcx ns cnenapnx. ecrnne xnmua es e]nca ananornuno nparme Perl use mo-
ynt n nauane cnenapnx. Coornercrnenno, ecrnne xnmua c e]ncom ananornuno nparme
no moynt. Hpn crapre nnrepnperaropa n xomanno crpoxe moxno yxastnart necxont-
xo napamerpon -m nnn -M.
-n Perl cunraer, uro cnenapn saxnmuen n nnxn while (<>)(cm. anee pasen urenne
noroxa nnoa ornocnrentno xoncrpyxnnn <>). Hanpnmep, cneymmax xomana ntnonr
na +xpan coepxnmoe ]ana file.txt:
pe1 -ne "p1nf" f11e.fxf
-p Perl cunraer, uro cnenapn saxnmuen n cneymmn nnxn (ero paora nmnrnpyer
peaxrop sed):
Wh11e {<>} {
...
|ceuapuu]
...
} conf1nue {
p1nf o d1e "-p desf1naf1on: $!\n"
}
-P nponycrnrt cnenapn uepes npenponeccop xomnnnxropa C. 3ro nosnonxer nc-
nontsonart npexrnnt C nna #define n #include, a raxxe oneparopt ycnonno xomnnnx-
nnn. Hocne npenponeccopa pesyntrar nocrynaer na nno Perl.
Crp.31
-s paspemaer pasopart xnmun, nepeannte cnenapnm n xauecrne napamerpa (cnncox
arguments, yxastnaemt n xomanno crpoxe nocne nmenn ]ana). Hanpnmep, cneym-
max xomana neuaraer coomenne Found the switch, ecnn npn crapre cnenapnm tn sa-
an xnmu -www:
1f {$WWW} {p1nf "Iound fhe sW1fch\n"}
-S sacrannxer Perl ncnontsonart nepemennym oxpyxennx PATH nx noncxa cnenapnx.
-T axrnnnsnpyer nponepxy meuentx anntx (nponepxa cexpernocrn ocryna). uacro
rpeyercx xax cocrannax uacrt npn CGI-nporpammnponannn.
-u sacrannxer Perl sanncart amn namxrn (core dump) nocne xomnnnxnnn cnenapnx.
-U - paspemaer Perl ntnonnxrt neesonacnte onepannn (nanpnmep, yanenne xaranoron).
-v ntnonr nomep nepcnn, nonepcnn n nomep narua Perl, a raxxe nnar]opmosann-
cnmym nn]opmannm o nnrepnperarope (nocnenxx moxer trt ouent cymecrnenno).
-V noponax pacneuarxa rexyme xon]nrypannn Perl (ro ecrt ncex xon]nrypann-
onntx nepemenntx).
-Y.u+ pacneuarxa xon]nrypannonno nepemenno c yxasanntm nmenem.
-w ntnonrt npeynpexammne coomennx (cm. raxxe cneymmn xnmu). Pexo-
menyercx ncera sanycxart Perl c +rnm xnmuom.
-x nnn -xrama.oe yxastnaer Perl, uro cnenapn coepxnrcx nnyrpn coomennx.
Bno ne yer opaartnartcx, noxa nnrepnperarop ne ncrpernr crpouxy c manonom !# n
nocrpoxo perl. Konen opaartnaemoro rexcra connaaer c xonnom ]ana nnn cnnrax-
cnuecxo xoncrpyxnne __END__. Ecnn yxasan xaranor, ro Perl ntnonnxer xomany nepe-
xoa x xaranory nepe sanycxom cnenapnx.
-- -+rn na e]nca xnnxmrcx neoxsarentntmn. Onn osnauamr xonen cnncxa xnmue,
nepeanaemtx nnrepnperaropy.
Knk - u npoeepka cuufakcuca
Hpn paore c Perl cronr ncera saanart xnmu -w. Fontmnncrno snaroxon crnnx xstxa Perl
]anarnuno noxnonxmrcx emy. raxxe pexomenym yxastnart +ror xnmu. Knmu -w ntno-
nr npeynpexammne coomennx no mnornm nonoam. Cpen nnx:
nmena nepemenntx, ynomnnammnxcx rontxo onn pas,
cxanxpnte nepemennte (npoe npocrtx nepemenntx), xoropte ncnontsymrcx o nnnnna-
nnsannn,
nepeonpeenenne nonporpamm,
cctnxn na neonpeenennte ecxpnnropt ]anon,
ecxpnnropt ]anon, orxptrtx rontxo nx urennx, no nx xoroptx nponsnonrcx nontr-
xa sanncn,
snauennx, ncnontsyemte xax uncna, no ntrnxxmne nnaue, uem uncna,
ncnontsonanne maccnna xax cxanxpno nepemenno,
nonporpammt c rnynno pexypcnn ontme 100.
Crp.32
Tekcfoeu eeop u eueop c norouk cfaupapfuux peckpunfopoe qanoe
Per paccmarpnnaer nno n ntno anntx xax noroxn, a paora c +rnmn noroxamn oprannsy-
ercx uepes ecxpnnropt ]anon. ecxpnnrop ]ana +ro npocro snauenne, xoropoe n Perl
coornercrnyer ]any. Hporpamma nonyuaer ecxpnnrop, xora orxptnaer ]an.
nx paort c rexcrom ncnontsymrcx rpn npeonpeenenntx ecxpnnropa;
STDIN cranaprnt norox nnoa.
STDUT cranaprnt norox ntnoa.
STDE cranaprnt norox ntnoa nx coomenn o omnxax.
Ho ymonuannm nce rpn ]ana coornercrnymr repmnnany. B +ro rnane (nanpnmep, n cne-
ymmem pasene) mt orpannunmcx npeonpeenenntmn ecxpnnropamn ]anon, ncnontsyx
nx ntnoa rexcra STDUT.
Bueop fekcfa
urot ntnecrn rexcr n ]an (nxnmuax cranaprnt norox ntnoa STDUT), ncnontsyercx
xomana print. Ona nmeer rpn ]opmt:
p1nf ecxpun1op cnucox
p1nf cnucox
p1nf
Ecnn ecxpnnrop ]ana ne saan, ncnontsyercx STDUT. Ecnn ne saan cnncox (xoropt
moxer cocroxrt n ns onoro +nemenra), Perl ntnonr coepxnmoe cnennantno nepemenno
$_. (Ona no ymonuannm ncnontsyercx nx coxpanennx pesyntraron nocnene onepannn ure-
nnx ns noroxa nnoa cm. anee pasen urenne noroxa nnoa.)
Hpocremn npnmep, n xoropom n norox ntnoa ornpannxercx cnono Hello n cnmnon xonna
crpoxn:
p1nf "he11o\n"
he11o!
uynxnnx print paoraer co cnncxom aprymenron (cnncxn Perl yyr paccmorpent n cneym-
me rnane). 3ro snaunr, uro e moxno noart na nxo cnncox +nemenron, pasenenntx sanx-
rtmn, n onn yyr naneuarant:
p1nf "he11o", " fhee!\n"
he11o fhee!
Ormernm, uro n Perl rexcront ntno moxer trt onontno nsompenntm, naroapx cnenn-
antntm ]opmaram n xomane printf (cm. rnany 11).
Reafu uorepa fekye cfpoku cueuapun u ureuu cueuapun
urot ncnontsonart nomep crpouxn cnenapnx, xoropym n annt momenr opaartnaer Perl,
nao cocnartcx na nero c nomomtm xoncrpyxnnn INE__.Touno rax xe moxno ntnecrn nmx
]ana, n xoropom xpannrcx cnenapn, ecnn ncnontsonart xoncrpyxnnm__FIE__. Hpnmep:
xpe1 -e "p1nf LlNL"
1
Crp.33
Roefop fekcfa npu neafu
Moxno nonropnrt ntno crpoxn necxontxo pas, ne xonnpyx ee n rene cnenapnx n ne ncnont-
syx oneparop nnxna. nx +roro cnyxnr oneparop nonropennx, xoropt ntrnxnr cneymmnm
opasom:
p1nf "he11o!\n" x 5
he11o!
he11o!
he11o
he11o!
he11o!
Hanpnmep, nor rax ntnonrcx ropnsonrantnax uepra ns e]ncon:
p1nf "-" x 30
------------------------------
Ocuoeuue koraupu qoprafupoeauun
C nomomtm escape-nocneonarentnocre n Perl moxno ntnonnxrt nexoropte xomant ]op-
marnponannx. Escape-noc.eooeame.iuocmi +ro naop cnmnonon, xoroptm npemecrnyer
oparnax xocax uepra (). Ona ocotm opasom opaartnaercx nnyrpn crpoxn, orpannuenno
nontmn xantuxamn (pasnnna mexy crpoxamn, orpannuenntmn nontmn xantuxamn, n
crpoxamn, orpannuenntmn anocrpo]amn, paccmarpnnaercx n cneymme rnane). Hexoropte
escape-nocneonarentnocrn npnneent n ran. 1.1.
Hanpnmep, nor xax naneuarart nonte xantuxn n rexcrono crpoxe:
p1nf "\"he11o!\"\n"
"he11o!"
Hocuasua. Cu+eo.i , $ u ro+ouuauu euoa uoeumuquramop u ] marre uaoo uaoupami c no+ouim
escape-noc.eooeame.iuocme, mar rar euvmpu cmporu, oepauu:euuo oeoui+u raei:ra+u, omu cu+eo.i
uumepnpemupvmmc ocooi+ oopaso+ (e :acmuocmu, cu+eo. rar ua:a.o escape-noc.eooeame.iuocmu.)
A nor rax ncnontsyercx raynxnnx:
p1nf "he11o\ffom\fPe1.\n"
he11o fom Pe1.
3ect c nomomtm cnmnonon nono crpoxn ntnonrcx cpasy necxontxo crpouex:
p1nf "he11o\nfom\nPe1.\n"
he11o
fom
Pe1.
3ro nnmt asonte xomant ]opmarnponannx, n n Perl nosmoxnt ropaso onee cnoxnte xon-
crpyxnnn. B +rom xstxe ncera cymecrnyer eme onn cnoco ntnonnennx ro nnn nno one-
pannn c rexcrom, n xonne xonnon, on cosanancx nmenno xax xstx nx opaorxn rexcron.
1abnua 1.1. 1abnua 1.1. 1abnua 1.1. 1abnua 1.1. hexo1opue escape-nocneoaa1enuuoc1u
Cuuaon Cuuaon Cuuaon Cuuaon 3uaueuue 3uaueuue 3uaueuue 3uaueuue
\` Ouuouuan xaauuxa, unu anoc1po {`}
\ Obpa1uuu anoc1po {}
\" aouuan xaauuxa {"}
\\ Obpa1uan xocan uep1a {\}
\$ Cuuaon onnapa {$}
\0 Cuuaon af-xouuepuecxoe {0}
\e Cuuaon escape {L5C}
Crp.34
\f Cuuaon 1abynnuu {1A8, h1}
\v Cuuaon ropuaou1anuuou 1abynnuu {v1}
\n Cuuaon uoaou c1poxu {LI}
\ Cuuaon aoaapa1a xape1xu {Ck}
\f Cuuaon nporoua c1pauuu {II}
\b Cuuaon aabon {85}
\a Cuuaon aayxoaoro curuana {8LL}
\033 8ocuuepuuuuu cuuaon
\x1b mec1uaa1epuuuuu cuuaon
\c| xnpaannk|uu cuuaon {confo1 chaacfe}
Bueop ueqoprafupoeauuoro fekcfa: ecfpoeuuue pokyreufu
Perl nosnonxer ntnonrt rexcr n rounocrn rax, xax on napan n rene cnenapnx. urot orme-
rnrt nauano noonoro rexcronoro ]parmenra (n Perl onn nastnamrcx here-documents), nc-
nontsyercx xomana <<, sa xoropo cneyer nexoropax merxa (n namem npnmepe +ro ED,
uro xnnxercx coxpamennem or end-of-document):
p1nf <<LOD
1h1s
1s
a
"hee"
documenf.
LOD
Texcr, or]opmarnponannt n rene cnenapnx, saxanunnaercx nenrn]nxaropom, yxasanntm n
ero nauane. (Oparnre nnnmanne, uro n nepno crpoxe nocne nenrn]nxaropa, mapxnpymme-
ro nauano ncrpoennoro rexcra, cronr rouxa c sanxro, a nocne nenrn]nxaropa, mapxn-
pymmero xonen rexcra, rouxn c sanxro ner.) B pesyntrare na +xpan yer ntneeno:
1h1s
1s
a
"hee"
documenf.
Korreufapuu
Hpn cosannn cnoxntx cnenapnen nyxno oannxrt xommenrapnn cnoeopasnte na-
nomnnannx camomy cee, uro nmenno enaer annt xo. 3ro enaer crpyxrypy cnenapnx
onee npospauno n oneruaer paory c nnm. Hpn +rom cam Perl xommenrapnn nrnopnpyer.
Kommenrapnn Perl naunnamrcx c cnmnona . Perl nrnopnpyer nect rexcr, nymn or cnm-
nona o xonna crpoxn. Bor npnmep cnenapnx Perl-oonouxn c xommenrapnxmn, pasxc-
nxmmnmn nasnauenne xoa:
#!/us/1oca1/b1n/pe15 -W # use Pe1 5 W1fh Wan1ngs
use sf1cf # kequ1e va1ab1e dec1aaf1ons, efc.
my $counf = 0 # $counf used fo mafch {}, {}, efc.
my $sfafemenf = "" # sfafemenf ho1ds mu1f1-11ne commands
1oca1 $5lG{WAkN} = sub{} # suppess eo epof1ng
Wh11e {<>} { # Accepf 1npuf fom fhe keyboad
chomp # C1ean-up 1npuf
Wh11e {/{|\{|\|/g} {$counf++} # Wafch fo {, {, efc.
Wh11e {/}|\}|\]/g} {$counf--} # Wafch fo }, }, efc.
Crp.35
$sfafemenf .= $ # Append 1npuf fo cuenf sfafemenf
1f {!$counf} { # Lva1uafe on1y 1f {, { mafches }, }
eva1 $sfafemenf # Lva1uafe Pe1 sfafemenf
1f {$0} {p1nf "5ynfax eo.\n"} # lf eo ...
$sfafemenf = "" # C1ea fhe cuenf sfafemenf
$counf = 0 # C1ea fhe counfe
}
}
Hocuasua. Bi +oreme er.m:umi np+o e me.o npoepa++i oorv+eumaum (POD Plain Old Document), onuci-
eamuvm, rar uaoo paoomami c eaue npoepa++o u.u +oov.e+. Oua ovoem ueuopupoeamic npu saepvsre, uo ei
eceeoa +oreme use.e:i ee e euoe ASCII-qa.a u.u oare e euoe pas+e:euuoeo HTML-qa.a c no+ouim vmu.um,
exoouux e cocmae Perl. Kar omo coe.ami, paccrasieaemc e e.aee 15 (pasoe. orv+eumupoeauue +oov.e).
-feuue nofoka eeopa
Panee n +ro rnane mt nnenn, uro nx ntnoa rexcra ncnontsyercx ]ynxnnx print, no xax
eeecmu rexcr Ero moxno unrart ns cranaprnoro noroxa nnoa STDIN c nomomtm yrnontx
cxoox < n >. B cneymmem npnmepe nnxn while (paccmarpnnaemt n rnane 5) nomoraer no-
crpouno cunrtnart nnonmt nontsonarenem rexcr, sanomnnart ero n nepemenno $temp n
sarem ntnonrt na +xpane:
Wh11e {$femp = <51DlN>} {
p1nf $femp
}
Ecnn sanycrnrt +ror cnenapn n nnecrn c xnannarypt cnono Hello, cnenapn nonropnr ero
na +xpane:
he11o!
he11o!
Ha camom ene (xax +ro otuno n tnaer n Perl) onrtcx noonoro +]]exra moxno n
onee npocrtm nyrem. (B Perl nmeercx cnennantnoe xapronnoe ntpaxenne: There is
more than one way to do it Cenart uro-nno moxno necxontxnmn cnocoamn, co-
xpamaemoe o TMTOWTDI n nponsnocnmoe npnmepno xax Tim Toady.) nx +roro npn-
ercx sarnxnyrt n cneymmn pasen.
Cneuuanuuan nepereuuan $_
Kora xoncrpyxnnx <STDIN> ncnontsyercx es yxasannx, xya nomecrnrt nosnpamaemoe
snauenne, Perl anromarnuecxn sannctnaer ero n cnennantnym nepemennym $_. Mnorne ]ynx-
nnn Perl no ymonuannm nonyuamr aprymenrt ns nee (ecnn nontsonarent ne saan pyroro
nxonoro snauennx). Hanpnmep, xomana print es aprymenron ntneer coepxnmoe nmenno
$_. (Ecrt raxxe macca pyrnx cnennantntx nepemenntx nanpnmep, $!, xoropax xpannr
nn]opmannm o nocnene omnxe, ecnn ra nponsomna. Fonee nopono o cnennantntx ne-
pemenntx paccxastnaercx n rnane 9.)
Ha camom ene xnmuenoe cnono STDIN n yrnontx cxoxax moxno onycrnrt nx nyc-
rtx yrnontx cxoox no ymonuannm npenonaraercx ecxpnnrop STDIN. (B Perl nmeercx
macca npannn no ymonuannm, noontx +romy. 3ro enaer nporpammt npome nx +xcnep-
ron, no sanyrannee nx nonnuxon. Bosmoxno, nmenno no+romy Perl rax npannrcx +xcnep-
ram.) Tem camtm xo ns npetymero pasena moxer trt sanncan xax
Wh11e{<>} { p1nf }
Crp.36
3ro noncrnny xparxax sannct nx ananornuntx ecrnn cnenapnx
Wh11e {$ = <51DlN>} {
p1nf $
}
Oucfka eeepeuuoro fekcfa
uepes cranaprnt norox STDIN unraercx nce, uro nanpaercx c xnannarypt nnn nocry-
naer ns ]ana, nxnmuax cnmnon nono crpoxn n xonne. urot nsannrtcx or nero, moxno
ncnontsonart ]ynxnnm chop nnn chomp. Bor xax ntrnxnr ]ynxnnx chop:
chop nepeueuuan
chop cnucox
chop
Ona orcexaer nocnenn cnmnon n crpoxe n nosnpamaer ero n xauecrne pesyntrara. Ecnn ony-
meno nmx nepemenno, ]ynxnnx paoraer co cnennantno nepemenno $_. Hanpnmep, cnena-
pn,
Wh11e {<>} {
p1nf
}
ntnonr na +xpane coepxnmoe noroxa nnoa, nxnmuax cnmnont nono crpoxn. Ecnn xe
ncnontsonart raxo napnanr:
Wh11e {<>} {
chop
p1nf }
ro cnmnonon-napasnron nono crpoxn ne yer.
Bmecro ]ynxnnn chop pexomenyercx ncnontsonart chomp, xoropax ntstnaercx anano-
rnuntm opasom:
chomp nepeueuuan
chomp cnucox
chomp
3ra ]ynxnnx onee esonacna ona yanxer ns xonna crpoxn cnmnont, coorner-
crnymmne rexymemy snauennm cnennantno nepemenno Perl $/, xpanxme cnmnon, nt-
crynammn n xauecrne pasennrenx nxontx sannce. Ho ymonuannm ncnontsyercx cnm-
non nono crpoxn \n. uynxnnx chomp nosnpamaer uncno yanenntx cnmnonon. Otuno
ona ncnontsyercx nx yanennx cnmnona nono crpoxn ns sanncn, npounranno uepes no-
rox nnoa. Ecnn orcyrcrnyer nmx nepemenno, opaartnaercx $_.
Kak useafu uerepneuuoro sakpufun okua MS-DOS e Windos 95/98/NT
Ecnn nt ncnontsyere Perl nx Windows 95/9 nnn Windows NT ro, ouennno, ormernnn ony
paspaxammym erant. Hocne nonoro menuxa no nmenn ]ana c pacmnpennem .pl noxnnx-
ercx oxno ceanca MS-DOS; n nem ntnonnxercx cnenapn, no nemenenno no ero sanepmennn
oxno saxptnaercx, ne ocrannxx nnxaxoro manca npocmorpert ntneennym nn]opmannm.
3ro moxno ncnpannrt, ecnn sacrannrt cnenapn xart nnoa c xnannarypt nocne sanep-
mennx. nx +roro ocrarouno oannrt n xonen nporpammt ne crpouxn:
p1nf "he11o!\n"
Crp.37
p1nf "Pess <Lnfe> fo co p1nf "Pess <Lnfe> fo co p1nf "Pess <Lnfe> fo co p1nf "Pess <Lnfe> fo con nn nf1nue... f1nue... f1nue... f1nue... " " " "
< << <51DlN 51DlN 51DlN 51DlN> >> >
Ecrecrnenno, <STDIN> moxno samennrt npocro na <>:
p1nf "he11o!\n"
p1nf "Pess <Lnfe> fo conf1nue..."
<>
Crp.38
Fnana
Ckannpume nepeueuume u cnucku

Kopo1ko
B +ro rnane mt naunnaem paorart c anntmn. Perl ocoenno xopom no uacrn opaorxn
anntx, n n +ro xnnre nt naere nemano nn]opmannn no anno reme. Ceuac mt no-
npoyem pasopartcx, xax Perl paoraer c nymx xonxperntmn rnnamn anntx: cxanxp-
ntmn nepemenntmn n cnncxamn.
Ckannpuue nepereuuue
Cxanxpnte nepemennte +ro ro, uro n ontmnncrne xstxon nporpammnponannx Hast-
naercx npocrtmn nepemenntmn (n Perl onn raxxe nastnamrcx cra.pa+u). Onn xpanxr
onn +nemenr anntx: uncno, rexcronym crpoxy nnn cctnxy (o cctnxax n Perl cmorpn
rnany ). Hx nastnamr cxanxpamn, urot ornnuart or xoncrpyxnn, cnocontx xpannrt
necxontxo +nemenron (nanpnmep, maccnnon).
Hocuasua. B uav:uix mep+uuax cra.p omo npocmoe :uc.oeoe sua:euue, e mo epe+ rar eermopi +oevm
u+emi uecro.iro :uc.oeix ro+noueum. Oouo+epuie +accuei e npoepa++upoeauuu :acmo uasieamm eermopa+u.
Hmx cxanxpno nepemenno naunnaercx c cnmnona $. 3ro moxer noxasartcx crpanntm, no
Perl ncnontsyer nmenno +ror cnoco. Tem camtm nn ona ns nepemenntx ne yer xon-
]nnxronart c sapesepnnponanntmn xnmuentmn cnonamn Perl (ro ecrt ncrpoenntmn n
xstx nenrn]nxaropamn). 3nauennx cxanxpntm nepemenntm npncnannamrcx c nomomtm
oneparopa , nanpnmep:
$sca1a1 = 5
$sca1a2 = "he11o fhee!"
Cnucku
Kax cneyer ns nasnannx, cnncxn +ro cnncxn +nemenron anntx. 3rn +nemenrt ne ox-
sarentno xnnxmrcx cxanxpamn, onn moryr trt n maccnnamn, n x+m-rannnamn (oa +rn
rnna anntx yyr paccmorpent n cneymmem pasene), a raxxe cocrnenno cnncxamn.
B ornnune or cxanxpon nnn maccnnon, nx cnncxon ne cymecrnyer cnennantnoro rnna
anntx. Onaxo nonxrne cnncxa n Perl ouent naxno, n mt yem ncnontsonart ero na
ncem nporxxennn xnnrn. Cnncox +ro cnnraxcnuecxax xoncrpyxnnx, oennxmmax
pasposnennte +nemenrt anntx. Hpn onpeenennn cnncxa +nemenrt anntx pasenxmr-
cx sanxrtmn. Hanpnmep, mt neuaraem noyxnenno cnono Hello c nomomtm ]ynxnnn
print, xoropax ymeer opaartnart aprymenrt, saannte n nne cnncxa:
p1nf "h", "e", "1", "1", "o"
he11o
Oparnre nnnmanne, uro cnncox H, e, l, l, o nentsx npncnonrt orentno nepe-
Crp.39
menno, rax xax n Perl n xnnom nne nepemenntx raxoro rnna ne cymecrnyer
1
.
Bcrpoennte ]ynxnnn Perl pasenxmrcx na ne rpynnt. Onn ncnontsymr cxanxpnte ap-
rymenrt, a pyrne cnncxn (xorx na camom ene mnorne ]ynxnnn nonnmamr n ro n py-
roe). Orxya xe Perl snaer, xora nnrepnpernponart annte xax cxanxp, a xora xax
cnncox On npnnnmaer pemenne, ncxox ns roumercma. B Perl cymecrnymr na ocnonntx
xonrexcra cxanxpnt n cnncount. Hmeercx n onee ronxax nepapxnx xonrexcron. Ha-
npnmep, uncnono n crpoxont xonrexcrt xnnxmrcx cxanxpntmn.
Taxnm opasom, xora Perl oruoaem ncrpernrt cnncox (nanpnmep, xora ]ynxnnx onyc-
xaer n xauecrne aprymenra rontxo cnncxn), annte nnrepnpernpymrcx xax cnncox. Ecnn
oxnaercx cxanxpnoe snauenne, annte nnrepnpernpymrcx xax cxanxp. Ha npaxrnxe +ro
osnauaer, uro neoxonmo sapanee snart, xaxne ]ynxnnn xnnxmrcx cxanxpntmn, a xaxne
cnncountmn. yy yxastnart, x xaxomy rnny ornocnrcx ]ynxnnx, npn nepnom yno-
mnnannn o ne n +ro xnnre, yxastnax rnn ee aprymenron. Hanpnmep, onpeenenne nx
]ynxnnn map yer nmert nn:
map bnox cnucox
B +rom pasene cxanxpnt n cnncount xonrexcrt nosnee yyr paccmorpent onee
nopono. Bneenne saxonueno nepeem x paore c nporpammntm xoom.
Henocpepc1neuume pemeuun
-fo fakoe ckannpuan nepereuuan?
Cxanxpnax nepemennax +ro nmx onacrn namxrn, n xoropo xpanxrcx annte. Onn mo-
ryr trt uncnamn, rexcrontmn crpoxamn nnn cctnxamn (cctnxn paoramr noono ape-
cy pyroro +nemenra anntx). Kpome +roro, cxanxpnax nepemennax moxer coepxart n
cnennantnt rnn anntx Perl rax nastnaemt neonpeenennt rnn (cm. pasen Pa-
ora c neonpeenenntmn anntmn: nenrn]nxarop undeI anee n +ro rnane).
Oparnre nnnmanne, uro cxanxpt moryr xpannrt pasnnunte rnnt anntx; nntmn cnona-
mn, n Perl cxanxpt ne nmemr onpeenennoro rnna (sa ncxnmuennem cctnox, nx xoroptx
rnn anntx xecrxo nponepxercx). B +rom ornnune Perl or raxnx xstxon, xax C. Perl onpe-
enxer, xaxo rnn anntx naxonrcx n cxanxpno nepemenno (uncno, crpoxa n r. .), oc-
nontnaxct na xonrexcre ntnonnxemo onepannn. Honesno ocoennocrtm Perl xnnxercx
ro, uro uncna n crpoxn n nem n nexoropom cmtcne nepasnnunmt. Tax, ecnn nocrannrt
crpoxy 123.5 ram, re Perl oxnaer ynnert uncno, ro npeopasonanne x :uc.v 123.5
yer nponsneeno anromarnuecxn. Ananornuntm opasom npn neoxonmocrn uncno
npeopasyercx n crpoxy.
(3ro cnocrno nosnonxer npnmenxrt necranaprnte nporpammnte pemennx, nexoropte ns
nnx mt ynnnm nnxe.)
Hreua ckannpuux nepereuuux

1
Onaxo nt moxere cosart cctnxy na cnncox, sanecrn ee n otunym nepemennym n anee paorart c coepxnmtm cctnxn
(ro ecrt cnncxom) nourn xax c otuno nepemenno. Fonee nopono o cctnxax peut noer n rnane . Hpu+e:. peo.
Crp.40
Hmx, ncnontsyemoe nx cxanxpno nepemenno, moxer coepxart yxnt, nn]pt n no-
uepxnnannx. Onaxo ono onxno naunnartcx c cnmnona $, uro npeornpamaer xon]nnxrt
c sapesepnnponanntmn nenrn]nxaropamn Perl. Hmx nepemenno moxer trt nnnntm
o 255 cnmnonon, no +ra xapaxrepncrnxa sanncnr or nnar]opmt (ro ecrt nexoropte
peannsannn Perl onycxamr n onee nnnnte nmena, no nmx c nnno ne ntme 255 yer
npannntno pacnosnano .moi+ napnanrom Perl). Hocxontxy nmena cxanxpntx nepemenntx
ncera naunnamrcx c cnmnona $ n rem camtm ne xon]nnxrymr c sapesepnnponanntmn
nenrn]nxaropamn, nx moxno sannctnart crpountmn yxnamn, xax +ro enaer ontmnn-
crno nporpammncron. (Hourn nce cnyxente cnona n Perl ncnontsymr crpounte yxnt.
Hcxnmuennem xnnxmrcx ecxpnnropt ]anon rnna STDIN nnn ]ynxnnn, noonte no-
xy BEGIN nx naxera.) Homnnre, uro nmena cxanxpntx nepemenntx uyncrnnrentnt x pe-
rncrpy: nmx $variable1 +ro ne ro xe camoe, uro nmx $Variable1.
Hocne nauantnoro cnmnona $ n nmenn nepemenno moxer croxrt yxna nnn snax nouep-
xnnannx. Ha camom ene moxno ncnontsonart n nn]pt, no rora nce nmx onxno cocroxrt
rontxo ns nn]p. Moxno axe ncnontsonart neyxnennte cnmnont, ornnunte or snaxa
nouepxnnannx, no n raxom cnyuae nmx onxno cocroxrt (xpome nauantnoro $) ponno ns
onoro cnmnona xax, nanpnmep, n cnyuae cnennantntx nepemenntx Perl rnna $!.
Hocuasua 1. 3a+emime, :mo, xom cra.puie nepe+euuie u ue +oevm rouq.urmoeami c sapesepeupoeauui-
+u uoeumuquramopa+u Perl us-sa ua:a.iuoeo cu+eo.a $, +orem eosuuruvmi rouq.urm c u+eua+u o. oo-
ermoe, ue mpeovmuu+u ua:a.iuoeo $, uanpu+ep, oecrpunmopa+u qa.oe u.u +emra+u. Umooi usoerami
rouq.urma co c.vreoui+u c.oea+u, o. marux u+eu .v:ue ucno.isoeami oouv u.u uecro.iro sae.aeuix ovre.
Hocuasua 2. Hepe+euuie, u+eua romopix cocmom mo.iro us uqp, cosoammc ca+u+ Perl o. cneu-
a.iuix e.e (c+. e.aev 6). Ho.isoeame.i +orem :umami sua:euu omux nepe+euuix, uo ue +orem uu:e-
eo u+ npuceaueami. To:uo mar re ueromopie cneua.iuie nepe+euuie Perl oocmvnui mo.iro o. :me-
uu npuceoeuue uoeoeo sua:euu eisoeem ouuorv.
Cnmnon $, c xoroporo naunnaercx nmx cxanxpno nepemenno, nastnaercx n Perl pastme-
nontnammnm npe]nxcom (nnn, ecnn ncnontsonart xapron, funnv character). Bor pyrne
pastmenontnammne npe]nxct, xoropte ncnontsymrcx n Perl:
$ cxanxpnte nepemennte;
x+mn (onn xe acconnarnnnte maccnnt, xax +ro pasxcnxercx n cneymmem
pasene);
maccnnt;
nonporpammt;
sannct rannnt cnmnonon rnn anntx typeglob (onee nopono o +rom
rnne anntx paccxastnaercx n rnane 3).
Rpuceoeuue ckannpuux nepereuuux
Kax n cxanxpnym nepemennym sanocxrcx annte nx +roro ncnontsyercx oneparop npn-
cnoennx. Hanpnmep, nt moxere nomecrnrt n nepemennym $variable1 snauenne 5:
$va1ab1e1 = 5
Touno raxxe ntnonnxercx npncnoenne rexcrontx crpox:
$va1ab1e1 = "he11o fhee!"
Oneparop npncnoennx moxer ncnontsonartcx nx npncnannannx anntx nmomy oexry,
croxmemy cnena or nero (rax nastnaemoe nenoe snauenne lvalue). Ecnn nt ne snaere,
Crp.41
uro raxoe nenoe snauenne, npounrare cneymmn pasen. Hoono ocrantntm ]ynx-
nnxm Perl oneparop npncnoennx nosnpamaer nexoropoe snauenne. 3nauennem oneparopa
xnnxercx annoe, nomemennoe n coornercrnymmym onacrt namxrn:
p1nf $1npuf = 123
123
-fo fakoe neeoe suaeuue?
Henoe snauenne +ro +nemenr, croxmn cnena or oneparopa npncnannannx n cnyxa-
mn mnmentm nx +ro onepannn. Otuno nenoe snauenne npecrannxer coo o-
nacrt namxrn xomntmrepa, n xoropym, yxasan ee nmx, nt moxere sanocnrt annte. B xa-
uecrne nenoro snauennx moxer ntcrynart nmax nepemennax. Ha camom ene n Perl +ry
pont nnora moxer nrpart n cama onepannx npncnannannx, xax cneyer ns npnmepa:
chop{$1npuf = 123}
p1nf $1npuf
12
Hocnenn cnmnon orpesaercx or nepemenno $input, a ne or snauennx, nosnpamaemoro
onepanne npncnoennx. 3ra xoncrpyxnnx moxer uacro ncrpeuartcx, xora neoxonmo
npounrart crpoxy ns nxonoro noroxa, yannrt cnmnon xonna crpoxn n sanecrn n nepe-
mennym $input, npnuem ntnonnnrt nce +ro sa onn pas:
chop {$1npuf = <>}
Hcnonusoeauue ucen e ckannpuux nepereuuux
Oparnre nnnmanne na cneymmym sannct uncnonoro snauennx, xoropoe nosnonxer
crpynnnponart nn]pt no paspxam nx onee nerxoro urennx pesyntrara:
$va1ab1e1 = 1234567

1abnu 1abnu 1abnu 1abnua 2.1. a 2.1. a 2.1. a 2.1. ucnoaue 1unu auuux
1un 1un 1un 1un hpuuep hpuuep hpuuep hpuuep
3uaueuue c nnaaak|eu 1ouxou 1.23
mec1uaa1epuuuoe auaueuue 0x123
enoe uucno 123
8ocuuepuuuoe auaueuue 0123
ucno a uayuuou uo1auu 1.234
ucno c rpynnupoaxou no paapnau 12345674
Paofa c ueonpepeneuuuru pauuuru: upeufuqukafop unde
Kpome uncen, crpox n cctnox cxanxpnte nepemennte moryr coepxart ueonpeoe.euuoe
snauenne, xoropoe n Perl oosnauaercx xax undef (sapesepnnponannoe cnono). Ono nos-
npamaercx nexoroptmn ]ynxnnxmn, n nt moxere nponepnrt ero c nomomtm ]ynxnnn de-
fined. Moxno raxxe npncnonrt nepemenno snauenne undef c nomomtm ncrpoenno
]ynxnnn undef (cm. nnxe). Ecnn noneprnyrt nenocpecrnenno nponepxe snauenne un-
def, ro ono nnrepnpernpyercx xax 0 (nynenoe snauenne) n uncnonom xonrexcre n xax
(nycrax crpoxa) n crpoxonom. Paccmorpnm npnmep. Hycrt nepemenno $variable1 npn-
cnoeno snauenne 5:
Crp.42
$va1ab1e1 = 5
3arem c nomomtm ]ynxnnn undef +ra nepemennax enaercx neonpeenenno:
$va1ab1e1 = 5
undef undef undef undef $ $$ $va1ab1e va1ab1e va1ab1e va1ab1e1 11 1
Tenept nponepnm c nomomtm ]ynxnnn defined, xnnxercx nn snauenne nepemenno neon-
peenenntm:
$va1ab1e1 = 5
undef $va1ab1e1
1f {def1ned $va1ab1e1} { 1f {def1ned $va1ab1e1} { 1f {def1ned $va1ab1e1} { 1f {def1ned $va1ab1e1} {
p1nf " p1nf " p1nf " p1nf "\ \\ \$va1ab1e1 1s def1ned. $va1ab1e1 1s def1ned. $va1ab1e1 1s def1ned. $va1ab1e1 1s def1ned. \ \\ \n" n" n" n"
} e1se { } e1se { } e1se { } e1se {
p1nf " p1nf " p1nf " p1nf "\ \\ \$va1ab1e1 1s nof d $va1ab1e1 1s nof d $va1ab1e1 1s nof d $va1ab1e1 1s nof de ee ef1ned. f1ned. f1ned. f1ned. \ \\ \n nn n" " " "
} }} }
B pesyntrare xo coomnr nontsonarenm, uro nepemennax xnnxercx neonpeenenno:
$va1ab1e1 1s nof def1ned.
Onucauue koucfauf
B Perl ner cnennantno cnnraxcnuecxo xoncrpyxnnn nx uncnontx xoncranr, no nx nee
moxno cosart samennrent camocroxrentno (ncnontsyx rexnnxy, ocyxaemym n rnane n
n pasene rnant 3, nocnxmennomy sanncxm rannnt cnmnonon cnennantnomy rnny
anntx typeglob). Hmx nepemenno, ornocxmecx x +romy rnny, naunnaercx c cnmnona
(snesouxa, nnn acrepncx). urot cosart xoncranry, nao npncnonrt rnny anntx type-
glob cctnxy, xax +ro enaercx n cneymmem npnmepe (sect xoncranra xpannr maxcn-
mantnoe uncno ]anon):
"MAIlLL5 = \100
(Oparnre nnnmanne: nmx cocranneno ncxnmunrentno ns sarnanntx yxn, urot nouepx-
nyrt, uro +ro xoncranra.)
ocryn x cosanno xoncranre ocymecrnnxercx uepes nmx $MAFIES, xax ecnn t +ro
tna otunax cxanxpnax nepemennax:
"MAIlLL5 = \100
p1nf p1nf p1nf p1nf "$ "$ "$ "$MAIlLL5 MAIlLL5 MAIlLL5 MAIlLL5\ \\ \n nn n" " " "
Hpn nontrxe npncnonrt nepemenno $MAFIES nonoe snauenne, nt nonyunre coo-
menne o omnxe. Hanpnmep, nocne ntnonnennx xoa
"MAIlLL5 = \100
p1nf "$MAIlLL5\n"
$ $$ $MAIlLL5 MAIlLL5 MAIlLL5 MAIlLL5 = == = 101 101 101 101
na +xpan yer ntneeno
100
Mod1f1caf1on of a ead-on1y va1ue affempfed af consfanf.p1 11ne 3.
Paofa c norueckuru pauuuru e Perl
Hmeercx eme onn naxnt rnn anntx, o xoropom nao ynomxnyrt: nornuecxne annte,
nnn snauennx ncrnna/noxt. Vcnonnte xomant, ynpannxmmne ntnonnennem xoa (na-
Crp.43
npnmep, ycnonnte oneparopt if) ncnontsymr nornuecxne nponepxn.
Hoono xstxy C, nmoe nenycroe n nenynenoe snauenne coornercrnyer ycnonnm ucmuua,
nynenoe nnn nycroe ycnonnm .ori. Coornercrnenno, cnennantnoro nornuecxoro rnna
anntx n Perl ner.
Tor ]axr, uro nmoe nenynenoe snauenne coornercrnyer ycnonnm ncrnna (nanomnnm, uro
nx Perl nycrax crpoxa ror xe nont), nectma nonesen nx ycnonntx oneparopon. Ha-
npnmep, naroapx +ro ocoennocrn cneymmn nnxn yer ntnonnxrtcx o camoro
xonna nxonoro noroxa, nocxontxy opamenne x <> ncera uro-ro nosnpamaer axe ec-
nn nneena nycrax crpoxa (n +rom cnyuae nosnpamaercx cnmnon \n):
Wh11e {<>} {
p1nf
}
Hocuasua. B Perl e ra:ecmee sua:euu uoxo eicmvnamm c.eovmuue ee.u:uui. a) :uc.oeie sua:euu,
paeuie uv.m, 6) nvcma cmpora "", nvcmo +accue () u.u nvcmo xou ]}, e) ueonpeoe.euuoe sua:euue un-
def, nepe+euua c ueonpeoe.euui+ sua:euue+, cnucor ueonpeoe.euuix sua:euu u/u.u nepe+euuix, e) oe-
crpunmop qa.a, ue cesauui c raru+-.uoo qa.o+, e) nvcmie cci.ru u uesano.ueuuie sanucu mao.ui
cu+eo.oe. Bce ocma.iuie sua:euu pacc+ampueammc rar ucmuua.
Rpeopasoeauue eocurepuuux, pecnfuuux u uecfuapuafepuuux ucen
B Perl noctmepnunte uncna saamrcx c neymnm nynem (nanpnmep, 0123), a mecrnanarepnu-
nte c npe]nxcom 0x (nanpnmep, 0xlAB). Hpn paore c uncnamn, nmemmnmn pasnte uncno-
nte ocnonannx, nonesno snart, xax npeopasontnart nx ns onoro ]opmara n pyro.
Rpeopasoeauue uecfuapuafepuuoro ucna e pecnfuuoe
nx npeopasonannx mecrnanarepnunoro ]opmara n ecxrnunt ncnontsyercx ]ynxnnx hex:
p1nf hex 0x1A8
1063
Ecnn nx ]ynxnnn hex ne saan aprymenr, ncnontsyercx cnennantnax nepemennax $_.
Rpeopasoeauue pecnfuuoro ucna e uecfuapuafepuuoe
urot npeopasonart ecxrnunoe uncno n crpoxy, xnnxmmymcx ero mecrnanarepnuntm
npecrannennem, ncnontsyre ]ynxnnm Perl sprintf c manonom x:
p1nf sp1nff "xx", 16
10
Rpeopasoeauue eocurepuuoro ucna e pecnfuuoe
urot npeopasonart noctmepnunoe uncno n ecxrnunoe, ncnontsyercx ]ynxnnx oct:
p1nf ocf 10

Ecnn nx ]ynxnnn oct ne saan aprymenr, yer ncnontsonana cnennantnax nepemennax $_.
Crp.44
Rpeopasoeauue pecnfuuoro ucna e eocurepuuoe
Kax n n cnyuae mecrnanarepnuntx uncen, nx npeopasonannx ecxrnunoro uncna n
crpoxy, xnnxmmymcx ero noctmepnuntm npecrannennem, ncnontsyre ]ynxnnm Perl
sprintf, no c manonom o:
p1nf sp1nff "xo", 16
20
Okpyrneuue ucen
urot oxpyrnnrt uncnonoe snauenne o onpeenennoro uncna ecxrnuntx snaxon, nc-
nontsyre ]ynxnnm sprintf c noxoxmnm ]opmarom. Hanpnmep, urot oxpyrnnrt uncno
o nyx nn]p nocne sanxro, ronrcx ]opmar .2f. Bor xax +ror mero paoraer npn ox-
pyrnennn uncna nn:
p1nf sp1nff "x.2f", 3.14159265359
3.14
To, uro nponcxonr nmenno oxpyrnenne, a ne orpactnanne nnmnnx paspxon, nnno na
cneymmem npnmepe, re nn oxpyrnxercx o uertpex nn]p nocne sanxro n nocnenxx
nn]pa cranonnrcx panno , a ne 5:
p1nf sp1nff "x.4f", 3.14159265359
3.1416
Onaxo npnneennte npnmept nnmt noxastnamr, xax ntnecrn oxpyrnennte snauennx.
Bosmoxno, norpeyercx raxxe oxpyrnxrt uncnonte snauennx n paorart c nnmn xax c
otuntmn uncnamn. Tax xax Perl opaartnaer annte ncxox ns xonrexcra, ro ecnn yx n
namnx cnnax nnrepnpernponart rexcronym crpoxy xax uncno, ro n Perl +ro moxer (ecnn
rexcronax crpoxa ecrnnrentno onnctnaer uncno). B cneymmem npnmepe mt oxpyrnxem
uncno n sanomnnaem crpoxy n nepemenno $variable1:
$va1ab1e1 = sp1nff "x.4f", 3.14159265359
Tenept moxno ncnontsonart snauenne, naxoxmeecx n nepemenno $variable1, xax uncno, nt-
nonnnn na nnm apn]mernuecxym onepannm, nanpnmep, npnannn x nemy snauenne 0.01:
$va1ab1e1 = sp1nff "x.2f", 3.14159265359
$ $$ $va1ab1e va1ab1e va1ab1e va1ab1e1 1 1 1 += += += += .01 .01 .01 .01
Tenept nponepnm pesyntrar:
$va1ab1e1 = sp1nff "x.2f", 3.14159265359
$va1ab1e1 += .01
p1nf $va1ab1e1 p1nf $va1ab1e1 p1nf $va1ab1e1 p1nf $va1ab1e1
3.15
Hcnonusoeauue cfpok e ckannpuux nepereuuux
B cxanxpntx nepemenntx moxno xpannrt xax uncna, rax n crpoxn:
$va1ab1e1 = "he11o!"
Hao ormernrt, uro nx xonxarenannn (oennennx) nyx crpox n Perl nentsx ncnontsonart
onepannm cnoxennx, xax +ro npnnxro n pyrnx xstxax. Hanpnmep, cneymmne xomant:
$va1ab1e1 = "he11o "
Crp.45
$va1ab1e2 = "fhee\n"
p1nf $va1ab1e1 + $va1ab1e2
ayr rontxo coomenne o omnxe n nont n xauecrne pesyntrara:
Agumenf "fhee\n" 1snf nume1c 1n add af - 11ne 3
Agumenf "he11o " 1snf nume1c 1n add af - 11ne 3
0
Hpnunny nerxo nonxrt: n annom cnyuae Perl ntraercx nnrepnpernponart rexcr ncxox ns
uncnonoro xonrexcra (onepannx cnoxennx).
Bmecro +roro cneyer ncnontsonart oneparop xonxarenannn Perl, n ponn xoroporo ntcry-
naer rouxa (.):
$va1ab1e1 = "he11o "
$va1ab1e2 = "fhee\n"
p1nf $va1ab1e1 . $va1ab1e2
he11o fhee
Crpoxn moryr saanartcx c nomomtm onnapntx nnn nontx xantuex:
$va1ab1e1 = "he11o."
$va1ab1e2 = he11o aga1n.
Mexy +rnmn cnocoamn ecrt ronxoe pasnnune. Perl ntuncnxer nepemennte n nexoropte
ntpaxennx n rene crpoxn, xora ona orpannuena nontmn xantuxamn (onee nopono
o +rom paccxasano n cneymmem pasene). Ecnn crpoxa orpannuena onnapntmn xantu-
xamn (anocrpo]amn), ro Perl paccmarpnnaer ee xax xoncranry es nontrox nnrepnpernpo-
nart reno crpoxn.
B crpoxax, orpannuenntx nontmn xantuxamn, moxno ncnontsonart escape-no-
cneonarentnocrn, ynpannxmmne nx ]opmarnponannem n nosnonxmmne saanart cnmno-
nt, xoropte nnaue sanncart ne yacrcx (ran. 2.2). Hanpnmep, urot nnecrn n rexcr
nonym xantuxy, moxno ncnontsonart nocneonarentnocrt \":
p1nf "l sa1d, \"he11o\"."
l sa1d, "he11o".
B crpoxax, orpannuenntx onnapntmn xantuxamn, escape-nocneonarentnocrn ne paoramr.
1abn 1abn 1abn 1abnua 2.2. ua 2.2. ua 2.2. ua 2.2. Lscape-nocneoaa1enuuoc1u u ux auaueuue
Cuuaon 3uaueuue Cuuaon 3uaueuue Cuuaon 3uaueuue Cuuaon 3uaueuue
\` Ouuapuan xaauuxa, unu anoc1po {`}
\ Obpa1uuu anoc1po {}
\" aouuan xaauuxa {"}
\\ Obpa1uan xocan uep1a {\}
\$ Cuuaon onnapa {$}
\0 Cuuaon af-xouuepuecxoe {0}
\f Cuuaon 1abynnuu {1A8, h1}
\v Cuuaon aep1uxanuuou 1abynnuu {v1}
\n Cuuaon uoaou c1poxu {LI}
\e Cuuaon escape {L5C}
\u Cena1u cneyk|yk nu1epy aarnaauou
\1 Cena1u cneyk|yk nu1epy c1pouuou
\u Cena1u cneyk|yk rpynny nu1ep {o xouauu \} aarnaauuuu
\L Cena1u cneyk|yk rpynny nu1ep {o xouauu \} c1pouuuuu
\ 8 cneyk|eu rpynne nu1ep {o xouauu \} cuu1a1u, u1o xo aceu uebyxaeuuuu nu1epau obaa-
neua obpa1uan xocan uep1a, - 1o aac1aanne1 Pe1 uu1epnpe1upoaa1u ux xax obuuuue cuuaonu
\ 3aaepae1 xouauu \L, \u u \
\ Cuuaon aoaapa1a xape1xu {Ck}
\f Cuuaon nporoua c1pauuu {II}
Crp.46
\b Cuuaon aabon {85}
\a Cuuaon aayxoaoro curuana {8LL}
\033 8ocuuepuuuuu cuuaon
\x1b mec1uaa1epuuuuu cuuaon
\c| xnpaannk|uu cuuaon {confo1 chaacfe}
Cureon uoeo cfpoku e fekcfoeux cfpokax
Bo ncex onepannonntx cncremax cnenapnn Perl ncnontsymr cnmnon \n, urot oosnaunrt
xonen crpoxn. Ha camom ene raxo nemn, xax nensmennt n ]nsnuecxn cymecrnymmn n
xonne crpoxn rexcra cnmnon \n, ne cymecrnyer. 3ro nnmt nnnmsnx, xoropym oronopn-
nnct noepxnnart nnnorexn xstxa C n nnrepnperarop Perl. Tax, nanpnmep, nocne xo-
mant print "\n"; Perl nx Unix ntneer n ntxono ]an nonunt cnmnon 0x0D, a Perl
nx Windows napy cnmnonon 0x0A n 0x0D. (Ananornunoe npeopasonanne xonna crpo-
xn nponcxonr npn nnoe, rax uro c rouxn spennx nontsonarenx n 99 cnyuaen +ro pacxo-
xenne mexy nnrepnperaropamn Perl ne yer samerno.)
To ecrt anexo ne nce cncremt nocnpnnnmamr \r xax nonunt cnmnon n \n xax nonu-
nt cnmnon LF n xonponxe ASCII. Hanpnmep, nx xomntmrepon Macintosh +rn cnmnont ne-
pecrannent mecramn. Ha nexoroptx cncremax, ne ncnontsymmnx cnmnon xonna crpoxn, ne-
uart \n nnn \r moxer npnnecrn x norepe ntnonmtx anntx. Ho+romy ncnontsyre cnmnon
\n, xora nt nmeere n nny xonen crpoxn n pamxax name onepannonno cncremt, no menx-
re ero na xnnt cnmnon, xora nam rpeyercx ncrnnnoe nonunoe snauenne.
(Hanpnmep, ontmnncrno cerentx nporoxonon oxnamr (n npenounramr) n xauecrne
cnmnona xonna crpoxn xomnnannn CRLF (012015 nnn cJcM), n xorx onn, cxopee nce-
ro, nomyr n +rom xauecrne cnmnon LF, onn npx nn opaoramr xax xonen crpoxn on-
nount CR. Ho+romy ecnn nt npnntxnere ncnontsonart n cerentx npnnoxennxx cnmnon
\n, nt pano nnn nosno cronxnerect c nenpnxrnocrxmn.)
Ropcfauoeka nepereuuux (uufepnonnuun cfpok)
Kora nt ncnontsyere n crpoxe, saxnmuenno n nonte xantuxn, nmena nepemenntx,
Perl nocrannxer nmecro nnx snauennx, npncnoennte nepemenntm. Hanpnmep, ecnn y nac
ecrt nepemennax $text, n xoropo xpannrcx cnono Hello
$fexf = "he11o"
ro nt moxere ncnontsonart ee nmx n rene crpoxn, n Perl nocrannr Hello nmecro nmenn
nepemenno:
$fexf = "he11o"
p1nf "Pe1 sa p1nf "Pe1 sa p1nf "Pe1 sa p1nf "Pe1 says: $fexf! ys: $fexf! ys: $fexf! ys: $fexf!\ \\ \n" n" n" n"
Pe1 says: he11o!.
3ror nponecc nastnaercx nocranonxo, nnn nnrepnonxnne (interpolation). B uacrnocrn,
n namem npnmepe Perl nnrepnonnponan snauenne, coepxameecx n nepemenno $text, n re-
no crpoxn, saxnmuenno n nonte xantuxn. Onaxo ecnn saxnmunrt reno crpoxn n on-
napnte xantuxn (anocrpo]t), ro Perl ne yer ntnonnxrt nnrepnonxnnm:
$fexf = "he11o"
p1nf Pe1 says: $fexf! p1nf Pe1 says: $fexf! p1nf Pe1 says: $fexf! p1nf Pe1 says: $fexf!\ \\ \n n n n
Pe1 says: $fexf!\n.
Crp.47
To ecrt onnapnte xantuxn ncnontsymrcx, xora ne rpeyercx ntuncnxrt ntpaxenne.
A uro ecnn nt xornre nnrepnonnponart nepemennym xax uacrt pyroro, a ne orentno
nsxroro cnona Hanpnmep, uro ecnn nepemennax $text coepxnr npe]nxc un n nyxno
oannrt ero x cnony happy Ouennno, uro ncnontsonart ntpaxenne rnna $texthappy
ne nonyunrcx, rax xax Perl yer ncxart nepemennym c +rnm nmenem, a ne nnrepnonnpo-
nart $text c nentm noponrt cnono unhappy. Bmecro +roro cneyer ncnontsonart ]n-
rypnte cxoxn n ], nosnonxmmne orennrt nmx nepemenno, xoropym nt xornre nnrep-
nonnponart xax uacrt nonnoro cnona:
$fexf = "un"
p1nf "Donf be ${fexf}happy. p1nf "Donf be ${fexf}happy. p1nf "Donf be ${fexf}happy. p1nf "Donf be ${fexf}happy. " " " "
Donf be unhappy
Hporpammncrt uacro ncnontsymr nnrepnonxnnm nx xonxarenannn crpox:
$a = "he11o"
$b = "fhee"
p1nf "$a $b\n"
he11o fhee
Hpexe uem nepern x cneymmemy paseny, x noxaxy eme onn rpmx. Ecnn nt ncnont-
syere n xauecrne orpannunrenx crpoxn oparnt anocrpo] (), ro ee coepxnmoe nnrep-
npernpyercx xax xomana, nepeanaemax onepannonno cncreme, a pesyntrar ntnonnennx
xomant (ro ecrt rexcr, ntneennt na +xpan) sanocnrcx n nepemennym n xauecrne sna-
uennx. Hanpnmep, n Unix nt moxere raxnm opasom ntnonnnrt xomany uptime, xoropax
coomaer, cxontxo npemenn npomno c momenra nocnene sarpysxn xomntmrepa:
$upf1me = upf1me
p1nf $upf1me
4:29pm up 1 days, 21:22, 13 uses, 1oad aveage: 0.30, 0.39, 0.42
Touno rax xe +ror npnem paoraer n n onepannonno cncreme MS-DOS, re xomany dir
moxno ntnonnnrt cneymmnm opasom:
$d111sf = d1
p1nf $d111sf
D1ecfoy of C:\pe1book\femp
. <Dlk> 10-07-994:02p .
.. <Dlk> 10-07-994:02p ..
1LMP PL 3,535 10-07-994:06p 1. PL
Hocuasua. Bcmpoeuuie oorv+eumi (c+. pasoe. Bieoo ueqop+amupoeauuoeo mercma. ecmpoeuuie oorv+eu-
mi npeoiovue e.aei) e c+ic.e uumepno.uu oreuea.eumui cmpora+, sar.m:euui+ e oeouie raei:ru.
Cnouue cnyau uufepnonnuuu
Bt moxere oennnrt pesyntrar paort nonporpammt c pyrnmn crpoxamn c nomomtm
oneparopa xonxarenannn:
$sf1ng = $fexf1 . mysubouf1ne{$dafa} . $fexf2
C pyro cropont, nnannponanne c onepexennem nosnonxer (c nomomtm xoncrpyxnnn
$...]) nanpxmym nnrepnonnponart nnyrpt saxnmuenno n nonte xantuxn crpoxn sna-
uenne, nosnpamaemoe nonporpammo (o nonporpammax peut noer n rnane 7). Hanpn-
mep, nt xornre nnrepnonnponart nnyrpt crpox snauenne, nosnpamaemoe nonporpammo
getmessage. uopmantno +ro moxno npoenart cneymmnm opasom (oparnre nnnmanne,
uro rpeyercx n xnnom nne ncnontsonart pastmenontnammn npe]nxc nepnt n
Crp.48
otuno neoxsarentnt cnmnon n nmenn nonporpammt):
p1nf "${gefmessage}"
Onaxo uro na camom ene enaercx +ro xomano, rax +ro nocranonxa crpoxn, nosnpa-
maemo nonporpammo npn ee ntsone, a sarem nnrepnperannx pesyntrara xax nmenn ne-
pemenno, snauenne xoropo nao nocrannrt. Tpmx n rom, urot saart n nonporpamme
nyxnoe snauenne nx +ro nepemenno n nepnyrt nmx nepemenno:
p1nf "${gefmessage}"
sub gefmessage {
$msg = "he11o!"
efun "msg"
}
Tenept xomana print "$getmessage]" ntnonnnr ro, uro mt xornm:
p1nf "${gefmessage}"
sub gefmessage {
$msg = "he11o!"
efun "msg"
}
he11o!
Onaxo raxo npnem cpaoraer nnmt nx nanncanntx namn nonporpamm. Hmeercx cno-
co, paorammn n onee mnpoxom cnexrpe napnanron, xoropt sacrannxer Perl ntunc-
nnrt snauenne nonporpammt n nocrannrt ero nnyrpt crpoxn (on ncnontsyer cnnraxcn-
uecxne xoncrpyxnnn, xoropte cranyr nonxrnt nnmt nocne npourennx rnant ). Bor xax
ntrnxnr ncxomax cnnraxcnuecxax xoncrpyxnnx:
$sf1ng = "fexf ${\{sca1afuncf1on dafa}} fexf"
Ecnn xe nt ncnontsyere ]ynxnnm, nosnpamammym cnncox snauenn, ro nam nomoxer
xoncrpyxnnx no nasnannem oesi+uui, nnn auouu+ui +accue (anonymous array):
$sf1ng = "fexf 0{|11sffuncf1on dafa]} fexf"
Hanpnmep, ecnn nt xornre ncnontsonart ]ynxnnm Perl uc nx roro, urot nnonmax cne-
om sa ne yxna crana crpouno (n satnn, uro +ro nerxo peannsyercx c nomomtm escape-
nocneonarentnocrn \u), +ro moxno cenart cneymmnm opasom:
p1nf "${\{uc \"x\"}}"

Oparnre nnnmanne: nnyrpennne nonte xantuxn, ncnontsonannte nx aprymenra
]ynxnnn, saant c nomomtm escape-nocneonarentnocrn \" n npornnnom cnyuae y Perl
tnn t nponemt npn cnnraxcnuecxom pasope crpoxn xax ennoro nenoro.
Opaofka kaeuek u cnoe es kaeuek
B Perl xantuxn, oxpyxammne onnounoe cnono, nnora oxastnamrcx nnmnnmn. Tax npo-
ncxonr n cnyuae, xora cnono moxer trt nnrepnpernponano onosnauno. Hanpnmep, n
cnyuae, paccmorpennom nnxe, npncnoenne crpoxn nepemenno $text ouennno, no+romy
xantuxn ne rpeymrcx:
$fexf = he11o
Ecnn renept ntnecrn nepemennym $text, nonyunm ro, uro n cneonano oxnart:
$fexf = he11o
Crp.49
p1nf p1nf p1nf p1nf $ $$ $fexf fexf fexf fexf
he11o
Taxne cnona es xantuex nastnamrcx n Perl onnountmn cnonamn, nnn npocmi+u c.oea-
+u (barewords). Onaxo ecnn ncnontsyercx onee onoro cnona, ro +ro yxe ne npocroe
cnono, n noonax xoncrpyxnnx paorart ne yer:
$fexf = he11o fhee # Nof good
p1nf $fexf # Does nof Wok
Hnora npocrte cnona moryr nyrartcx c merxamn nnn ecxpnnropamn ]anon (nn ro nn
pyroe ne rpeyer pastmenontnammero npe]nxca). Bt moxere cenart Perl menee rep-
nnmtm x onnountm cnonam n sacrannrt nnrepnperarop neuarart npeynpexammee co-
omenne ncxxn pas, xora cnono es xantuex ne moxer trt nponnrepnpernponano xax
nmx nonporpammt:
use 11sf subs
Kpome roro, uro moxno onycxart nnmnne xantuxn, cnennantnte cnnraxcnuecxne xoncr-
pyxnnn ns ran. 2.3 nomoryr oannrt xantuxn cpecrnamn camoro Perl.
1abnua 2.3. 1abnua 2.3. 1abnua 2.3. 1abnua 2.3. Cuu1axcuuecxue xouc1pyxuu nn xaauuex
Cuu1axcuc Cuu1axcuc Cuu1axcuc Cuu1axcuc eaynu1a1 eaynu1a1 eaynu1a1 eaynu1a1 u1epnonup u1epnonup u1epnonup u1epnonupy yy ye1 e1 e1 e1 Obex1 Obex1 Obex1 Obex1
q/.../ ... he1 C1poxa
qq/.../ "..." a C1poxa
qx/.../ ... a ouaua
qW/.../ {...} he1 Cnucox cnoa
/.../ m/.../ a mabnou noucxa
s/../../ s/../../ a hoc1auoaxa {subsf1fufe}
y/../../ f/../../ he1 3aueuu {fans1afe}
Hanpnmep, ecnn nam xouercx naneuarart crpoxy I said, Hello., ro +ro moxno, xoneuno,
cenart c nomomtm escape-nocneonarentnocre:
p1nf "l sa1d, \"he11o.\""
l sa1d, "he11o. "
Ho nyume nserart raxoro xonnuecrna escape-nocneonarentnocre (na xaprone Perl-
nporpammncron +ro nastnaercx LTS yuennuecxn cnnpom syouncrxn (learning
toothstick syndrome): naunnammne nmemr cxnonnocrt ncnontsonart nsnnmne mnoro cnm-
nonon oparno xoco ueprt, ro ecrt syouncrox). urot nosaornrtcx o nontx xa-
ntuxax nnyrpn crpoxn, moxno ncnontsonart xoncrpyxnnm q//:
p1nf qq/l sa1d, "he11o."/
l sa1d, "he11o. "
Ha camom ene ncnontsonanne naxnonno ueprt ne oxsarentno. onycrnm nourn nmo
neyxnennt cnmnon, ormeuammn nauano n xonen crpoxn:
p1nf qq|l sa1d, "he11o."|
l sa1d, "he11o. "
Moxno axe ncnontsonart cnmnont, xoropte n pyrnx cnyuaxx nrpamr n Perl cnennant-
nym pont, nanpnmep, cnmnon xommenrapnx (sect narnxno nnno, uro n Perl nmte
xoncrpyxnnn sanncxr or xonrexcra):
p1nf qq#l sa1d, "he11o."#
l sa1d, "he11o. "
Moxno raxxe ncnontsonart nx +ro nenn cxoxn:
p1nf qq{l sa1d, "he11o."}
l sa1d, "he11o. "
Crp.50
B annom cnyuae cxoxn cnyxar nx orpannuennx xoncrpyxnnn qq, a ne nx rpynnnponxn
napamerpon npn opamennn x nonporpamme. (Kcrarn, o nonporpammax. B Perl, re nou-
rn ncera ecrt necxontxo cnocoon nx ocrnxennx xaxoro-nno pesyntrara, paspemaercx
eme n onycxart cxoxn npn ntsone nonporpammt, ecnn +ro ne ntstnaer nyrannnt c oc-
rantntmn unenamn ntpaxennx.)
Haxonen, neoxonmo ormernrt, uro orpannunnartcx rpve.i+u cxoxamn roxe ner neoxonmo-
crn: moxno ncnontsonart .moie cxoxn xpyrnte, ]nrypnte, xnaparnte nnn axe yrnonte:
p1nf qq{l sa1d, "he11o."}
l sa1d, "he11o. "
p1nf qq|l sa1d, "he11o."]
l sa1d, "he11o. "
p1nf qq<l sa1d, "he11o.">
l sa1d, "he11o. "
-fo fakoe cnucok?
Perl nosnonxer conpart cxanxpnte nepemennte (n pyrne rnnt anntx maccnnt,
x+m-rannnt) n cnucru. Cnncxn ouent naxnt nx Perl. Bcrpoennte ]ynxnnn Perl pasnrt
na ne rpynnt: nx paort co cxanxpamn n nx paort co cnncxamn (na camom ene nexo-
ropte paoramr n c rem n c pyrnm.)
B Perl ner cnennantnoro rnna anntx nx cnncxon. Onaxo nmeercx onepamop cnucra,
a nmenno, napa xpyrntx cxoox. urot cosart cnncox, ocrarouno nepeuncnnrt uepes
sanxrym ero +nemenrt n saxnmunrt ncm xoncrpyxnnm n xpyrnte cxoxn. Hanpnmep, nt-
paxenne (1, 2, 3) coornercrnyer cnncxy, cocroxmemy ns rpex +nemenron: 1, 2 n 3.
Oneparop print xnnxercx cnncountm
1
. Ecnn nt noanre emy na nxo cnncox, on oe-
nnnr ero +nemenrt n ony crpoxy (ntnonnnr xonxarenannm orentntx crpox). Hanpn-
mep, ecnn saart na nxoe cnncox (1, 2, 3)
p1nf {1, 2, 3}
ro na ntxoe mt ynnnm 123
Hocuasua. J.e+euma+u cnucra +oevm oimi mo.iro cra.puie sua:euu. Bi, roue:uo, +oreme ucno.i-
soeami roucmpvrum euoa (1, 2, (4, 5), 7, 8), uo omo mo re ca+oe, :mo u (1, 2, 4, 5, 7, 8) uuoueuova.i-
uocmi euvmpeuueeo cnucra mepemc, a eeo o.e+eumi npocmo e.ueammc eo eueuuu cnucor.
Kax n pantme, xpyrnte cxoxn noxpyr cnncxa moxno onycrnrt n +rom cnyuae Perl y-
er nnrepnpernponart print xax xomany, a ne xax ntson ]ynxnnn:
p1nf 1, 2, 3
123
Ccunka ua snereufu cnucka epes uupekc
Hocne cosannx cnncxa nt moxere cctnartcx na ero orentnte +nemenrt, ncnontsyx
xnaparnte cxoxn ]. Moxno cunrart, uro +ro oneparop nnexca cnncxa. Hanpnmep, ecnn
cenart cnncox, cocroxmn ns nnrep a, b n c (nx ner neoxonmocrn saxnmuart n xantu-
xn, nocxontxy +ro npocrte cnona), ro moxno cocnartcx na +nemenr c nnexcom 1 (yxna

1
Kax n n C, n Perl ner pasnnunx mexy ]ynxnnxmn n nponeypamn (xomanamn). Onaxo no mnornx cnyuaxx ner pasnnunx n
mexy ]ynxnnxmn n oneparopamn rax, xnmuenoe cnono print nrpaer pont oneparopa, xora ero aprymenrt yxasant es
cxoox, n pont ]ynxnnn, xora ero aprymenrt saxnmuent n xpyrnte cxoxn (noponee cm. rnany ). Hpu+e:. nepee.
Crp.51
b n Perl +nemenrt nymepymrcx c nynx). Hanpnmep,
$va1ab1e1 = {a, b, c}|1]
Ecnn pacneuarart +ry nepemennym, ro nonyunm oxnaemt pesyntrar:
$va1ab1e1 = {a, b, c}|1]
p1nf $va1ab1e1 p1nf $va1ab1e1 p1nf $va1ab1e1 p1nf $va1ab1e1
b
Oparnre nnnmanne, uro moxno nnexcnponart axe cnncox, nosnpamaemt ]ynxnne.
Taxnm opasom, mt nonyuaem npocro cnoco paort co cnncxontmn ]ynxnnxmn, xora
na ntxoe rpeyercx rontxo cxanxpnt pesyntrar ns nonnoro cnncxa snauenn.
Rpuceaueauue cnuckoe cnuckar
Moxno npncnonrt onn cnncox pyromy, ncnontsyx oneparop npncnoennx . Hanpnmep, sect
mt npncnannaem +nemenram cnncxa ($a, $b) coornercrnymmne nm +nemenrt cnncxa ($c, $d):
{$a, $b} = {$c, $d}
3nemenrt cnncxa, croxmne cnpana, paccmarpnnamrcx xax ntuncnxemte ntpaxennx, a +nemenrt
cnncxa cnena xax nente snauennx oneparopa npncnannannx. Tem camtm na cnncxa moryr
coepxart nepecexammnecx nepemennte, n npn +rom ne nosnnxner nyrannna cnncox n npano
uacrn ntuncnxercx o onepannn npncnannannx. Hanpnmep, raxnm cnocoom moxno nomenxrt
mecramn snauennx nepemenntx $a n $b, ne npnerax x nomomn npomexyrouno nepemenno:
{$a, $b} = {$b, $a}
Cnncxn, yuacrnymmne n +ro onepannn, moryr trt pasno nnnt. B cneymmem npnmepe ne-
pemenntm $a n $b npncnannamrcx nepnte na +nemenra cnncxa, cocroxmero ns rpex +nemenron:
{$a, $b} = {1, 2, 3}
p1nf $a
1
p1nf $b
2
Ecnn uncno +nemenron n neno uacrn oxaxercx ontme uncna +nemenron n npano, ocran-
mnecx nepemennte nonyuar neonpeenennoe snauenne undef. 3ro nponsoer axe n rom
cnyuae, ecnn o onepannn npncnoennx y nnx tno nexoropoe snauenne:
$a =0 $b = 0 $c = 0
{$a, $b, $c} = {1, 2}
p1nf $a, "\n"
p1nf $b, "\n"
p1nf $c, "\n"
1
2
use an un1n1f1a11ed va1ue af - 11ne 5
Ecnn n xauecrne nentx snauenn saart undef, ro coornercrnymmne +nemenrt cnncxa
cnpana yyr nponyment:
{$a, undef, $b} = {1, 2, 3}
p1nf $a
1
p1nf $b
3
Crp.52
Rpeopasoeauue cnucka
urot ntnonnnrt ony n ry xe onepannm na ncemn +nemenramn cnncxa, ncnontsyercx
]ynxnnx map:
map bnox cnucox
map aupaeuue cnucox
Ona ntnonnxer xo, yxasannt xax nox oneparopon nnn xax ntpaxenne, nx xaxoro +nemenra
cnncxa. Hepe ntnonnennem ouepeno onepannn cnennantno nepemenno $_ npncnannaercx
coornercrnymmn +nemenr cnncxa. Ha ntxoe nonyuaem pesyntrar ntuncnenn. Hanpnmep,
urot npeopasonart nce +nemenrt cnncxa x crpountm yxnam, moxno ncnontsonart ncrpoen-
nte ]ynxnnn Perl: lc (xoropax samenxer n crpoxe sarnannte yxnt na crpounte) n map:
{$a, $b} = map {1c, , 8}
p1nf $a, $b
ab
(3ect ncnontsonano ro cnocrno, uro ]ynxnnx lc ncnontsyer nepemennym $_ n xauecrne
nxonoro snauennx, ecnn e ne yxasart napamerpt.)
OLepuueuue snereufoe cnucka e cfpoky
nx xonxarenannn ncex +nemenron cnncxa n ony crpoxy moxno ncnontsonart ]ynxnnm oin:
o1n aupaeuue, cnucox
3ra ]ynxnnx oennxer n ony crpoxy +nemenrt cnncxa (nanomnnm, uro n Perl apn]me-
rnuecxne n crpoxonte annte cnoono npeopasymrcx pyr n pyra), ncnontsyx saan-
noe ntpaxenne n xauecrne pasennrenx mexy +nemenramn. Bor npnmep ee paort:
p1nf o1n {":", "12", "00", "00"}
12:00:00
Crporo ronopx, moxno tno t oornct n es nontx xantuex, no ecnn ypart no-
nte xantuxn noxpyr 00, ro Perl yer nnrepnpernponart +ror +nemenr xax uncno nont, n
n pesyntrare mt norepxem neymne nynn, nonyunn na ntxoe 12:0:0.
B cnyuae uncro xonxarenannn es pasennrene ocrarouno saart n xauecrne ntpa-
xennx nycrym crpoxy:
p1nf o1n {"", h, e, 1, 1, o}
he11o
Rpeepaeuue cfpoku e cnucok
urot pasnrt crpoxy na uacrn n npenparnrt ee n cnncox, moxno ncnontsonart ncrpoen-
nym ]ynxnnm split:
sp11f /abnou/, aupaeuue, aepxuuu-npeen
sp11f /abnou/, aupaeuue
sp11f /abnou/
sp11f
uynxnnx split pasnnaer crpoxonoe eipareuue no rpannnam, ormeuenntm nocrpoxo-
uao.ouo+, n opasyer ns uacre cnncox (npn +rom cam manon yanxercx). Ecnn saan
eepxuu npeoe., xoropt onxen trt nonoxnrentntm uncnom, uncno +nemenron cnncxa
Crp.53
ne yer npentmart saannoe. Ecnn eipareuue ne saano, ncnontsyercx cnennantnax
nepemennax $_. Ecnn orcyrcrnyer uao.ou, nponcxonr pasnnxa no rax nastnaemtm
npoentntm cnmnonam (cma nxonr npoen, snax raynxnnn, cnmnon nono crpoxn n
r. ., npnuem necxontxo npoentntx cnmnonon nopx paccmarpnnamrcx xax onn).
Cneymmn npnmep npenpamaer crpoxy H,e,l,l,o n cnncox ns orentntx yxn n ntnonr
ero na neuart:
p1nf sp11f /,/, "h,e,1,1,o"
he11o
Copfupoeka cnuckoe
nx coprnponxn cnncxon ncnontsyercx ]ynxnnx sort:
sof nonporpauua cnucox
sof bnox cnucox
sof cnucox
3ra ]ynxnnx coprnpyer cnncox n nosnpamaer orcoprnponannt pesyntrar. Hontsonarent
moxer saart npannno coprnponxn n nne nmenn nonporpammt nnn n nne noxa oneparo-
pon. Honporpamma onxna nosnpamart pesyntrar cpannennx nyx napamerpon ananornuno
oneparopam cpannennx <> n cmp (cm. rnany ). Honporpamma moxer trt samenena no-
xom oneparopon, ntnonnxmmnx ry xe ]ynxnnm. Ecnn ne yxasant nn nmx nonporpammt, nn
nox oneparopon, ]ynxnnx sort ncnontsyer cranaprnym onepannm cpannennx crpox. Ha-
npnmep, anare orcoprnpyem cnncox ("c", "b", "a"):
p1nf sof {"c", "b", "a"}
abc
Moxno xnno saart nox, cpannnnammn ne crpoxn, n nonyunrt ror xe pesyntrar:
p1nf sof {$a cmp $b} {"c", "b", "a"}
abc
Moxno coprnponart cnncox n oparnom nopxxe:
p1nf sof {$b cmp $a} {"c", "b", "a"}
cba
Moxno ncnontsonart uncnonoe cpannenne nx coprnponxn snauenn:
p1nf sof {$a <=> $b} {3, 2, 1}
123
Haxonen, ror xe xo moxno nomecrnrt n nonporpammy:
sub myfuncf1on {
efun {sh1ff{0} <=> sh1ff{0}}
}
p1nf sof myfuncf1on {3, 2, 1}
123
uro +xnnnanenrno noxy
p1nf sof {myfuncf1on{$a, $b}} {3, 2, 1}
123
Hueepfupoeauue cnucka
Crp.54
urot nnneprnponart cnncox, ncnontsyre ]ynxnnm reverse:
evese cnucox
Bor xax moxno ncnontsonart +ry ]ynxnnm, urot nnneprnponart +nemenrt n cnncxe (1, 2, 3):
p1nf evese {1, 2, 3}
321
Buop snereufoe us cnucka
nx ntenennx ns cnncxa nocnncxa, cocroxmero ns +nemenron, coornercrnymmnx nexo-
ropomy xpnrepnm, ncnontsyercx ]ynxnnx grep:
gep bnox cnucox
gep aupaeuue cnucox
3ro ananor xomant grep onepannonno cncremt Unix. uynxnnx ntuncnxer nox xoman
nnn ntpaxenne nx xaxoro +nemenra cnncxa (npn +rom cnennantnax nepemennax $_ panna
ouepenomy +nemenry cnncxa) n cosaer nocnncox ns +nemenron, nx xoroptx ntuncnennoe
snauenne coornercrnyer ycnonnm ncrnna (ro ecrt ornnuno or nynx, nycro crpoxn n r. .).
Otuno grep ncnontsyer noncx no manony, xax, nanpnmep, n cneymme crpoxe, re
cosaercx nocnncox ns yxn, xoropte ne xnnxmrcx yxno x:
p1nf gep {!/x/, a, b, x, d}
abd
Fonee nopono o noncxe no manony nt ysnaere ns rnant , no, xax nnno yxe ceuac,
]ynxnnx grep npeocrannxer momnt nncrpymenr nx cosannx ns cnncxon nocnncxon,
ocnontnaxct na onpeenennom xpnrepnn noncxa.
Ckannpuu u cnuckoeu koufekcfu
Cxanxpnt n cnncxont xonrexcrt +ro na ocnonntx xonrexcra anntx n xstxe Perl.
Ouent naxno nonnmart pasnnny mexy nnmn. Mt yem nocroxnno ncrpeuartcx co cxa-
nxpntm n cnncxontm xonrexcramn n +ro xnnre.
Kora Perl oxnaer nonyunrt cnncox, on paccmarpnnaer annte n cnucroeo+ roumercme,
a xora cxanxpnoe ntpaxenne n cra.puo+. B pesyntrare, xora annte npeono.aea-
mmc n nne cnncxa, onn n rpaxrymrcx xax cnncox. Coornercrnenno, xora annte npe-
nonaramrcx n nne cxanxpnoro ntpaxennx, onn rpaxrymrcx xax cxanxp.
pyrnmn cnonamn, ro, xax nmenno nnrepnpernpymrcx annte, npn nporpammnponannn na
Perl onpeenxercx ueeui+ oopaso+ ns xonrexcra, n xoropom +rn annte ncnontsyercx, n
ne saaercx n xnno nne c nomomtm nporpammnoro xoa. Hanpnmep, ecnn nt paoraere c
]ynxnnxmn, xoropte ncnontsymr aprymenrt-cnncxn nnn nosnpamamr snauennx-cnncxn,
ro +rn snauennx anromarnuecxn nnrepnpernpymrcx xax cnncxn.
B cxanxpnom xonrexcre cnncox npeopasyercx n cxanxp (otuno n nocnenn +nemenr cnn-
cxa, cm. cneymmn pasen), a n cnncxonom xonrexcre cxanxp npenpamaercx n cnncox ns ono-
ro +nemenra. Onaxo nyxno samernrt, uro n Perl ner ennoro npannna, onpeenxmmero npeo-
pasonanne anntx npn nepexoe ns onoro xonrexcra n pyro. Hanpnmep, npn nepexoe ns
cnncxonoro xonrexcra n cxanxpnt onn oneparopt moryr nocrannxrt uncno +nemenron cnn-
cxa, xoropt onxen tn trt npn cnncxonom xonrexcre, pyrne nosnpamamr nepnoe sna-
Crp.55
uenne cnncxa, rpertn nosnpamamr nocnenn +nemenr cnncxa, a nexoropte axe nosnpa-
mamr uncno ycnemno nponeenntx onepann na cnncxom. 3ro moxer noxasartcx cnoxntm,
no otuno nepexonrt or onoro xonrexcra x pyromy (ocoenno or cnncxonoro x cxanxpnomy)
ne rpeyercx, rax uro npx nn cnnmxom uacro npnercx saocrpxrt nnnmanne na +rnx eranxx.
4opcupoeauue ckannpuoro koufekcfa
uynxnnx scalar rpeyer nnrepnperannn ntpaxennx n cxanxpnom xonrexcre (npn +rom n
Perl ner napnoro oneparopa, ntnyxammero nnrepnpernponart ntpaxenne n cnncxonom
xonrexcre). uynxnnx scalar ncnontsyercx rax:
sca1a aupaeuue
Hanpnmep, moxno ntnecrn cnncox (1, 2, 5) n cnncxonom xonrexcre:
p1nf {1, 2, 5}
125
Ecnn nt ncnontsyere ]ynxnnm scalar, ro ona ntnynr nnrepnpernponart cnncox n cxa-
nxpnom xonrexcre. To ecrt yer nosnpamen nocnenn +nemenr cnncxa:
p1nf sca1a {1, 2, 5}
5
uynxnnx scalar nosnpamaer nocnenn +nemenr cnncxa, +mynnpyx rem camtm noneenne
oneparopa-sanxro xstxa C. On neer cex rouno rax xe, a nmenno nosnpamaer nocnenee
ntuncnennoe ntpaxenne n cnncxe ntpaxenn, pasenenntx sanxrtmn. To xe camoe npo-
ncxonr npn npncnoennn cxanxpno nepemenno ntpaxennx-cnncxa:
$a = {1, 2, 5}
p1nf $a
5
Kpome npncnoennx snauennx cxanxpno nepemenno, cxanxpnt xonrexcr nx annoro
cnncxa moxno ]opcnponart n menee +neranrno ntnonnnn na cnncxom apn]mernue-
cxym onepannm (nanpnmep, npnannn nont oneparopom cnoxennx +).
Crp.56
Fnana 3

Maccunm, xamu u sanucu
1anuum cuunonon




Kopo1ko
B +ro rnane yer npenpnnxra nontrxa ntxcnnrt, xax oprannsonant annte n raxnx
xpane naxntx crpyxrypax, xax +accuei n xouu. Kpome roro, yer onncana paora c
pyrnm naxntm rnnom anntx sanuc+u mao.ui cu+eo.oe (typeglob).
Maccueu
Maccnnt npecrannxmr coo cocroxmne ns cxanxpon cnncxn c nenouncnenntm nnex-
com. Hnexc nosnonxer cctnartcx na cxanxpt, sanecennte n maccnn, +ro ouent nonesno
nx nporpammnponannx, rax xax nosnonxer ynennunnart nnn ymentmart nnexc n nony-
uart ocryn ns nporpammt x nmomy +nemenry, paorax cpasy co ncem maccnnom. nx
cosannx maccnna neoxonmo npncnonrt nepemenno-maccnny n xauecrne snauennx cnn-
cox (n Perl raxne nepemennte naunnamrcx c npe]nxca ):
0aay = {1, 2, 3}
urot cctnartcx na orentnte +nemenrt maccnna, cneyer yxasart nnexc +nemenra n
xnaparntx cxoxax n samennrt npe]nxc na npe]nxc $ +ro noxastnaer, uro mt pa-
oraem co cxanxpom. Oparnre raxxe nnnmanne, uro nnexct nx maccnnon Perl orcunrt-
namrcx or nynx:
p1nf $aay|0]
1
B +ro rnane ocnonnoe nnnmanne yer yenxrtcx cranaprntm onomepntm maccnnam. B
rnane 13, peut n xoropo ner o crpyxrypax anntx, paccxastnaercx raxxe o nymepntx
maccnnax.
Xsuu
X+m-rannnt (x+mnponannte rannnt, nnn npocro x+mn), nastnaemte raxxe acconna-
rnnntmn maccnnamn nx ocryna x orentntm +nemenram anntx, ncnontsymr ne nnex-
ct, a xnmun. Hpn ncnontsonannn x+me snauennx acconnnpymrcx c rexcrontmn xnmuamn,
nanpnmep:
Crp.57
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
Tenept moxno ncnontsonart +rn annte, npnmennn nx ocryna x nnm xnmu
p1nf $hash{sandW1ch}
hambuge
Hpecrannenne namnx anntx n nne x+me, xax npannno, onee nnrynrnnno (n ornnune
or maccnnon), nocxontxy nx nsnneuennx anntx ncnontsymrcx xnmun. X+mn xnnxmrcx
neantntm cpecrnom nx cosannx sannce anntx.
3anucu fanuuu cureonoe typeglob
Tnn anntx typeglob eme onn ncrpoennt rnn Perl. Pastmenontnammn npe]nxc
nx nero snesouxa . Tnn anntx typeglob ncnontsyercx nx cosannx cnnonnmon nx
rnnon anntx, acconnnponanntx c yxasanntm nmenem. (Coornercrnenno, snesouxa, xo-
ropax xnnxercx raxxe n ynnnepcantntm manonom npn ntope ]anon, orpaxaer yx no-
noro rnna anntx.) Hanpnmep, npn nannunn nepemenntx $data n data:
$dafa = "hees fhe dafa"
0dafa = {1, 2, 3}
c nomomtm xoncrpyxnnn typeglob moxno cctnartcx na +rn nepemennte no pyrnm nmenem:
"a1sodafa = "dafa
Tenept $alsodata xnnxercx cnnonnmom nx $data, a alsodata nx data:
p1nf "$a1sodafa\n"
hees fhe dafa
Hocuasua. Tun oauuix typeglob *u+ omo euvmpeuu cmpvrmvpa (sanuci mao.ui cu+eo.oe), xpauua uu-
qop+aum ooo ecex nepe+euuix c u+eue+ u+, (mo ecmi o cra.puo nepe+euuo $u+a, +accuee u+a, xoue
"u+a, noonpoepa++e &u+a, oecrpunmope qa.a u+). B :acmuocmu, o. rarooeo muna oauuix e mao.uv cu+-
eo.oe sanucieaemc aopec oo.acmu na+mu (cci.ra), eoe ouo xpauumc. Hpuceoue oouo sanucu cooepru+oe
opveo sanucu, +i npocmo sacmaeu.u cci.ru oevx mao.u cci.amic ua oouv u mv re oo.acmi na+mu.
Ha +rom c nneennem noxonueno. B cneymmem pasene +rn rnnt anntx paccmar-
pnnamrcx onee nopono.
Henocpepc1neuume pemeuun
Cospauue raccueoe
Hmena maccnnon naunnamrcx c cnmnona . urot cosart maccnn, cneyer npncnonrt ne-
pemenno maccnna n xauecrne aprymenra cnncox
0aay = {1, 2, 3}
urot ynnert pesyntrar, ntneem nont maccnn
0aay = {1, 2, 3}
p1nf p1nf p1nf p1nf 0 00 0a aa a ay ay ay ay
123
Hocuasua. Ko+auoa print uumepnpemupvem +accue rar cnucor u ooeouuem eeo o.e+eumi e eouuoe e-
.oe. Bo.ee rpacueie cnocooi eieooa cooepru+oeo +accuea ei uaoeme e pasoe.e Bieoo +accuea.
Crp.58
Ha orentnte +nemenrt maccnna moxno cctnartcx, yxastnax nnexc n xnaparntx cxo-
xax n samenxx npe]nxc nmenn cnmnonom $ (noromy uro +nemenr maccnna cxanxp):
0aay = {1, 2, 3}
p1nf $aay|0]
1
Kpome uncen n maccnne moxno xpannrt n pyrne cxanxpt, nanpnmep crpoxn:
0aay = {"one", "fWo", "fhee"}
p1nf 0aay
onefWofhee
Hocxontxy Perl npn opaorxe cnncxon nponycxaer nce npoentnte cnmnont (nxnmuax
cnmnont nono crpoxn), aprymenr n nne cnncxa snauenn moxno nerxo pasnrt na ne-
cxontxo crpouex:
0aay = {"one", "fWo", "fhee",
"fou", "f1ve", "s1x",}
p1nf 0aay
onefWofheefouf1ves1x
Moxno raxxe ncnontsonart oneparop nonropennx, uro n annom cnyuae cosacr maccnn ns
cra nyne:
0aay = {0} x 100
Haxonen, moxno ncnontsonart oneparopt-xantuxn (cm. pasen Opaorxa xantuex n
cnon es xantuex n ran. 2.3 n npetyme rnane):
0aay = qW{one fWo fhee}
p1nf 0aay
onefWofhee
Kpome onncanntx cnocoon, nx cosannx maccnnon n oannennx n nnx nontx +nemenron
ncnontsymrcx raxxe ]ynxnnn push n unshift (onn paccmarpnnamrcx nnxe n +ro rnane).
Xorx +nemenrt n maccnnax Perl no ymonuannm orcunrtnamrcx c nynx, moxno nsmennrt
+ro npannno, sanecx nonoe asonoe snauenne n cnennantnym nepemennym $. Ee ncnontso-
nanne, onaxo, cunraercx ypntm ronom. Xorx +ror cnoco n ne sanpemen, npx nn ero
xro-ro oopnr. (B +rom ornnune Perl or xstxon rnna Java, n xoroptx merot, ntstnam-
mne neoopenne, npocro sanpemamrcx.)
Hcnonusoeauue raccueoe
Hocne cosannx maccnna moxno cctnartcx na ero orentnte +nemenrt xax na cxanxpt,
yxastnax nnexc n xnaparntx cxoxax n ncnontsyx npe]nxc $ nepe nmenem maccnna:
0aay = {"one", "fWo", "fhee"}
p1nf $aay|1]
fWo
Hocxontxy nx ocryna x +nemenram maccnna moxno ncnontsonart nnexc, maccnnt mo-
ryr paorart xax rannnt nocranonox. Tax n cneymmem npnmepe ecxrnunoe uncno ns
nanasona or 0 o 15, nnonmoe nontsonarenem, samenxercx na ero mecrnanarepnunt
+xnnnanenr:
0aay = {0, 1, 2, 3, 4, 5, 6, 7,
, 9, , 8, C, D, , I}
Wh11e {<>} { $hex = $aay|$]
Crp.59
p1nf "$hex\n" }
C rouxn spennx nporpammncra ror ]axr, uro maccnnt oecneunnamr ocryn x +nemenram
anntx no nomepy, xnnxercx momntm cpecrnom, nosnonxmmnm nocneonarentno opa-
artnart maccnn, nsmenxx n nnxne nnexc. Hanpnmep, rax ntnoxrcx nce +nemenrt mac-
cnna no ouepen:
0aay = {"one", "fWo", "fhee"}
fo {$1ndex = 0 $1ndex <= $#aay $1ndex++} {
p1nf $aay|$1ndex], " " }
one fWo fhee
Oparnre nnnmanne na ncnontsonanne xoncrpyxnnn $#array. B Perl ona osnauaer nocne-
nn nnexc maccnna array (cm. anee pasen Onpeenenne nnnt maccnna).
Onepauuu push u pop
Kpome npncnoennx cnncxom, nx nsmenennx maccnnon moxno ncnontsonart ]ynxnnn push
n pop. uynxnnx push oannxer onn nnn necxontxo +nemenron n xonen maccnna:
push uaccua, cnucox
3nauennx cnncxa oannxmrcx n xonen uaccnna, a nnna uaccnna ynennunnaercx na nn-
ny cnncxa.
uynxnnx pop yanxer annte ns maccnna
pop uaccua
pop
Ona yanxer n nosnpamaer n xauecrne cnoero pesyntrara nocnenee snauenne n uaccnne,
coxpamax nnny uaccnna na onn +nemenr. Cneymmn npnmep noxastnaer, xax nocne-
onarentno oannxmrcx +nemenrt n (nsnauantno nycro) maccnn:
push {0aay, "one"} push {0aay, "one"} push {0aay, "one"} push {0aay, "one"}
push {0aay, "fWo"} push {0aay, "fWo"} push {0aay, "fWo"} push {0aay, "fWo"}
push {0aay, "fhee"} push {0aay, "fhee"} push {0aay, "fhee"} push {0aay, "fhee"}
p1nf $#aay, :, 0aay
2:onefWofhee
(oparnre nnnmanne, uro $#array na ennnny mentme uncna +nemenron maccnna). Anano-
rnuntm opasom yanxmrcx annte ns xonna maccnna
0aay = {"one", "fWo", "fhee"}
$va1ab1e1 = pop {0aay} $va1ab1e1 = pop {0aay} $va1ab1e1 = pop {0aay} $va1ab1e1 = pop {0aay}
p1nf "$va1ab1e1/$#aay"
fhee/1
Onepauuu shit u unshit
uynxnnn shift n unshift paoramr c nauanom maccnna rax xe, xax push n pop c ero xon-
nom. Bor xax ncnontsyercx shift:
sh1ff uaccua
sh1ff
3ra ]ynxnnx yanxer nepnt +nemenr maccnna, nosnpamax ero xax pesyntrar. Maccnn co-
xpamaercx na onn +nemenr, a ocrantnte +nemenrt cnnramrcx na ony nosnnnm nnpano.
Crp.60
A nor xax ntrnxnr ]ynxnnx unshift:
unsh1ff uaccua, cnucox
3ra ]ynxnnx ntnonnxer onepannm, npornnononoxnym ecrnnxm ]ynxnnn shift. Ona no-
cneonarentno oannxer cnncox n nauano uaccnna, cnnrax ocrantnte +nemenrt nnpano
n ynennunnax nnny uaccnna na pasmep cnncxa.
Hpnmep ncnontsonannx ]ynxnnn shift nx nsnneuennx +nemenra maccnna:
0aay = {"one", "fWo", "fhee"}
$va1ab1e1 $va1ab1e1 $va1ab1e1 $va1ab1e1 = = = = sh1ff {0aay} sh1ff {0aay} sh1ff {0aay} sh1ff {0aay}
p1nf "$va1ab1e1/$#aay"
one/1
Onpepeneuue pnuuu raccuea
Ecnn onpeenen maccnn c nmenem array, nepemennax $#array coepxnr nnexc nocnenero
ero +nemenra. Hmx moxer trt nmtm: ecnn maccnn nasnan, nanpnmep, phonenumbers, ro
nnexc ero nocnenero +nemenra coepxnrcx n nepemenno $#phonenumbers.
Hanpnmep, npn nannunn maccnna
0aay = {1, 2, 3}
urot ntnecrn uncno ero +nemenron, nao oannrt ennnny x nepemenno $#array:
0aay = {1, 2, 3}
p1nf " p1nf " p1nf " p1nf "\ \\ \0aay has" . "{$#aay + 1} . 0aay has" . "{$#aay + 1} . 0aay has" . "{$#aay + 1} . 0aay has" . "{$#aay + 1} . " " " " e1emenfs e1emenfs e1emenfs e1emenfs." ." ." ."
0aay has 3 e1emenfs.
(Ennnna npnannxercx noromy, uro nnexct maccnna orcunrtnamrcx c nynx).
Hcnontsonanne maccnna n cxanxpnom xonrexcre raxxe nosnpamaer ero nnny. uro
t nepenecrn maccnn n cxanxpnt xonrexcr, moxno ntnonnnrt na nnm ]nxrnnnym un-
cnonym onepannm (nanpnmep, npnannrt nont):
0aay +0
nno, uro onee npo]eccnonantno, ncnontsonart ]ynxnnm scalar:
0aay = {1, 2, 3}
p1nf " p1nf " p1nf " p1nf "\ \\ \0aay has" . sca1a {0aay} . 0aay has" . sca1a {0aay} . 0aay has" . sca1a {0aay} . 0aay has" . sca1a {0aay} . " " " " e1emenfs e1emenfs e1emenfs e1emenfs." ." ." ."
0aay has 3 e1emenfs.
nno, naxonen, nonpocry npncnonrt maccnn cxanxpno nepemenno:
0aay = {1, 2, 3}
$ $$ $va1ab1e = 0 va1ab1e = 0 va1ab1e = 0 va1ab1e = 0a aa a ay ay ay ay
p1nf "\0aay has $va1ab1e e1emenfs."
0aay has 3 e1emenfs.
Hocuasua. Oopamume euu+auue, :mo ec.u ei npuceoume cra.puo nepe+euuo cnucor, a ue +accue, mo
pesv.imamo+ ovoem ue o.uua cnucra, a noc.eouu o.e+eum cnucra. B omo+, rar u eo +uoeux opveux npu-
+epax, npoe.emc roumercmua saeucu+ocmi ro+auo Perl, o romopo +i vre eoeopu.u e e.aee 2.
Yeenueuue unu cyeuue raccuea
urot nsmennrt uncno +nemenron n maccnne, ocrarouno npncnonrt nonoe snauenne ne-
pemenno $#array, xpanxme nnexc nocnenero +nemenra. Bor npnmep raxo onepannn:
$aay = {1, 2, 3}
Crp.61
$#aay = 10
$aay|5] = "here 1s a neW e1emenf!"
p1nf $aay|5]
Wee 1s a neW e1emenf!
(nce nononneennte +nemenrt maccnna nonyuamr neonpeenennoe snauenne undef).
Moxno nonnocrtm ouncrnrt maccnn, npncnonn nnexcy nocnenero +nemenra or-
pnnarentnoe uncno:
$#aay = -1
Cnunuue peyx raccueoe
urot oennnrt na maccnna, nx moxno npncnonrt rpertemy maccnny xax cnncox. B
cneymmem npnmepe maccnnt array1 n array2 oennxmrcx n maccnn bigarray:
0aay1 = {1, 2, 3}
0aay2 = {4, 5, 6}
0b1gaay = {0aay1, 0aay2}
C nontm maccnnom moxno ntnonnxrt nmte onepannn, onycrnmte nx maccnnon:
p1nf $b1gaay|5]
6
Ronyeuue cpesa raccuea
Cpes maccnna +ro uacrt maccnna, cosanaemax c nomomtm oneparopa nanasona. On nmeer
]opmar x..y] n coornercrnyer maccnny c +nemenramn, nmemmnmn nnexct x, x+1, ..., n anee
o y nxnmunrentno.
B cneymmem npnmepe c nomomtm +ro xoncrpyxnnn yer cosan nomaccnn array2,
cocroxmn ns +nemenron 2 n 3 maccnna array:
0aay = {1, 2, 3, 4, 5, 6, 7, , 9, 10}
0aay2 = 0aay|2..3]
p1nf o1n{", ", 0aay2}
3, 4
Uuknu u raccueu
Kax yxe ronopnnoct pantme n +ro rnane, c nomomtm nnxna for moxno nocneonarentno
nepepart +nemenrt maccnna, xnno opamaxct x xaxomy +nemenry no ero nnexcy:
0aay = {"one", "fWo", "fhee"}
fo {$1oop1nd=0 $1oop1nd<=$#aay $1oop1nd++} {
p1nf $aay|$1oop1nd]
}
onefWofhee
Moxno raxxe nocnontsonartcx pyrnm oneparopom nnxna - foreach. On nenocpecrnenno
nepenpaer +nemenrt maccnna:
0aay = {1, 2, 3, 4, 5}
foeach $e1emenf {0aay} {
p1nf "$e1emenf\n"
}
Crp.62
1
2
3
4
5
Moxno oprannsonart nnxn cpasy no necxontxnm maccnnam, nepeuncnnn nocnenne n cnn-
cxe (uro nnrepnonnpyer +nemenrt maccnnon n onn cnncox):
0aay1 = {1, 2, 3}
0aay2 = {4, 5, 6}
foeach $ foeach $ foeach $ foeach $e1emenf {0aay1, 0aay2} { e1emenf {0aay1, 0aay2} { e1emenf {0aay1, 0aay2} { e1emenf {0aay1, 0aay2} {
p1nf "$e1emenf\n"
}
1
2
3
4
5
6
Bmecro nnxna foreach moxno ncnontsonart nnxn Ior (na camom-ro ene for n foreach
+ro onn n ror xe oneparop):
0aay = {1, 2, 3, 4, 5}
fo fo fo fo $e1emenf {0aay} { $e1emenf {0aay} { $e1emenf {0aay} { $e1emenf {0aay} {
p1nf "$e1emenf\n"
}
1
2
3
4
5
Hpn xenannn moxno axe ncnontsonart nnxn for es cctnxn na xonxpernym nepemennym
nnxna, ncnontsyx cnennantnym nepemennym no ymonuannm $_:
0aay = {1, 2, 3, 4, 5}
fo fo fo fo {0 {0 {0 {0aay aay aay aay} { } { } { } {
p1nf }
12345
Hrax, nnxn moxer oprannsontnartcx mnoxecrnom cnocoon. Konxpernt napnanr sanncnr
rontxo or namero nxyca n or pesyntraron, xoropte onxna art nporpamma.
Bueop raccuea
Ecnn rpeyercx ntnecrn maccnn, moxno nepeart ero ]ynxnnn print xax
0aay = {"one", "fWo", "fhee"}
p1nf "hee 1s fhe aay: p1nf "hee 1s fhe aay: p1nf "hee 1s fhe aay: p1nf "hee 1s fhe aay: ",0aay ",0aay ",0aay ",0aay, , , , ". ". ". ". \ \\ \n" n" n" n"
hee 1s fhe aay: onefWofhee.
Heocrarox +roro meroa n rom, uro print paccmarpnnaer maccnn xax cnncox, a noromy ne-
uaraer +nemenrt onn sa pyrnm, uro nopoxaer na ntxoe cnono onetwothree.
Fonee yaunax nex ncnontsonart nnrepnonnponanne crpoxn, cocroxme ns saxnmuen-
ntx n nonte xantuxn cnon. Vxasan n rene crpoxn nmx maccnna, nonyunm:
0aay = {"one", "fWo", "fhee"}
p1nf "hee 1s fhe aay: 0aay. p1nf "hee 1s fhe aay: 0aay. p1nf "hee 1s fhe aay: 0aay. p1nf "hee 1s fhe aay: 0aay.\ \\ \n" n" n" n"
Crp.63
hee 1s fhe aay: one fWo fhee.
Perl nnrepnonnpyer maccnn, ncnontsyx no ymonuannm nnrepy-pasennrent none (xpannr-
cx n cnennantno nepemenno $,). Bosmoxno, rpeyercx pasnrt +nemenrt maccnna ne
npoenamn, a sanxrtmn. nx +roro nao npncnonrt cnmnon sanxrax nepemenno $,, n
nor uro nonyuaercx n pesyntrare:
0aay = {"one", "fWo", "fhee"}
$,= ", " $,= ", " $,= ", " $,= ", "
p1nf "hee 1s fhe aay: 0aay.\n"
hee 1s fhe aay: one, fWo, fhee.
Eme nyume ncnontsonart ]ynxnnm oin, cosan ns maccnna crpoxy n xnno pasennn +ne-
menrt sanxrtmn:
0aay = {1, 2, 3, 4, 5, 6, 7, , 9, 10}
p1nf p1nf p1nf p1nf o1n o1n o1n o1n{", ", 0 {", ", 0 {", ", 0 {", ", 0aay aay aay aay} } } }
1, 2, 3, 4, 5, 6, 7, , 9, 10
Ecrecrnenno, moxno ncnontsonart nnxn for nnn foreach no ncem +nemenram maccnna:
0aay = {"one", "fWo", "fhee"}
foeach $e1emenf {0aay} { foeach $e1emenf {0aay} { foeach $e1emenf {0aay} { foeach $e1emenf {0aay} {
p1nf "Cuenf e1emenf = $e1emenf p1nf "Cuenf e1emenf = $e1emenf p1nf "Cuenf e1emenf = $e1emenf p1nf "Cuenf e1emenf = $e1emenf\ \\ \n" n" n" n"
} }} }
Cuenf e1emenf = one
Cuenf e1emenf = fWo
Cuenf e1emenf = fhee
Hy n n sanepmenne nao samernrt, uro, nocxontxy nx ocryna x nmomy +nemenry mac-
cnna ocrarouno yxasart ero nnexc, moxno oprannsonart ntno maccnna nmtm cnoco-
om n n nmom ]opmare.
Cpaueauue (splicing) raccuea
Cpamnnanne maccnna osnauaer oannenne nontx +nemenron ns cnncxa, nosmoxno, c same-
mennem +nemenron, yxe xpanxmnxcx n maccnne. nx +ro nenn ncnontsyercx ]ynxnnx splice:
sp11ce uaccua, cue|euue, nuua, cnucox
sp11ce uaccua, cue|euue, nuua
sp11ce uaccua, cue|euue
Ecnn n xauecrne napamerpa saan cnncox, ro splice yanxer ns maccnna +nemenrt, onnct-
naemte napamerpamn c+eueuue n o.uua, n samemaer nx +nemenramn cnncxa.
B cnncxonom xonrexcre ]ynxnnx splice nosnpamaer +nemenrt, yanennte ns maccnna. B
cxanxpnom xonrexcre ona nosnpamaer nocnenn yanennt +nemenr (nnn undef, ecnn
+nemenrt ne yanxnnct). Hpn orcyrcrnnn napamerpa o.uua splice yanxer nce +nemenrt
o xonna maccnna, naunnax c +nemenra c nnexcom c+eueuue.
Hpnneem necxontxo npnmepon. Cnauana n maccnn, n xoropom yxe nmemrcx +nemenrt
one n two, yer oannen +nemenr three:
0aay = {"one", "fWo"}
sp11ce {0aay, 2 sp11ce {0aay, 2 sp11ce {0aay, 2 sp11ce {0aay, 2, , , , 0, "fhee"} 0, "fhee"} 0, "fhee"} 0, "fhee"}
p1nf o1n{", ", 0aay}
one, fWo, fhee
Tenept n xonen craporo maccnna yer oannen nropo maccnn:
0aay1 = {"one", "fWo"}
Crp.64
0aay2 = {"fhee", "fou"}
sp11ce sp11ce sp11ce sp11ce {0aay1, 2, 0, 0aay2} {0aay1, 2, 0, 0aay2} {0aay1, 2, 0, 0aay2} {0aay1, 2, 0, 0aay2}
p1nf o1n{", ", 0aay1}
one, fWo, fhee, fou
Haxonen, no npemx cpamnnannx moxno yanxrt +nemenrt onoro ns maccnnon.
Hanpnmep, +nemenr ero nepnoro maccnna samenxercx maccnnom, coepxamnm +nemenrt
two, three n four:
0aay = {"one", "eo"}
0aay2 = {"fWo", "fhee", "fou"}
sp11ce {0aay, sp11ce {0aay, sp11ce {0aay, sp11ce {0aay, 1, 1, 0aay2} 1, 1, 0aay2} 1, 1, 0aay2} 1, 1, 0aay2}
p1nf o1n{", ", 0aay}
one, fWo, fhee, fou
Hueepfupoeauue raccuea
urot nnneprnponart maccnn (pacnonoxnrt ero +nemenrt n oparnom nopxxe), ncnont-
syercx ]ynxnnx reverse:
0NeW = evese 0aay
Copfupoeka raccuea
nx coprnponxn maccnna npnmenxercx ]ynxnnx sort (nx maccnnon ona ntstnaercx n pa-
oraer rouno rax xe, xax n nx cnncxon, cm. onncanne sort n npetyme rnane):
0neW = sof {$a <=> $b} 0aay
C ee nomomtm moxno ntnonnxrt camte xnrpoymnte coprnponxn nanpnmep, nor rax
moxno coprnponart maccnn no ytnannm:
0neW = sof {$b <=> $a} 0aay
Cospauue xsue
X+mn (x+m-rannnt, x+mnponannte rannnt) n onee pannnx nepcnxx Perl nastnannct ac-
connarnnntmn maccnnamn, n +ro onnctnaer nx cyrt onee coepxarentno, rax xax nx oc-
ryna x anntm nmecro uncnonoro nnexca ncnontsyercx xnmu (ro ecrt rexcronax crpoxa), xo-
ropax acconnnpyercx co snauennem. Hocxontxy n x+max cctnxn na annte ocymecrnnxmrcx c
nomomtm xnmue, a ne uncen, ocryn x +nemenram otuno nnrynrnnnee npome, uem n cnyuae
maccnnon. Onaxo oprannsannx nnxnon nx x+me moxer oxasartcx rpynee, noromy uro x
+nemenry x+ma nenosmoxno nonyunrt ocryn c nomomtm uncnonoro nreparopa nnxna.
Hmena x+me naunnamrcx c npe]nxca . Tax cosaercx nycrax x+m-rannna:
xhash ={}
Kax n n cnyuae maccnnon, npn paore c +nemenramn x+me nao ncnontsonart past-
menontnammn npe]nxc $. Bor, nanpnmep, xax nomecrnrt n x+m nonte +nemenrt:
xhash = {}
$ $$ $hash hash hash hash{ {{ {fu1f fu1f fu1f fu1f} = } = } = } = app1e app1e app1e app1e
$hash{ san $hash{ san $hash{ san $hash{ sandW1ch} = hambuge dW1ch} = hambuge dW1ch} = hambuge dW1ch} = hambuge
$hash{d1nk} = bubb1y $hash{d1nk} = bubb1y $hash{d1nk} = bubb1y $hash{d1nk} = bubb1y
Crp.65
(3ect fruit nepnt xnmu x+m-rannnt, n on coornercrnyer snauennm apple; sandwich
xnnxercx nroptm xnmuom co snauennem hamburger n r. .)
Oparnre nnnmanne, uro nx xnmua ncnontsymrcx ]nrypnte (]), a ne xnaparnte (])
cxoxn, xax +ro tno c nnexcamn maccnnon.
Orentnte +nemenrt nsnnexamrcx ns x+ma raxxe c nomomtm xnmue:
xhash = {}
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
p1nf $hash{s p1nf $hash{s p1nf $hash{s p1nf $hash{sandW1ch} andW1ch} andW1ch} andW1ch}
hambuge
Ha camom ene ner neoxonmocrn cosanart nycro x+m, urot nauart ero sanonnenne.
Bcrpernn opamenne x necymecrnymmemy x+my, Perl cosaer ero anromarnuecxn. (3ro
mar nancrpeuy nporpammncram, nontrxa sacrannrt cncremy paorart rax, xax nt oxnae-
re.) Ho+romy cneymmn xo paoraer ne xyxe npetymero:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
p1nf $hash{sandW1ch}
hambuge
He satnare, uro Perl npn cunrtnannn nontx +nemenron onycxaer npoentnte cnmno-
nt. Ho+romy, nanpnmep, maccnnt c mnoxecrnom +nemenron moxno cenart onee npo-
spauntmn nx nonnmannx, ncnontsonan mnorocrpounte xoncrpyxnnn:
0aay = { "one", "fWo", "fhee",
"fou", "f1ve", "s1x" }
Touno rax xe moxno cosanart x+m-rannnt, yxastnax napt xnmu/snauenne:
xhash = { fu1f , app1e,
sandW1ch , hambuge,
d1nk , bubby }
p1nf "$hash{fu1f}\n"
app1e
Bmecro sanxro, pasenxmme napt, moxno ncnontsonart cnnonnm xoncrpyxnnm >.
C +rnm oneparopom ornomenne xnmu/snauenne ntrnxnr npospaunee, rax uro nporpammn-
crt uacro sannctnamr xomany cosannx x+ma rax:
xhash = { fu1f => app1e,
sandW1ch => hambuge,
d1nk => bubby }
p1nf "$hash{fu1f}\n"
app1e
Oneparop > ne ntnonnxer cnennantntx onepann +ro n rounocrn ro xe camoe, uro n
otunax sanxrax, sa ncxnmuennem roro ]axra, uro on ntnyxaer nnrepnpernponart cno-
no, croxmee cnena, xax crpoxy. Hanpnmep, xomana
p1nf "x"=>"y"=>""
xy
enaer ro xe, uro n xomana
p1nf "x", "y", ""
xy
Crp.66
He sanpemaercx ncnontsonart xnmun, coepxamne npoent. Hanpnmep, moxno cosart
+nemenr x+ma c xnmuom ice cream:
$hash2{cake} = choco1afe
$hash2{p1e} = b1uebey
$ $$ $hash hash hash hash2{ 2{ 2{ 2{1ce 1ce 1ce 1ce ceam ceam ceam ceam} = } = } = } = pecan pecan pecan pecan
n xora norpeyercx ncnontsonart ero:
p1nf p1nf p1nf p1nf "$hash{1ce ceam} "$hash{1ce ceam} "$hash{1ce ceam} "$hash{1ce ceam}\ \\ \n" n" n" n"
pecan
Hpn cosannn xnmue moxno ncnontsonart nnrepnonnponanne rexcrontx crpox, orpann-
uenntx nontmn xantuxamn, a raxxe npocro nepemennte.
$va1ue = $hash{$key}
X+mn oecneunnamr momnoe cpecrno nx xpanennx anntx, no cctnartcx na +nemenrt
x+m-rannnt nanpxmym, c nomomtm uncnonoro nnexca, nentsx. 3ro, xoneuno xe, ne
snaunr, uro nentsx oprannsonart nnxn no +nemenram x+ma, o +rom paccxastnaercx a-
nee n +ro rnane n pasene Hnxnt no x+my.
Hcnonusoeauue xsue
Hocne roro xax nt cosann x+m, moxno paorart c nnm, opamaxct x ero +nemenram c no-
momtm xnmue:
$va1ue = $hash{$key}
Kpome +roro, c nomomtm oneparopa npncnoennx nerxo oannrt n x+m nonte +nemenrt,
xax +ro tno cenano n npnmepe ns npetymero pasena:
$hash{fu1f} = app1e $hash{fu1f} = app1e $hash{fu1f} = app1e $hash{fu1f} = app1e
$hash{sandW1ch} = hambuge $hash{sandW1ch} = hambuge $hash{sandW1ch} = hambuge $hash{sandW1ch} = hambuge
$hash {d1nk} = bubb1y $hash {d1nk} = bubb1y $hash {d1nk} = bubb1y $hash {d1nk} = bubb1y
p1nf $hash{sandW1ch}
hambuge
B cnncxonom xonrexcre x+m nnrepnonnpyercx: napt xnmu/snauenne nepexoxr n ennt
cnncox +nemenron. B cxanxpnom xonrexcre nocrannxercx snauenne ucmuua (true), ecnn n
x+me ecrt xorx t ona napa xnmu/snauenne, n .ori (Ialse), ecnn x+m nycr.
oaeneuue snereufoe e xsu
urot oannrt nont +nemenr (ro ecrt napy xnmu/snauenne) n x+m, ncnontsyre onepa-
rop npncnoennx. B cneymmem npnmepe n x+m hash oannxmrcx na snauennx:
xhash={}
$hash{$key} = $va1ue
$hash{$key2} = $va1ue2
X+mn moxno cosanart, ncnontsyx npncnoenne cnncxa, n rouno rax xe moxno oannxrt
nonte +nemenrt. B cneymmem npnmepe raxnm opasom x x+my hash oannxercx no-
nax napa xnmu/snauenne:
xhash = { fu1f => app1e,
sandW1ch => hambuge,
d1nk => bubby }
x xx xh hh hash = {xhash, dess1ng, b1ue cheese} ash = {xhash, dess1ng, b1ue cheese} ash = {xhash, dess1ng, b1ue cheese} ash = {xhash, dess1ng, b1ue cheese}
Crp.67
p1nf $hash{dess1ng}
b1ue cheese
3ror npnmep paoraer, noromy uro oneparop cnncxa () cnepna nnrepnonnpyer x+m hash
n cnncox, a sarem nocnenn pacmnpxercx eme na ony napy xnmu/snauenne. Hs-sa nnrep-
nonxnnn, xoropax nponcxonr o npncnoennx cnncxom, n annom cnyuae nentsx ncnontso-
nart coxpamennym ]opmy c oneparopom +:
xhash += {dess1ng, b1ue cheese} # 1o ue pabo1ae1
Rpoeepka xsua ua uanuue snereufa
urot nponepnrt, ecrt nn n x+me nexoropt +nemenr, moxno ncnontsonart ]ynxnnm exists.
Hanpnmep, n cneymmem npnmepe n x+me hash nmercx +nemenr c xnmuom vegetable:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
1 11 1f {e f {e f {e f {ex xx x1sfs{$hash{"vegefab1e"}}} { 1sfs{$hash{"vegefab1e"}}} { 1sfs{$hash{"vegefab1e"}}} { 1sfs{$hash{"vegefab1e"}}} {
p1nf "L1emenf ex1sfs."
} e1se {
p1nf "L1emenf does nof ex1sf."
}
L1emenf does nof ex1sf
Ypaneuue snereufoe xsua
urot yannrt +nemenr ns x+ma, ncnontsyercx ]ynxnnx delete. B +rom npnmepe yanxer-
cx +nemenr x+ma n sarem nponepxercx, cymecrnyer nn eme raxo +nemenr:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
de1efe {$hash{"fu1f"}}
1f {e 1f {e 1f {e 1f {ex xx x1sfs{$hash{"fu1f"}}} { 1sfs{$hash{"fu1f"}}} { 1sfs{$hash{"fu1f"}}} { 1sfs{$hash{"fu1f"}}} {
p1nf "L1emenf ex1sfs."
} e1se {
p1nf "L1emenf does nof ex1sf."
}
L1emenf does nof ex1sf
Uuknu no xsuy
Cymecrnyer mnoxecrno cnocoon oprannsannn nnxna no +nemenram x+ma. uynxnnx each
nosnonxer ncnontsonart sanncn x+ma (ro ecrt n xnmu, n snauenne) nennxom.
Paccmorpnm npnmep. Cnauana cosanm x+m-rannny:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
Tenept moxno nsnneut ns nee napt xnmu/snauenne, ncnontsyx ]ynxnnm each n npncnoe-
nne cnncxom:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
Crp.68
$hash{d1nk} = bubb1y
Wh11e {{$key Wh11e {{$key Wh11e {{$key Wh11e {{$key, , , , $va1ue} = each{xhash}} { $va1ue} = each{xhash}} { $va1ue} = each{xhash}} { $va1ue} = each{xhash}} {
p1nf "$key => $va1ue p1nf "$key => $va1ue p1nf "$key => $va1ue p1nf "$key => $va1ue\ \\ \n" n" n" n"
} } } }
d1nk => bubb1y
sandW1ch => hambuge
fu1f => app1e
Hcnontsonanne each n annom cnyuae nonesno, rax xax nx xaxoro +nemenra x+ma npn
onom ee ntsone nosnpamaercx xax xnmu, rax n snauenne.
Kcrarn, +nemenrt x+ma nsnnexamrcx ne n rom nopxxe, n xaxom onn sanocnnnct. eno n
rom, uro Perl xpannr nx, ncnontsyx cocrnennt nnyrpennn anropnrm, onrnmnsnpymmn
sarpart namxrn n oecneunnammn nerxocrt ocryna. Coprnponxa +nemenron onnctnaer-
cx n pasene Coprnponxa x+ma anee n +ro rnane.
Hpn paore c +nemenramn x+ma moxno ncnontsonart nnxn foreach. Hanpnmep, conmecrno
c ]ynxnne keys, nosnpamamme cnncox xnmue x+ma, on nosnonxer nocneonarentno
nepepart nect x+m:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
foeach $key {keys xhash}{ foeach $key {keys xhash}{ foeach $key {keys xhash}{ foeach $key {keys xhash}{
p1nf $hash{$key} . p1nf $hash{$key} . p1nf $hash{$key} . p1nf $hash{$key} . " "" "\ \\ \n" n" n" n"
} }} }
bubb1y hambuge app1e
Onaxo nmecro keys moxno cpasy nocnontsonartcx ]ynxnne values. Ona nosnpamaer
cnncox snauenn, xpanxmnxcx n x+me. C ne nnxn, ntnoxmn nce snauennx x+ma, npn-
nnmaer cneymmn nn:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
foeach $va1ue {va1ues xhash}{ foeach $va1ue {va1ues xhash}{ foeach $va1ue {va1ues xhash}{ foeach $va1ue {va1ues xhash}{
p1nf "$va1ue p1nf "$va1ue p1nf "$va1ue p1nf "$va1ue\ \\ \n" n" n" n"
} }} }
bubb1y hambuge app1e
Kax nnnre, necmorpx na ro, uro x +nemenram x+ma nentsx nonyunrt ocryn c nomomtm
uncnonoro nnexca, Perl npeocrannxer ocrarouno cpecrn nx nx nepeopa.
Bueop xsua
urot ntnecrn coepxnmoe x+ma, moxno nnrepnonnponart ero n crpoxy, saxnmuennym n
nonte xantuxn:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
p pp p1nf "0{|xhash]} 1nf "0{|xhash]} 1nf "0{|xhash]} 1nf "0{|xhash]}\ \\ \n" n" n" n"
d1nk bubb1y sandW1ch hambuge fu1f app1e
B pesyntrare raxo neuarn x+m nepeneen n cnncxont xonrexcr, ro ecrt npecrannen n
nne nap xnmu/snauenne, pacnonoxenntx pyr sa pyrom. uynxnnx each n anno cnrya-
nnn aer onee yaunoe pemenne:
$hash{fu1f} = app1e
Crp.69
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
W WW Wh1 h1 h1 h11 11 1e e e e {{$key, $va1ue} {{$key, $va1ue} {{$key, $va1ue} {{$key, $va1ue} = = = = each{xhash}} each{xhash}} each{xhash}} each{xhash}}
{ { { { p1nf "$key: $va1ue p1nf "$key: $va1ue p1nf "$key: $va1ue p1nf "$key: $va1ue\ \\ \n" n" n" n" } } } }
d1nk: bubb1y
sandW1ch: hambuge
fu1f: app1e
Ecrt mnoxecrno pyrnx cnocoon oprannsonart nnxn no +nemenram x+ma (cm. pasen
Hnxnt no x+my panee n +ro rnane).
Copfupoeka xsua
nx coprnponxn x+me ncnontsyercx ra xe ]ynxnnx sort, uro n nx coprnponxn cnncxon n
maccnnon. Bor, nanpnmep, xax x+m coprnpyercx no snauennm xnmua:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
foeach foeach foeach foeach $key {sof keys xh $key {sof keys xh $key {sof keys xh $key {sof keys xhash} { ash} { ash} { ash} {
p1nf "$key => $hash{$key}\n"
}
d1nk => bubb1y
fu1f => app1e
sandW1ch => hambuge
Touno rax xe nmecro coprnponxn no xnmuy moxno ntnonnnrt coprnponxy no snauennm:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
foeach $va1ue {sof va1ues xhash} { foeach $va1ue {sof va1ues xhash} { foeach $va1ue {sof va1ues xhash} { foeach $va1ue {sof va1ues xhash} {
p1nf "$va1ue\n"
}
app1e
bubb1y
hambuge
Cnunuue xsue
nx oennennx nyx x+me moxno ncnontsonart npncnoenne cnncxom. Hanpnmep, o-
nycrnm, ecrt na x+ma:
$hash1{fu1f} = app1e
$hash1{sandW1ch} = hambuge
$hash1{d1nk} = bubb1y
$hash2{cake} = choco1afe
$hash2{p1e} = b1uebey
$hash2{1ce ceam} = pecan
Oa x+ma moryr trt oennent cneymmnm opasom:
xb1ghash = {xhash1, xhash2} xb1ghash = {xhash1, xhash2} xb1ghash = {xhash1, xhash2} xb1ghash = {xhash1, xhash2}
p1nf $b1ghash{1ce ceam}
pecan
Hcnonusoeauue xsue u raccueoe e npuceoeuuu cnuckor
Crp.70
Hpn npncnoennn cnncxom moryr ncnontsonartcx x+m-rannnt n maccnnt. Hecxontxo x+me
nnn cnncxon cnpana or oneparopa npncnannannx nnrepnonnpymrcx n onn cnncox, rax uro ne
nosnnxaer nnxaxnx cnnraxcnuecxnx nponem. Paccmorpnm npnmep ns npetymero pasena:
$hash1{fu1f} = app1e
$hash1{sandW1ch} = hambuge
$hash1{d1nk} = bubb1y
$hash2{cake} = choco1afe
$hash2{p1e} = b1uebey
$hash2{1ce ceam} = pecan
Hpn npncnoennn cnncxom x+mn oennxmrcx n onn cnncox:
xb1ghash xb1ghash xb1ghash xb1ghash = = = = {xha {xha {xha {xhash sh sh sh1 11 1, , , , xhash2} xhash2} xhash2} xhash2}
p1nf $b1ghash{1ce ceam}
pecan
Bor xax nponcxonr npncnoenne cnncxy, cocroxmemy ns nyx cxanxpntx nepemenntx n
onoro maccnna (maccnn oxsarentno onxen croxrt n cnncxe nepemenntx nocnennm):
{$va1 {$va1 {$va1 {$va1, , , , $va2 $va2 $va2 $va2, , , , 0 00 0a a a aay ay ay ay} = {1, 2, } = {1, 2, } = {1, 2, } = {1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 5, 6, 7, } 7, } 7, } 7, }
p1nf "$va1\n"
p1nf "$va2\n"
p1nf "0aay\n"
1
2
34567
Hpeynpexeuue. Koeoa ei eino.ueme npuceoeuue cnucrv nepe+euuix, cpeou romopix u+eemc +accue u.u
xou, uvruo oimi ocmoporui+. B Perl +accuei u xou-mao.ui npu npuceaueauuu u+ uoeix o.e+eumoe ae-
mo+amu:ecru +eumm pas+ep. Hoomo+v npu npuceaueauuu cnucrv sua:euu, uaxooue+vc c.eea om onepa-
mopa npuceaueauu, +accue u.u xou e omo+ cnucre oo.rui uaxooumic ua noc.eoue+ +ecme. B npomueuo+
c.v:ae ouu npocmo saxeamm ece o.e+eumi, romopie oo.rui oimi npuceoeui ocmaeuu+c nepe+euui+, u
me no.v:am ueonpeoe.euuoe sua:euue.
Hcnonusoeauue funa pauuux typeglob
Koncrpyxnnn typeglob nrpamr n Perl pont cnnonnmon otuntx nepemenntx. (B pannnx nepcn-
xx onn ntcrynann n xauecrne ananora cctnox, no renept n Perl noxnnnnct nonnonennte cctn-
xn.) 3ror rnn anntx nosnonxer cnxsart nmx ono nepemenno (nanpnmep, data) c nmenem
pyro (nanpnmep, alsodata). B pesyntrare nmena rnna $alsodata, alsodata n alsodata y-
yr cctnartcx na re xe oexrt, uro n $data, data n data (ro ecrt $alsodata yer cct-
nartcx na re xe annte, uro n $data, alsodata na re xe annte, uro n data, n r. .).
Hpnneem npnmep. Cnauana cosamrcx nepemennte $data n data:
$dafa = "hee 1s fhe dafa."
0dafa = {1, 2, 3}
Tenept nmenn data npncnannaercx cnnonnm alsodata:
$dafa = "hee 1s fhe dafa."
0dafa = {1, 2, 3}
" "" "a1sodafa a1sodafa a1sodafa a1sodafa = == = " "" "dafa dafa dafa dafa
A renept nmecro nmenn data moxno ncnontsonart nmx alsodata:
$dafa = "hee 1s fhe dafa."
0dafa = {1, 2, 3}
"a1sodafa = "dafa
p1nf p1nf p1nf p1nf "$a1sodafa "$a1sodafa "$a1sodafa "$a1sodafa\ \\ \n" n" n" n"
Crp.71
p1nf 0 p1nf 0 p1nf 0 p1nf 0a1 a1 a1 a1sodafa sodafa sodafa sodafa
hee 1s fhe dafa.
123
Ha camom ene nponcxonr nor uro: sannct rannnt cnmnonon, oosnauaemax xax *u+,
xpannr nn]opmannm oo ncex nepemenntx c omnm nmenem u+ nanpnmep, sannct
data rexyme rannnt cnmnonon xpannr nn]opmannm o nepemenntx $data, data,
data n r. . B uacrnocrn, nx xaxoro rnna anntx sannctnaercx apec onacrn namx-
rn, re +ro annoe xpannrcx, nnn cctnxa na nee (ro ecrt cctnxa na cxanxpnym nepemen-
nym $data, cctnxa na maccnn data, cctnxa na x+m-rannny data n r. .).
Hpn npncnoennn xoncrpyxnn typeglob Perl xonnpyer sanncn rannnt cnmnonon nx ono-
ro nmenn n pyroe nmx. Cnmnon , ncnontsyemt n xoncrpyxnnn typeglob, moxno cunrart
manonom nmoro npe]nxca rnna anntx ($, , n r. .). Fonee nopono nponecc xonnpo-
nannx sannce rannnt cnmnonon yer paccmorpen n cneymmem pasene.
Hocuasua. Umooi noc.e onepauu alsodata data nepe+euua $alsodata cma.a cuuouu+o+ $data, ua
+o+eum npuceoeuu nepe+euua $data +orem u ue oimi onpeoe.eua. Hocro.irv Perl ro+nu.upvem roo
uenocpeocmeeuuo nepeo eino.ueuue+ ceuapu, na+mi o. $data ovoem eioe.eua sapauee u cci.ra ua
uee vre nonaoem e cmpvrmvpv data. Hua:e e+ecmo cci.ru ua nepe+euuvm $data e sanuci mao.ui cu+-
eo.oe alsodata oi.a oi cronupoeaua uuqop+au o mo+, :mo maro cra.puo nepe+euuo eue uem u
nepe+euuie $data u $alsodata ue +oe.u oi cci.amic ua oouo u mo re sua:euue.
Hpn ncnontsonannn xoncrpyxnn typeglob neoxsarentno xonnponart ncm sannct rann-
nt cnmnonon, ornocxmymcx x annomy nmenn. Ecnn cnpana or oneparopa npncnannannx
cronr cctnxa rontxo na onn rnn anntx (nanpnmep, cxanxp), ro n nonoe nmx yer cct-
nartcx na +ror rnn anntx n ntcrynart cnnonnmom rontxo nx yxasannoro rnna:
$dafa = "hee 1s fhe dafa."
0dafa = {1, 2, 3}
" "" "a1sodafa a1sodafa a1sodafa a1sodafa= == =\ \\ \$ $$ $dafa dafa dafa dafa # 8aeeu cuuouuu 1onuxo nn cxannpoa
B annom cnyuae $alsodata cranonnrcx cnnonnmom $data, no alsodata ne yer cnno-
nnmom nx data, alsodata nx data n r. . Hntmn cnonamn, raxax xomana cpa-
oraer nopmantno:
p1nf "$a1sodafa\n"
hee 1s fhe dafa.
a raxax paorart ne yer:
p1nf 0a1sodafa
Tun pauuux typeglob u sanucu e fanuue cureonoe
Kax yxe ronopnnoct, Perl xpannr nn]opmannm o nepemenntx no nnyrpenne rannne
nmen (rannne cnmnonon) n nne orentntx sannce. Kaxax sannct rannnt cnmnonon
npecrannxer coo rnn typeglob. 3ror rnn anntx moxno cunrart neontmnm x+mem,
snauennxmn xoroporo xnnxmrcx cctnxn na onacrt namxrn, re xpanxrcx coornercrnym-
mne nepemenntm annte.
Knmuamn nx raxnx x+me (xorx +ro nn]opmannn nt, nosmoxno, ne naere n oxymenra-
nnn Perl) ntcrynamr nmena rnnon anntx, sanncannte sarnanntmn yxnamn (ro ecrt A-
A, HASH n r. .). 3ro moxer trt nonesntm, ecnn rpeyercx nenocpecrnennt ocryn x
rannne cnmnonon Perl. onycrnm, n nporpamme onpeenena nepemennax co snauennem 5:
$va1ab1e = 5
Crp.72
B +rom cnyuae variable nmx nepemenno rnna typeglob, xpanxme nn]opmannm o
$variable, a variableSAA] cctnxa na snauenne nepemenno $variable. urot
nonyunrt snauenne nepemenno $variable uepes cctnxy na onacrt anntx n rannne
cnmnonon, ncnontsyercx pastmenontnammn oneparop $:
$va1ab1e = 5
p1nf ${"va1ab1e{5CALAk}}
5
Crp.73
Fnana 4

Onepa1opm u
npuopu1e1m
onepa1opon


Kopo1ko
B rpex npetymnx rnanax ocyxannct cranaprnte ]opmart anntx n xstxe Perl. B
+ro rnane peut ner o paore c anntmn c nomomtm oneparopon. Oneparopt nosnonxmr
conepmart onepannn c anntmn, axe ecnn +ro npocroe cnoxenne:
p1nf 2 + 2
4
Oneparopt moryr ntnonnxrt n onee cnoxnte ntuncnennx, noono repnapnomy (rpo-
crnennomy) ycnonnomy oneparopy, paccmarpnnaemomy n cneymmem npnmepe:
Wh11e {<>}
{ p1nf $ < 10 $ : "${\{{a,b,c,d,e,f}|$ - 10]}}\n" }
ntpaxenne cunrtnaer ns nxontx anntx uncna or 0 o 15 n ntnonr mecrnanarepnu-
nym nn]py (ecnn nt ne snaxomt c oneparopom :, oparnrect x paseny Tepnapnt yc-
nonnt oneparop n +ro rnane).
Oneparopt Perl ntnonnxmr camte pasnnunte ecrnnx, no n nepnom npnnnxennn onn
moryr trt pasnrt na ynapnte, nnapnte, repnapnte n cnncxonte. Vnapntm oneparo-
pam rpeyercx onn onepan nanpnmep, n cnyuae oneparopa !, ntnonnxmmero nooum-
uoe .oeu:ecroe ompuauue, xomana $notvariable !$variable ocymecrnnr nornuecxoe
nnneprnponanne coepxnmoro nepemenno $variable n sanecer pesyntrar n nepemennym
$notvariable. Buuapuie onepamopi rpeymr nyx onepanon nanpnmep, nx oneparopa
cnoxennx + xomana $sum 2 + 2 ntuncnnr cymmy nyx nentx uncen n sanecer ee n ne-
pemennym $sum. Tepuapuie oneparopt ncnontsymr rpn onepana nanpnmep, n pesynt-
rare paort ycnonnoro oneparopa : xomana
$absva1ab1e = $va1ab1e >= 0 $va1ab1e : -$va1ab1e
naer n sanecer n nepemennym $absvariable aconmrnoe snauenne ntpaxennx $variable.
Haxonen, cnucroeie onepamopi, noono oneparopy print, ncnontsymr cnncxn onepan-
on (xax, nanpnmep, xomana print 1, 2, 3, 4, 5).
Cctnxa na qvurum print xax na onepamop na nepnt nsrnx moxer noxasartcx crpanno.
Ho n Perl ]ynxnnx c aprymenramn, yxasanntmn es xpyrntx cxoox, paccmarpnnaercx n
xauecrne oneparopa. Fonee roro, npn ntuncnennn ntpaxenn ncnontsymrcx npannna npn-
opnrera, onpeenennte nx oneparopon (onee nopono o +rom ronopnrcx n pasene
Hanntcmn npnopnrer: repmt n cnncxn, croxmne cnpana +ro rnant). Hpexe uem ne-
Crp.74
pexonrt x paore c oneparopamn, neoxonmo pasopartcx c nonxrnem cpannnrentnoro
npnopnrera.
Rpuopufef onepafopoe
Oneparopt Perl no mepe ytnannx npnopnrera (nepnax crpouxa coornercrnyer nanntcme-
my npnopnrery) nepeuncnent n ran. .1.
Bpemx or npemenn npn paore c Perl npnxonrcx yunrtnart cpannnrentnt npnopnrer
oneparopon. B onom ntpaxennn moxer ncnontsonartcx necxontxo oneparopon:
p1nf 2 + 3 " 4
Cnoxnr nn Perl 2 n 3 nepe ymnoxennem na 4 nnn cnepna ymnoxnr 3 na 4 n nnmt sarem
npnannr 2 3ror nonpoc npoxcnxercx npn snannn npannn npnopnrera oneparopon, cyme-
crnymmnx n Perl. Kax nnno ns ran. .1, ymnoxenne () nmeer onee ntcoxn npnopnrer,
uem cnoxenne (+). Ho+romy Perl cnauana ymnoxnr 3 na 4, a norom npnannr 2:
p1nf 2 + 3 " 4
14
Ecrecrnenno, moxno nsmennrt nopxox ntnonnennx onepann, paccrannn cxoxn:
p1nf {{2 + 3} " 4}
20
Kcrarn, oparnre nnnmanne, uro +ry xomany nentsx saart xax
p1nf {2 + 3} " 4
Hocxontxy print paoraer nno xax oneparop, nno xax ]ynxnnx, cxoxa yxastnaer Perl,
uro print nao ncnontsonart n xauecrne ]ynxnnn, a 2 + 3 +ro napamerp, nepeanaemt
e. Ho+romy Perl nocnymno ntneer:
p1nf {2 + 3} " 4
5
Onaxo +ry nponemy moxno oorn. Ecnn nnrepnperannx cnncounoro oneparopa (roro
xe print) c aprymenrom, naunnammnmcx co cxoxn, xax ntsona ]ynxnnn, ne rpeyercx, ro
nepe cxoxamn moxno nocrannrt ynapnt oneparop +. On ne oxaxer nnxaxoro nosecr-
nnx na camo ntpaxenne, onaxo coomnr Perl, uro nnrepnpernponart xoncrpyxnnm n
cxoxax xax ntson ]ynxnnn ne cneyer:
p1nf +{2 + 3} " 4
20
Hrax, cpannnrentnt npnopnrer oneparopon nectma naxen. Pasent +ro rnant yyr op-
rannsonant n coornercrnnn c ran. .1, ro ecrt oneparopt c nanntcmnm npnopnrerom o-
cyxamrcx n nepnym ouepet.
1abnua 4.1. 1abnua 4.1. 1abnua 4.1. 1abnua 4.1. Cpaauu1enuuuu npuopu1e1 onepa1opoa {onepa1opu, nepeuucneuuue a ouou rpynne, uuek1
ouuaxoauu npuopu1e1}
Onepa1op Onepa1op Onepa1op Onepa1op ccoua1uauoc1u ccoua1uauoc1u ccoua1uauoc1u ccoua1uauoc1u
1epuu eaan
npaauu onepa1op cnucxa
-> eaan
++ he onpeeneua
--
"" hpaaan
Crp.75
! hpaaan

\
yuapuuu +
yuapuuu -
= eaan
!
" eaan
/
x
x
+ eaan
-
.
<< eaan
>>
uueuoaauuue yuapuue he onpeeneua
onepa1opu,
onepa1opu npoaepxu aunoa
<> he onpeeneua
<= >= 1f gf 1e ge
== he onpeeneua
!= <=> eq ne cmp
eaan
| eaan

eaan
|| eaan
.. he onpeeneua
...
: hpaaan
= hpaaan
+= "= /= x= = |= x=
""= <<= >>= = ||=
, eaan
=>
neauu onepa1op cnucxa he onpeeneua
nof hpaaan
and eaan
o eaan
xo

Henocpepc1neuume pemeuun
Haueucuu npuopufef: fepru u cnucku, cfonue cnpaea
Tepmt nmemr n Perl nanntcmn npnopnrer. K repmam ornocxrcx nepemennte, ntpaxennx
n xantuxax, ntpaxennx n cxoxax, xoncrpyxnnn do n eval, estmxnnte (anonnmnte) mac-
cnnt n x+mn, cosanaemte c nomomtm xoncrpyxnn ] n ] (cm. - rnany ), a raxxe ]ynx-
nnn c aprymenramn, saxnmuenntmn n cxoxn. Touno rax xe nanntcmn npnopnrer, na-
npannennt e.eeo (leItward precedence), nmeer oneparop cnncxa (naop +nemenron, pase-
Crp.76
nenntx sanxro). 3ro snaunr, uro no cpannennm c oneparopamn, naxoxmnmncx cnena or
nero, on nmeer nanntcmn npnopnrer, a xora ero npnopnrer cpannnnaercx c oneparopa-
mn, croxmnmn cnpaea, +ror xe oneparop cnncxa nonyunr ropaso onee cnat npaei
npnopnrer (rightward precedence).
Paccmorpnm cneymmn npnmep:
p1nf 1, 2, 3, 4, sof 9, , 7, 6, 5
12345679
Kax nnno ns pesyntrara, cnepna oneparop sort coprnpyer cnncox, yxasannt n xauecrne
ero aprymenra, a sarem print ntnonr cnncox, xnnxmmncx oennennem nyx cnncxon.
Kora ner ananns uncna 9, ro cnena or nero oxastnaercx oneparop sort, a cnpana one-
parop cnncxa. Tax xax no ornomennm x neno cropone y oneparopa cnncxa ontmn npn-
opnrer, ro Perl cnepna oennnr uncno 9 n onn cnncox c nocneymmnmn +nemenramn
(cm. raxxe pasen Cnncxn, croxmne cnena nnxe x xonny rnant) n nnmt sarem npnme-
nnr x pesyntrary onepannm coprnponxn. C pyro cropont, xora Perl oxonr o uncna
, ro nmeer eno c nymx nymnmn nopx oneparopamn oneparopom cnncxa n onepa-
ropom sort. Oneparop cnncxa nmeer onee cnat npnopnrer no ornomennm x pacnono-
xennomy cnpana or nero oneparopy sort, no+romy oennenne +nemenron n cnncox yer
ornoxeno o rex nop, noxa oneparop coprnponxn ne ntnonnnr cnom paory.
Onepafop-cfpenka
Hn]nxcnt oneparop ->, pastmenymmn cctnxy, sanmcrnonan ns xstxa C. Ecnn cnpana
or nero naxonrcx xoncrpyxnnx ...] nnn ...], ro cnena onxna naxonrtcx cctnxa na mac-
cnn nnn x+m. B cneymmem npnmepe cosaercx cctnxa na x+m (c nomomtm oneparopa \,
nosnpamammero apec nepemenno), a sarem c nomomtm oneparopa -> oecneunnaercx
ocryn x coepxnmomy x+ma uepes nonyuennym cctnxy:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y:
$hashef = $hashef = $hashef = $hashef = \ \\ \xhash xhash xhash xhash
p1nf p1nf p1nf p1nf $ $$ $hashef hashef hashef hashef - -- -> { > { > { > {sandW1ch sandW1ch sandW1ch sandW1ch} } } }
hambuge
Kora cnpana or oneparopa ner xnaparntx ...] nnn ]nrypntx ...] cxoox, a nenax uacrt
ne npecrannxer coo cctnxn na maccnn nnn x+m, ro cnena onxen naxonrtcx nno o-
exr, nno nmx xnacca, a cnpana ero mero:
$esu1f = $myobecf -> mymefhod{$dafa}
(Fonee nopono o oexrax n xnaccax paccxastnaercx n rnane 1.)
Aefonpupaeuue u aefoyreuuueuue
Oneparop npnpamennx () n oneparop ymentmennx () n Perl paoramr rouno rax xe,
xax n xstxe C. Haxoxct nepe nmenem nepemenno, onn ynennunnamr nnn ymentmamr ee
snauenne na ennnny o roro, xax nocrannrt snauenne n ntpaxenne. Kora +rn oneparo-
pt oxastnamrcx nocne nmenn nepemenno, onn cnepna nocrannxmr n ntpaxenne snaue-
nne nepemenno, a norom ynennunnamr nnn ymentmamr ee coepxnmoe. onycrnm, onpe-
enent nepemennte $variable1 n $variable2:
Crp.77
$va1ab1e1 = 1
$va1ab1e2 = 1
Vnennunrt snauenne nepemenno moxno, npnmennn oneparop ++ n xauecrne npe]nxca:
p1nf ++$va1ab1e1 . "\n"
p1nf $va1ab1e1 . "\n"
2
2
Ecnn ror xe oneparop ncnontsonart n xauecrne cy]]nxca, snauenne nepemenno nsmennr-
cx nnmt nocne roro, xax oneparop nepner ee rexymee snauenne:
p1nf $va1ab1e2++ . "\n"
p1nf $va1ab1e2 . "\n"
1
2
Ormernm, uro oneparop paoraer raxxe n c rexcrontmn crpoxamn, xpanxmnmncx n cxa-
nxpntx nepemenntx (onaxo rontxo xora cxanxpnax nepemennax ne ncnontsyercx n un-
cnonom xonrexcre). Hanpnmep, nporpamma
$va1ab1e =
p1nf ++$va1ab1e . "\n"
$va1ab1e = bbb
p1nf ++$va1ab1e . "\n"
$va1ab1e =
p1nf ++$va1ab1e . "\n"
ntnonr pesyntrar:
AA8
bbc
aaaa
Boseepeuue e cfeneuu
Oneparop nosneennx n crenent ntrnxnr xax . 3ro nnapnt oneparop, xoropt nos-
nonr nepnt aprymenr n crenent, yxasannym nroptm aprymenrom. Hanpnmep:
p1nf 2 "" 10
1024
Yuapuue onepafopu
n Perl ecrt uertpe ynapntx oneparopa:
! nornuecxoe orpnnanne (nornuecxax onepannx not).
- apn]mernuecxoe orpnnanne (ynapnt mnnyc).
+ nycrax apn]mernuecxax onepannx (ynapnt nnmc).
nonrnoe orpnnanne (nonrnoe ononnenne o ennnnt).
\ cosanne cctnxn (ntuncnenne apeca oexra).
Hanpnmep, nornuecxoe orpnnanne uncna nont ecrt ennnna:
p1nf !0
1
Honrnoe xe orpnnanne nnneprnpyer nce nrt n uncne. B uacrnocrn, onepannx 0 nosno-
Crp.78
nnr onpeennrt maxcnmantnoe, npecrannmoe n xonxperno cncreme essnaxonoe nenoe
(ona ycranonnr nce nonunte paspxt cnona, ntenennoro nx uncna, n ennnnt):
p1nf 0
1294967295
Orcma cneyer, uro cnono na anno mamnne nmeer 32 paspxa, rax xax
1294967295 = 2""32 1294967295 = 2""32 1294967295 = 2""32 1294967295 = 2""32 - -- - 1 11 1
Onepafop censueauun
Oneparop cnxstnannx cnxstnaer cxanxpnym nepemennym n onepannm noncxa/sament
no manony (onee nopono +rn onepannn paccmarpnnamrcx n rnane ). Crpoxonte one-
paropt s/.../.../, m/.../, tr/.../ no ymonuannm paoramr c nepemenno $_. Oneparop cnxstna-
nnx ncnontsyercx, urot onn paorann c xonxperno cxanxpno nepemenno. Hanpn-
mep, nomecrnm crpoxy n nepemennym $line:
$11ne = ".he11o!"
Tenept ntnonnnm noncx no manony:
$11ne = ".he11o!"
1f {$11ne = m/-\./}
{ p1nf "5hou1df sfaf a senfence W1fh a pe1od!" }
5hou1df sfaf a senfence W1fh a pe1od!
Oneparop ! paoraer rouno rax xe, sa rem ncxnmuennem, uro on ntnonnxer nornuecxoe
orpnnanne na pesyntrarom, nosnpamaemtm oneparopom (xax n pyrne ]ynxnnn, one-
pannn noncxa n sament nosnpamamr nexoropoe snauenne n xauecrne pesyntrara cnoe pa-
ort onee nopono o +rom paccxastnaercx n rnane ).
Yruoeuue u peneuue
Oneparop ymnoxennx nepemnoxaer na uncna:
p1nf 2 " 4

Oneparop enennx / ennr ono uncno na pyroe:
p1nf 16 / 3
5.33333333333333
Oneparop ntuncnxer ocrarox or nenouncnennoro enennx nyx snauenn:
p1nf 16 x 3
1
Oneparop nonropennx x ynnpyer ecrnne saannoe xonnuecrno pas. B cxanxpnom xon-
rexcre on nosnpamaer crpoxy, nonyuennym ns snauennx, croxmero cnena or oneparopa, ny-
rem nonropennx +roro snauennx (xonxarenannn c coo) crontxo pas, cxontxo saano unc-
nom cnpana or oneparopa. Hanpnmep, nor xax naneuarart crpoxy ns 30 e]ncon:
p1nf - x 30
------------------------------
Ecnn nent onepan xnnxercx cnncxom, saxnmuenntm n xpyrnte cxoxn, ro oneparop no-
nropennx npoynnpyer cnncox neoxonmoe uncno pas. Bor xax, nanpnmep, cosart cnn-
Crp.79
cox ns noctmnecxrn ennnn n npncnonrt ero maccnny:
0ones = {1} x 0
Cneymmn neouennnt npnmep samenxer nce ennnnt na nxrepxn:
0ones = {5} x 0ones
(nocxontxy npant onepan oneparopa nonropennx onxen trt uncnom, ro maccnn
ones nnrepnpernpyercx n cxanxpnom xonrexcre, ro ecrt xax nenoe uncno, pannoe rexy-
memy xonnuecrny +nemenron maccnna).
Cnoeuue, euufauue u koukafeuauun
Oneparop cnoxennx (+) cxnatnaer na uncna:
p1nf 2+2
4
Oneparop ntunrannx (-) ntunraer ono uncno ns pyroro:
p1nf 4-2
2
Oneparop xonxarenannn (.) oennxer ne crpoxn:
p1nf "he11o " . "fhee."
he11o fhee.
Bce +rn oneparopt xnnxmrcx nnapntmn (n uacrnocrn, ne nao nyrart nnapnte onepa-
ropt cnoxennx n ntunrannx c ynapntm nnmcom n ynapntm mnnycom).
Onepafop cpeura
Oneparop cnnra nneno << nosnpamaer snauenne nenoro aprymenra, cnnnyroro nneno na
uncno nron, onpeenennoe npantm aprymenrom:
p1nf 2 << 10
204
Oneparop cnnra nnpano >> nosnpamaer snauenne nenoro aprymenra, cnnnyroro nnpano
na uncno nron, onpeenennoe npantm aprymenrom. Hanpnmep:
p1nf 204 >> 3
256
Hreuoeauuue yuapuue onepafopu
Perl paccmarpnnaer ]ynxnnn c onnm cxanxpntm aprymenrom (ne cnncxom), xax nmenonan-
nte ynapnte oneparopt, ecnn aprymenr ne saxnmuen n xpyrnte cxoxn. (Taxnmn ]ynxnnxmn
xnnxmrcx, x npnmepy, sqrt, defined, eval, return, chdir, rmdir, oct, hex, undef, exists n mno-
rne pyrne.) Bor, nanpnmep, xax ncnontsyercx n xauecrne oneparopa ]ynxnnx sqrt:
p1nf sqf 4
2
Onepafopu npoeepku qanoe
Crp.80
Perl noepxnnaer maccy oneparopon nx nponepxn cocroxnnx ]anon, uro nosnonxer
nonnocrtm xonrponnponart ]ant n nx ecxpnnropt (cm. rnany 12 ornocnrentno paort
c ]anamn n Perl). Bor xax sannctnamrcx +rn oneparopt ( samenxer nmo oneparop):
- ecxpun1op-auna
- aupaeuue
-
Bmecro nao nocrannrt onn ns oneparopon, npnneenntx n ran. .2
1
. Bosmoxno, xa-
xax-ro nn]opmannx ns +ro rannnt oxaxercx nx nac nesnaxomo, ecnn rontxo nt ne pa-
oraere c onepannonno cncremo Unix. Tax, apennarypt UID n GID osnauamr coor-
nercrnenno nenrn]nxarop nontsonarenx (user ID) n rpynnt (group ID). Ecnn onymen ap-
rymenr, ncnontsyercx nepemennax $_ (sa ncxnmuennem oneparopa -t, xoropt no ymonua-
nnm paoraer c nxontm noroxom anntx STDIN).
Bor necxontxo npnmepon nponepxn ]anonoro ecxpnnropa STDIN (no ymonuannm coor-
nercrnyer nxonomy noroxy anntx):
p1nf -e 51DlN # Cy|ec1aye1 nu 51DlN
1
p1nf -f 51DlN # hpucoeuueu nu ou x 1epuuuany
7
p1nf - 51DlN # uee1 nu ou uyneaou paauep
1
(Oparnre nnnmanne, uro nornuecxoe snauenne ncrnna (true) nosnpamaercx +rnmn onepa-
ropamn, xax uncno onn.)
1abnua 4.2. 1abnua 4.2. 1abnua 4.2. 1abnua 4.2. Onepa1opu 1ec1upoaauun aunoa
Onepa1op Onepa1op Onepa1op Onepa1op hpoaepneuan uuopuaun hpoaepneuan uuopuaun hpoaepneuan uuopuaun hpoaepneuan uuopuaun
- 8peun, npoeee c uoueu1a nocneuero obpa|euun x auny
-b nouuuu aun
-8 aouuuuu aun
-c Cuuaonuuuu aun
-C 8peun, npoeee c uoueu1a nocneuero uaueueuun uuexcuoro ecxpun1opa
-d a1anor
-e aun cy|ec1aye1
-f Obuuuuu aun
-g xc1auoaneu a1puby1 5L1GlD
-k xc1auoaneu a1puby1 5f1cky b1f
2

-l aun nanne1cn cuuaonuuecxou ccunxou
- 8peun cy|ec1aoaauun auna a unx ua uoueu1 aanycxa ceuapun
-o aun npuuaneu1 1exy|euy nonuaoaa1enk {effecf1ve ulD}
-O aun npuuaneu1 peanuuouy nonuaoaa1enk {ea1 ulD}
-p aun nanne1cn uueuoaauuuu xauanou {IlIO}
- aun oc1yneu nn u1euun 1exy|euy nonuaoaa1enk {effecf1ve ulD} unu rpynne {effecf1ve GlD}
-k aun oc1yneu ua u1euue peanuuouy nonuaoaa1enk {ea1 ulD} unu rpynne {ea1 GlD}
-s aauep auna {ou e - npoaepxa auna ua uyneaou paauep}
-5 aun nanne1cn coxe1ou {ce1eauu coeuueuueu}
-f aun o1xpu1 ua 1exy|eu 1epuuuane
-1 1exc1oauu aun

1
B oxymenrannn, conponoxamme Perl (]an perlIunc), moxno narn n pyrne oneparopt, ne nxnmuennte n ran.
.2. Ho ontme uacrn nx paora cnntno sanncnr or cncremt (n uacrnocrn, n Windows mnorne npocro ecnonesnt, a
mnorne paoramr nenpannntno nx FAT1 n FAT32). Hpu+e:. peo.
2
Hnnxn nr ycranannnnaercx nx xaranora n sanpemaer nontsonarenm, nmemmemy npano na sannct n +ror xara-
nor, yanxrt ]ant, nnaentnem xoroptx on ne xnnxercx. nx nontsonarene cncremt Unix +ro nonxrne snaxomo n
es nepenoa, a n onepannonno cncreme Windows nnuero noonoro npocro ner. Hpu+e:. peo.

Crp.81
-u nn auna yc1auoaneu a1puby1 5L1ulD
-W aun oc1yneu nn aanucu 1exy|euy nonuaoaa1enk {effecf1ve ulD} unu rpynne {effecf1ve GlD}
-W aun oc1yneu nn u1euun peanuuouy nonuaoaa1enk {ea1 ulD} unu rpynne {ea1 GlD}
-x aun oc1yneu nn aunonueuun 1exy|euy nonuaoaa1enk {effecf1ve ulD} unu rpynne {effecf1ve
GlD}
- aun oc1yneu nn aunonueuun peanuuouy nonuaoaa1enk {ea1 ulD} unu rpynne {ea1 GlD}
- aun uuee1 uyneaou paauep
Onepafopu cpaeueuun
Oneparopt cpannennx +ro nnapnte oneparopt, xoropte cpannnnamr annte, nos-
npamax ennnny n xauecrne snauennx ucmuua (true) n nycrym crpoxy n xauecrne snauennx
.ori (false). Onn nepeuncnent n ran. .3. Oparnre nnnmanne, uro onn oneparopt nc-
nontsymrcx nx cpannennx uncen, a pyrne nx cpannennx crpox.
Hocuasua. Oopamume euu+auue, :mo onepamop oo.iue u.u paeuo ooosua:eu rar ~, :mooi ue cnv-
mami eeo c onepamopo+ > cuuouu+o+ sanmo (pasoe.ume. o.e+eumoe cnucra).
B cneymmem npnmepe nponepxmrcx uncna, nnonmte nontsonarenem, n, ecnn nneennoe
uncno oxastnaercx ontme cra, ntaercx coomenne o omnxe:
Wh11e {<>} {
1f {$ > 100}
{ p1nf "1oo b1g!\n" }
}
Bt moxere ncnontsonart nornuecxne oneparopt n [[ nnn nx coponue and n or (or-
nnuammnxcx mentmnm npnopnrerom) nx oennennx orentntx nornuecxnx yrnepxe-
nn. B cneymmem npnmepe saaercx rpeonanne roro, urot nneennax nontsonarenem
yxna tna mexy k n m:
p1nf "P1ease enfe 1effes fom k fo m\n"
Wh11e {<>} {
chop
1f {$ 1f k o $ gf m} {
p1nf "P1ease enfe 1effes fom k fo m\n"
} e1se {
p1nf "1hank you - 1efs have anofhe!\n"
}
}

1abnua 4.3. 1abnua 4.3. 1abnua 4.3. 1abnua 4.3. Onepa1opu cpaaueuun
Onepa1op Onepa1op Onepa1op Onepa1op 1un auuux 1un auuux 1un auuux 1un auuux 8oaapa|aeuoe auaueuue 8oaapa|aeuoe auaueuue 8oaapa|aeuoe auaueuue 8oaapa|aeuoe auaueuue
< ucno c1uua, ecnu neauu onepau ueuue npaaoro
> ucno c1uua, ecnu neauu onepau bonue npaaoro
<= ucno c1uua, ecnu neauu onepau ueuue unu paaeu npaaouy
>= ucno c1uua, ecnu neauu onepau bonue unu paaeu npaaouy
1f C1poxa c1uua, ecnu neauu onepau ueuue npaaoro
gf C1poxa c1uua, ecnu neauu onepau bonue npaaoro
1e C1poxa c1uua, ecnu neauu onepau ueuue unu paaeu npaaouy
ge C1poxa c1uua, ecnu neauu onepau bonue unu paaeu npaaouy
Onepafopu paeeucfea
Kpome paccmorpenntx n npetymem pasene oneparopon cpannennx Perl noepxnnaer one-
paropt nponepxn na panencrno, npnneennte n ran. .. (Hocxontxy onn nmemr mentmn
Crp.82
npnopnrer, uem oneparopt cpannennx, nx nnx orneen ocot pasen.) Kax n n cnyuae onepa-
ropon cpannennx, cymecrnymr orentnte oneparopt nx uncnontx anntx n rexcrontx crpox.
B cneymmem npnmepe nontsonarent onxen nnecrn yxny y (nrpex). 3anpoc nonropxercx o
ecxoneunocrn c ntnoom coomennx o omnxe, noxa nontsonarent ne ntnonnnr rpeyemoe
ecrnne:
p1nf "P1ease fype fhe 1effe y\n"
Wh11e {<>} {
chop
1f {$ ne y} {
p1nf "P1ease fype fhe 1effe y\n"
} e1se {
p1nf "Do you a1Ways do Whaf youe fo1d\n"
ex1f
}
}
B pesyntrare paort +ro nporpammt na ntxoe moxer nonyunrtcx cneymmn rexcr:
P1ease fype fhe 1effe y
a
P1ease fype fhe 1effe y
b
P1ease fype fhe 1effe y
c
P1ease fype fhe 1effe y
y
Do you a1Ways do Whaf youe fo1d

1abnua 4.4. 1abnua 4.4. 1abnua 4.4. 1abnua 4.4. Onepa1opu cpaaueuun
Onepa1op Onepa1op Onepa1op Onepa1op 1un au 1un au 1un au 1un auuux uux uux uux 8oaapa|aeuoe auau 8oaapa|aeuoe auau 8oaapa|aeuoe auau 8oaapa|aeuoe auaue ee euue uue uue uue
== ucno c1uua, ecnu neauu onepau paaeu npaaouy
!= ucno c1uua, ecnu neauu onepau ue paaeu npaaouy
<=> ucno -1,0 unu 1 a aaaucuuoc1u o1 1oro, nanne1cn nu neauu onepau ueuue npa-
aoro, paauuu npaaouy unu bonue npaaoro
eq C1poxa c1uua, ecnu neauu onepau paaeu npaaouy
ne C1poxa c1uua, ecnu neauu onepau ue paaeu npaaouy
cmp C1poxa -1,0 unu 1 a aaaucuuoc1u o1 1oro, nanne1cn nu neauu onepau ueuue npa-
aoro, paauuu npaaouy unu bonue npaaoro
Roufuoe H
Oneparop nonrnoro nornuecxoro H () nosnpamaer pesyntrar nornuecxoro ymnoxennx
and nanramn onepanon, ntponnenntx pyr ornocnrentno pyra. (Tannna nornuecxo-
ro ymnoxennx nron npnneena n ran. .5.)
Hanpnmep, ntnonnenne +ro onepannn na uncnamn 5 (nrt 0 n 2 pannt ennnne) n
(rontxo nr 2 panen ennnne) aer pesyntrar :
p1nf 5 4
4

1abnua 1abnua 1abnua 1abnua 4.5. 4.5. 4.5. 4.5. Onepa1op
and 0 1
O 0 0
1 0 1
Crp.83
Roufuoe HPH
Oneparop nonrnoro nornuecxoro HHH ([) nosnpamaer pesyntrar nornuecxoro cnoxennx
or na nramn onepanon, ntponnenntx pyr ornocnrentno pyra. (Tannna nornuecxoro
cnoxennx nron npnneena n ran. ..)
1abnua 4.6. 1abnua 4.6. 1abnua 4.6. 1abnua 4.6. Onepa1op
o 0 1
0 0 1
1 1 1

Hanpnmep, ntnonnenne +ro onepannn na uncnamn (nr 2 panen ennnne) n 1 (nr 0
panen ennnne) aer pesyntrar 5 (re ennnne pannt nrt 0 n 2):
p1nf 4 | 1
5
Roufuoe Hcknkakee HPH
Oneparop nonrnoro nornuecxoro HCKHPUAPLEIO HHH () nosnpamaer pesyntrar
nornuecxo onepannn xor na nramn onepanon, ntponnenntx pyr ornocnrentno pyra.
(Tannna onepannn xor nx nron npnneena n ran. .7.) Oparnre nnnmanne, uro nx
ncxnmuammero nnn, ecnn n n nepnom, n no nropom onepane nr panen ennnne, na nt-
xoe nonyuaercx nont. Hecxontxo npnmepon:
p1nf 0 0
O
p1nf 1 0
1
p1nf 1 1
O
p1nf 0 1
1
p1nf 5 4
1

1abnua 4.7. 1abnua 4.7. 1abnua 4.7. 1abnua 4.7. Onepa1op cxnkuak|ee
xo 0 1
1 1 0
1 1 0
Porueckoe H e cfune nsuka C
Oneparop ntnonnxer nornuecxym onepannm H na nymx nornuecxnmn snauennxmn.
On moxer oennxrt na oneparopa cpannennx, nponepxx, urot oa nosnpamann snaue-
nne ucmuua nepe rem, xax nepnyrt omo snauenne n xauecrne pesyntrara. Hpnmep:
p1nf "P1ease enfe numbes fom 5 fo 10\n"
Wh11e {<>} {
chop
1f {$ >= 5 $ <= 10} {
p1nf "1hank you - 1efs have anofhe!\n"
} e1se {
p1nf "P1ease enfe numbes fom 5 fo 10\n" }
}
Crp.84
Peut ner o oneparope n crnne C, nocxontxy n annom cnyuae ncnontsyercx ror xe ca-
mt cnnraxcnc, a xpome roro, +ror oneparop nmeer raxo xe npnopnrer, xax n n C. (B Perl
cymecrnyer n eme onn nornuecxn oneparop ananornunoro ecrnnx, oosnauaemt xax
and, no ero npnopnrer mentme.) 3ror oneparop raxxe nsnecren xax onepamop ropomroeo
oecmeu ecnn nent onepan coornercrnyer ycnonnm .ori, ro nropo onepan ne
ntuncnxercx n ne nponepxercx.
B ornnune or C n Perl +ror oneparop nosnpamaer ne nont nnn ennnny, a nocnenee nt-
uncnennoe snauenne, ro ecrt nepnt onepan, ecnn on coornercrnyer ycnonnm .ori, nnn
nropo, ecnn nepnt coornercrnyer ycnonnm ucmuua (nanomnnm, uro n Perl ycnonnm
.ori coornercrnymr nmte nycrte snauennx nycrax crpoxa, neonpeenennax nepe-
mennax, undef n r. ., a ycnonnm ucmuua nce ocrantnte).
Porueckoe HPH e cfune nsuka C
Oneparop ntnonnxer nornuecxym onepannm HHH na nymx nornuecxnmn snauennxmn.
On moxer oennxrt na oneparopa cpannennx, nosnpamax snauenne ucmuua, ecnn xorx
t onn ns onepanon nosnpamaer snauenne ucmuua. B +rom npnmepe neuaraercx coome-
nne o omnxe, ecnn nneennoe nontsonarenem uncno ne nxonr n yxasannt nanason:
p1nf "P1ease enfe numbes fom 5 fo 10\n"
Wh11e {<>} {
chop
1f {$ < 5 || $ > 10} {
p1nf "P1ease enfe numbes fom 5 fo 10\n"
} e1se {
p1nf "1hank you - 1efs have anofhe!\n" }
}
Onxrt-raxn, mt ronopnm o crnne C, nocxontxy n annom cnyuae ncnontsyercx ananornu-
nt cnnraxcnc n ananornunt npnopnrer. (B Perl ecrt n pyro nornuecxn oneparop ana-
nornunoro ecrnnx, oosnauaemt xax or, no ero npnopnrer mentme.) Kax n npetymn
oneparop , on xnnxercx oneparopom xoporxoro ecrnnx ecnn nent onepan coor-
nercrnyer ycnonnm ncrnna, ro nropo onepan ne ntuncnxercx n ne nponepxercx.
B ornnune or C n Perl +ror oneparop nosnpamaer ne nont nnn ennnny, a nocnenee nt-
uncnennoe snauenne, ro ecrt nepnt onepan, ecnn on coornercrnyer ycnonnm ucmuua,
nnn nropo, ecnn nepnt coornercrnyer ycnonnm .ori (nanomnnm, uro n Perl ycnonnm
.ori coornercrnymr nmte nycrte snauennx nycrax crpoxa, neonpeenennax nepe-
mennax, undef n r. ., a ycnonnm ucmuua nce ocrantnte).
Tor ]axr, uro oneparop nosnpamaer nocnenee ntuncnennoe snauenne, a ne snauennx
ucmuua nnn .ori (nmenno n raxom crnne o nem n nanexnr ymart npn nanncannn npo-
rpammt), nosnonxer ncnontsonart ero nx camtx pasntx onepann. Hanpnmep, moxno nc-
ntrart necxontxo napnanron ecrnnx n n cnyuae oxonuarentno neyaun ntnecrn coo-
menne o omnxe, npexparnn paory:
$esu1f = fh1s{$dafa} || fhaf{$dafa} || d1e "Canf gef fh1s{} o fhaf{} fo Wok\n"
3ect nopmantnax nocneonarentnocrt ecrnn npeptnaercx c nomomtm ]ynxnnn die.
Ona ntsoner ntxo ns nporpammt c ntnoom coomennx nna Cant get this() or that() to
work at trv.pl line X.
Crp.85
Onepafop puanasoua e cnuckoeor koufekcfe
B sanncnmocrn or xonrexcra nosmoxnt na npnnnnnnantno pasntx napnanra paort one-
paropa nanasona (..). B cnncxonom xonrexcre on nosnpamaer maccnn snauenn c marom 1,
nexamnx n nanasone, yxasanntx nentm n npantm onepanamn, naunnax co snauennx,
yxasannoro nentm onepanom. Hanpnmep, cneymmax xomana ntnonr ony n ry xe
crpoxy nxrt pas nopx:
fo {1 .. 5}
{ p1nf "hee We ae aga1n!\n" }
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
To xe camoe nponsoer, ecnn nemnoro ymentmnrt nepxnmm rpannny nanasona:
fo {1 .. 4.5}
{ p1nf "hee We ae aga1n!\n" }
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
hee We ae aga1n!
(Ecnn nepxnxx rpannna ne connaaer c nocnennm crenepnponanntm snauennem, ro onepa-
rop nanasona nosnpamaer na onn +nemenr ontme na nocnenem mare on nxnmuaer n
cnncox snauenne, yonnernopxmmee ycnonnm ontme nnn panno.)
Heoxonmo ormernrt, uro n Perl nepcnn 5 nx oneparopon for n foreach noonax xoncr-
pyxnnx ne npnnonr x ntenennm npomexyrounoro npemennoro maccnna. Hanpornn, n
craptx nepcnxx Perl npn ntnonnennn nnxna
fo {1 .. 1000000000} {
# xo 1ena uxna
}
nt moxere cronxnyrtcx c ceptesntmn nponemamn nexnarxn namxrn. B cnncxonom xon-
rexcre oneparop nanasona paoraer ne rontxo c uncnamn, no n co crpoxamn (noono
paccmorpenntm panee oneparopam ++ n --). Hanpnmep, moxno ncnontsonart xomany
0a1phabef = { .. }
urot nonyunrt cnncox sarnanntx narnncxnx yxn, nnn xomany
0hexd1g1f = {0 .. 9,a .. f}|$num 15]
nx nonyuennx mecrnanarepnuno nn]pt, nnn xomany
0dafe = {00 .. 31} p1nf $dafe|$day]
urot ntnecrn ary c neymnmn nynxmn
Onepafop puanasoua e ckannpuor koufekcfe
B cxanxpnom xonrexcre oneparop nanasona nosnpamaer nornuecxoe snauenne, coornercr-
nymmee ycnonnm .ori nnn ucmuua. B ornnune or paccmorpenntx panee oneparopon
cpannennx, xaxt oneparop nanasona, ncnontsyemt nporpammo na Perl n cxanxpnom
xonrexcre, neer cex xax manentxn rpnrrep y nero ecrt cocrnennoe nnyrpennee co-
Crp.86
croxnne, xoropoe yunrtnaercx npn ouepenom ntnonnennn annoro ]parmenra xoa.
Paccmorpnm, xax paoraer oneparop nanasona n cxanxpnom xonrexcre. Hauantnoe cocroxnne
oneparopa coornercrnyer ycnonnm .ori. Ono raxnm n ocranercx, ecnn npn ouepenom nt-
uncnennn oneparopa nent onepan coornercrnyer ycnonnm .ori, npnuem snauenne, ntpa-
artnaemoe oneparopom nanasona, yer raxxe coornercrnonart ycnonnm .ori. Ecnn xe
npn ouepeno nponepxe nent onepan oparnncx n ucmuuv, nnyrpennee cocroxnne onepa-
ropa nanasona craner ucmuuo coornercrnenno nosnpamaemoe oneparopom snauenne rax-
xe yer ucmuuo. Oneparop yer ocranartcx n raxom cocroxnnn o rex nop, noxa n ucmuuv
ne oparnrcx npant onepan. Kax rontxo +ro nponsoer, nnyrpennee cocroxnne oneparopa
nemenenno oparnrcx cnona n .ori, onaxo nt +ro ynnnre rontxo npn cneymmem opa-
mennn x oneparopy nocnenee ntannoe oneparopom nanasona snauenne yer ucmuua.
Ouepenoe ntnonnenne oneparopa nanasona npnneer x nponepxe nenoro onepana, n nect
nnxn moxer nonropnrtcx cnauana.
Oneparop nanasona nponepxer rontxo nent onepan, ecnn nnyrpennee cocroxnne oneparo-
pa coornercrnyer ycnonnm .ori, n rontxo npant onepan, ecnn ero nnyrpennee cocroxnne
coornercrnyer snauennm ucmuua. Hpn nepexnmuennn nnyrpennero cocroxnnx oneparopa ns
ucmuui n .ori nponepxa nenoro onepana orxnatnaercx o cneymmero opamennx x one-
paropy, a n xauecrne ntxonoro snauennx oneparopa nosnpamaercx ucmuua. Hpn nepexnmue-
nnn xe ns cocroxnnx .ori n cocroxnne ucmuua oneparop nanasona otuno ryr xe nponepx-
er cno npant onepan. Ecnn pesyntrar oxasancx ucmuuo, nnyrpennee cocroxnne nepexnm-
uaercx oparno n .ori, onaxo snauenne .ori onxrt-raxn moxer trt ntano n xauecrne
ntxonoro snauennx rontxo npn cneymmem opamennn x annomy yuacrxy xoa, a rexymnm
ntxontm snauennem yer ucmuua. Ecnn xe nt xornre npn nepexnmuennn ns cocroxnnx
.ori n cocroxnne ucmuua ornoxnrt nponepxy npanoro onepana o cneymmero opamennx
x oneparopy nanasona, ncnontsyre rpn rouxn ... nmecro nyx, (Hepnt napnanr noneennx
xapaxrepen nx awk, nropo nx sed.) Onaxo uacro nmecro npanoro n nenoro onepanon
oneparopa nanasona yxastnamrcx ornmt ne nornuecxne ycnonnx, a uncnonte nnn crpoxo-
nte xoncranrt. Ecnn nent nnn npant onepant oxastnamrcx xoncranro, ro nmecro nt-
uncnennx coornercrnymmero onepana (nponepxn ycnonnx) cnennantnax nepemennax Perl
$. (ona xpannr rexymn nomep cunranno ns ]ana crpoxn nx ]ana, x xoropomy tno no-
cnenee opamenne) cpannnnaercx c xoncranro no npnsnaxy ontme nnn panno. Taxoe
crpannoe noneenne oneparopa nanasona n cxanxpnom xonrexcre cnxsano c rem, uro on mo-
ennpyer oneparopt, xoropte n awk n sed nponepxmr nomep crpoxn, cunranno ns ]ana.
Hanpnmep, urot naneuarart nnmt uacrt crpox ns nxonoro ]ana, ncnontsyercx xomana:
Wh11e {<>} {
1f {101..200} {p1nf} # heua1u c1pox 101-200
}
urot nonxrt paory oneparopa nanasona n cxanxpnom xonrexcre, pexomenyercx nnn-
marentno pasopartcx c necxontxnmn opasnamn xoa, nanncannoro npo]eccnonantntmn
nporpammncramn. Hexoropte ns npnmepon npnneent n oxymenrannn Perl, a raxxe mo-
ryr trt nsxrt ns ero nnnoreuntx moyne.
Hocuasua. B ra:ecmee vc.oeu uoxo onepamop ouanasoua eosepauaem nvcmvm cmporv, e ra:ecmee vc.oeu
ucmuua ro.u:ecmeo vcneuuix oopaueuu r oauuo+v onepamopv ouanasoua. Hpu nepeo+ vcneuuo+ oopa-
ueuuu omo :uc.o paeuo eouuue. Ouo copacieaemc oopamuo e eouuuv e c.v:ae nepexooa r c.eovmue+v one-
pamopv ouanasoua u.u npu eosuuruoeeuuu vc.oeu uoxo. Umooi ocooi+ oopaso+ oopaoomami nepei o.e-
+eum, o. romopoeo onepamop ouanasoua eepuv. sua:euue ucmuua, oocmamo:uo npoeepumi, ue paeuo .u omo
sua:euue eouuue. Umooi ocooi+ oopaso+ oopaoomami noc.eouu o.e+eum, +oruo ucno.isoeami c.eovm-
uee ceocmeo Perl. ec.u onepamop ouanasoua eisieaemc noc.eouu pas (mo ecmi e +o+eum nepexooa e co-
cmouue uoxo), eosepauae+oe u+ sua:euue-c:em:ur cooeprum cvqqurc E0 (sua:euue, eosepauae+oe one-
Crp.87
pamopo+ ouanasoua, e.emc cmpouo, a ue uucuo+, uo, rar vre om+e:a.oci, Perl ue oe.aem oo.iuo pas-
uui +erov :uc.a+u u cmpora+u, npeoopasv ux us oouoeo qop+ama e opveo no +epe ueooxoou+ocmu).
Jmom cvqqurc, xaparmepui o. :uc.a c n.aeamue mo:ro, ue e.uem ua :uc.oeoe sua:euue cmporu-
c:em:ura, oouaro +orem c.vrumi uuouramopo+, :mo oocmueuvm noc.eouu o.e+eum.
Tepuapuu ycnoeuu onepafop
Vcnonnt oneparop : rpeyer rpex onepanon. Ero paora noona xoncrpyxnnn if-then-
else. Ecnn onepan, yxasannt nepe nonpocnrentntm snaxom, coornercrnyer ycnonnm
ucmuua, ro ntuncnxercx n nosnpamaercx n xauecrne snauennx nropo onepan, pacnono-
xennt mexy nonpocnrentntm snaxom n noerounem. Ecnn xe snauenne nepnoro one-
pana coornercrnyer ycnonnm .ori, ro ntuncnxercx n nosnpamaercx rpern onepan.
B cneymmem npnmepe ycnonnt oneparop ntuncnxer aconmrnoe snauenne uncna, nno-
nmoro nontsonarenem (npenonoxnm, uro ncrpoenno ]ynxnnn abs ne cymecrnyer):
Wh11e {<>}
{ p1nf $ >= 0 $ : -$ }
Bnonmoe uncno cpannnnaercx c nynem. Ecnn uncno neorpnnarentnoe (ontme nnn panno ny-
nm), ro nporpamma ntnonr uncno es nsmenenn. B npornnnom cnyuae ncnontsyercx ynap-
nt mnnyc, xoropt nnneprnpyer snax uncna nepe ntnoom.
B cneymmem npnmepe uncna ns nanasona 0-15, nnonmte nontsonarenem, npeopasymrcx n
mecrnanarepnunt ]opmar:
Wh11e {><}
{p1nf $ < 10 $ : "{\{{a, b, c, d, e, f}|$ - 10]}}\n"
}
3ect ner nponepxn na omnxn. Moxno cocrannrt onee axxyparnym nporpammy c nno-
xenntmn oneparopamn :, xoropax nponepxer nnonmoe snauenne n ntnonr coomenne
o omnxe, ecnn nneennoe uncno ne moxer trt npeopasonano x mecrnanarepnunomy
uncny, cocroxmemy ns ono nn]pt:
Wh11e {<>} {
p1nf $ > 0 $ < 10 $ :
"{\{$ < 16 {a, b, c, d, e, f}|$ - 10] : \"Numbe 1s nof a s1ng1e hex d1g1f.\" }}\n"
}
(urot pasopartcx, xax paoraer +ror npnmep, rpeyercx marepnan, xoropt paccmarpn-
naercx nosnee.)
Onepafop npuceoeuun
Oneparop npncnoennx sanocnr annte, yxasannte n xauecrne npanoro onepana, no mecry,
yxasannomy nentm onepanom. B xauecrne nenoro onepana onxnt ntcrynart rax nastnae-
mte nente snauennx (otuno +ro nepemennte, onee nopono cm. rnany 2, pasen uro
raxoe nenoe snauenne), n xauecrne npanoro onepana nmoe ntuncnxemoe ntpaxenne:
$va1ab1e = 5
Hoono xstxy C, n Perl moxno ncnontsonart coxpamennte ]opmt oneparopa npncnannannx.
Hanpnmep, xomnnnponanne npncnoennx c ymnoxennem sannctnaercx cneymmnm opasom:
$doub1eme "= 2
Crp.88
B +rom npnmepe coepxnmoe nepemenno $doubleme ymnoxaercx na na, a pesyntrar no-
memaercx n ry xe camym nepemennym. Cnncox paspemenntx xoporxnx oneparopon npn-
cnoennx ntrnxnr xax
""= += "= = <<= =\ -= /= |= >>= ||= .= x= = x=
B ornnune or C n Perl oneparop npncnoennx npecrannxer coo nonnonennoe nenoe sna-
uenne, xoropoe n cnyuae neoxonmocrn moxno ncnontsonart xax nepemennym. Hanpn-
mep, n cneymmem cnyuae mt orcexaem nocnenn cnmnon nmenno or nepemenno $input,
a ne or ntpaxennx, nosnpamaemoro oneparopom npncnannannx:
chop {$1npuf = 123}
p1nf $1npuf
12
3ro cnocrno nomoraer cosanart nemnoro onee xomnaxrnt xo. Hanpnmep, cneymmn
]parmenr nporpammt cunrtnaer crpoxy ns nxonoro noroxa, yanxer nocnenn cnmnon
(xonen crpoxn) n ocrannxer pesyntrar n nepemenno $input n nce n ony crpouxy:
chop {$1npuf = <>}
Onepafop-sannfan
3ror oneparop paoraer pasnnuntm opasom n cxanxpnom n cnncxonom xonrexcre. B cxa-
nxpnom xonrexcre on ntuncnxer cno nent aprymenr, orpactnaer nonyuennt pesynt-
rar, sarem ntuncnxer npant aprymenr n nosnpamaer ero n xauecrne snauennx. Hanpnmep:
$va1ab1e = {3, 4, 5}
p1nf $va1ab1e
5
B cnncxonom xonrexcre oneparop-sanxrax ntcrynaer n xauecrne pasennrenx cnncxa, no-
memax n cnncox oa aprymenra, xax n cneymmem npnmepe:
0aay = {3, 4, 5}
p1nf o1n{", ", 0aay}
3, 4, 5
Koncrpyxnnx > xnnxercx cnnonnmom nx oneparopa-sanxro. Haunnax c nepcnn Perl 5.001 +ror
oneparop sacrannxer nnrepnpernponart aprymenr, yxasannt cnena, xax rexcronym crpoxy.
Hocuasua. Cu+eo. > e.emc ouepaqo+. uepaq omo cu+eo., cocmae.euui us oevx uovuux noopo .umep.
Cnucku, cfonue cneea
Oneparop cnncxa (naop +nemenron, pasenenntx sanxro) nmeer pasnnunt npnopnrer, xo-
ra cpannnnaercx c oneparopamn, croxmnmn cnena n cnpana. Ornocnrentno oneparopon,
croxmnx cnpana, npnopnrer y oneparopa-cnncxa mentme, uem nx oneparopon, croxmnx cne-
na. Hanpnmep, ror ]axr, uro oneparop-sanxrax na ony crynent onee npnopnreren, uem npa-
nt oneparop cnncxa, nosnonxer sanepmnrt cnncox o roro, xax noart ero na nxo npeme-
crnymmero cnncounoro oneparopa. Bor npnmep, yxe ncnontsonanmncx panee:
p1nf 1, 2, 3, 4, sof 9, , 7, 6, 5
12345679
Paccmorpnm, nanpnmep, sanxrym, croxmym nocne uncna 7. Cnncox ns rpex uncen 9, n 7,
croxmn cnena or nee, nmeer onee cnat npnopnrer, uem sanxrax, n no+romy nmecro ro-
Crp.89
ro, urot nemenenno cxopmnrt nmemmncx cnncox oneparopy sort, yer npoonxeno
nononnenne cnncxa nontm +nemenrom .
Porueckoe NOT
Oneparop not nosnpamaer nornuecxoe orpnnanne cnoero onepana. Ero ecrnne anano-
rnuno oneparopy !, paccmorpennomy panee, sa rem ncxnmuennem, uro npnopnrer +roro
oneparopa snaunrentno nnxe (raxnm opasom, moxno ne ecnoxonrtcx o xpyrntx cxoxax
n ntpaxennxx, pasenenntx +rnm oneparopom). Bor npnmep:
p1nf ".", {nof 0}, "\n"
p1nf ".", {nof 1}, "\n"
. 1.
..
(3ect n xauecrne nornuecxoro snauennx .ori ntnonrcx nycrax crpoxa.)
Porueckoe ND
Oneparop and paoraer ananornuno , no nmeer onee nnsxn npnopnrer. On cnauana
ntuncnxer nent onepan, a ero npant onepan ntuncnxercx rontxo n rom cnyuae, xora
coornercrnyer ycnonnm ucmuua. pyrnmn cnonamn, oneparop and paoraer no coxpamen-
no cxeme, xax n oneparop . (B pasene, nocnxmennom oneparopy or, noxasano, xax +ro
moxno ncnontsonart.)
Porueckoe OR
Oneparop or paoraer ananornuno [[, no nmeer onee nnsxn npnopnrer. Hnsxn npnopn-
rer oneparopa rapanrnpyer, uro ero moxno ncnontsonart npn ntsonax cnncxontx onepa-
ropon es neoxonmocrn saxnmuart cnncxn n xpyrnte cxoxn.
Oneparop or cnauana ntuncnxer nent onepan, a npant onepan ntuncnxercx rontxo n
rom cnyuae, ecnn nent coornercrnyer ycnonnm .ori. pyrnmn cnonamn, or paoraer no
raxo xe coxpamenno cxeme, uro n oneparop [[.
Ecnn xorx t onn ns onepanon coornercrnyer ycnonnm ucmuua, ro oneparop or nosnpa-
maer snauenne nepnoro raxoro onepana n xauecrne pesyntrara (nocxontxy n Perl nmoe
nenynenoe n nenycroe snauenne paccmarpnnaercx xax ucmuua, nosmoxna macca napnan-
ron). 3ro moxno ncnontsonart nx coxpamennx uncna ycnonntx oneparopon. Hanpnmep, n
cneymmem ]parmenre xoa enaercx nontrxa orxptrt ]an, a n cnyuae neyaun ntno-
nrcx coomenne o omnxe n paora nporpammt npexpamaercx c nomomtm ]ynxnnn die:
open I11ehand1e, $f11ename o d1e "Cannof open $f11ename\n"
(Tax xax npnopnrer oneparopa or mentme, uem y oneparopon open n die, cxoxn ne nyxnt.)
Porueckoe OR
Oneparop xor nosnpamaer nornuecxoe ncxnmuammee nnn nx nyx oxpyxammnx ero one-
panon. Paora xor ananornuna paore oneparopa , no ero npnopnrer cymecrnenno nnxe.
Crp.90
Fnana 5
Ycnonume onepa1opm
u uuknm


Kpa1koe nnepeuue
B +ro rnane paccxastnaercx, xax c nomomtm ycnonntx oneparopon n nnxnon ynpannxrt
nopxxom ntnonnennx xoman Perl. Kpome roro, yyr paccmorpent n pyrne xomant,
ynpannxmmne xoom ntnonnennx cnenapnx, nanpnmep, goto, exit n die.
Ycnoeuue onepafopu
Vcnonnte oneparopt, raxxe nastnaemte oneparopamn nernnennx, nosnonxmr nanpannxrt
ntnonnenne cnenapnx n ry nnn nnym cropony n sanncnmocrn or pesyntrara nornuecxo
nponepxn. Hntmn cnonamn, c nx nomomtm na +rane ntnonnennx xoa moxno npnnnmart
pemennx n ecrnonart n coornercrnnn c nocnennmn.
Hanpnmep, cneymmn ]parmenr cnenapnx ncnontsyer xomany if nx nponepxn snauennx
nepemenno $variable. Ecnn snauenne panno nxrn, ntnonrcx rexcr es, it is five. B
npornnnom cnyuae na +xpane noxnnxercx crpoxa No, it is not five.:
$va1ab1e = 5
1f {$va1ab1e == 5} {
p1nf "es, 1fs f1ve.\n"
} e1se {
p1nf "No, 1fs nof f1ve.\n"
}
es, 1fs f1ve.
axe +ror npocro npnmep noxastnaer nosmoxnocrn xomant if. Ona nponepxer ycnonne,
saannoe n xpyrntx cxoxax, n ecnn pesyntrar ntuncnenn coornercrnyer ucmuue (ro ecrt ne
xnnxercx nynem, nycro crpoxo n r. .), nporpamma yer ntnonnxrt nepnt nox xoa. B
npornnnom cnyuae ocymecrnnxercx nepexo x noxy else (on moxer n orcyrcrnonart).
Komana if xnnxercx cocranno, ro ecrt nxoxmn n nee nox (nnn noxn) xoa ntenxer-
cx ]nrypntmn cxoxamn. Hocxontxy Perl nponycxaer npoentnte cnmnont, nxnmuax
cnmnont nepenoa crpoxn, npetymym xomany moxno sanncart xax
$va1ab1e = 5
1f {$va1ab1e == 5}
{
p1nf "es, 1f s f1ve.\n"
}
e1se
{
p1nf "No, 1fs nof f1ve.\n"
Crp.91
}
Onaxo sanpeuaemc ncnontsonart cnnraxcnuecxym xoncrpyxnnm n crnne xstxa C, xora
]nrypnte cxoxn onycxamrcx. axe ecnn nox cocronr ns ono xomant, Perl eceeoa
rpeyer nannunx ]nrypntx cxoox:
$va1ab1e = 5
1f {$va1ab1e == 5} # 1o uenpaaunuuo!
p1nf "es, 1fs f1ve.\n"
e1se
p1nf "No, 1fs nof f1ve.\n"
Vcnonnte oneparopt, noonte if, nosnonxmr ynpannxrt nopxxom ntnonnennx npo-
rpammt. B +rom n ecrt cyrt nporpammnponannx npannntno npnnxrt pemenne.
Onepafopu uukna
Oneparopt nnxna raxxe xnnxmrcx momntm nncrpymenrom nporpammnponannx, noromy
uro nosnonxmr ntnonnxrt nrepannonnte onepannn na naopamn anntx. 3ro nmenno
ro, n uem xomntmrept npenocxoxr nme tcrpte nonropxmmnecx ntuncnennx.
Oneparop nnxna npoonxaer ntnonnxrt xomant, nxoxmne n ero reno, noxa ne yer
ntnonneno saannoe ycnonne.
Hnxn while yxe ne pas ncrpeuancx n +ro xnnre. B cneymmem npnmepe on ncnontsyercx
nx nocrpounoro ntnoa coepxnmoro nxonoro ]ana:
Wh11e {<>} {
p1nf
}
Fonee cnoxnte nnxnt moryr ncnontsonart nnexc nnxna. Hanpnmep, +ror nnxn for nt-
uncnxer snauenne ]axropnana:
$facfo1a1 = 1
fo {$1oop1nd = 1 $1oop1nd <= 6 $1oop1nd++} {
$facfo1a1 "= $1oop1nd
}
p1nf "6! = $facfo1a1\n"
6! = 720
a +ror nnxn foreach nosnonxer nepepart yxasannt cnncox snauenn:
foeach $va1ab1e1 {"one", "fWo", "fhee"} {
p1nf "/$va1ab1e1/"
}
/one//fWo//fhee/
C nomomtm nnexca nnxna moxno cctnartcx na annte ns nexoroporo naopa, nocneo-
narentno opaartnax snauenne sa snauennem. B xauecrne npnmepa npnneem nnxn, nepe-
npammn +nemenrt maccnna:
0aay = {"one", "fWo", "fhee"}
fo {$1oop1nd = 0 $1oop1nd <= #aay $1oop1nd++} {
p1nf $aay|$1oop1nd] . " "
}
one fWo fhee
Oneparopt nnxna cnocont ntnonnxrt n onee cnoxnte ecrnnx rax, nox continue,
nomemennt n xonne nnxna, saaer nocneonarentnocrt onepann, xoropax ntnonnxercx n
nmom cnyuae (rounee, no:mu n nmom cnyuae) nocne sanepmennx ocnonno uacrn nnxna:
Crp.92
0aay = {"one", "fWo", "fhee"}
$1oop1nd = 0
Wh11e {$1oop1nd <= #aay} {
p1nf $aay|$1oop1nd] . " "
}
conf1nue {
$1oop1nd++
}
one fWo fhee
Komana redo, yxasannax n rene nnxna nnn pasene continue, nepeaer ynpannenne n nauano
rena nnxna (mnnyx sarononox nnxna), xomana last oecneunnaer ntxo ns nnxna, mnnyx pas-
en continue, xomana next nepeaer ynpannenne paseny continue c nocneymmnm nepexo-
om x nono nrepannn (ecnn rontxo n +rom pasene ner pyrnx xoman nepeaun ynpannennx
nnn nrepannx ne xnnxercx nocnene). Hpnmep pyunoe ynpannenne ntpoxenntm nnx-
nom for, nmnrnpymmee paory nnxna foreach $index (1, 2, 3, 4, 5):
fo {$1ndex = 1 $1ndex == 1 $1ndex = 1} {
1f {$1ndex > 5} {1asf} # auxo ua uxna
p1nf "he11o"
1f {$1ndex == 5} {nexf} # nepexo cpaay x conf1nue
p1nf "... "
}
conf1nue {
$1oop1ndex++ # uaueueuue uuexca u aepuy1ucn a uauano uxna, uruopupoaaa
edo # xouauu, yxaaauuue a aaronoaxe uxna
}
p1nf "!\n"
he11o... he11o... he11o... he11o.!
(Oparnre nnnmanne, xax xomana redo n noxe continue oxonr sarononox nnxna npn
nepeaue ynpannennx n nauano rena nnxna n npornnnom cnyuae nponepxa nnexca n
npncnoenne nnexcy ennnnt ncnoprnnn t nopmantnym paory +roro npnmepa.)
Ha +rom saxonunm c nneennem. Ho cymecrny, ycnonnte oneparopt nosnonxmr npnnnmart
pemennx no xoy ntnonnennx xoa, a oneparopt nnxna npeocrannxmr nosmoxnocrt
ocymecrnnxrt nonropxmmnecx onepannn c anntmn. Oa xnacca xoman xnnxmrcx mom-
ntmn nncrpymenramn. Ho+romy nocmorpnm, xax sacrannrt nx paorart.

Heuepneuume pemeuun
Ycnoeuu onepafop i
Oneparop if xnnxercx asontm ycnonntm oneparopom n Perl. On nponepxer ycnonne, sa-
annoe n xpyrntx cxoxax, n ecnn pesyntrar ntuncnenn aer nenynenoe snauenne, nt-
nonnxercx nox xoman, acconnnponannt c anno xomano. (Hycrte crpoxn, neonpe-
enennte nepemennte, snauennx undef n r. . n annom xonrexcre raxxe paccmarpnnamr-
cx xax nornuecxn nont.) Moxno raxxe saart nox xoman, ntnonnxemtx n cnyuae nox-
nocrn nponepxemoro ycnonnx. 3ro enaercx c nomomtm noxa else. Koncrpyxnnx elsif (o-
parnre nnnmanne: ne else if n ne elseif) npoonxaer nponepxy ononnnrentntx ycnonn.
Bor xax sannctnaercx +ra xomana:
1f {aupaeuue} {bnox}
Crp.93
1f {aupaeuue} {bnox} e1se {bnox}
1f {aupaeuue} {bnox} e1s1f {bnox} ... e1se {bnox}
Paccmorpnm npnmep. Oneparop nponepxn na panencrno onennnaer, panno nn snauenne yxa-
sanno nepemenno nxrn, n ecnn +ro rax, coomaer o pesyntrare nontsonarenm:
$va1ab1e = 5
1f {$va1ab1e == 5} {
p1nf "es, 1fs f1ve.\n"
}
es, 1fs f1ve.
Moxno oannrt ononnnrentnt pasen else, xoropt yer nn]opmnponart nontsona-
renx o rom, uro nponepxa ne npomna:
$va1ab1e = 6
1f {$va1ab1e == 5} {
p1nf "es, 1fs f1ve.\n"
} e1se {
p1nf "No, 1fs nof f1ve.\n"
}
No, 1fs nof f1ve.
Haxonen, nx ntnonnennx nponsnontnoro xonnuecrna nponepox moxno oannrt pasent elsif:
$va1ab1e = 2
1f {$va1ab1e == 1} {
p1nf "es, 1fs one.\n"
} } } } e1s1f {$va1ab1e == 2} { . e1s1f {$va1ab1e == 2} { . e1s1f {$va1ab1e == 2} { . e1s1f {$va1ab1e == 2} { .
p1nf "es, 1fs fWo. p1nf "es, 1fs fWo. p1nf "es, 1fs fWo. p1nf "es, 1fs fWo.\ \\ \n" n" n" n"
} e1s1f {$va1ab1e == 3} { } e1s1f {$va1ab1e == 3} { } e1s1f {$va1ab1e == 3} { } e1s1f {$va1ab1e == 3} {
p1nf "es. 1fs fhee. p1nf "es. 1fs fhee. p1nf "es. 1fs fhee. p1nf "es. 1fs fhee.\ \\ \n" n" n" n"
} e1s1f {$va1ab1e } e1s1f {$va1ab1e } e1s1f {$va1ab1e } e1s1f {$va1ab1e == == == == 4} { 4} { 4} { 4} {
p1nf "es, 1fs fou. p1nf "es, 1fs fou. p1nf "es, 1fs fou. p1nf "es, 1fs fou.\ \\ \n" n" n" n"
} e1s1f {$va1ab1e } e1s1f {$va1ab1e } e1s1f {$va1ab1e } e1s1f {$va1ab1e == == == == 5} { 5} { 5} { 5} {
p1nf "es, 1fs f1ve. p1nf "es, 1fs f1ve. p1nf "es, 1fs f1ve. p1nf "es, 1fs f1ve.\ \\ \n" n" n" n"
} e1se {
p1nf "5oy, cans mafch 1f!\n"
}
es, 1fs fWo
Koraupa unless
Komana unless xnnxercx xax t nsnanxo if: ona paoraer rax xe, no acconnnponannt c yc-
nonnem nox ntnonnxercx, ecnn ycnonne oxastnaercx .orim. Bor xax ntrnxnr +ra xomana:
un1ess {aupaeuue} {bnox}
un1ess {aupaeuue} {bnox} e1se {bnox}
un1ess {aupaeuue} {bnox} e1s1f {bnox} ... e1se {bnox}
Bor npnmep c ncnontsonannem nnxna while:
Wh11e {<>} {
chomp
un1ess {/q/1} {
p1nf
} e1se {
ex1f
}
}
3ra nporpamma neuaraer rexcr, xoropt nnonrcx nontsonarenem, ecnn rontxo nontsona-
Crp.94
rent ne nneer crpouxy, naunnammymcx c nnrep q nnn (coxpamenne or quit nnn QUIT).
B npornnnom cnyuae paora nporpammt npexpamaercx. Hponepxa ycnonnx ntnonnxercx sa
cuer cpannennx rexcra, xpanxmerocx n nepemenno $_, c manonom (manont n ycnonnx
connaennx rexcra c manonom paccmarpnnamrcx n rnane ).
Onepafop uukna or
Oneparop nnxna for ncnontsyercx nx nrepannonnoro ntnonnennx xoman, naxoxmnxcx
n rene nnxna. Otuno npn ntnonnennn nrepannn ncnontsyercx nepemennax-nnexc nnx-
na. Omn nn oneparopa nnxna for ntrnxnr xax:
ue1xa fo {aupaeuue1 aupaeuue2 aupaeuue3} {bnox}
Hepnoe ntpaxenne ntuncnxercx nepe nauanom nnxna. Bropoe ntpaxenne ntuncnxercx
nepe nauanom xaxo nrepannn, n ecnn ono oxastnaercx noxtm, ntnonnenne nnxna npe-
ptnaercx. Tperte ntpaxenne ntuncnxercx n xonne xaxo nrepannn. Merxa, ro ecrt
nenrn]nxarop, saxanunnammncx noerounem, ncnontsyercx nx nepeaun ynpannennx n
nauano nnxna n cnyuae, xora nopmantnoe ntnonnenne rena nnxna npeptnaercx (cm. onn-
canne xoman next, redo n last n nocneymmnx pasenax).
Hocuasua. Ec.u npu nepeo+ exooe e ur. npoeepe+oe vc.oeue orasieaemc .orui+, me.o ur.a ue ei-
no.uemc eoooue.
Hmeercx mnoxecrno nyre ncnontsonannx oneparopa nnxna for. Knaccnuecxn cnoco co-
cronr n rom, urot onpeennrt nepemennym nnxna n nsmenxrt ee snauenne c saanntm
marom, noxa ona ne nter sa nexoropte rpannnt. B cneymmem npnmepe mt ncnontsy-
em nepemennym nnxna $loop_ind, urot nxrt pas naneuarart crpoxy Hello!\n:
fo {$1oop1nd = 1 $1oop1nd <= 5 $1oop1nd++} {
p1nf "he11o!\n"
}
he11o! he11o! he11o! he11o! he11o!
He nospanxercx ncnontsonart necxontxo nepemenntx nnxna:
fo {$1oop1nd = 0, $doub1e fo {$1oop1nd = 0, $doub1e fo {$1oop1nd = 0, $doub1e fo {$1oop1nd = 0, $doub1e = = = = 0.0, $fexf = 0.0, $fexf = 0.0, $fexf = 0.0, $fexf = \ \\ \n n n n
$1oop1nd <= 4 $1oop1nd <= 4 $1oop1nd <= 4 $1oop1nd <= 4
$1oop1nd $1oop1nd $1oop1nd $1oop1nd++, $doub1e ++, $doub1e ++, $doub1e ++, $doub1e = = = = 2.0 " 1oop1nd} { 2.0 " 1oop1nd} { 2.0 " 1oop1nd} { 2.0 " 1oop1nd} {
p1nf "Loop 1ndex " . $1oop1nd . " doub1ed equa1s " . $doub1e . $fexf
}
Loop 1ndex 0 doub1ed equa1s 0
Loop 1ndex 1 doub1ed equa1s 2
Loop 1ndex 2 doub1ed equa1s 4
Loop 1ndex 3 doub1ed equa1s 6
Loop 1ndex 4 doub1ed equa1s
Ho sanepmennn nnxna moxno nponepnrt nepemennym-nnexc, urot ysnart, cxontxo t-
no ntnonneno nrepann:
$facfo1a1 = 1
fo {$1nd1oop = 1 $1nd1oop <= 6 $1nd1oop++} {
$facfo1a1 "= $1nd1oop
}
p1nf $1nd1oop-1 . "! = $\facfo1a1\n"
6! = 720
Hpeynpexeuue. Tar nocmvnami ue pero+euovemc, nocro.irv paoomocnocoouocmi maroeo rooa e no-
c.eovmuux eepcux Perl ue eapaumupvemc (mo ecmi nepe+euua ur.a e oa.iueue+ +orem ue coxpa-
umi ceoe noc.eouee sua:euue npu eixooe us ur.a).
Crp.95
Ecnn nt xornre, urot nepemennte nnxna tnn neocrynnt nne ero, ncnontsyre npn nx
onncannn cnenn]nxarop my:
$facfo1a1 = 1
fo {my $1nd1oop = 1 $1nd1oop <= 6 $1nd1oop++} {
$facfo1a1 "= $1nd1oop
}
(cm. raxxe pasen Vnpannenne onacrtm nnnmocrn (xnmuente cnona my n local) n rnane 7).
Ha camom ene, npn paore c for ner neoxonmocrn ynennunnart nnexc nnxna n npone-
pxrt ero snauenne. B cneymmem npnmepe ns nxonoro noroxa anntx STDIN cunrtna-
mrcx n ntnoxrcx crpoxn o rex nop, noxa ne ncrpernrcx crpoxa, naunnammaxcx c yxn q
nnn . (uro xnnxercx coxpamennem nx quit nnn QUIT). Oparnre nnnmanne, uro nxono
rexcr neoxonmo npncnonrt npomexyrouno nepemenno $line, nocxontxy nno ns nxo-
noro noroxa nanpannxercx no ymonuannm n nepemennym $_ rontxo n cnyuae nnxna while:
fo {$11ne = <> $11ne = /q/1 $11ne = <> } {
p1nf $11ne
}
Oneparop nnxna foreach, paccmarpnnaemt n cneymmem pasene, nnora ncnontsyercx nen-
rnuno oneparopy nnxna for (no cyrn ena n Perl onn ntnonnxmr onn n re xe ecrnnx). B cne-
ymmem npnmepe oneparop nnxna foreach nonnonenno paoraer c nepemenno-nnexcom nnxna:
foeach foeach foeach foeach {$1oop1nd = {$1oop1nd = {$1oop1nd = {$1oop1nd = 1 $1oop1nd 1 $1oop1nd 1 $1oop1nd 1 $1oop1nd <= <= <= <= 5 $1oop1nd++} { 5 $1oop1nd++} { 5 $1oop1nd++} { 5 $1oop1nd++} {
p1nf "he11o!\n"
}
he11o!
he11o!
he11o!
he11o!
he11o!
Haxonen, paccmorpnm raxxe npnmep, xora oneparop for paoraer nanoone foreach:
0aay = {"he11o ", "fhee.\n"}
f ff fo {0aay} {p1nf} o {0aay} {p1nf} o {0aay} {p1nf} o {0aay} {p1nf}
he11o fhee.
Onepafop uukna oreach
B Perl oneparop nnxna foreach xnnxercx cnnonnmom for. Onaxo, xora rpeyercx, urot
nepemennax nnxna nocneonarentno nepenpana annte ns nexoroporo saannoro cnncxa
snauenn, nporpammncrt sauacrym ncnontsymr n xnnom nne oneparop foreach (urot
nouepxnyrt, uro cmtcn nnxna ntpaxaercx ]paso Ior each element in...). 3ror onepa-
rop nnxna sannctnaercx xax:
ue1xa foeach nepeueuuan {cnucox} {bnox}
Bo npemx nrepannn ns cnncxa nsnnexaercx ouepenoe snauenne, npncnannaercx nepemen-
no n ntnonnxercx reno nnxna. Hnxn sanepmaercx, xora cnncox ncuepnan. Kax n pantme,
merxa ncnontsyercx nx nepeaun ynpannennx n nauano nnxna n cnyuae, xora nopmantnoe
ntnonnenne ouepeno nrepannn npeptnaercx (cm. onncanne xoman next, redo n last n
nocneymmnx pasenax).
Ecnn n sarononxe nnxna ne saano nmx nepemenno, ncnontsyercx nmx $_ (+ro yono npn
paore c ]ynxnnxmn, ncnontsymmnmn $_ xax aprymenr no ymonuannm nanpnmep, print):
Crp.96
0aay = {"he11o ", "fhee.\n"}
foeach {0aay} {p1nf} foeach {0aay} {p1nf} foeach {0aay} {p1nf} foeach {0aay} {p1nf}
he11o fhee.
Ecnn nepe nepemenno nnxna yxasano xnmuenoe cnono my, ro ona yer onpeenena rontxo
nnyrpn rena nnxna. Ecnn my orcyrcrnyer, nepemennax nce panno yer nmert noxantnym o-
nacrt nnnmocrn a nmenno, npn ntxoe ns nnxna ee nocnenee (nnyrpn rena nnxna) snaue-
nne repxercx, n nepemennax noccranannnnaer snauenne, xoropoe ona nmena nepe nxoom n
nnxn. Coornercrnenno, ncnontsonanne nepemenno nnxna (c xnmuentm cnonom my nnn es ne-
ro) nx nonporpamm n ]opmaron, onpeenenntx nnyrpn nnxna, moxer npnnecrn x omnxam.
Oneparop foreach moxno ncnontsonart n nx nrepannn no coepxnmomy x+ma, ncnontsyx
pesyntrar paort ]ynxnnn keys nnn values:
$hash{fu1f} = oange
$hash{sandW1ch} = c1ubbuge
$hash{d1nk} = 1emonade
foeach $key {keys xhash} {
p1nf $hash{$key} . "\n"
}
1emonade
c1ubbuge
oange
V nnxna foreach ecrt ona ocoennocrt: nmecro roro urot npuceaueami nepemenno
snauenne ns cnncxa, nepemennax cranonnrcx cuuouu+o+ +roro snauennx. B uacrnocrn, ec-
nn ouepentm +nemenrom cnncxa xnnxercx nmx nepemenno, ro nsmenenne snauennx ne-
pemenno nnxna nsmennr n +ry nepemennym. Ecnn n xauecrne cnncxa ntcrynaer nepemen-
nax-maccnn, ro nsmenennx cxastnamrcx na ee coepxnmom:
0aay = {1, 2, 3}
foeach $e1emenf {0aay} {
$e1emenf += 1
}
p1nf o1n{", ", 0aay}
2, 3, 4
He cneyer nsmenxrt crpyxrypy cnncxa, ncnontsyemoro xax aprymenr xomant foreach, n
nponecce paort nnxna (nanpnmep, c nomomtm ]ynxnnn splice, npnmenenno x maccnny,
yxasannomy nmecro cnncxa) n npornnnom cnyuae nnxn, cxopee ncero, yer paorart
nenpannntno. Ananornuno, ne cronr ncnontsonart n xauecrne nepemenno nnxna cnenn-
antnte nepemennte Perl nnn nmena, cnxsannte c pyrnmn oexramn (c nomomtm ]ynx-
nnn tie nnn meroa TIESCALAR, nanpnmep).
uynxnnx each nosnonxer oprannsontnart paory c x+mamn n crnne, nanomnnammem nt-
nonnenne nnxna foreach. Ona nocneonarentno nosnpamaer napt xnmu/snauenne, xpanx-
mnecx n x+me, xax noxasano n cneymmem npnmepe:
$hash{fu1f} = oange
$hash{sandW1ch} = c1ubbuge
$hash{d1nk} = 1emonade
Wh11e {{$key, $va1ue} = each{xhash}}
{ p1nf "$key => $va1ue\n" }
d1nk => 1emonade
fu1f => oange
sandW1ch => c1ubbuge
Crp.97
Onepafop uukna hile
Oneparop while nrpaer naxnym pont n Perl. Bor xax on ntrnxnr:
ue1xa Wh11e {aupaeuue} {bnox}
ue1xa Wh11e {aupaeuue} {bnox} conf1nue {bnox`}
Teno nnxna ntnonnxercx, noxa ntpaxenne n sarononxe nnxna ocraercx ucmuuui+ (nepe
xaxtm ntnonnennem rena nnxna ono ntuncnxercx nonropno). Merxa ncnontsyercx nx
nepeaun ynpannennx n nauano nnxna n cnyuae, xora nopmantnoe ntnonnenne rena nnxna
npeptnaercx (cm. onncanne xoman next, redo n last n nocneymmnx pasenax).
Cneymmn npnmep cymmnpyer oxot nontsonarenx o rex nop, noxa cymma ne npent-
cnr mnnnnon
1
:
$sav1ngs = 0
Wh11e {$sav1ngs < 1000000} {
p1nf "Lnfe fhe amounf you eaned foday: "
$sav1ngs +- <>
} p1nf "Congafu1af1ons, m1111ona1e.\n"
A n +rom npnmepe (on ncnontsyercx raxxe n npetymem pasene) nnxn while npnmenxer-
cx nx nepeopa ncex snauenn x+ma c nomomtm ]ynxnnn each, nosnpamamme npn xax-
om opamennn ouepenym napy xnmu/snauenne:
$hash{fu1f} = oange
$hash{sandW1ch} = c1ubbuge
$hash{d1nk} = 1emonade
W WW Wh11e h11e h11e h11e {{$key {{$key {{$key {{$key, , , , $va1ue} = each{xhash}} $va1ue} = each{xhash}} $va1ue} = each{xhash}} $va1ue} = each{xhash}}
{ p1nf "$key => $va1ue { p1nf "$key => $va1ue { p1nf "$key => $va1ue { p1nf "$key => $va1ue\ \\ \n" n" n" n" } } } }
d1nk => 1emonade
fu1f => oange
sandW1ch => c1ubbuge
Cnennantnax ]opma nnxna nnxn while (<>) nmeer nonesnoe cnocrno: nnyrpennxx
nepemennax Perl $_ anromarnuecxn sanonnxercx anntmn, nocrpouno nnonmtmn uepes
cranaprnt norox nnoa. 3ro osnauaer, uro moxno c nontso npnmenxrt mnorouncnen-
nte ]ynxnnn Perl, ncnontsymmne nepemennym $_ xax aprymenr no ymonuannm:
Wh11e {<>} { p1nf }
Fnox continue, ecnn on saan, ntnonnxercx ncxxn pas, xora reno nnxna ntnonneno non-
nocrtm nnn uacrnuno (cm. anee pasen Komana next: xax nepern x cneymme nrepa-
nnn) nepe ouepeno nponepxo ycnonnx nnxna. Hanpnmep, c nomomtm noxa continue
moxno sacrannrt nnxn while necrn cex nanoone for:
$1oop1ndex = 1
Wh11e {$1oop1ndex <= 5} {
p1nf "he11o!\n"
} conf1nue
{ $1oop1ndex++ }
he11o!
he11o!
he11o!
he11o!
he11o!

1
Boome-ro ronopx, nx paorocnoconocrn annoro npnmepa neoxonmo yanxrt xonen crpoxn nocne nnoa sna-
uennx c repmnnana ($_ <>; chomp $_;). Kpome roro, nocroxnnoe ncnontsonanne anropom chop nmecro chomp
roxe ypno crnnt. Hpu+e:. peo.
Crp.98
Oneparop while nponepxer ycnonne nepeo ntnonnennem rena nnxna, rax uro ono moxer
noome nn pasy ne ntnonnnrtcx. 3ro yono, ecnn reno nnxna oprannsonano raxnm opa-
som, uro ero ntnonnenne npn napymennom ycnonnn nnxna moxer ntstnart nponemt.
Hanpnmep, n cneymmem npnmepe nporpamma ne yer neuarart crpoxn, nnonmte ns
]ana, ecnn ecxpnnrop ]ana FileHandle ne cnxsan nn c onnm ]anom:
Wh11e {<I11ehand1e>} { Wh11e {<I11ehand1e>} { Wh11e {<I11ehand1e>} { Wh11e {<I11ehand1e>} {
p1nf
}
Onepafop uukna until
Oneparop until ntnonnxer re xe ]ynxnnn, uro n while, sa rem ncxnmuennem, uro nx nt-
nonnennx rena nnxna rpeyercx, urot nponepxemoe ycnonne tno .orim. 3ror nnxn sa-
nnctnaercx xax:
ue1xa unf11 {aupaeuue} {bnox}
ue1xa unf11 {aupaeuue} {bnox} conf1nue {bnox}
Ho ananornn c npnmepom ns npetymero pasena, naneuaraem nxrt pas nopx c nomo-
mtm oneparopa nnxna until rexcr "Hello!\n":
$1oop1ndex = 1
unf11 unf11 unf11 unf11 {$1oop1ndex > 5} { {$1oop1ndex > 5} { {$1oop1ndex > 5} { {$1oop1ndex > 5} {
p1nf "he11o!\n"
} conf1nue
{ $1oop1ndex++ }
he11o!
he11o!
he11o!
he11o!
he11o!
Mopuqukafopu i, unless, until, hile u or
Kpome ]opmantntx xoncrpyxnn ycnonntx oneparopon n oneparopon nnxna Perl nosnonxer
oannxrt mon]nxaropt c ananornuntmn ]ynxnnxmn n xonen nmo cranaprno xomant:
1f aupaeuue
un1ess aupaeuue
Wh11e aupaeuue
unf11 aupaeuue
fo {cnucox}
foeach {cnucox}
Paora +rnx mon]nxaropon n snaunrentno crenenn nonropxer paory cranaprntx one-
paropon nnxna n ycnonnoro oneparopa, no onn sauacrym oneruamr urenne xoa. Hanpn-
mep, nor xax neuaraercx coomenne Too big!\n c nomomtm mon]nxaropa if, ecnn nont-
sonarent nnonr uncno ontme cra:
Wh11e {<>} {
p1nf "1oo b1g!\n" 1f $ > 100
}
B cneymmem npnmepe mon]nxarop unless ncnontsyercx, urot ntnecrn coomenne o
omnxe n ntrn ns nporpammt, ecnn orxptrt ]an nenosmoxno:
Crp.99
d1e "Cannof open fhe f11e.\n" un1ess open{$f11ename}
Touno rax xe, nanpnmep, nx neuarn nxonoro noroxa nmecro nnxna while c renom, cocrox-
mnm ns ono xomant print, moxno ncnontsonart xomany print c mon]nxaropom while:
p1nf Wh11e {<>}
Mon]nxaropt for n foreach (xoropte ornnuamrcx pyr or pyra rontxo sannctm, no ne
ecrnnem) rpeymr ononnnrentnoro pasxcnennx. B ornnune or onepamopoe for n foreach,
+oouquramopi for n foreach ne nosnonxmr saanart nepemennym-nnexc nx +ro nenn
ncera ncnontsyercx cnennantnax nepemennax $_, nopasymenaemax no ymonuannm, a n xaue-
crne ycnonnx, onpeenxmmero nrepannn, cnyxnr cnncox snauenn, a ne rpoxa xoman. Bo
ncem ocrantnom paora mon]nxaropon ne ornnuaercx or paort oneparopon nnxna:
p1nf $ foeach {1, 2, 3, 4, 5, 6, 7, }
1234567
Hocuasua. Ocooeuuocmu paoomi +oouquramopoe while u until c ro+auoo do pacc+ampueammc e c.e-
ovmue+ pasoe.e.
Kak cospafu uukn do hile
Mnorne nporpammncrt ymamr, uro ecnn n xstxe nporpammnponannx ecrt nnxn while, ro
onxen trt n nnxn do while. Onaxo n Perl +ro ne rax. Tounee, ner orentnoro nnxna do
while, no ecrt xomana do, xoropax sannctnaercx xax:
do {bnox}
do nonporpauua {napaue1pu} # he pexoueuye1cn
do aupaeuue
Komana do nox xouan] ntnonnxer yxasannym nocneonarentnocrt xoman n nos-
npamaer snauenne, coornercrnymmee nocnene ntnonnenno xomane. Komana do no-
nporpauua (napaue1pm) ntnonnxer ntson nonporpammt, n ee pexomenyercx samenxrt
na onee cranaprnym xomany call (nnn o]opmnxrt ntson nonporpammt n nne noxa).
Komana do nmpaaenne nnrepnpernpyer ntpaxenne xax nmx ]ana (nanpnmep, do my-
script.pl) n ntnonnxer norox xoman, coepxamnxcx n +rom ]ane.
Ecnn ncnontsonart xomany do c mon]nxaropom while, ro mt nonyunm xoncrpyxnnm,
nmnrnpymmym noneenne nnxna do while:
do { p1nf }
Wh11e {<>}
Heoxonmo ormernrt, uro xomana do c mon]nxaropom while yer ntnonnena, no xpa-
ne mepe, onn pas, ro ecrt axe n rom cnyuae, ecnn ycnonne oxastnaercx .orim c camoro
nauana (+ro onn ns npnmepon uyncrnnrentnocrn xoncrpyxnn Perl x xonrexcry, n xoropom
onn ncnontsymrcx). Hanpnmep, n ornnune or xomant print "AB" while (0);, xoropax ne
yer ntnonnena nn pasy, xomana do print "AB"] while (0); naneuaraer rexcr.
Hocuasua. To:uo maru+ re oopaso+ coe+ecmuo c onepamopo+ do paoomaem u +oouquramop until. A
u+euuo, ro+auoa do ]print "ABC"} until (1); uane:amaem mercm "ABC", xom u oouu pas.
Onaxo neoxonmo nouepxnyrt, uro raxne xoncrpyxnnn, rem ne menee, ne xnnxmrcx na-
croxmnmn oneparopamn nnxna. B uacrnocrn, xomant next, redo n last, ynpannxmmne
ntnonnennem nnxna n onncannte n cneymmnx pasenax, ne yyr paorart n cnyuae xo-
mant do c mon]nxaropom while nnn until.
Crp.100
Koraupa net: kak nepefu k cnepyke ufepauuu
Komana next, yxasannax nnyrpn nnxna, nosnonxer nemenenno nauart cneymmym nrepa-
nnm, nponycxax xomant, xoropte moryr naxonrtcx nocne nee n rene noxa. Kax n nonoxe-
no, nepe nauanom nrepannn nponepxercx ycnonne, yxasannoe n sarononxe nnxna (nnxnt
while, until n for), nsmenen nnexc nnxna (nnxn for), cenana ntopxa cneymmero +nemenra
cnncxa (foreach) n r. . Ecnn xomana next nepeaer ynpannenne cneymme nrepannn nnxna
while nnn until, nx xoroporo nmeercx pasen xoman continue, ro nox xoman continue y-
er ntnonnen o ouepeno nponepxn ycnonnx nnxna n cneymme nrepannn.
Komana next ncnontsyercx c nenrn]nxaropom-merxo n xauecrne napamerpa, urot
yxasart, x xaxomy nmenno nnxny ona ornocnrcx (merxa, sa xoropo cneyer noeroune,
onxna trt yxasana nepe coornercrnymmnm oneparopom nnxna). Ecnn merxa onymena,
oneparop next yer coornercrnonart rexymemy nnxny (ro ecrt nnxny c camtm rnyoxnm
yponnem nnoxennocrn).
paccmorpnm cneymmn npnmep, n xoropom mt neuaraem nnonmte nontsonarenem an-
nte, ecnn rontxo onn ne xnnxmrcx orpnnarentntmn (orpnnarentnoe nneennoe snauenne
onennnaercx no snaxy mnnyca nepe nnm):
NuM8Lk NuM8Lk NuM8Lk NuM8Lk: Wh11e {<>} { : Wh11e {<>} { : Wh11e {<>} { : Wh11e {<>} {
nexf NuM8Lk 1f / nexf NuM8Lk 1f / nexf NuM8Lk 1f / nexf NuM8Lk 1f /- -- -/ / / /
p1nf
}
B +1ou npnmepe, ecnn crpoxa naunnaercx c mnnyca, mt nepexonm x cneymme nrepa-
nnn, mnnyx xomany print.
B Perl paspemaercx nepern x cneymme nrepannn nmoro nnemnero nnxna, ecnn nx nero on-
peenena merxa n nt naxonrect n rene nnxna. (B +rom ornnune Perl or C, n xoropom paspemaercx
nepeanart ynpannenne rontxo nnxamemy nnxny). Hanpnmep, naxoxct nnyrpn nnoxennoro
nnxna INNE, moxno cpasy nepern x cneymme nrepannn nnemnero nnxna UTE:
Ou1Lk: fo {$oufe = 0 $oufe < 10 $oufe++} {
$esu1f = 0
lNNLk: fo {$1nne = 0 $1nne < 10 $1nne++} {
$esu1f += $1nne " $oufe
nexf Ou1Lk 1f $1nne == $oufe
p1nf "$esu1f\n"
}
}
Koraupa last: kak npepeafu eunonueuue uukna
Komana last nemenenno npexpamaer ntnonnenne nnxna (noono xomane break xstxa
C). Ecnn ecrt nox xoman continue, ro on raxxe ne ntnonnxercx.
cneymmem npnmepe nnxn while ncnontsyercx nx yanennx ns ]ana nauantntx xom-
menrapnen, npnuem xomana last npeptnaer nnxn, ecnn crpoxa naunnaercx ne c cnmnona :
5f1p fh1s 11ne
5f1p fh1s 11ne foo
COMMLN15: Wh11e {<>} {
1asf COMMLN15 1f !/#/
}
do { p1nf }
Crp.101
Wh11e {<>}
Ecnn sanycrnrt +ry nporpammy, noan n xauecrne nxonoro ]ana ee cocrnennt rexcr
(nanpnmep, napan n xomanno crpoxe perl strip.pl strip.pl), ro na ntxoe nonyunm:
COMMLN15: Wh11e {<>} {
1asf COMMLN15 1f !/#/
}
do { p1nf }
Wh11e {<>}
Koraupa redo: kak eepuyfucn k uaany ufepauuu
Komana redo naunnaer rexymym nrepannm es nponepxn ycnonnx, yxasannoro n sarononxe
nnxna (nnxnt while, until n for), es npnpamennx nepemenno nnxna (nnxn for), es ntopxn
ns cnncxa ouepenoro snauennx (nnxn foreach) n r. . Btnonnenne nnxna npoonxaercx, rax
xax ecnn t rexyme nrepannn eme ne tno (xorx, ecrecrnenno, nce nsmenennx snauenn ne-
pemenntx, cenannte no npemx rexyme nrepannn, coxpanxmrcx). Ecnn y nnxna nmeercx
pasen continue (nnxnt while nnn until), yxasannte n nem xomant raxxe nponycxamrcx.
Hpenonoxnm, uro y nac nmeercx cnenapn Perl (]an c nmenem code.pl), n xoropom n xa-
uecrne cnmnona npoonxennx crpoxn ncnontsyercx snax nouepxnnannx (uro sanpemeno
cnnraxcncom Perl):
fo {$1oop1ndex = 0
$1oop1ndex <= 10
$1oop1ndex++} {
p1nf $1oop1ndex }
urot npounrart ]an code.pl, oennnrt xomant, pasnrte na orentnte crpoxn, n
ntnonnnrt nonyunnmymcx nporpammy c nomomtm xomant Perl eval, moxno ncnontso-
nart cneymmn cnenapn:
Wh11e {<>} {
1f {s///g} # Mafch and emove undescoes
{ $ .= <>
edo }
eva1
}
Ecnn +ror cnenapn nomecrnrt n ]an evaluate n ntnonnnrt c ero nomomtm xo, coep-
xamncx n ]ane code.pl, ro mt nonyunm:
xeva1uafe code.p1
01234567910
Hocuasua. Bo.ee noopoouo o ro+auoe eval paccrasieaemc oa.ee e omo e.aee e pasoe.e Bino.ueuue
rooa Perl c no+ouim ro+auoi eval.
Enok koraup kak onepafop uukna
Xorx orentnt nox xoman ]opmantno n ne xnnxercx nnxnom, n rene noxa moxno nc-
nontsonart re xe camte xomant next, redo n last, xoropte ynpannxmr n ntnonnennem
nnxnon. Cnnraxcnc Perl onycxaer xoncrpyxnnn nna
ue1xa {bnox}
ue1xa {bnox} conf1nue {bnox`}
Crp.102
re nenrn]nxarop-merxa (saxanunnammncx noerounem) moxer ncnontsonartcx xoman-
amn nepeaun ynpannennx next, redo, last n axe xomano goto.
Ecnn nx noxa yxasana xoncrpyxnnx continue, ro, xax n n cnyuae nnxnon while n until,
acconnnponannt c nem naop xoman ntnonnnrcx axe n rom cnyuae, xora nopmantnoe
ntnonnenne rena noxa npepnano c nomomtm xomant next. Ecnn xe ntnonnenne noxa
npeptnaercx xomanamn redo nnn last, pasen continue yer nponymen. Komana redo,
yxasannax n rene noxa nnn pasene continue, nepeaer ynpannenne n nauano noxa, xo-
mana last oecneunnaer ntxo ns noxa, mnnyx pasen continue, xomana next nepeaer
ynpannenne paseny continue c nocneymmnm ntxoom ns noxa (ecnn rontxo n +rom pas-
ene ner pyrnx xoman nepeaun ynpannennx).
Hpnmep (nmnrannx nnxna for):
$1ndex = 1
51Ak1: {
1f {$1ndex > 5} {1asf 51Ak1}
p1nf "he11o"
1f {$1ndex == 5} {nexf 51Ak1}
p1nf "... "
} conf1nue {
$1ndex++
edo 51Ak1
}
p1nf "!\n"
he11o... he11o... he11o... he11o... he11o.!
Ecnn nocne xoman next, redo nnn last saan nenrn]nxarop-merxa (es noerounx), ro
+ra xomana nepeaun ynpannennx ornocnrcx x noxy nnn nnxny, utx merxa yxasana. Ecnn
nepeaua ynpannennx ornocnrcx x nnxamemy noxy, nnyrpn xoroporo naxonrcx xo-
mana, merxy yxastnart neoxsarentno.
Cospauue nepeknkafene sitch
Oneparopt switch cpannnnamr nponepxemoe snauenne c naopom pyrnx snauenn, n n sann-
cnmocrn or pesyntraron cpannennx ntnonnxmr ror nnn nno ]parmenr xoa. B Perl ner
ncrpoennoro oneparopa switch
1
, no nt moxere cosart ero samennrent, ncnontsyx pyrne
xoncrpyxnnn Perl.
Hanpnmep, nx nmnrannn oneparopa switch moxno ncnontsonart noxn xoa:
Wh11e {<>} {
5Wl1Ch: {
/un/ do {
$message = "kunn1ng\n"
1asf 5Wl1Ch
}
/sfop/ do {
$message = "5fopped\n"
1asf 5Wl1Ch
}
/connecf/ do {
$message = "Connecfed\n"

1
Bepoxrno, raxo oneparop noxnnrcx n cneymme nepcnn Perl. Hpu+e:. peo.

Crp.103
1asf 5Wl1Ch
}
/f1nd/ do {
$message = "Iound\n"
1asf 5Wl1Ch
}
DLIAuL1:
$message = "No mafch.\n"
}
}
3ect ncnontsyercx cnocrno tcrpo paort oneparopa : ecnn nepnt onepan
oxastnaercx .orim, nropo ne ntuncnxercx. nx cpannennx nepemenno $_ c pasnnunt-
mn napnanramn snauenn (run, stop, connect n find) ncnontsyercx cpannenne no
manony. Hocxontxy n Perl nox xoa npecrannxer coo nnxn, ncnonnxemt onn pas,
xomana last oecneunnaer ntxo ns noxa. Ecnn nontsonarent nneer ono ns cnon run,
stop, connect nnn find, yer ntneeno coornercrnymmee coomenne.
Hocuasua. pveo no.esui cnocoo cosoami xou u cpaeuueami npoeepe+oe sua:euue c eeo r.m:a+u.
B oxymenrannn, npnnarammecx x Perl, nt naere n pyrne npnmept xoncrpyxnn, same-
mammnx oneparop switch. (Kax npannno, nanonee npospauno xoncrpyxnne nx nmnra-
nnn paort oneparopa switch yer ncnontsonanne ycnonnoro oneparopa if-elsif-...-elsif-else.)
Onepafop esycnoeuo nepepau ynpaeneuun goto
B Perl ecrt xomana goto, no sect ona onnctnaercx no ontme uacrn pan nonnort n
saxonuennocrn onncannx xstxa. Otuno npnmenenne goto ne xnnxercx ouent yx xopome
nee npexe ncero, noromy, uro Perl oecneunnaer xopomn naop antrepnarnnntx
xoman ntxoa ns nnxna nnn noxa. Ecnn nonarartcx na goto, ro moxno nenapoxom cos-
art nepexot, xoropte yer ouent rpyno orcnexnnart, rax xax onn nnesanno nepea-
mr ynpannenne conepmenno nonomy xonrexcry.
Hmemrcx rpn napnanra xomant goto:
gofo ue1xa
gofo aupaeuue
gofo nonporpauua
B nepnom cnyuae ynpannenne nepeaercx xomane, nepe xoropo pacnonoxena coorner-
crnymmax merxa (ro ecrt nenrn]nxarop, nomeuammn onpeenennym xomany npo-
rpammt n orenennt or +ro xomant noerounem). Ecnn saano ntpaxenne, ro pesynt-
rarom ero ntuncnennx onxna trt ona ns cymecrnymmnx merox. Haxonen, nocnenxx
]opma oneparopa goto ncnontsyercx nx nonporpamm.
Paccmorpnm onee nopono, xax merxn paoramr. B ontmnncrne cnyuaen n Perl merxa
+ro na camom ene ne merxa, a nmx oneparopa nnxna, nocxontxy nox moxer paccmarpn-
nartcx xax onoxparno ntnonnxemt nnxn. Oneparop goto naxonr raxym merxy n nepe-
aer ynpannenne n nauano coornercrnymmero oneparopa nnn noxa oneparopon. He pas-
pemaercx nepeanart ynpannenne nnyrpt xoncrpyxnn, rpeymmnx nnnnnannsannn (no-
nporpammt, oneparopt nnxna, orxomnnnnponannte nnemnne moynn n r. .). Onaxo, na-
npnmep, moxno ncnontsonart oneparop goto nx nepeaun ynpannennx nsnyrpn nonpo-
rpammt nnemnemy noxy (xorx, ecrecrnenno, nyume +roro ne enart). B cneymmem npn-
mepe oneparop goto ncnontsyercx nx cosannx ecxoneunoro nnxna, unrammero nnon-
Crp.104
mte snauennx o rex nop, noxa nontsonarent ne naeper cnono exit:
lNPu1: $11ne = <>
1f {$11ne ! /ex1f 1f {$11ne ! /ex1f 1f {$11ne ! /ex1f 1f {$11ne ! /ex1f/} {p1nf "1y aga1n /} {p1nf "1y aga1n /} {p1nf "1y aga1n /} {p1nf "1y aga1n\ \\ \n" n" n" n" gofo lNPu1} gofo lNPu1} gofo lNPu1} gofo lNPu1}
Bropax ]opma oneparopa goto nopasymenaer n xauecrne aprymenra nmx merxn (rexcronoe
ntpaxenne), ntuncnxemoe n nponecce ntnonnennx nporpammt. B uacrnocrn, c ero nomo-
mtm moxno nmnrnponart nnexcnpyemt oneparop goto xstxa FORTRAN:
gofo {"LA8LL1, "LA8LL2", "L8LIlNAL"} |$1ndex]
Haxonen, rpertx ]opma goto npecrannxer coo nexn marnuecxn rpmx n npn otuntx
ocroxrentcrnax npx nn yer saecrnonana. Hpn ntsone xomant goto &noonpoepa++a
nponcxonr nepeaua ynpannennx coornercrnymme nonporpamme, onaxo maccnn _,
coepxamn ee aprymenrt, ne ]opmnpyercx (cm. rnany 7 ornocnrentno nonporpamm Perl)
n apec nosnpara ne sanomnnaercx. Tem camtm +ror oneparop moxer ncnontsonartcx ront-
xo nnyrpn nonporpammt, urot cxptrtm (nx rouxn ntsona) opasom nepeart ynpan-
nenne pyro, nsnauantno ne ntstnaemo nontsonarenem nonporpamme. Bce mon]nxa-
nnn maccnna _ coxpanxmrcx npn nepeaue ynpannennx nono nonporpamme. Hpn ntxo-
e ns nonporpammt, yxasanno n oneparope goto, ynpannenne nosnpamaercx rouxe ntsona
nepno nonporpammt. Cronr nouepxnyrt, uro n noannxmmem ontmnncrne cnyuaen cne-
yer ncnontsonart crpyxrypnte oneparopt ynpannennx xoom nporpammt Perl (ycnonnt
oneparop if-then-else, oneparopt nnxna for, foreach n while, oneparopt next, last n redo, ne-
pexnar ncxnmunrentntx cnryann c nomomtm napt xoman eval ] n die (), ycnonnte mon-
]nxaropt xoman n r. .). Onn cnennantno onrnmnsnponant nx paort nnrepnperaropa Perl
n, nomnmo ynyumennx crpyxrypt nporpammt, samerno nnnxmr na cxopocrt ntnonnennx.
Bunonueuue kopa Perl c norouk koraupu eal
urot ntnonnnrt ]parmenr xoa Perl, ncnontsyercx xomana eval:
eva1 aupaeuue
eva1 {bnox}
Ecnn aprymenr ne saan, ncnontsyercx ntpaxenne, coepxameecx n nepemenno Perl $_.
Hanpnmep, rax ntnonnnm xomany print Hellon:
eva1 "p1nf \"he11o\n\""
he11o
(sect nnyrpennne nonte xantuxn saant, ecrecrnenno, uepes escape-nocneonarentnocrn).
Bt moxere ntnonnnrt sa onn pas necxontxo xoman nnn axe nent cnenapn Perl:
eva1 "p1nf \"he11o \"
p1nf \"fhee\n\""
he11o fhee
Bor xax nnrepaxrnnno ntnonnxmrcx xomant Perl, ecnn rontxo onn sannmamr ne onee o-
no crpoxn:
Wh11e {<>} {eva1}
Ecnn nosnnxaer omnxa, coomenne o ne sanocnrcx n nepemennym $, uro oecneunnaer
yont cnoco opaorxn omnox nnrepnperaropa. (B uacrnocrn, ncnontsonanne xo-
mant eval pexomenonano nx opaorxn n Perl npeptnann n ncxnmunrentntx cnrya-
nn.) anee n +ro xnnre nt ysnaere ontme o pasnnuntx npnmenennxx xomant eval.
Crp.105
Buxop us nporparru c norouk koraupu eit
Komana exit sanepmaer paory nporpammt:
ex1f aupaeuue
mpaaenne (ecnn saano) ncnontsyercx n xauecrne xoa sanepmennx nporpammt. Cne-
ymmn npnmep noxastnaer, xax pasopnart ecxoneunt nnxn oxnannx nnoa yxnt y:
p1nf "P1ease fype fhe 1effe y\n"
Wh11e {<>} {
chop
1f {$ ne y} {
p1nf "P1ease fype fhe 1effe y\n"
} e1se {
p1nf "Do you a1Ways do Whaf youe fo1d p1nf "Do you a1Ways do Whaf youe fo1d p1nf "Do you a1Ways do Whaf youe fo1d p1nf "Do you a1Ways do Whaf youe fo1d\ \\ \n" n" n" n"
ex1f ex1f ex1f ex1f
}
}
Buxop us nporparru c norouk koraupu die
Komana die npenasnauena nx ntxoa ns nporpammt n cnyuae nosnnxnonennx omnox n
pyrnx nenpennenntx cnryann. Ona neuaraer n ntxono norox STDE cnncox sna-
uenn, yxasannt n xauecrne ee napamerpa, n sanepmaer paory nporpammt:
d1e cnucox
Kpome ocranonxn nporpammt, xomana die nosnparnr n xauecrne xoa sanepmennx rexy-
mee snauenne cnennantno nepemenno Perl $!. Ecnn die ntnonnxercx c nomomtm eval, ro
coomenne o omnxe nomemaercx n cnennantnym nepemennym $ (a ne n STDE), a
nmecro npexpamennx paort cnenapnx npexpamaercx nnmt ntnonnenne xomant eval.
B cneymmem npnmepe enaercx nontrxa orxptrt necymecrnymmn ]an:
$fname = "nonex1sf.p1"
open I11ehand1e, $fname o d1e "Cannof open $fname open I11ehand1e, $fname o d1e "Cannof open $fname open I11ehand1e, $fname o d1e "Cannof open $fname open I11ehand1e, $fname o d1e "Cannof open $fname\ \\ \n" n" n" n"
3ror cnenapn sanepmaercx c coomennem o omnxe:
Cannof open nonex1sf.p1
Crp.106
Fnana 6
Perynnpume nmpaxeuun
Kopo1ko
Paora c rexcrom +ro ro, n uem Perl npenocxonr pyrne nporpammt. Perynxpnte nt-
paxennx oecneunnamr snaunrentnym uacrt nosmoxnocre Perl no opaorxe rexcron.
Onn nosnonxmr conocrannxrt rexcr c yxasanntm manonom (a nmenno, cpannnnart ne
crpoxn c nomomtm ynnnepcantntx cnmnonon, nnrepnpernpyemtx cnennantntm opasom)
n ntnonnxrt sameny rexcra. Taxnm opasom, Perl npeocrannxer momnt nncrpymenr nx
mannnynnponannx rexcrom no ynpannennem nporpammt nontsonarenx.
C pyro cropont, ne nonexnr comnennm, uro perynxpnte ntpaxennx Perl +ro ona
ns rex onacre, xoropte rpeymr ceptesntx ycnnn co cropont nporpammncra. 3auac-
rym rpeyercx npemx nx roro, urot pasopartcx n rom, uro enamr axe ornocnrentno
npxmonnnente n onosnaunte xoncrpyxnnn. Hanpnmep, cneymmee perynxpnoe ntpa-
xenne ocymecrnnxer noncx n rexcre mapxepon HTML <A> n <IMG> , a raxxe rexcra, sa-
xnmuennoro nnyrpn pasmerxn nnnort o xonnentx mapxepon </A> n </IMG>):
$fexf = "<>here 1s an ancho.</A>"
1f {$fexf = /<{|lMG|A]}>|\W\s\.]+<\/\1>/1}
{
p1nf "Iound an 1mage o ancho fag"
}
Iound an 1mage o ancho fag
3ro nanonee coxponennax onacrt xstxa Perl. A noromy x nocrapamct nnecrn n +ror no-
npoc maxcnmantnym xcnocrt
1
.
Hcnonusoeauue perynnpuux eupaeuu
B Perl nmemrcx rpn ocnonntx oneparopa, paorammnx co crpoxamn:
m/.../ nponepxa connaenn (matching),
s/.../.../ nocranonxa rexcra (substitution),
tr/.../.../ samena rexcra (translation).
Oneparop m/.../ anannsnpyer nxono rexcr n nmer n nem nocrpoxy, connaammym c yxa-
sanntm manonom (on saan perynxpntm ntpaxennem). Oneparop s/.../.../ ntnonnxer no-
cranonxy onnx rexcrontx ]parmenron nmecro pyrnx, ncnontsyx nx +ro nenn perynxp-
nte ntpaxennx. Oneparop tr/.../.../ raxxe nsmenxer nxono rexcr, no npn +rom on ne nc-
nontsyer perynxpnte ntpaxennx, ocymecrnnxx sameny nocnmnontno.

1
Tem ne menee, neoxonmo ormernrt, uro marepnan, nsnoxennt sect, xnnxercx nnmt nneennem n +ry naxnym
onacrt perynxpntm ntpaxennxm n ocoennocrxm paort c nnmn nocnxment nente xnnrn, nopono paccmarpn-
nammne cnennantnte npnemt, rpmxn n roronte pemennx. Hpu+e:. peo.

Crp.107
Onepafop npoeepku coenapeuu /.../
Oneparop m/.../ ntraercx conocrannrt manon, yxasannt n xauecrne aprymenra, n saan-
nt rexcr (no ymonuannm rexcr epercx ns nepemenno Perl $_). B npnneennom nnxe
npnmepe mt nmem no nnonmom nontsonarenem rexcre crpoxy exit (mon]nxarop i nocne
nropo naxnonno ueprt enaer nponepxy neuyncrnnrentno x perncrpy):
Wh11e {<>}
{ 1f {m/ex1f/1} {ex1f} }
Bmecro roro urot ncnontsonart nepemennym $_, moxno saart ncrounnx nponepxemoro
rexcra c nomomtm oneparopa . B namem npnmepe nx +ro nenn ncnontsyercx nepemen-
nax $line (annt xo ne menxer coepxnmoro nepemenno $line, xorx oneparop n na-
nomnnaer cnmnon npncnoennx):
Wh11e {$11ne = <>}
{ 1f {$11ne = m/ex1f/1} {ex1f} }
Cmtcn cpannennx moxno nsmennrt na npornnononoxnt, ecnn nmecro oneparopa nc-
nontsonart oneparop !:
Wh11e {$11ne = <>}
{ 1f {$11ne ! /ex1f/1} {} e1se {ex1f} }
Hocxontxy n Perl oneparop m/.../ ncnontsyercx ouent uacro, moxno ncnontsonart ero co-
xpamennym ]opmy, onycrnn nauantnym yxny m. Ecnn xe nauantnax yxna m npncyrcr-
nyer, ro nmecro cnmnonon naxnonno ueprt (cn+ma) n xauecrne orpannunrene moxno nc-
nontsonart, sa pexnm ncxnmuennem, nmo pyro cnmnon (cm. anee onncanne oneparo-
pa nocranonxn s/.../.../):
Wh11e {$11ne = <>} {
1f {$11ne = /ex1f/1} {ex1f} 1f {$11ne = /ex1f/1} {ex1f} 1f {$11ne = /ex1f/1} {ex1f} 1f {$11ne = /ex1f/1} {ex1f}
1f {$11ne = m 1f {$11ne = m 1f {$11ne = m 1f {$11ne = m\ \\ \qu1f qu1f qu1f qu1f\ \\ \1} {ex1f} 1} {ex1f} 1} {ex1f} 1} {ex1f}
1f {$11ne = m 1f {$11ne = m 1f {$11ne = m 1f {$11ne = mxsfopx1} {ex1f} xsfopx1} {ex1f} xsfopx1} {ex1f} xsfopx1} {ex1f}
}
Hocuasua. Ec.u uao.ou cooeprum cu+eo.i roco :epmi, :mo sa:acmvm ecmpe:aemc, uanpu+ep, npu
aua.use u+eu qa.oe c vrasauue+ nvmu u/u.u +emor HTML, mo cmoum omrasamic om ucno.isoeauu
omux cu+eo.oe e ra:ecmee oepauu:ume.. Jmo noseo.um ue cmaeumi oopamuvm rocvm :epmv nepeo rar-
oi+ cu+eo.o+ roco :epmi euvmpu uao.oua u coxpauum eeo npospa:uocmi.
Onepafop nopcfauoeku s/.../.../
Oneparop s/.../.../ ntnonnxer sameny onnx ]parmenron rexcra na pyrne. Hanpnmep, n
cneymmem cnyuae mt samenxem nocrpoxy young na nocrpoxy old:
$fexf = "Peffy young."
$fexf = s/young/o1d/ $fexf = s/young/o1d/ $fexf = s/young/o1d/ $fexf = s/young/o1d/
p1nf $fexf
Peffy o1d.
Ho ymonuannm oneparop sament paoraer c nepemenno Perl $_. Kax n n cnyuae oneparo-
pa m/.../, xocym uepry ncnontsonart ne oxsarentno ronrcx nmo cnmnon, xoropt
ne ncrynaer n npornnopeune c saanntm ntpaxennem. Hanpnmep, nmecro xoco ueprt
moxno ncnontsonart [:
$fexf = "Peffy young."
$ $$ $fexf fexf fexf fexf = = = = s ss s| || |yo yo yo young ung ung ung| || |o1d o1d o1d o1d| | | |
p1nf $fexf
Crp.108
Peffy o1d.
nno npocro nocrannrt n xauecrne orpannunrenx otunte cxoxn:
$fexf = "Peffy young."
$fexf = s{young}{o1d} $fexf = s{young}{o1d} $fexf = s{young}{o1d} $fexf = s{young}{o1d}
p1nf $fexf
Peffy o1d.
Fonee nopono o ]opme sannce xoman m/.../ n s/.../.../ paccxastnaercx anee n pasene
Ocoennocrn paort xoman m/.../ n s/.../.../.
Hocuasua. Cmapameci ue npu+eumi e ra:ecmee oepauu:ume.e eonpocume.iui suar (?) u anocmpoq
(') uao.oui, oepauu:euuie omu+u cu+eo.a+u, oopaoamieammc uua:e, :e+ ooi:uie (c+. pasoe.
Ocooeuuocmu paoomi ro+auo m/.../ u s/.../.../ oa.ee e omo e.aee).
Oparnre nnnmanne, uro oneparopt s/.../.../ n m/.../ neyr noncx c nepnoro cnmnona rex-
crono crpoxn o nepnoro connaennx. Ecnn ono naeno, es cnennantnoro yxasannx no-
ncx ne npoonxaercx:
$fexf = "Peffy young, buf nof vey young."
$fexf = s/young/o1d/
p1nf $fexf
Peffy o1d, buf nof vey young.
Onepafop sareuu tr/.../.../
Kpome oneparopon m/.../ n s/.../.../ nx paort co crpoxamn n Perl nmeercx oneparop
tr/.../.../. On raxxe ntnonnxer sameny onnx ]parmenron rexcra na pyrne, onaxo n ornn-
une or s/.../.../, ne ntraercx opaartnart perynxpnte ntpaxennx, nocrannxx rexcr onn
x onomy. B cneymmem npnmepe mt samenxem c ero nomomtm yxny o na yxny i:
$fexf = "h1s name 1s 1om."
$fexf = $fexf = $fexf = $fexf = f/o/1/ f/o/1/ f/o/1/ f/o/1/
p1nf $fexf
h1s name 1s 11m.
Hocuasua. B Perl onepamopi tr/.../.../ u y/.../.../ eino.umm oouuaroeie oecmeu. To:uee tr u y omo oea
u+euu oouoeo u mom re onepamopa.
Hrax, mt nepeuncnnnn oneparopt, c xoroptmn yem paorart n +ro rnane. Hame nnee-
nne, onaxo, nnmt ena sarponyno paccmarpnnaemym remy. Tenept nacrynaer npemx nsy-
unrt cosanne perynxpntx ntpaxenn nceptes, uro nosnonnr nonnonenno paorart c no-
ncxom n sameno crpox.
Henocpepc1neuume pemeuun
Cospauue perynnpuux eupaeuu
Perynxpnte ntpaxennx ocnona paort c oneparopamn m/.../ n s/.../.../, rax xax onn ne-
peamrcx nocnennm n xauecrne aprymenron. Pasepemcx, xax ycrpoeno perynxpnoe ntpa-
xenne \b(A-a-]+)\b, ocymecrnnxmmee noncx orentntx cnon n crpoxe:
$fexf = "Pe1 1s fhe subecf."
$fexf = /\b{|A-a-]+}\b/
p1nf $1
Pe1
Crp.109
Btpaxenne \b(A-a-]+)\b nxnmuaer n cex rpynnnpymmne meracnmnont ( n ), meracnm-
non rpannnt cnona \b, xnacc ncex narnncxnx yxn A-a-] (on oennxer sarnannte n
crpounte yxnt) n xnanrn]nxarop +, xoropt yxastnaer na ro uro rpeyercx narn onn
nnn necxontxo cnmnonon paccmarpnnaemoro xnacca.
Hocxontxy perynxpnte ntpaxennx, xax +ro tno n npetymem npnmepe, moryr trt
ouent cnoxntmn, n +ro rnane onn pasnpamrcx no uacrxm. B omem cnyuae perynxpnoe
ntpaxenne cocronr ns cneymmnx xomnonenron:
onnounte cnmnont (characters),
xnacct cnmnonon (character classes),
antrepnarnnnte manont (alternative match patterns),
xnanrn]nxaropt (quantiIiers),
mnnmte cnmnont (assertions),
cctnxn na naennt rexcr (backreIerences),
ononnnrentnte xoncrpyxnnn (regular expression extensions).
Kaxt ns +rnx +nemenron ocronn ocooro nsyuennx. Hx ocyxennm nocnxmeno ne-
cxontxo cneymmnx pasenon.
Opuuouue cureonu e perynnpuux eupaeuunx
B perynxpnom ntpaxennn nmo cnmnon coornercrnyer camomy cee, ecnn rontxo on ne
xnnxercx meracnmnonom co cnennantntm snauennem (raxnmn meracnmnonamn xnnxmrcx \,
[, (, ), , , , +, , $, n .). B cneymmem npnmepe nponepxercx, ne nnen nn nontsonarent
xomany quit (n ecnn +ro rax, ro npexpamaem paory nporpammt):
Wh11e {<>} {
1f {m/qu1f/} {ex1f} 1f {m/qu1f/} {ex1f} 1f {m/qu1f/} {ex1f} 1f {m/qu1f/} {ex1f}
}
Hpannntnee nponepnrt, uro nneennoe nontsonarenem cnono quit ne nmeer cocennx cnon,
nsmenxmmnx cmtcn npenoxennx. (Hanpnmep, nporpamma ntnonnnr saneomo nenepnoe
ecrnne, ecnn nmecro quit nontsonarent nneer xomany Dont quit!.) 3ro moxno ce-
nart c nomomtm meracnmnonon n $. 3aono, urot cpannenne tno neuyncrnnrentno x
pasnnne mexy nponncntmn n sarnanntmn yxnamn, ncnontsyem mon]nxarop i:
Wh11e {<>} {
1f {m/qu1f$/1} {ex1f}
}
(O paore meracnmnonon n $ paccxastnaercx n pasene Mnnmte cnmnont n perynxp-
ntx ntpaxennxx. O mon]nxarope i moxno noponee ysnart n pasene Mon]nxaro-
pt xoman m/.../ n s/.../.../.)
Kpome otuntx cnmnonon Perl onpeenxer cnennantnte cnmnont. Onn nnoxrcx c nomo-
mtm oparno xoco ueprt (escape-nocneonarentnocrn) n raxxe moryr ncrpeuartcx n pe-
rynxpnom ntpaxennn:
\077 noctmepnunt cnmnon,
\a cnmnon BEL (snonox),
\c ynpannxmmne cnmnont (xomnnannx Ctrl cu+eo., n annom cnyuae +ro
ynpannxmmn cnmnon ESC),
Crp.110
\d coornercrnyer nn]pe,
\D coornercrnyer nmomy cnmnony, xpome nn]pt,
\e cnmnon escape (ESC).
\ xonen ecrnnx xoman \, \U n \,
\f cnmnon nporona crpannnt (FF),
\l cneymmax nnrepa cranonnrcx crpouno (lowercase),
\ nce nocneymmne nnrept cranonxrcx crpountmn nnnort o xomant \,
\n cnmnon nono crpoxn (LF, NL),
\ nnnort o xomant \ nce nocneymmne meracnmnont cranonxrcx otuntmn
cnmnonamn,
\r cnmnon nepenoa xaperxn (CR),
\s coornercrnyer nmomy ns npoentntx cnmnonon (npoen, neprnxantnax
nnn ropnsonrantnax raynxnnx, cnmnon nono crpoxn n r. .),
\S nmo cnmnon, xpome npoentnoro,
\t cnmnon ropnsonrantno raynxnnn (HT, TAB),
\u cneymmax nnrepa cranonnrcx sarnanno (uppercase),
\U nce nocneymmne nnrept cranonxrcx sarnanntmn nnnort o xomant \,
\v cnmnon neprnxantno raynxnnn (T),
\w an]annrno-nn]pono cnmnon (nmax yxna, nn]pa nnn cnmnon nouepxnnannx),
\ nmo cnmnon, xpome yxn, nn]p n cnmnona nouepxnnannx,
\x1 mecrnanarepnunt cnmnon.
Bt raxxe moxere samnrnrt nmo meracnmnon, ro ecrt sacrannrt Perl paccmarpnnart
ero xax otxnonennt cnmnon, a ne xax xomany, nocrannn nepe meracnmnonom opar-
nym xocym uepry \.
Oparnre nnnmanne na cnmnont rnna \w, \d n \s, xoropte coornercrnymr ne onomy, a nmo-
my cnmnony ns nexoropo rpynnt. Taxxe samertre, uro onn raxo cnmnon, yxasannt n
manone, coornercrnyer ponno onomy cnmnony nponepxemo crpoxn. Ho+romy nx saannx
manona, coornercrnymmero, nanpnmep, cnony nn yxn, nn]p n cnmnonon nouepxnnannx,
nao ncnontsonart xoncrpyxnnm \w+, xax +ro cenano n cneymmem npnmepe:
$fexf = "hee 1s some fexf."
$fexf $fexf $fexf $fexf = = = = s/ s/ s/ s/\ \\ \W+/1hee/ W+/1hee/ W+/1hee/ W+/1hee/
p1nf $fexf
1hee 1s some fexf.
(3nax + osnauaer onn nnn onee cnmnonon, coornercrnymmnx manony. Fonee nopo-
no o nem paccxasano n pasene Knanrn]nxaropt n perynxpntx ntpaxennxx.)
Coenapeuue c nkur cureonor
B Perl nmeercx eme onn momnt cnmnon a nmenno, rouxa (.). B manone on coorner-
crnyer nmomy snaxy, xpome cnmnona nono crpoxn. Hanpnmep, cneymmax xomana sa-
Crp.111
menxer n crpoxe nce cnmnont na snesouxn (ncnontsonan mon]nxarop g, oecneunnam-
mn rnoantnym sameny):
$fexf = "NoW 1s fhe f1me."
$fexf = s/./"/g
p1nf $fexf
""""""""""""""""
A uro enart, ecnn rpeyercx nponepnrt connaenne nmenno c rouxo Cnmnont npoe rouxn
(xonxperno, \[()$+.), nrpammne n perynxpnom ntpaxennn ocoym pont) nastnamrcx,
xax yxe tno cxasano ntme, +emacu+eo.a+u, n ecnn nt xornre, urot onn nnyrpn manona
nnrepnpernponannct xax otunte cnmnont, meracnmnony onxna npemecrnonart oparnax
xocax uepra. Touno rax xe oparnax xocax uepra npemecrnyer cnmnony, ncnontsyemomy n
xauecrne orpannunrenx nx xomant m/.../, s/.../.../ nnn tr/.../.../, ecnn on ncrpeuaercx nnyrpn
manona n ne onxen paccmarpnnartcx xax orpannunrent. Paccmorpnm npnmep:
$11ne = ".he11o!"
1f {$11ne = m/ 1f {$11ne = m/ 1f {$11ne = m/ 1f {$11ne = m/\ \\ \./} { ./} { ./} { ./} {
p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od!\ \\ \n" n" n" n"
} } } }
5hou1dnf sfaf a senfence W1fh a pe1od!
Knaccu cureonoe e perynnpuux eupaeuunx
Cnmnont moryr trt crpynnnponant n xnacct. Vxasannt n manone xnacc cnmnonon co-
nocrannxercx c nmtm ns cnmnonon, nxoxmnm n +ror xnacc. Knacc +ro cnncox cnmno-
non, saxnmuennt n xnaparnte cxoxn n ]. Moxno yxastnart xax orentnte cnmnont,
rax n nx nanason (nanason saaercx nymx xpannmn cnmnonamn, coennenntmn rnpe).
Hanpnmep, cneymmn xo nponsnonr noncx rnacntx:
$fexf = "here 1s fhe fexf."
1f {$fexf = /|ae1ou]/} 1f {$fexf = /|ae1ou]/} 1f {$fexf = /|ae1ou]/} 1f {$fexf = /|ae1ou]/} { {{ {p1nf "voWe1s: p1nf "voWe1s: p1nf "voWe1s: p1nf "voWe1s: We gof em. We gof em. We gof em. We gof em.\ \\ \n"} n"} n"} n"}
voWe1s: We gof em.
pyro npnmep: c nomomtm manona A-a-]+ (meracnmnon + osnauaer yrnepxenne:
onn nnn onee raxnx cnmnonon cm. anee pasen Knanrn]nxaropt n perynxpntx
ntpaxennxx) nmercx n samenxercx nepnoe cnono:
$fexf = "Whaf 1s fhe subecf."
$fexf = $fexf = $fexf = $fexf = s/| s/| s/| s/|A AA A- -- -a a a a- -- -]+/Pe1/ ]+/Pe1/ ]+/Pe1/ ]+/Pe1/
p1nf $fexf
Pe1 1s fhe subecf.
Hocuasua. Ec.u mpeovemc saoami +uuvc rar cu+eo., exoouu e r.acc cu+eo.oe, nepeo uu+ uaoo no-
cmaeumi oopamuvm rocvm :epmv (-).
Ecnn cpasy nocne orxptnamme xnaparno cxoxn cronr cnmnon , ro cmtcn menxercx
na npornnononoxnt. A nmenno, +ror xnacc conocrannxercx nmomy cnmnony, rpo+e ne-
peuncnenntx n xnaparntx cxoxax. B cneymmem npnmepe nponsnonrcx samena ]par-
menra rexcra, cocrannennoro ne ns yxn n ne ns npoenon:
$fexf = "Pe1 1s fhe subecf on page 493 of fhe book."
$fexf = $fexf = $fexf = $fexf = s/|A s/|A s/|A s/|A- -- -a a a a- -- - \ \\ \s]+/500/ s]+/500/ s]+/500/ s]+/500/
p1nf $fexf
Pe1 1s fhe subecf on page 500 of fhe book.
Crp.112
Anufepuafueuue uanouu e perynnpuux eupaeuunx
Bt moxere saart necxontxo antrepnarnnntx manonon, ncnontsyx cnmnon [ xax pasenn-
rent. Antrepnarnnnte manont nosnonxmr npenparnrt nponeypy noncxa ns ononanpan-
nennoro nponecca n pasnernnennt: ecnn ne noxonr onn manon, Perl nocrannxer pyro
n nonropxer cpannenne, n rax o rex nop, noxa ne nccxxnyr nce nosmoxnte antrepnarnnnte
xomnnannn. Hanpnmep, cneymmn ]parmenr nponepxer, ne nnen nn nontsonarent exit,
quit nnn stop:
Wh11e {<>} {
1f {m/ex1f|qu1f|sfop/} {ex1f}
}
urot tno xcno, re naunnaercx n re saxanunnaercx naop antrepnarnnntx manonon,
nx saxnmuamr n xpyrnte cxoxn nnaue cnmnont, pacnonoxennte cnpana n cnena or
rpynnt manonon, moryr cmemartcx c antrepnarnnntmn manonamn. B cneymmem npn-
mepe meracnmnont n $ oosnauamr nauano n xonen crpoxn (cm. pasen Mnnmte cnm-
nont n perynxpntx ntpaxennxx) n orenxmrcx or naopa antrepnarnnntx manonon c
nomomtm cxoox:
Wh11e {<>}
{ 1f {m/{ex1f|qu1f|sfop}$/} {ex1f}
}
Antrepnarnnnte napnanrt nepenpamrcx cnena nanpano. Kax rontxo naena nepnax ant-
repnarnna, nx xoropo ntnonnxercx connaenne c manonom, nepeop npexpamaercx.
Hocuasua 1. V:acmru uao.oua, sar.m:euuie e rpve.ie crooru, eino.umm cneua.iuvm po.i npu ei-
no.ueuuu onepau noucra u sa+eui. Oo omo ocooeuuocmu rpve.ix crooor paccrasieaemc e pasoe.ax
Cci.ru ua uaoeuui mercm u Ocooeuuocmu paoomi ro+auo m/.../ u s/.../.../.
Hocuasua 2. Ec.u cu+eo. uaxooumc e reaopamuix croorax, ou uumepnpemupvemc rar ooi:ui cu+-
eo.. Hoomo+v ec.u ei ucno.isveme roucmpvrum uao.oua euoa [1im1om1amj, mo oua ovoem oreuea-
.eumua r.accv cu+eo.oe [1ioam j. To:uo mar re oo.iuuucmeo opveux +emacu+eo.oe u ro+auo, cneu-
qu:uix o. peev.puix eipareuu e :acmuocmu, reaumuquramopi u +uu+ie cu+eo.i, onucauuie e
oevx noc.eovmuux pasoe.ax, euvmpu reaopamuix crooor npeepauammc e ooi:uie cu+eo.i u.u es-
cape-noc.eooeame.iuocmu mercmoeix cmpor.
Keaufuqukafopu e perynnpuux eupaeuunx
Knanrn]nxaropt yxastnamr na ro, uro ror nnn nno manon n crpoxe moxer nonropxrtcx
onpeenennoe xonnuecrno pas. Hanpnmep, moxno ncnontsonart xnanrn]nxarop + nx no-
ncxa mecr neonoxparnoro nonropennx nopx narnncxo yxnt e n nx sament na on-
nounym yxny e:
$fexf = "he11o fom Peeeeeeeeeeeeeee1."
$fexf = s/e+/e/ $fexf = s/e+/e/ $fexf = s/e+/e/ $fexf = s/e+/e/
p1nf $fexf
he11o fom Pe1.
Knanrn]nxarop + coornercrnyer ]pase onn nnn necxontxo. Hepeuncnnm nce ocryn-
nte n Perl xnanrn]nxaropt:
nont nnn necxontxo connaennn,
+ ono nnn necxontxo connaenn,
nont connaennn nnn ono connaenne,
]n} ponno n connaenn,
Crp.113
]n,} no xpane mepe n connaenn,
]n,m} or n o m connaenn.
Hanpnmep, nor xax nponepnrt, uro nontsonarent nnen ne menee nanarn cnmnonon:
Wh11e {<>}
{ 1f {!m/.{20,}/} {p1nf "P1ease fype 1onge 11nes!\n"} }
Hocuasua. Keaumuquramop oecmevem mo.iro ua npeouecmevmuu e+v o.e+eum uao.oua. Hanpu+ep,
roucmpvru d[a-zj+ ovoem coomeemcmeoeami noc.eooeame.iuocmu us oouo u.u uecro.irux cmpo:uix
.amuucrux ovre, ua:uuamuec c uqpi, a ue noc.eooeame.iuocmu, cocmae.euuo us :epeovmuuxc uqp
u ovre. Umooi eioe.umi epvnnv o.e+eumoe, ua romopvm oecmevem reaumuquramop, uvruo ucno.iso-
eami rpve.ie crooru. (d[a-zj)+.
Xapuocfu keaufuqukafopoe
Vurnre, uro xnanrn]nxaropt xonnuecrna no ymonuannm xnnxmrcx xantmn, ro ecrt
nosnpamamr camt nnnnt ]parmenr rexcra, coornercrnymmn yxasannomy - manony,
naunnax c rexyme nosnnnn crpoxn. Hanpnmep, nt xornre samennrt ]pasy That is some
text, isnt it na Thats some text, isnt it, nocrannn Thats nmecro That is. Hocmor-
pnm, uro nonyunrcx, ecnn ncnontsonart xomany
$fexf = "1haf 1s some fexf, 1snf 1f":
$fexf = s/."1s/1hafs/ $fexf = s/."1s/1hafs/ $fexf = s/."1s/1hafs/ $fexf = s/."1s/1hafs/
p1nf $fexf
B cnny xanocrn xnanrn]nxaropa xoncrpyxnnx .is yer conocrannena maxcnmantno
nosmoxnomy ]parmenry rexcra. To ecrt Perl coornecer c ne nce cnmnont, npemecrnym-
mne nocnenemy is (nxnmuax n cam is). B pesyntrare ntnonnennx xomant nonyunrcx:
1hafsnf 1f
Hponeme, xax sacrannrt xnanrn]nxaropt xonnuecrna trt menee xantmn, nocnxmen orent-
nt pasen +ro rnant (cm. anee pasen Kax orpannunrt xanocrt xnanrn]nxaropon).
Perynxpnte ntpaxennx, ncnontsymmne xnanrn]nxaropt, moryr nopoxart nponecc, xo-
ropt nastnaercx nepeoop c eosepamo+ (backtracking). urot nponsomno connaenne
rexcra c manonom, nao nocrponrt coornercrnne mexy rexcrom n ece+ perynxpntm nt-
paxennem, a ne ero :acmim. Hauano manona moxer coepxart xnanrn]nxarop, xoropt
nonauany cpaartnaer, no nnocnecrnnn npnnonr x romy, uro nx uacrn manona ne xna-
raer rexcra nnn nosnnxaer necoornercrnne mexy rexcrom n manonom. B raxnx cnyuaxx
Perl nosnpamaercx nasa n naunnaer nocrpoenne coornercrnnx mexy rexcrom n mano-
nom c camoro nauana, orpannunnax xanocrt xnanrn]nxaropa (nmenno no+romy nponecc
n nastnaercx nepeop c nosnparom).
Muurue cureonu e perynnpuux eupaeuunx
B Perl nmemrcx cnmnont (meracnmnont), xoropte coornercrnymr ne xaxo-nno nnrepe
nnn nnrepam, a osnauamr ntnonnenne onpeenennoro ycnonnx (no+romy n anrnncxom
xstxe nx nastnamr assertions, nnn vmeeproeuu+u). Hx moxno paccmarpnnart xax mnn-
mte cnmnont nynenoro pasmepa, pacnonoxennte na rpannne mexy peantntmn cnmnona-
mn n rouxe, coornercrnymme onpeenennomy ycnonnm:
nauano crpoxn rexcra,
Crp.114
$ xonen crpoxn nnn nosnnnx nepe cnmnonom nauana nono crpoxn, pacno-
noxennoro n xonne,
\b rpannna cnona,
\ orcyrcrnne rpannnt cnona,
\A ncrnnnoe nauano crpoxn,
\ ncrnnnt xonen crpoxn nnn nosnnnx nepe cnmnonom nauana nono crpoxn,
pacnonoxennoro n ncrnnnom xonne crpoxn,
\ ncrnnnt xonen crpoxn,
\G rpannna, na xoropo ocranonnncx npetymn rnoantnt noncx, ntnonnxemt
xomano m/.../g,
( uao.ou) nocne +ro rouxn ecrt ]parmenr rexcra, xoropt coornercrnyer yxasan-
nomy perynxpnomy ntpaxennm,
(! uao.ou) nocne +ro rouxn uem rexcra, xoropt t coornercrnonan yxasannomy
perynxpnomy ntpaxennm,
(< uao.ou) nepeo +ro rouxo ecrt ]parmenr rexcra, coornercrnymmn
yxasannomy perynxpnomy ntpaxennm,
(<! uao.ou) nepe +ro rouxo uem ]parmenra rexcra, coornercrnymmero
yxasannomy perynxpnomy ntpaxennm.
Hocuasua. Vmeeproeuu euoa (? ...), (?! ...), (?< ...) u (?<! ...) pacc+ampueammc oo.ee oema.iuo oa-
.ee e omo e.aee e pasoe.ax ono.uume.iuie roucmpvruu e peev.puix eipareuux u Vmeeproeuu,
npoeepmuue mercm nepeo u noc.e uao.oua.
Hanpnmep, nor xax ntnonnnrt noncx n sameny cnona, ncnontsyx meracnmnont rpannnt cnon:
$fexf = "hee 1s some fexf."
$fexf = $fexf = $fexf = $fexf = s/ s/ s/ s/\ \\ \b{|A b{|A b{|A b{|A- -- -a a a a- -- -]+} ]+} ]+} ]+}\ \\ \b/1hee/ b/1hee/ b/1hee/ b/1hee/
p1nf $fexf
1hee 1s some fexf.
(Perl cunraer rpannne cnona rouxy, pacnonoxennym mexy \w n \, nesanncnmo or roro,
n xaxom nopxxe cneymr +rn cnmnont.)
B cneymmem npnmepe ntnonrcx coomenne o rom, uro nontsonarent nnen cnono yes,
npn ycnonnn, uro ono enncrnennoe, uro nnen nontsonarent. nx +roro manon nxnmuaer
mnnmte cnmnont nauana n xonna crpoxn:
Wh11e {<>} {
1f {m/ye 1f {m/ye 1f {m/ye 1f {m/yes$/} { s$/} { s$/} { s$/} {
p1nf "1hank you fo be1ng ageeab1e. p1nf "1hank you fo be1ng ageeab1e. p1nf "1hank you fo be1ng ageeab1e. p1nf "1hank you fo be1ng ageeab1e. \ \\ \n nn n" " " " } } } }
}
Hpnneennt ntme npnmep rpeyer xommenrapnx. Hpexe ncero, pocaercx n rnasa na-
nnune nyx rpynn meracnmnonon nx nauana n xonna crpoxn. B ontmnncrne cnyuaen onn
osnauamr ono n ro xe, rax xax otuno cnmnont nono crpoxn (ro ecrt \n), ncrpeuam-
mnecx nnyrpn rexcronoro ntpaxennx, ne paccmarpnnamrcx xax nnoxennte crpoxn. Ona-
xo ecnn nx xomant m/.../ nnn s/.../.../ yxasan mon]nxarop m (cm. anee pasen Mon-
]nxaropt xoman m/.../ n s/.../.../), ro rexcronoe ntpaxenne yer paccmarpnnartcx xax
mnorocrpount rexcr, n xoropom rpannnamn crpox ntcrynamr cnmnont nono crpoxn \n.
B cnyuae mnorocrpounoro rexcra meracnmnon conocrannxercx c nosnnne nocne .mooeo
Crp.115
cnmnona nono crpoxn, a ne rontxo c nauanom rexcronoro ntpaxennx. Touno raxxe mera-
cnmnon $ +ro nosnnnx nepe .moi+ cnmnonom nono crpoxn, pacnonoxenntm nnyrpn
rexcronoro ntpaxennx, a ne oxsarentno xonen rexcronoro ntpaxennx nnn xe nosnnnx
nepe xonnentm cnmnonom \n. Onaxo meracnmnon \A nauano rexcronoro ntpaxennx, a
meracnmnon \ xonen rexcronoro ntpaxennx nnn nosnnnx nepe xonnentm cnmnonom
\n, axe ecnn n rexcronom ntpaxennn nmemrcx nnoxennte cnmnont \n n npn ntnonne-
nnn onepannn noncxa nnn sament yxasan mon]nxarop m.
Hocuasua. Memacu+eo. mo:ra (.) coomeemcmevem .moo+v cu+eo.v, rpo+e cu+eo.a uoeo cmporu n. Hesa-
eucu+o om moeo, saoau .u +oouquramop m, oua ue ovoem conocmae.mic uu c euvmpeuuu+u, uu c roueei+u
cu+eo.a+u n. Eouucmeeuui cnocoo sacmaeumi mo:rv pacc+ampueami n rar ooi:ui cu+eo. ~ ucno.iso-
eami +oouquramop s (c+. pasoe. Moouquramopi ro+auo m/.../ u s/.../.../ oa.ee e omo e.aee).
Orcma nonxrna pasnnna mexy meracnmnonamn \ n \. Ecnn n xauecrne rexcronoro ntpaxe-
nnx ncnontsyercx pesyntrar urennx nxonoro noroxa anntx, ro c ontmo nepoxrnocrtm
annoe ntpaxenne saxanunnaercx cnmnonom \n, sa ncxnmuennem roro napnanra, xora npo-
rpamma npeycmorpnrentno ormnnnyna ero c nomomtm ]ynxnnn chop nnn chomp. Mera-
cnmnon \ nrnopnpyer xonneno cnmnon \n, ecnn on cnyuano ocrancx na mecre, paccmarpnnax
oe cnryannn xax xonen crpoxn. B ornnune or nero meracnmnon \ oxastnaercx onee nynx-
ryantntm n paccmarpnnaer xonneno cnmnon \n xax neoremnemym uacrt nponepxemoro rex-
cronoro ntpaxennx, ecnn rontxo nontsonarent ne nosaornncx o yanennn +roro cnmnona.
Orentno cneyer ocranonnrtcx na meracnmnone \G. On moxer yxastnartcx n perynxpnom nt-
paxennn rontxo n rom cnyuae, ecnn ntnonnxercx rnoantnt noncx (ro ecrt ecnn xomana
m/.../ nmeer mon]nxarop g cm. pasen Mon]nxaropt xoman m/.../ n s/.../.../). Meracnm-
non \G, yxasannt n manone, coornercrnyer rouxe, na xoropo ocranonnnact npetymax
onepannx noncxa. Fonee nopono o paore anropnrma noncxa npn nannunn mon]nxaropa g n
meracnmnona \G paccxastnaercx anee n pasene Ocoennocrn paort xoman m/.../ n
s/.../.../.
Hpeynpexeuue. B mervue eepcuu Perl +emacu+eo. C oreuea.eumeu +emacu+eo.v A, ec.u npu ei-
no.ueuuu onepauu noucra ue vrasau +oouquramop g. Oouaro omo c.v:auoe u ueoorv+eumupoeauuoe
ceocmeo, romopoe .eero +orem us+euumic e ovovue+.
Ccunku ua uapeuuu fekcf
Hnora nyxno cocnartcx na nocrpoxy rexcra, nx xoropo nonyueno connaenne c nexo-
ropo uacrtm manona. Hanpnmep, npn opaorxe ]ana HTML moxer norpeonartcx
ntenxrt ]parmenrt rexcra, orpannuennte orxptnammnmn n saxptnammnmn merxamn
HTML (nanpnmep, <A> n </A>). B nauane +ro rnant yxe npnnonncx npnmep, n xoropom
ntenxncx rexcr, orpannuennt merxamn HTML <A> n <IMG>. Cneymmn npnmep no-
snonxer ntenxrt rexcr, pacnonoxennt mexy nmtmn npannntno saxptrtmn merxamn:
$fexf = "<>here 1s an ancho.</A>"
1f 1f 1f 1f {$fexf {$fexf {$fexf {$fexf = m mm mx<{|A x<{|A x<{|A x<{|A- -- -a a a a- -- -]+}>| ]+}>| ]+}>| ]+}>|\ \\ \W WW W\ \\ \s ss s\ \\ \.]+</ .]+</ .]+</ .]+</\ \\ \1>x1} 1>x1} 1>x1} 1>x1}
{ p1nf "h1ML fag W1fh some fexf 1ns1de 1f 1s found." }
h1ML fag W1fh some fexf 1ns1de 1f 1s found.
(Oparnre nnnmanne, uro nmecro xoco ueprt n xauecrne orpannunrenx manona ncnont-
sonan pyro cnmnon. 3ro nosnonxer ncnontsonart cnmnon xoco ueprt nnyrpn manona
es npemecrnymme emy oparno xoco ueprt.)
Kaxomy ]parmenry manona, saxnmuennomy n xpyrnte cxoxn, coornercrnyer onpeenen-
nax nnyrpennxx nepemennax. Hepemennte nponymeponant, rax uro na nnx moxno cctnartcx
Crp.116
nnyrpn manona, nocrannn nepe nomepom oparnym xocym uepry (\1, \2, \3, ...). Ha snauennx
nepemenntx moxno cctnartcx nnyrpn manona, xax na otunt rexcr, no+romy </\1> coor-
nercrnyer </A>, ecnn orxptnamme merxo cnyxnr <A>, n </IMG>, ecnn orxptnamme
merxo cnyxnr </IMG>.
3rn xe camte nnyrpennne nepemennte moxno ncnontsonart n nne manona, cctnaxct na
nnx xax na cxanxpt c nmenamn $1, $2, $3, ..., $n:
$fexf = "l have 4 app1es."
1f 1f 1f 1f {$fexf {$fexf {$fexf {$fexf = /{ = /{ = /{ = /{\ \\ \d+}/} { d+}/} { d+}/} { d+}/} {
p1nf "hee 1s fhe numbe of app1es: p1nf "hee 1s fhe numbe of app1es: p1nf "hee 1s fhe numbe of app1es: p1nf "hee 1s fhe numbe of app1es: $1. $1. $1. $1.\ \\ \n"} n"} n"} n"}
hee 1s fhe numbe of app1es: 4.
Kaxo nape cxoox nnyrpn manona nocne sanepmennx onepannn noncxa yer coornercrno-
nart cxanxpnax nepemennax c coornercrnymmnm nomepom. 3ro moxno ncnontsonart npn nte-
nennn nyxntx nx nocneymme paort ]parmenron anannsnpyemo crpoxn. B cneymmem
npnmepe mt nsmenxem nopxox rpex cnon n rexcrono crpoxe c nomomtm xomant s/.../.../:
$fexf = "l see you."
$fexf $fexf $fexf $fexf = = = = s/{ s/{ s/{ s/{\ \\ \W+} W+} W+} W+} "{ "{ "{ "{\ \\ \W+} W+} W+} W+} "{ "{ "{ "{\ \\ \W+}/$ W+}/$ W+}/$ W+}/$3 $2 $1/ 3 $2 $1/ 3 $2 $1/ 3 $2 $1/
p1nf $fexf
you see l.
Hepemennte, coornercrnymmne ]parmenram manona, nymepymrcx cnena nanpano c yue-
rom nnoxennocrn cxoox. Hanpnmep, nocne cneymme onepannn noncxa yyr nponnn-
nnannsnponant mecrt nepemenntx, coornercrnymmnx mecrn napam cxoox:
$fexf = "A8CDLIGh"
$f $f $f $fexf exf exf exf = = = = a/{ a/{ a/{ a/{\ \\ \W{ W{ W{ W{\ \\ \W}{ W}{ W}{ W}{\ \\ \W}}{{ W}}{{ W}}{{ W}}{{\ \\ \W}{ W}{ W}{ W}{\ \\ \W}}/ W}}/ W}}/ W}}/
p1nf "$1/$2/$3/$4/$5/$6/"
A8C/8/C/DL/D/L
Hocuasua 1. Kpo+e nepe+euuix, cci.amuuxc ua uaoeuui mercm, +oruo ucno.isoeami cneua.iuie
nepe+euuie Perl. Tar, $& cooeprum uaoeuuoe coenaoeuue (mo ecmi qpae+eum mercma, o. romopoeo
uaoeuo coomeemcmeue +erov uao.ouo+ u mercmo+ npu noc.eoue onepauu noucra u.u sa+eui), $` co-
oeprum mercm nepeo uaoeuui+ coenaoeuue+, $' mercm noc.e uaoeuuoeo coenaoeuu, $+ coenaoe-
uue o. oopaoomauuoeo noc.eouu+ qpae+euma uao.oua, sar.m:euuoeo e rpve.ie crooru (ec.u v uao.oua
uem qpae+eumoe, sar.m:euuix e rpve.ie crooru, oua no.v:aem ueonpeoe.euuoe sua:euue).
Hocuasua 2. Hapi crooor, ucno.isve+ie e cuumarcu:ecrux roucmpvrux euoa (? ...), (?! ...), (?< ...) u m. o.
(c+. pasoe. ono.uume.iuie roucmpvruu e peev.puix eipareuux), ue noporoamm uv+epoeauuix nepe-
+euuix.
ononuufenuuue koucfpykuuu e perynnpuux eupaeuunx
B perynxpntx ntpaxennxx Perl nepcnn 5 nmemrcx ononnnrentnte cnnraxcnuecxne xon-
crpyxnnn, ncnontsymmne cxoxn n xomnnannn c nonpocnrentntm snaxom. Hmemr cmtcn
cneymmne xoncrpyxnnn:
(#mercm) xommenrapn. Texcr xommenrapnx nrnopnpyercx.
(:uao.ou) nnn (+oouquramopi:uao.ou) rpynnnpyer +nemenrt manona. B ornn-
une or otuntx xpyrntx cxoox, ne cosaer nymeponanno nepemenno. Mon]nxaropt,
xoropte moxno yxastnart n +ro xoncrpyxnnn, onncant anee n pasene Mon]nxaro-
pt xoman m/.../ n s/.../.../. Hanpnmep, mon]nxarop i ne yer enart pasnnunx mexy
crpountmn n sarnanntmn yxnamn, onaxo onacrt ecrnnx +roro mon]nxaropa yer
orpannuena rontxo yxasanntm manonom (cm. raxxe anee xoncrpyxnnm (u+)).
Crp.117
(uao.ou) sarnxtnanne nnepe. Tpeyer, urot nocne rexyme rouxn naxonncx
rexcr, coornercrnymmn annomy manony. Taxax xoncrpyxnnx opaartnaercx xax ycnonne
nnn mnnmt cnmnon, nocxontxy ne nxnmuaercx n pesyntrar noncxa. Hanpnmep, noncx c no-
momtm xomant /w+(\s+)/ naer cnono, sa xoroptm cneymr onn nnn necxontxo npo-
entntx cnmnonon, onaxo camn onn n pesyntrar ne noyr.
(!uao.ou) cnyua, npornnononoxnt npetymemy. Hocne rexyme rouxn ue
oo.ruo oimi rexcra, coornocnmoro c saanntm manonom. Tax, ecnn manon w+(\s)
+ro cnono, sa xoroptm cneyer npoentnt cnmnon, ro manon w+(!\s) +ro cno-
no, sa xoroptm uem npoentnoro cnmnona.
(<uao.ou) sarnxtnanne nasa. Tpeyer, urot nepe rexyme rouxo naxonncx
coornercrnymmn rexcr. Tax, manon (<\s)w+ nnrepnpernpyercx xax cnono, nepeo xoro-
ptm nmeercx npoentnt cnmnon (n ornnune or sarnxtnannx nnepe, sarnxtnanne
nasa moxer paorart rontxo c ]nxcnponanntm uncnom nponepxemtx cnmnonon).
(<!uao.ou) orpnnanne, npetymero ycnonnx. Hepe rexyme rouxo ne oo.ruo
oimi rexcra, coornocnmoro c saanntm manonom. Coornercrnenno, or xomant
/(<!\s)w+/ rpeyercx narn cnono, nepe xoroptm uem npoentnoro cnmnona.
(roo]) ycnonne (mnnmt cnmnon), xoropoe ncera ntnonnxercx. Cnonrcx x nt-
nonnennm xoman Perl n ]nrypntx cxoxax. Bt moxere ncnontsonart +ry xoncrpyxnnm,
rontxo ecnn n nauane cnenapnx yxasana xomana use re eval. Hpn nocneonarentnom co-
ornecennn rexcra n manona, xora Perl oxonr o raxo xoncrpyxnnn, ntnonnxercx yxa-
sannt xo. Ecnn nonnoro coornercrnnx nx ocranmnxcx +nemenron narn ne yanoct, ro
npn nosnpare nenee anno rouxn manona ntuncnennx, npoenannte c noxantntmn ne-
pemenntmn, orxartnamrcx nasa. (Vcnonne xnnxercx +xcnepnmenrantntm. B oxymenra-
nnn, npnnaraemo x Perl, moxno narn onontno erantnoe paccmorpenne (c npnmepamn)
paort +roro ycnonnx n nosmoxntx rpynocre n cnyuae ero npnmenennx.)
(>uao.ou) nesanncnmt nnn anronomnt manon. Hcnontsyercx nx onrnmnsannn
nponecca noncxa, nocxontxy sanpemaer noncx c nosnparom. Taxax xoncrpyxnnx coornercr-
nyer nocrpoxe, na xoropym nanaraercx saannt manon, ecnn ero saxpennrt n rexyme
rouxe es yuera nocneymmnx +nemenron manona. Hanpnmep, manon (>a)ab n ornnune
or aab ne moxer coornercrnonart nnxaxo crpoxe. Ecnn nocrannrt n nmom mecre manon
a, on cecr nce yxnt a, ne ocrannn nn ono manony ab. (nx manona aab annernr
xnanrn]nxarop yer orpannuen sa cuer paort noncxa c nosnparamn: nocne roro xax na
nepnom +rane ne yacrcx narn coornercrnne mexy manonom n rexcrom, Perl cenaer mar
nasa n ymentmnr xonnuecrno yxn a, saxnartnaemtx xoncrpyxnne a.)
((vc.oeue)uao.ou-oauao.ou-uem) nnn ((vc.oeue)uao.ou-oa) ycnonnt oneparop,
xoropt nocrannxer ror nnn nno manon n sanncnmocrn or ntnonnennx saannoro yc-
nonnx. Fonee nopono onncan n oxymenrannn Perl.
(+oouquramopi) saaer mon]nxaropt (onn onncant anee n pasene Mon]n-
xaropt xoman m/.../ n s/.../.../), xoropte .ora.iui+ opasom menxmr paory nponeypt
noncxa. B ornnune or rnoantntx mon]nxaropon, nmemr cnny rontxo nx rexymero no-
xa, ro ecrt nx nnxame rpynnt xpyrntx cxoox, oxnartnammnx xoncrpyxnnm. Ha-
npnmep, manon ((i)text) coornercrnyer cnony text es yuera perncrpa.
Hpnmept ncnontsonannx ononnnrentntx xoncrpyxnn n perynxpntx ntpaxennxx npnnoxr-
cx n pasene Vrnepxennx, nponepxmmne rexcr nepe n nocne manona n xonne +ro rnant.
Crp.118
Mopuqukafopu koraup /.../ u s/.../.../
B Perl nmeercx necxontxo mon]nxaropon, ncnontsyemtx c xomanamn m/.../ n s/.../.../:
i nrnopnpyer pasnnune mexy sarnanntmn n crpountmn yxnamn.
s meracnmnony rouxa paspemeno coornercrnonart cnmnonam \n.
m paspemaer meracnmnonam n $ npnnxstnartcx x npomexyrountm cnmnonam \n,
nmemmnmcx n rexcre. He nnnxer na paory meracnmnonon \A, \ n \.
x nrnopnpyer npoentnte cnmnont n manone (nmemrcx n nny ncrnnnte npo-
ent, a ne meracnmnont \s n npoent, cosannte uepes escape-nocneonarentnocrn). Pas-
pemaer ncnontsonart nnyrpn manona xommenrapnn.
g ntnonnxer rnoantnt noncx n rnoantnym sameny (noponee cm. cneymmn
pasen).
c nocne roro xax n cxanxpnom xonrexcre npn noncxe c mon]nxaropom g ne yanoct
narn ouepenoe connaenne, ue nosnonxer cpactnart rexymym nosnnnm noncxa (no-
ponee cm. cneymmn pasen). Paoraer rontxo nx xomant m/.../ n rontxo nmecre c
mon]nxaropom g.
o sanpemaer nonropnym xomnnnxnnm manona npn xaxom opamennn x annomy
oneparopy noncxa nnn sament. Hontsonarent, onaxo, onxen rapanrnponart, uro manon
ne menxercx mexy ntsonamn annoro ]parmenra xoa.
e noxastnaer, uro npant aprymenr xomant s/.../../ +ro ]parmenr ntnonnxemoro
xoa. B xauecrne rexcra nx nocranonxn yer ncnontsonano nosnpamaemoe snauenne
nosmoxno, nocne nponecca nnrepnonxnnn (noponee cm. cneymmn pasen).
ee noxastnaer, uro npant aprymenr xomant s/.../../ +ro crpoxonoe ntpaxenne,
xoropoe nao ntuncnnrt n ntnonnnrt xax ]parmenr xoa (uepes ]ynxnnm eval). B xaue-
crne rexcra nx nocranonxn ncnontsyercx nosnpamaemoe snauenne nosmoxno, nocne
nponecca nnrepnonxnnn (noponee cm. cneymmn pasen).
(Hexoropte mon]nxaropt nanpnmep, i, s, m, x moryr naxonrtcx n onon-
nnrentntx xoncrpyxnnxx, paccmorpenntx n npetymem pasene.) B xauecrne npnmepa
paccmorpnm cnenapn, n xoropom nontsonarent ntnonnxer xomany ntxoa, nnox cnono
stop, STOP nnn axe StOp, ro ecrt es yuera perncrpa:
Wh11e {<>} {
1f {m/sfop$/1} {ex1f }
}
Ocoeuuocfu paofu koraup /.../ u s/.../.../
o cnx nop mt paccmarpnnann perynxpnte ntpaxennx, ncnontsyemte n xauecrne mano-
non nx xoman m/.../ n s/.../.../, n ne ocoo nnrepeconannct, xax paoramr +rn xomant.
Hacrano npemx nocnonnnrt npoent.
Komana m/.../ nmer rexcr no saannomy manony. Ee paora n nosnpamaemoe snauenne cnntno
sanncxr or roro, n cxanxpnom nnn cnncxonom xonrexcre ona ncnontsyercx n nmeer nn mon]n-
xarop g (rnoantnt noncx). Fonee nopono paora xomant m/.../ paccmarpnnaercx nnxe.
Komana s/.../.../ nmer nporornn, coornercrnymmn manony, n, ecnn noncx oxastnaercx yc-
Crp.119
nemntm, samenxer ero na nont rexcr. Fes mon]nxaropa g samena nponsnonrcx rontxo nx
nepnoro naennoro connaennx, c mon]nxaropom g ntnonnxmrcx sament nx ncex conna-
enn no nxonom rexcre. Komana nosnpamaer n xauecrne pesyntrara uncno ycnemntx sa-
men nnn nycrym crpoxy (ycnonne .ori false), ecnn nn ono sament cenano ne tno.
B xauecrne anannsnpyemoro rexcra ncnontsyercx cnennantnax nepemennax Perl $_ (pexnm no
ymonuannm) nnn ntpaxenne, npncoennennoe x manony c nomomtm oneparopa nnn !. B
cnyuae noncxa (xomana m/.../) xoncrpyxnnx, pacnonoxennax cnena or oneparopon nnn !,
moxer n ne trt nepemenno. B cnyuae sament (xomana s/.../.../) n neno uacrn onxna cro-
xrt cxanxpnax nepemennax, nnn +nemenr maccnna, nnn +nemenr x+ma, nnn xe xomana npn-
cnoennx onomy ns yxasanntx oexron (cm. pasen uro raxoe nenoe snauenne n rnane 2).
Bmecro xoco ueprt n xauecrne orpannunrenx nx aprymenron xoman m/.../ n s/.../.../
moxno ncnontsonart nmo cnmnon, sa ncxnmuennem npoentnoro cnmnona, yxnt nnn
nn]pt. Hanpnmep, n +rom xauecrne moxno ncnontsonart cnmnon xommenrapnx, xoropt
yer paorart xax orpannunrent:
$fexf = "A8C-abc"
$fexf $fexf $fexf $fexf = = = = s#8#xxx#1g s#8#xxx#1g s#8#xxx#1g s#8#xxx#1g
p1nf $fexf
AxxxC-axxxc
Hocuasua. B ra:ecmee oepauu:ume.e ue cmoum ucno.isoeami eonpocume.iui suar u anocmpoq (oou-
uapuvm raei:ru) uao.oui c maru+u oepauu:ume.+u oopaoamieammc cneua.iui+ oopaso+.
Ecnn xomana m/.../ ncnontsyer cnmnon xoco ueprt n xauecrne pasennrenx, ro yxny m
moxno onycrnrt:
Wh11e {def1ned{$fexf = <>}}
{ 1f {$fexf = /ex1f$/1} {ex1f}
}
Ecnn n xauecrne orpannunrenx nx xomant m/.../ ncnontsyercx nonpocnrentnt snax, ro
yxny m raxxe moxno onycrnrt. Onaxo manont, orpannuennte cnmnonom , n cnyuae
noncxa paoramr ocotm opasom (nesanncnmo or nannunx nnn orcyrcrnnx nauantno m).
A nmenno, onn neyr cex xax rpnrrept, xoropte cpaartnamr onn pas n norom ntamr
cocroxnne .ori (false), noxa nx ne nsneyr cnona, ntsnan ]ynxnnm reset (ona ounmaer
craryc noxnponxn cpasy ncex xoncrpyxnn ..., noxantntx nx annoro naxera). Hanpn-
mep, cneymmn ]parmenr cnenapnx nponepxer, ecrt nn n ]ane nycrte crpoxn:
Wh11e {<>} {
1f {$} {p1nf "1hee 1s an empfy 11ne hee.\n"}
} conf1nue {
esef 1f eof #ouuc1u1u nn cneyk|ero auna
}
narnocrnuecxoe coomenne yer naneuarano rontxo onn pas, axe ecnn n ]ane npn-
cyrcrnyer necxontxo nycrtx crpox.
Hpeynpexeuue. Ko+auoa noucra c eonpocume.iui+ suaro+ omuocumc r nooospume.iui+ ro+au-
oa+, a nomo+v +orem ue eomu e uoeie eepcuu Perl.
B xauecrne orpannunrene moxno raxxe ncnontsonart pasnnunte (napnte) xoncrpyxnnn
cxoox:
Wh11e {<>} {
1f {m/qu1f$/1} {ex1f}
1f {m{sfop$}1} {ex1f}
1f {m|end$]1} {ex1f}
1f {m{bye$}1} {ex1f}
Crp.120
1f {m<ex1f$>1} {ex1f}
}
B cnyuae xomant s/.../.../ n ncnontsonannx cxoox xax orpannunrene nx nepnoro apry-
menra, orpannunrenn nroporo aprymenra moryr ntnpartcx nesanncnmo:
$fexf = "Pe1 1s Wondefu1."
$fexf = s/1s/1s vey/
$fexf = s|Wondefu1]{beauf1fu1}
$fexf = s{\.}/!/
p1nf $fexf
Pe1 1s vey beauf1fu1!
Rpepeapufenuuan opaofka perynnpuux eupaeuu
Aprymenramn xoman m/.../ n s/.../.../ xnnxmrcx perynxpnte ntpaxennx, xoropte nepe naua-
nom paort nnrepnonnpymrcx noono crpoxam, saxnmuenntm n nonte xantuxn (cm. pas-
en Hocranonxa nepemenntx (nnrepnonxnnx crpox) n rnane 2). B ornnune or rexcrontx
crpox, nx manona ne ntnonnxercx nnrepnonxnnx nmen rnna $), $[ n onnounoro $ Perl
cunraer, uro raxne xoncrpyxnnn coornercrnymr meracnmnony xonna crpoxn, a ne cnennant-
no nepemenno. Ecnn xe n pesyntrare nnrepnonxnnn manon noncxa oxasancx nycro crpo-
xo, Perl ncnontsyer nocnenn manon, xoropt npnmenxncx nm nx noncxa nnn sament.
Ecnn nt ne xornre, urot Perl ntnonnxn nnrepnonxnnm perynxpnoro ntpaxennx, n xaue-
crne orpannunrenx nao ncnontsonart anocrpo] (onnounym xantuxy), rora manon y-
er necrn cex, xax rexcronax crpoxa, saxnmuennax n anocrpo]t. Onaxo, nanpnmep, n
cnyuae xomant sament s/.../.../ c mon]nxaropom e nnn ee (nx paora onnctnaercx uyrt
antme) nx nroporo aprymenra yer ntnonnxrtcx nnrepnonxnnx axe n rom cnyuae, ec-
nn on saxnmuen n anocrpo]t.
Ecnn nt ynepent, uro npn nmom opamennn x xomane noncxa nnn sament manon oc-
raercx nensmenntm (nanpnmep, necmorpx na nnrepnonxnnm, cxanxpnte nepemennte
nnyrpn manona ne yyr menxrt cnoero snauennx), ro moxno saart mon]nxarop o (cm.
ntme pasen Mon]nxaropt xoman m/.../ n s/.../.../). Tora Perl xomnnnnpyer manon n
cnoe nnyrpennee npecrannenne rontxo npn nepno ncrpeue c anno xomano noncxa
nnn sament. Hpn ocrantntx opamennxx x xomane yer ncnontsonart orxomnnnnpo-
nannoe snauenne. Onaxo, ecnn nnesanno nsmennrt snauenne nepemenntx, saecrnonan-
ntx n manone, Perl +roro axe ne samernr.
Komana sament s/.../.../ ncnontsyer perynxpnoe ntpaxenne, yxasannoe n xauecrne nroporo
aprymenra, nx sament rexcra. Hocxontxy ono opaartnaercx (nnrepnonnpyercx) noc.e
roro, xax ntnonnena ouepenax onepannx noncxa, n nem moxno, n uacrnocrn, ncnontso-
nart npemennte nepemennte, cosannte na +rane noncxa. B cneymmem npnmepe mt no-
cneonarentno samennm mecramn napt cnon, saanntx no nxonom rexcre, ocrannn mexy
nnmn no onomy npoeny:
$fexf = "One 1Wo 1hee Iou I1ve 51x"
$fexf = s/{\W+}\s"{\W+}/$2 $1 /g
1Wo One Iou 1hee 51x I1ve
Onaxo Perl onycxaer n onee cnoxnte cnocot onpeenennx samenxmmero rexcra. Tax,
ecnn nx xomant s/.../.../ yxasart mon]nxarop e, ro n xauecrne nroporo aprymenra nao
yxasart xo, xoropt neoxonmo ntnonnnrt (nanpnmep, ntsnart ]ynxnnm). Honyuennoe
ntpaxenne yer ncnontsonano xax rexcr nx nocranonxn. Hpn +rom nocne ntuncnennx
Crp.121
rexcronoro snauennx, no nepeo ero nocranonxo yer ntnonnen nponecc nnrepnonxnnn,
ananornunt nponeccy nnrepnonxnnn rexcrontx crpox, saxnmuenntx n nonte xantuxn
(cm. pasen Hocranonxa nepemenntx (nnrepnonxnnx crpox) n rnane 2).
Eme onee cnoxnax cxema peannsyercx, ecnn saan mon]nxarop ee. B +rom cnyuae nro-
po aprymenr xomant s/.../.../ +ro crpoxonoe ntpaxenne, xoropoe cnepna nao ei:uc-
.umi (ro ecrt nnrepnonnponart), sarem eino.uumi n xauecrne xoa (ntsnan ncrpoennym
]ynxnnm Perl eval), n rontxo nocne nropo uumepno.uu nonyuennt pesyntrar no-
crannxercx nmecro naennoro rexcra.
Paofa koraupu /.../ e peure opuokpafuoro noucka
B cxanxpnom xonrexcre n es mon]nxaropa g xomana m/.../ nosnpamaer nornuecxoe sna-
uenne nenoe uncno 1 (ucmuua (true)), ecnn noncx oxasancx ycnemntm, n nycrym crpoxy
"" (.ori (false)), ecnn nyxnt ]parmenr rexcra narn ne yanoct. Ecnn nnyrpn manona
nmemrcx rpynnt +nemenron, saxnmuennte n xpyrnte cxoxn, ro nocne onepannn noncxa
cosamrcx nymeponannte nepemennte $1, $2,, n xoroptx coepxnrcx rexcr, coornercr-
nymmn xpyrntm cxoxam. B uacrnocrn, ecnn nect manon saxnmunrt n xpyrnte cxoxn,
ro n cnyuae ycnemnoro noncxa nepemennax $1 yer coepxart rexcr, coornecennt c
manonom. (Hocne ycnemnoro noncxa moxno raxxe ncnontsonart cnennantnte nepemen-
nte $, $, $ n $+ cm. panee pasen Cctnxn na naennt rexcr.) Hpnmep:
$fexf = "---one---fWo---fhee---"
$sca1a = {$fexf = m/{\W+}/}
p1nf "kesu1f: $sca1a {$1}."
kesu1f: 1 {one}.
Ecnn nt ncnontsyere xomany m/.../ n cnncxonom xonrexcre, ro nosnpamaemoe snauenne
cnntno sanncnr or roro, ecrt nn rpynnt ns xpyrntx cxoox n namem manone. Ecnn onn ecrt
(ro ecrt ecnn cosamrcx nymeponannte nepemennte), ro nocne ycnemnoro noncxa n xauecrne
pesyntrara yer nonyuen cnncox, cocrannennt ns nymeponanntx nepemenntx ($1, $2, ...):
$fexf = "---one, fWo, fhee---"
0aay = {$fexf = m/{\W+},\s+{\W+},\s+{\W+}/}
p1nf o1n {"=", 0aay}
one-fWo-fhee.
B ornnune or pannnx nepcn, Perl 5 npncnannaer snauennx nymeponanntm nepemenntm,
axe ecnn xomana noncxa paoraer n cnncxonom xonrexcre:
$fexf = "---one, fWo, fhee---"
{$Ia, $Ib, $Ic} = {$fexf = m/{\W+},\s+{\W+},\s+{\W+}/}
p1nf "/$Ia/$Ib/$Ic/\n"
p1nf "$1=$2=$3.\n"
/one/fWo/fhee/
one=fWo=fhee.
Ecnn xe n manone ner rpynn, ntenenntx xpyrntmn cxoxamn, ro n cnyuae ycnemnoro no-
ncxa nosnpamaercx cnncox, cocroxmn ns onoro +nemenra uncna 1. Hpn neyaunom nonc-
xe nesanncnmo or roro, tnn nn n manone xpyrnte cxoxn, nosnpamaercx nycro cnncox:
$fexf = "---one, fWo, fhee---"
0aay = {$fexf = m/\W+/}
p1nf "kesu1f: /", 0aay, "/\n"
p1nf "51e: ", $#aay+1, ".\n"
kesu1f://
Crp.122
51e: 0.
(Oparnre nnnmanne na pasnnny mexy nvcmi+ u ueonpeoe.euui+ cnncxamn.)
Paofa koraupu /.../ e peure rnoanuuoro noucka
Komana m/.../ paoraer nnaue, ecnn yxasan mon]nxarop g, saammn rnoantnt noncx
ncex nxoxenn manona no ncemy rexcry. Ecnn oneparop ncnontsyercx n cnncxonom xon-
rexcre n n manone ecrt rpynnt xpyrntx cxoox, ro n cnyuae yaunoro noncxa nosnpama-
ercx cnncox, cocroxmn ns ecex naenntx rpynn, pacnonoxenntx pyr sa pyrom:
$fexf = "---one---fWo---fhee---"
0aay = {$fexf = m/{-{\W+}}/}:
p1nf "51ng1e: |", o1n{", ", 0aay}, "].\n"
0aay = {$fexf = m/{-{\W+}}/g}
p1nf "G1oba1: |", o1n{", ", 0aay}, "].\n"
51ng1e: |-one, one].
G1oba1: |-one, one, -fWo, fWo, -fhee, fhee].
Ecnn xe n manone ner rpynn xpyrntx cxoox, ro oneparop noncxa nosnpamaer cnncox
ncex naenntx nporornnon manona, ro ecrt neer cex rax, xax ecnn t nect manon
tn saxnmuen n xpyrnte cxoxn:
$fexf = "---one---fWo---fhee---"
0aay = {$fexf = m/\W+/}
p1nf "kesu1f: {", o1n{", ", 0aay}, "}.\n"
kesu1f: {one, fWo, fhee}.
B cnyuae neyaunoro noncxa, xax n n npetymnx napnanrax, nosnpamaercx nycro cnncox.
B cxanxpnom xonrexcre n c mon]nxaropom g xomana m/.../ neer cex conepmenno ocotm
opasom. Cnennantnax nepemennax $_ nnn nepemennax, croxmax cnena or oneparopa nnn !-
, npn noncxe c mon]nxaropom g nonyuaer ononnnrentnte cnocrna n nee sannctnaercx
nocnenee cocroxnne. Hpn xaxom nocneymmem opamennn x annomy ]parmenry xoa no-
ncx yer npoonxartcx c roro mecra, na xoropom on ocranonnncx n nocnenn pas. Hanpn-
mep, cneymmax xomana nocunrtnaer xonnuecrno yxn x n saanno crpoxe rexcra:
$fexf = "hee 1s fexxxxxf."
$counfe = 0
Wh11e {$fexf = m/x/g} { Wh11e {$fexf = m/x/g} { Wh11e {$fexf = m/x/g} { Wh11e {$fexf = m/x/g} {
p1nf "Iound anofhe x.\n"
$counfe++
}
p1nf "1ofa1 amounf = $counfe.\n"
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
1ofa1 amounf = 5.
Cocroxnne (rounee, nosnnnx) noncxa coxpanxercx axe n cnyuae nepexoa x cneymmemy
oneparopy noncxa, nmemmemy mon]nxarop g. Heyaunt noncx cpactnaer snauenne n
ncxonoe cocroxnne, ecnn rontxo nx xomant m/.../ ne yxasan mon]nxarop c (ro ecrt xo-
mana onxna nmert nn m/.../gc). Hsmenenne rexcronoro y]epa, nx xoroporo ntnonnx-
ercx noncx, raxxe cpactnaer nosnnnm noncxa n ncxonoe cocroxnne. B cneymmem npn-
mepe ns rexcrono crpoxn nocneonarentno nsnnexamrcx n ntnoxrcx napt nmx/snauenne
Crp.123
o rex nop, noxa crpoxa ne saxonunrcx:
$fexf = "=5 117e=3.1416 femp=1024"
$docyc1e = 1 $counfe = 0
Wh11e {$docyc1e} {
undef $name undef $va1ue
1f {$fexf = m/{ 1f {$fexf = m/{ 1f {$fexf = m/{ 1f {$fexf = m/{\ \\ \W+} W+} W+} W+}\ \\ \s"= s"= s"= s"=\ \\ \s"/g} {$name = $1} s"/g} {$name = $1} s"/g} {$name = $1} s"/g} {$name = $1}
1f {$fexf 1f {$fexf 1f {$fexf 1f {$fexf = m/{| = m/{| = m/{| = m/{|\ \\ \d dd d\ \\ \. .. .\ \\ \+ ++ +\ \\ \- -- -]"} ]"} ]"} ]"}\ \\ \s"/g} {$va1ue = $1} s"/g} {$va1ue = $1} s"/g} {$va1ue = $1} s"/g} {$va1ue = $1}
1f {def1ned{$name} and def1ned{$va1ue}} {
p1nf "Name=$name, va1ue=$va1ue.\n"
$counfe++
} e1se {
$docyc1e = 0
}
}
p1nf "l have found $counfe va1ues.\n"
Name=, va1ue=5.
Name=117e, va1ue=3. 1416.
Name=femp, va1ue=1024.
l have found 3 va1ues.
Hosnnnx, na xoropo ocranonnncx noncx, moxer trt npounrana n axe nepeycranonnena
c nomomtm ncrpoenno ]ynxnnn Perl pos. B manone na rexymym nosnnnm noncxa mox-
no cctnartcx c nomomtm meracnmnona \G. B cneymmem npnmepe ns crpoxn nocneona-
rentno nsnnexamrcx yxnt p, o n q n ntnonrcx rexymax nosnnnx noncxa:
$1ndex = 0
$ = "ppooqppqq"
Wh11e {$1ndex++ < 2} {
p1nf "1: "
p1nf $1 Wh11e /{o}/gc p1nf ", pos=", pos, "\n"
p1nf "2: "
p1nf $1 1f /\G{q}/gc p1nf ", pos=", pos, "\n"
p1nf "3: "
p1nf $1 Wh11e /{p}/gc p1nf ", pos=", pos, "\n"
}
1: oo, pos=4
2: q, pos=5
3: pp, pos=7
1: , pos=7
2: q, pos=
3: , pos=
B oxymenrannn Perl npnnonrcx ocnonannt na +rom mexannsme nnrepecnt npnmep
nocneonarentnoro nexcnuecxoro pasopa rexcra. B nem xaxax nocneymmax xomana
noncxa ouepeno nexcnuecxo ennnnt naunnaer ntnonnxrtcx c roro mecra, re sanep-
mnna cnom paory npetymax. Conerym nnnmarentno pasopartcx c +rnm npnmepom
(crpannna pyxonocrna perlop, pasen Regexp Quote-Like Operators, onncanne xomant
m/PATTEN/), ecnn nt xornre pacmnpnrt ocrynnt nam nncrpymenrapn Perl!
3areua cfpok c norouk koraupu tr/.../.../
Kpome xoman m/.../ n s/.../.../ crpoxn moxno opaartnart c nomomtm xomant tr/.../.../
(ona xe xomana y/.../.../):
f/cnucox1/cnucox2/uouuxa1opu
y/cnucox1/cnucox2/uouuxa1opu
Crp.124
B ornnune or m/.../ n s/.../.../, +ra xomana ne ncnontsyer manont n perynxpnte ntpaxe-
nnx, a ntnonnxer nocnmnontnym sameny, nocrannxx n rexcr nmecro nnrep ns nepnoro
cnncxa coornercrnymmne nm nnrept ns nroporo cnncxa. Hanpnmep, n cneymmem cnyuae
nponsnonrcx samena nnrep i na o:
$fexf = "My name 1s 11m."
$fexf = f/1/o/
p1nf $fexf
My name 1s 1om.
B xauecrne cnncxon ncnontsymrcx nymne pyr sa pyrom cnmnont, ne pasenxemte sa-
nxrtmn (ro ecrt +ro cxopee crpoxn, uem cnncxn). B ornnune or manonon xoman m/.../ n
s/.../.../, aprymenrt xomant tr/.../.../ ue uumepno.upvmmc (ro ecrt nocranonxn snauenn
nmecro nmen nepemenntx ne nponcxonr), xorx escape-nocneonarentnocrn, yxasannte
nnyrpn aprymenron, opaartnamrcx npannntno.
Hoono m/.../ n s/.../.../, xomana tr/.../.../ no ymonuannm paoraer c nepemenno $_:
Wh11e {<>} {
f/1l//
p1nf
}
B xauecrne cnncxon moxno yxastnart nanasont cnmnonon xax, nanpnmep, n cneym-
mem ]parmenre xoa, samenxmmem crpounte yxnt na sarnannte:
$fexf = "hee 1s fhe fexf."
$fexf = f/a-/A-/
p1nf $fexf
hLkL l5 1hL 1L1.
Kax n n cnyuae m/.../ n s/.../.../, xomana tr/.../.../ ne rpeyer ncnontsonart nmenno snaxn
xoco ueprt n xauecrne orpannunrene. Moxno ncnontsonart npaxrnuecxn nmo cnm-
non, ornnunt or npoentntx, yxn n nn]p, a raxxe napnte cxoounte xoncrpyxnnn,
onncannte panee n pasene Ocoennocrn paort xoman m/.../ n s/.../.../.
Komana tr/.../.../ nosnpamaer uncno ycnemntx samen. B uacrnocrn, ecnn ne tno cenano
nnxaxnx samen, ona nosnpamaer uncno nont. 3ro nosnonxer, nanpnmep, nocunrart c no-
momtm xomant tr/.../.../ xonnuecrno nxoxenn yxnt x n crpoxy $text, ne menxx coep-
xnmoro +ro nepemenno:
$fexf = "hee 1s fhe fexf."
$xcounf = {$fexf = f/x/x/}
p1nf $xcounf
1
Ecnn y xomant tr/.../.../ ner mon]nxaropon (cm. anee pasen Mon]nxaropt xomant
tr/.../.../), ro ee aprymenrt npn otuntx ycnonnxx onxnt trt onnaxono nnnt. Ecnn
nropo aprymenr nnnnee nepnoro, ro on ycexaercx o nnnt nepnoro aprymenra rax, xo-
mana tr/abc/0-9/ +xnnnanenrna xomane tr/abc/012/. Ecnn nepnt aprymenr nnnnee nroporo n
nropo ne nycr, ro nx nroporo aprymenra neoxonmoe uncno pas nonropxercx ero nocnenn
cnmnon rax, xomana tr/0-9/abc/ +xnnnanenrna xomane tr/0123456789/abcccccccc/. Ecnn xe
nropo aprymenr nycr, ro xomana tr/.../.../ nocrannxer nmecro nero nepnt aprymenr.
Kax nerxo samernrt, ecnn nropo aprymenr nycr, ro (npn orcyrcrnnn mon]nxaropon) xo-
mana tr/.../.../ ne nponsnonr nnxaxnx ecrnn, a nosnpamaemoe em snauenne panno unc-
ny connaenn mexy nepntm aprymenrom n opaartnaemtm rexcrom. Hanpnmep, cne-
ymmax xomana nocunrtnaer xonnuecrno nn]p n crpoxe:
Crp.125
$fexf = "P1=3.1415926536, e=2.712"
$d1g1fcounfe={$fexf = f/0-9//}
p1nf $d1g1fcounfe
16
Komana tr/.../.../ paoraer es pexypcnn, npocro nocneonarentno samenxer cnmnont
nxonoro rexcra. Hanpnmep, nx sament sarnanntx yxn na crpounte, n naoopor, ocra-
rouno ntnonnnrt xomany:
$fexf = "M5 W1ndoWs 95/9/N1"
$fexf = f/A-a-/a-A-/
p1nf $fexf
ms WlNDOW5 95/9/nf
Ecnn n cnncxe, yxasannom n xauecrne nepnoro aprymenra, ecrt nonropxmmnecx cnmnont,
ro nx sament ncnontsyercx nepnoe nxoxenne cnmnona:
$fexf = "8111y Gafes"
$fexf = f/fff/mvd/
p1nf $fexf
8111y Games
Mopuqukafopu koraupu tr/.../.../
Komana tr/.../.../ onycxaer ncnontsonanne cneymmnx mon]nxaropon:
d yanxer nenapnte cnmnont, ne ntpannnnax aprymenrt no nnne.
c n xauecrne nepnoro aprymenra ncnontsyer nonnt cnncox ns 25 cnmnonon
sa ntuerom yxasanntx n cnncxe cnmnonon.
s yanxer opasonanmnecx n pesyntrare sament nonropxmmnecx cnmnont.
Ecnn yxasan mon]nxarop d, a nepnt aprymenr xomant nnnnee nroporo, ro nce cnmnont
ns nepnoro cnncxa, ne nmemmne coornercrnnx co nroptm cnncxom, yanxmrcx ns opaart-
naemoro rexcra. Hpnmep: yanxem crpounte narnncxne yxnt n samenxem npoent na cn+mn:
$fexf = "hee 1s fhe fexf."
$fexf = f| a-]|/]d
p1nf $fexf
h///.
(Hannune mon]nxaropa d enncrnennt cnyua, xora nepnt n nropo aprymenrt ne
ntpannnnamrcx pyr ornocnrentno pyra. B ocrantntx napnanrax nropo aprymenr nno yce-
xaercx, nno nocnenn cnmnon n nem nonropxercx o rex nop, noxa aprymenrt ne cpannxmrcx,
nno, ecnn nropo aprymenr nycr, nmecro nroporo aprymenra epercx xonnx nepnoro.)
Ecnn yxasan mon]nxarop c, ro n xauecrne nepnoro aprymenra paccmarpnnamrcx nce cnm-
nont, rpo+e yxasanntx. Hanpnmep, samennm na snesouxn nce cnmnont, xpome crpountx
narnncxnx yxn:
$fexf = "hee 1s fhe fexf."
$fexf = f/a-/"/c
p1nf $fexf
"ee"1s"fhe"fexf"
Ecnn yxasan mon]nxarop s, ro n cnyuae ecnn samemaemte cnmnont opasymr nenouxn ns
onnaxontx cnmnonon, onn coxpamamrcx o onoro. Hanpnmep, samennm cnona, cocrox-
mne ns narnncxnx yxn, na onoxparnte cnmnont xoco ueprt:
Crp.126
$fexf = "hee 1s fhe fexf."
$fexf = f{A-a-}{/}s
p1nf $fexf
/ / / /.
Fes mon]nxaropa s pesyntrar tn t pyrnm:
$fexf = "hee 1s fhe fexf."
$fexf = f{A-a-}{/}
p1nf $fexf
//// // /// ////.
Hpnmept:
1. 3amennrt mnoxecrnennte npoent n nerexcronte cnmnont na onnounte npoent:
$fexf = "hee 1s fhe fexf."
$fexf = f|\000-\040\177\377]|\040]s
p1nf $fexf
hee 1s fhe fexf.
2. Coxparnrt ynoennte, yrpoennte n r. . yxnt:
$fexf = "hee 1s fhe fexxxxxxf."
$fexf = f/a-A-//s
p1nf $fexf
hee 1s fhe fexf.
3. Hepecunrart xonnuecrno neyxnenntx cnmnonon:
$xcounf={$fexf = f/A-a-//c}:
. Onynnrt noctmo nr cnmnonon, yannrt nerexcronte cnmnont:
$fexf = f{\200-\377}{\000-\177}
$fexf = f|\000-\037\177]|]d
5. 3amennrt nerexcronte n -nrnte cnmnont na onnount npoen:
$fexf = f/\021-\176/ /cs:
Rouck ofpenuuux cnoe
urot ntennrt cnono, moxno ncnontsonart meracnmnon \S, coornercrnymmn cnmno-
nam, ornnuntm or npoentntx:
$fexf = "NoW 1s fhe f1me."
$ $$ $fexf fexf fexf fexf = /{ = /{ = /{ = /{\ \\ \5 55 5+}/ +}/ +}/ +}/
p1nf $1
NoW
Onaxo meracnmnon \S coornercrnyer raxxe n cnmnonam, otuno ne ncnontsyemtm nx
nenrn]nxaropon. urot oropart cnona, cocrannennte ns narnncxnx yxn, nn]p n cnm-
nonon nouepxnnannx, nyxno ncnontsonart meracnmnon \w:
$fexf = "NoW 1s fhe f1me."
$ $$ $fexf fexf fexf fexf = = = = /{ /{ /{ /{\ \\ \W WW W+}/ +}/ +}/ +}/
p1nf $1
NoW
Ecnn rpeyercx nxnmunrt n noncx rontxo narnncxne yxnt, nao ncnontsonart xnacc cnmnonon:
$fexf = "NoW 1s fhe f1me."
$fexf $fexf $fexf $fexf = = = = /{|A /{|A /{|A /{|A- -- -a a a a- -- -]+}/ ]+}/ ]+}/ ]+}/
p1nf $1
Crp.127
NoW
Fonee esonacnt mero cocronr n rom, urot nxnmunrt n manon mnnmte cnmnont rpa-
nnnt cnona:
$fexf = "NoW 1s fhe f1me."
$fexf = / $fexf = / $fexf = / $fexf = /\ \\ \b{|A b{|A b{|A b{|A- -- -a a a a- -- -]+} ]+} ]+} ]+}\ \\ \b/ b/ b/ b/
p1nf $1
NoW
Rpuenska k uaany cfpoku
Hauany crpoxn coornercrnyer meracnmnon (mnnmt cnmnon) . urot npnnxsart manon
x nauany crpoxn, nao saart +ror cnmnon n nauane perynxpnoro ntpaxennx. Hanpnmep,
nor rax moxno nponepnrt, uro rexcr ne naunnaercx c rouxn:
$11ne = ".he11o!"
1f {$11ne = m/ 1f {$11ne = m/ 1f {$11ne = m/ 1f {$11ne = m/\ \\ \./} { ./} { ./} { ./} {
p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od! p1nf "5hou1dnf sfaf a senfence W1fh a pe1od!\ \\ \n" n" n" n"
} }} }
5hou1dnf sfaf a senfence W1fh a pe1od!
Hocuasua. Umooi mo:ra, vrasauua e uao.oue, ue uumepnpemupoea.aci rar +emacu+eo., nepeo ue
npuu.oci nocmaeumi oopamuvm rocvm :epmv.
Rpuenska k kouuy cfpoku
urot npnnxsart manon x xonny crpoxn, ncnontsyercx meracnmnon (mnnmt cnmnon) $.
B namem npnmepe mt ncnontsyem npnnxsxy manona x nauany n x xonny crpoxn, urot
yenrtcx, uro nontsonarent nnen rontxo cnono exit:
Wh11e {<>} {
1f {m/ex1f$/} {ex1f} 1f {m/ex1f$/} {ex1f} 1f {m/ex1f$/} {ex1f} 1f {m/ex1f$/} {ex1f}
}
Rouck ucen
nx nponepxn roro, ecrnnrentno nn nontsonarent nnen uncno, moxno ncnontsonart me-
racnmnont \d n \D. Meracnmnon \D coornercrnyer nmomy cnmnony, xpome nn]p. Hanpn-
mep, cneymmn xo nponepxer, ecrnnrentno nn nneennt rexcr npecrannxer coo
nenoe snauenne es snaxa n napasnrntx npoenon:
$fexf = "he11o!"
1f {$fexf = / 1f {$fexf = / 1f {$fexf = / 1f {$fexf = /\ \\ \D/} { D/} { D/} { D/} {
p1nf "lf 1 p1nf "lf 1 p1nf "lf 1 p1nf "lf 1s nof a numbe. s nof a numbe. s nof a numbe. s nof a numbe. \ \\ \n" n" n" n"
} }} }
lf 1s nof a numbe.
To xe camoe moxno npoenart, ncnontsonan meracnmnon \d:
$fexf = "333"
1f {$fexf 1f {$fexf 1f {$fexf 1f {$fexf = = = = / / / /\ \\ \d+$/} { d+$/} { d+$/} { d+$/} {
p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. \ \\ \n" n" n" n"
} }} }
lf 1s a numbe.
Bt moxere norpeonart, urot uncno coornercrnonano npnntunomy ]opmary. To ecrt
Crp.128
uncno moxer coepxart ecxrnunym rouxy, nepe xoropo cronr, no xpane mepe, ona
nn]pa n, nosmoxno, xaxne-ro nn]pt nocne nee:
$fexf = "3.1415926"
1f {$fexf = /{ 1f {$fexf = /{ 1f {$fexf = /{ 1f {$fexf = /{\ \\ \d+ d+ d+ d+\ \\ \. .. .\ \\ \d"| d"| d"| d"|\ \\ \d+}$/} { d+}$/} { d+}$/} { d+}$/} {
p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. \ \\ \n nn n" " " "
} }} }
lf 1s a numbe.
Hocuasua. Umooi mo:ra, vrasauua e uao.oue, ue uumepnpemupoea.aci rar +emacu+eo., ue saovoime no-
cmaeumi nepeo ue oopamuvm rocvm :epmv.
Kpome roro, npn nponepxe moxno yunrtnart ror ]axr, uro nepe uncnom moxer croxrt
xax nnmc, rax n mnnyc (nnn nycroe mecro):
$fexf = "-2.712"
1f 1f 1f 1f {$fexf = /{|+ {$fexf = /{|+ {$fexf = /{|+ {$fexf = /{|+- -- -]" ]" ]" ]"\ \\ \d+{ d+{ d+{ d+{\ \\ \. .. .\ \\ \d"|}$/} { d"|}$/} { d"|}$/} { d"|}$/} {
p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. \ \\ \n" n" n" n"
} }} }
lf 1s a numbe.
Hocuasua. Hocro.irv n.mc e.emc +emacu+eo.o+, eeo uaoo sauuuami oopamuo roco :epmo. Oouaro
euvmpu reaopamuix crooor, mo ecmi r.acca cu+eo.oe, ou ue +orem oimi reaumuquramopo+. 3uar +uuvc
euvmpu r.acca cu+eo.oe ooi:uo uepaem po.i onepamopa ouanasoua u noomo+v oo.reu sauuuamic oopam-
uo roco :epmo. Oouaro e ua:a.e u.u e roue uao.oua ou uurar ue +orem ooosua:ami ouanasou, u noomo-
+v oopamua roca :epma ueoosame.iua.
Haxonen, onee crporax nponepxa rpeyer, urot snax, ecnn on npncyrcrnyer, tn rontxo onn:
$fexf = "+0.142571425714257"
1f 1f 1f 1f {$fexf {$fexf {$fexf {$fexf = /{+| = /{+| = /{+| = /{+|- -- -|} |} |} |}\ \\ \d+{ d+{ d+{ d+{\ \\ \. .. .\ \\ \d"|}$/} { d"|}$/} { d"|}$/} { d"|}$/} {
p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. p1nf "lf 1s a numbe. \ \\ \n" n" n" n"
} }} }
lf 1s a numbe.
Hocuasua. A.imepuamueuie uao.oui, ec.u ouu npucvmcmevmm, npoeepmmc c.eea uanpaeo. Hepeoop eapu-
aumoe oopieaemc, rar mo.iro uaoeuo coomeemcmeue +erov mercmo+ u uao.ouo+. Hoomo+v, uanpu+ep,
nopoor a.imepuamue e uao.oue (.d[) +oe oi cmami rpumu:ui+, ec.u oi ue npuesra r rouv cmporu.
Haxonen, nor xax moxno nponsnecrn nponepxy roro, uro rexcr xnnxercx mecrnanarepnu-
ntm uncnom es snaxa n ocrantntx arpnyron:
$fexf = "1A0"
un1ess {$fexf un1ess {$fexf un1ess {$fexf un1ess {$fexf = = = = m/|a m/|a m/|a m/|a- -- -fA fA fA fA- -- -I II I\ \\ \d]+$/} d]+$/} d]+$/} d]+$/} { {{ {
p1nf "lf 1s nof a hex numbe. p1nf "lf 1s nof a hex numbe. p1nf "lf 1s nof a hex numbe. p1nf "lf 1s nof a hex numbe. \ \\ \n nn n" " " "
} }} }
Hocuasua. Hpuov+ame ca+u, rar oooaeumi oecmu:uvm +aumuccv Enn r qop+amv oecmu:uoeo :uc.a, a
marre suar u npequrc Ox r qop+amv uecmuaoamepu:uom :uc.a.
Rpoeepka upeufuqukafopoe
C nomomtm meracnmnona w moxno nponepnrt, cocronr nn rexcr rontxo ns yxn, nn]p n cnm-
nonon nouepxnnannx (+ro re cnmnont, xoropte Perl nastnaer c.oeui+u (word characters)):
$fexf = "abc"
1f {$fexf = / 1f {$fexf = / 1f {$fexf = / 1f {$fexf = /\ \\ \W+$/} { W+$/} { W+$/} { W+$/} {
p1nf p1nf p1nf p1nf "On1 "On1 "On1 "On1y Wod chaacfes found. y Wod chaacfes found. y Wod chaacfes found. y Wod chaacfes found. \ \\ \n" n" n" n"
} }} }
On1y Wod chaacfes found.
Onaxo, ecnn nt xornre yenrtcx, uro rexcr coepxnr narnncxne yxnt n ne coepxnr
nn]p nnn cnmnonon nouepxnnannx, npnercx ncnontsonart pyro manon:
Crp.129
$fexf = "a8c"
1f 1f 1f 1f {$fexf = /|A {$fexf = /|A {$fexf = /|A {$fexf = /|A- -- -a a a a- -- -]+$/} { ]+$/} { ]+$/} { ]+$/} {
p1nf "On1y 1effe chaacfes found. p1nf "On1y 1effe chaacfes found. p1nf "On1y 1effe chaacfes found. p1nf "On1y 1effe chaacfes found. \ \\ \n" n" n" n"
} }} }
On1y 1effe chaacfes found.
Haxonen, nx nponepxn, uro rexcr xnnxercx nenrn]nxaropom, ro ecrt naunnaercx c yx-
nt n coepxnr yxnt, nn]pt n cnmnont nouepxnnannx, moxno ncnontsonart xomany:
$fexf = "125c"
1f 1f 1f 1f {$fexf {$fexf {$fexf {$fexf = = = = /|A /|A /|A /|A- -- -a a a a- -- -] ] ] ]\ \\ \W+$/} W+$/} W+$/} W+$/} { {{ {
p1nf "1h1s 1s 1denf1f1e. p1nf "1h1s 1s 1denf1f1e. p1nf "1h1s 1s 1denf1f1e. p1nf "1h1s 1s 1denf1f1e. \ \\ \n" n" n" n"
} }} }
1h1s 1s 1denf1f1e.
Hocuasua. Hpuov+ame, rar ucr.m:umi us cu+eo.oe, ucno.isve+ix o. nocmpoeuu uoeumuquramopoe,
suar noo:eprueauu.
Kak uafu ruoecfeeuuue coenapeuun
nx noncxa necxontxnx nxoxenn manona moxno ncnontsonart mon]nxarop g. Cne-
ymmn npnmep, xoropt mt yxe nnenn panee, ncnontsyer xomany m/.../.../ c mon]n-
xaropom g nx noncxa ncex nxoxenn yxnt x n rexcre:
$fexf = "hee 1s fexxxxxf."
W WW Wh11e {$fexf h11e {$fexf h11e {$fexf h11e {$fexf = = = = m/x/g} { m/x/g} { m/x/g} { m/x/g} {
p1nf "Iound anofhe x.\n"
}
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
Iound anofhe x.
Mon]nxarop g enaer noncx rnoantntm. B annom (cxanxpnom) xonrexcre Perl nomnnr,
re on ocranonnncx n crpoxe npn npetymem noncxe. Cneymmn noncx npoonxaercx c
ornoxenno rouxn. Fes mon]nxaropa g xomana m/.../ yer ynopno naxonrt nepnoe
nxoxenne yxnt x, n nnxn yer npoonxartcx ecxoneuno.
B ornnune or xomant m/.../ xomana s/.../.../ c mon]nxaropom g ntnonnxer rnoantnym same-
ny sa onn pas, paorax rax, yro nnyrpn nee yxe nmeercx ncrpoennt nnxn noncxa, noo-
nt npnneennomy ntme. Cneymmn npnmep sa onn pas samenxer nce nxoxennx x na :
$fexf = "hee 1s fexxxxxf."
$fexf = s/x//g $fexf = s/x//g $fexf = s/x//g $fexf = s/x//g
p1nf $fexf
hee 1s fef.
Fes mon]nxaropa g xomana s/.../.../ samennr rontxo nepnym yxny x.
Komana s/.../.../ nosnpamaer n xauecrne snauennx uncno cenanntx nocranonox, uro mo-
xer oxasartcx nonesntm:
$fexf = "hee 1s fexxxxxf."
p1nf {fexf = s/x//g} p1nf {fexf = s/x//g} p1nf {fexf = s/x//g} p1nf {fexf = s/x//g}
5
Rouck ueyecfeufenuuux k perucfpy coenapeuu
Crp.130
Bt moxere ncnontsonart mon]nxarop i, urot cenart noncx neuyncrnnrentntm x pas-
nnne mexy sarnanntmn n crpountmn yxnamn. B cneymmem npnmepe nporpamma no-
nropxer na +xpane nneennt nontsonarenem rexcr o rex nop, noxa ne yer nneeno
nnn q (coxpamenne nx QUIT nnn quit), nocne uero nporpamma npexpamaer paory:
Wh11e {<>} {
chomp
un1ess {/q$/1} { p1nf }
e1se { ex1f }
}
Bupeneuue nopcfpoku
urot nonyunrt naennym nocrpoxy rexcra, moxno ncnontsonart xpyrnte cxoxn n rene
manona. (Ecnn +ro onee yono, moxno raxxe ncnontsonart ncrpoennym ]ynxnnm substr.)
B cneymmem npnmepe mt ntpesaem ns rexcrono crpoxn nyxnt nam rnn nsennx:
$ecod = "Poducf numbe: 12345
Poducf fype: p1nfe
Poducf p1ce: $325"
1f {$ecod = /Poducf fype: 1f {$ecod = /Poducf fype: 1f {$ecod = /Poducf fype: 1f {$ecod = /Poducf fype:\ \\ \s"{|a s"{|a s"{|a s"{|a- -- -]+}/1} ]+}/1} ]+}/1} ]+}/1} { {{ {
p1nf "1he poducfs fype 1s $1. p1nf "1he poducfs fype 1s $1. p1nf "1he poducfs fype 1s $1. p1nf "1he poducfs fype 1s $1.\ \\ \n" n" n" n"
} }} }
1he poducfs fype 1s p1nfe.
Busoe qyukuu u euucneuue eupaeuu npu nopcfauoeke fekcfa
Hcnontsyx nx xomant s/.../.../ mon]nxarop e, nt ro rem camtm noxastnaere, uro npa-
nt onepan (ro ecrt noc1annneumn rexcr) +ro ro ntpaxenne Perl, xoropoe nao nt-
uncnnrt. Hanpnmep, c nomomtm ncrpoenno ]ynxnnn Perl uc (uppercase) moxno samennrt
nce crpounte yxnt cnon crpoxn na sarnannte:
$fexf = "NoW 1s fhe f1me."
$fexf = s/{ $fexf = s/{ $fexf = s/{ $fexf = s/{\ \\ \W+}/uc{$1}/ge W+}/uc{$1}/ge W+}/uc{$1}/ge W+}/uc{$1}/ge
p1nf $fexf
NOW l5 1hL 1lML.
Bmecro ]ynxnnn uc($1) moxno nomecrnrt nponsnontnt xo, nxnmuax ntsont nonporpamm.
Rouck n-ro coenapeuun
C nomomtm mon]nxaropa g nepenpamrcx nce nxoxennx saannoro manona. Ho uro
enart, ecnn nyxna nnonne onpeenennax rouxa connaennx c manonom - nanpnmep, nro-
pax nnn rpertx Oneparop nnxna while n couerannn c xpyrntmn cxoxamn, ntenxmmnmn
nyxnt opasen, nomoxer nam:
$fexf = "Name:Anne Name:8ukaf Name:C1a1e Name:Dan"
$mafch = 0
Wh11e {$fexf = / Wh11e {$fexf = / Wh11e {$fexf = / Wh11e {$fexf = /Name: Name: Name: Name:\ \\ \s"{ s"{ s"{ s"{\ \\ \W+}/g} W+}/g} W+}/g} W+}/g}
{ ++$mafch { ++$mafch { ++$mafch { ++$mafch
p1nf p1nf p1nf p1nf "Mafch numbe $mafch 1s $1. "Mafch numbe $mafch 1s $1. "Mafch numbe $mafch 1s $1. "Mafch numbe $mafch 1s $1.\ \\ \n" n" n" n" } } } }
Mafch numbe 1 1s Anne
Mafch numbe 2 1s 8ukaf
Mafch numbe 3 1s C1a1e
Mafch numbe 4 1s Dan
Crp.131
3ror npnmep moxno nepenncart, ncnontsyx nnxn for:
$fexf = "Name:Anne Name:8ukaf Name:C1a1e Name:Dan"
fo {$mafch = 0 fo {$mafch = 0 fo {$mafch = 0 fo {$mafch = 0
$fexf = /Name: $fexf = /Name: $fexf = /Name: $fexf = /Name:\ \\ \s"{ s"{ s"{ s"{\ \\ \W+}/g W+}/g W+}/g W+}/g
p1nf "Mafch numbe ${ p1nf "Mafch numbe ${ p1nf "Mafch numbe ${ p1nf "Mafch numbe ${\ \\ \++$mafch} 1s $1. ++$mafch} 1s $1. ++$mafch} 1s $1. ++$mafch} 1s $1.\ \\ \n"} n"} n"} n"}
{} {} {} {}
Mafch numbe 1 1s Anne
Mafch numbe 2 1s 8ukaf
Mafch numbe 3 1s C1a1e
Mafch numbe 4 1s Dan
Ecnn xe nam rpeyercx onpeennrt nyxnoe connaenne ne no nomepy, a no coepxannm (na-
npnmep, no nepno yxne nmenn nontsonarenx), ro nmecro cuerunxa $match moxno anannsn-
ponart coepxnmoe nepemenno $1, ononnxemo npn xaxom naennom connaennn.
Kora rpeyercx ne narn, a samennrt nropoe nnn rperte nxoxenne rexcra, moxno npn-
mennrt ry xe cxemy, ncnontsonan n xauecrne rena nnxna ntpaxenne Perl, ntstnaemoe nx
ntuncnennx samenxmme crpoxn:
$fexf = "Name:Anne Name:8ukaf Name:C1a1e Name:Dan"
$mafch = 0
$fexf = s/{Name: $fexf = s/{Name: $fexf = s/{Name: $fexf = s/{Name: \ \\ \s"{ s"{ s"{ s"{\ \\ \W+}}/ W+}}/ W+}}/ W+}}/ ### ### ### ### uauuuae1cn uauuuae1cn uauuuae1cn uauuuae1cn xo xo xo xo Pe1 Pe1 Pe1 Pe1
1f 1f 1f 1f {++$ {++$ {++$ {++$mafch mafch mafch mafch == 2} == 2} == 2} == 2} # yaenuuu1u cue1uux # yaenuuu1u cue1uux # yaenuuu1u cue1uux # yaenuuu1u cue1uux
{" {" {" {"Name Name Name Name: :: :ohn ohn ohn ohn {$2}"} {$2}"} {$2}"} {$2}"} # # # # aepuy1u uoaoe auaueuue aepuy1u uoaoe auaueuue aepuy1u uoaoe auaueuue aepuy1u uoaoe auaueuue
e1se e1se e1se e1se {$1} {$1} {$1} {$1} # oc1aau1u c1apoe auaueuue # oc1aau1u c1apoe auaueuue # oc1aau1u c1apoe auaueuue # oc1aau1u c1apoe auaueuue
/ // /gex gex gex gex
p1nf $fexf
Name:Anne Name:ohn {8ukaf} Name:C1a1e Name:Dan
B nponecce rnoantnoro noncxa npn xaxom naennom connaennn ntuncnxercx ntpa-
xenne, yxasannoe n xauecrne nroporo onepana. Hpn ero ntuncnennn ynennunnaercx sna-
uenne cuerunxa, n n sanncnmocrn or nero n xauecrne sament nocrannxercx nno crapoe
snauenne rexcra, nno nonoe. Mon]nxarop x nosnonxer oannrt n none manona xom-
menrapnn, enax xo onee npospauntm. (Oparnre nnnmanne, uro nam npnmnoct saxnm-
unrt nect manon n xpyrnte cxoxn, urot nonyunrt snauenne naennoro rexcra n no-
crannrt ero na npexnee mecro nonnocrtm.)
Kak orpauuufu apuocfu keaufuqukafopoe
Ho ymonuannm xnanrn]nxaropt neyr cex xax xante oexrt. Haunnax c rexyme
nosnnnn noncxa, onn saxnartnamr camym nnnnym crpoxy, xoropo moxer coornercrno-
nart perynxpnoe ntpaxenne, croxmee nepe xnanrn]nxaropom. Anropnrm nepeopa c nos-
nparamn, ncnontsyemt Perl, cnocoen orpannunnart annernr xnanrn]nxaropon, nosnpa-
maxct nasa n ymentmax nnny saxnauenno crpoxn, ecnn ne yanoct narn coornercrnnx
mexy rexcrom n manonom (erann cm. n nopasene anocrt xnanrn]nxaropon
pasena Knanrn]nxaropt n perynxpntx ntpaxennxx). Onaxo +ror mexannsm ne ncera
paoraer rax, xax xorenoct t.
Paccmorpnm cneymmn npnmep. Mt xornm samennrt rexcr That is rexcrom Thats.
Onaxo n cnny xanocrn xnanrn]nxaropa perynxpnoe ntpaxenne .is conocrannxercx
]parmenry rexcra or nauana crpoxn n o nocnenero naennoro is:
$fexf = "1haf 1s some fexf, 1snf 1f"
$fexf = s/."1s/1hafs/
p1nf $fexfs
Crp.132
1hafsnf 1f
urot cenart xnanrn]nxaropt ne cront xantmn, a nmenno sacrannrt nx saxnartnart mn-
nnmantnym crpoxy, c xoropo conocrannmo perynxpnoe ntpaxenne, nocne xnanrn]nxaro-
pa nyxno nocrannrt nonpocnrentnt snax. Tem camtm xnanrn]nxaropt, nepeuncnennte n
pasene Knanrn]nxaropt n perynxpntx ntpaxennxx, npnnnmamr cneymmn nn:
nont nnn necxontxo connaenn,
+ ono nnn necxontxo connaenn,
nont connaenn nnn ono connaenne,
n] ponno n connaenn,
n,] no xpane mepe n connaenn,
n,m] connaenn no xpane mepe n, no ne onee, uem m.
Oparnre nnnmanne, uro cmtcn xnanrn]nxaropa or +roro ne menxercx, menxercx rontxo
noneenne anropnrma noncxa. Ecnn n nponecce conocrannennx manona n rexcra nporornn
onpeenxercx onosnauno, ro anropnrm noncxa c nosnparamn ynennunr xanocrt raxoro
xnanrn]nxaropa rouno rax xe, xax on orpannunnaer annernr ero copara. Onaxo ecnn
ntop neonosnauen, ro pesyntrar noncxa yer pyrnm:
$fexf = "1haf 1s some fexf, 1snf 1f"
$fexf = s/."1s/1hafs/
p1nf $fexfs
1hafs some fexf, 1snf 1f
Kak ypanufu eepyue u saeepuakue npoenu
urot orceut or crpoxn nauantnte npoentnte cnmnont, moxno ncnontsonart cne-
ymmym xomany:
$fexf = " NoW 1s fhe f1me."
$fexf = s/\s+//
p1nf $fexfs
NoW 1s fhe f1me.
urot orceut xnocronte npoent, ronrcx xomana:
$fexf = "NoW 1s fhe f1me. "
$fexf = s/\s+$//
p1nf $fexfs
NoW 1s fhe f1me.
urot orceut n nauantnte, n xnocronte npoent, nyume ntsnart nocneonarentno +rn
ne xomant, uem ncnontsonart manon, enammn orceuenne nenyxntx npoenon sa
onn pas. Hocxontxy nponeypa conocrannennx manona n rexcra ocrarouno cnoxna, na
+ry npocrym onepannm moxer yrn ropaso ontme npemenn, uem xorenoct t.
Yfeeppeuun, npoeepnkue fekcf nepep u nocne uanoua
Kax yxe ynomnnanoct, n perynxpntx ntpaxennxx Perl ecrt xoncrpyxnnn (mnnmte cnmno-
nt nyneno nnnt), nosnonxmmne nponepxrt yrnepxennx o ]parmenrax rexcra, pacno-
noxenntx nepe nponepxemtm manonom nnn nocne nero:
(uao.ou) nocne rexyme rouxn naxonrcx ]parmenr rexcra, coornercrnymmn
yxasannomy perynxpnomy ntpaxennm,
Crp.133
(!uao.ou) nocne rexyme rouxn uem rexcra, coornercrnymmero yxasannomy pery-
nxpnomy ntpaxennm,
(<uao.ou) nepeo rexyme rouxo ecrt ]parmenr rexcra, coornercrnymmn
yxasannomy perynxpnomy ntpaxennm,
(<!uao.ou) nepe rexyme rouxo uem ]parmenra rexcra, coornercrnymme
ro yxasannomy perynxpnomy ntpaxennm.
(Koncrpyxnnn (<uao.ou) n (<!uao.ou) paoramr rontxo c manonamn, coor-
nercrnymmnmn ]nxcnponannomy uncny cnmnonon. Hntmn cnonamn, n manonax, yxast-
naemtx nx (<...) n (<!....), ne onxno trt xnanrn]nxaropon.)
3rn ycnonnx nonesnt, ecnn nyxno nponepnrt, uro nepe onpeenenntm ]parmenrom rex-
cra nnn nocne nero naxonrcx nyxnax crpoxa, onaxo ee ne rpeyercx nxnmuart n pesynt-
rar noncxa. 3ro tnaer neoxonmo, ecnn n xoe ncnontsymrcx cnennantnte nepemennte
$ (]parmenr, nx xoroporo naeno coornercrnne mexy rexcrom n perynxpntm ntpa-
xennem), $ (rexcr, npemecrnymmn naennomy ]parmenry) n $ (rexcr, cneymmn sa
naenntm ]parmenrom). (Fonee rnxnm npecrannxercx npnmenenne nymeponanntx ne-
pemenntx $1, $2, $3, ..., n xoropte sanocxrcx orentnte uacrn naennoro ]parmenra.)
B cneymmem npnmepe nmercx cnono, sa xoroptm cneyer npoen, no cam npoen ne
nxnmuaercx n pesyntrar noncxa:
$fexf = "May 1om Iank"
Wh11e {$fexf = /\W+{=\s}/g} {
p1nf $ . "\n" }
May 1om Iank
Toro xe pesyntrara moxno onrtcx, ecnn saxnmunrt n xpyrnte cxoxn nnrepecymmym
nac uacrt manona n sarem ncnontsonart ee xax nepemennym $1:
$fexf = "May 1om Iank"
Wh11e {$fexf = /{\W+}\s/g} {
p1nf $1 . "\n" }
May 1om Iank
Cneyer uerxo nonnmart, uro nt nmeere n nny, xora ncnontsyere ro nnn nnoe ycnonne.
Paccmorpnm cneymmn npnmep:
$fexf = "May+1om"
1f {$fexf = m|{!May 1f {$fexf = m|{!May 1f {$fexf = m|{!May 1f {$fexf = m|{!May\ \\ \+}1om|} +}1om|} +}1om|} +}1om|} { {{ {
p1nf "1om 1s W1fhouf May!\n"
} e1se {
p1nf "1om 1s busy...\n"
}
Bonpexn namnm oxnannxm, Perl naneuaraer:
1om 1s W1fhouf May!
3ro nponsoer no cneymme npnunne. Hpoyx pasnnunte nauantnte rouxn nxono
crpoxn, or xoropo naunnaercx conocrannenne manona n rexcra, Perl pano nnn nosno
oepercx o nosnnnn, pacnonoxenno npxmo nepe nmenem Tom. Vcnonne (!Mary\+)
rpeyer, urot noc.e rexyme rouxn ne naxonncx rexcr Mary, n +ro ycnonne nx pac-
c+ampueae+o rouxn yer ntnonneno. anee, Perl nocneonarentno nponepxer, uro no-
cne rexyme rouxn cneymr yxnt T, o n m, n +ro rpeonanne raxxe n cnne (nocne
nponepxn ycnonnx (!Mary\+) rexymax rouxa ocraercx na mecre). Tem camtm naeno co-
ornercrnne mexy nocrpoxo Tom n manonom, no+romy xomana noncxa nosnpamaer
Crp.134
snauenne ucmuua.
Perynxpnoe ntpaxenne (!Mary\+)....Tom, pesepnnpymmee uertpe cnmnona no rexcr
Mary, nx npnneennoro ntme cnyuax ntneer ro, uro rpeonanoct, no ntacr omn-
ount orner, ecnn nepe nmenem Tom uem uertpex cnmnonon:
$fexf = "O, 1om! "
1f 1f 1f 1f {$fexf = m|{!May {$fexf = m|{!May {$fexf = m|{!May {$fexf = m|{!May\ \\ \+}....1om|} +}....1om|} +}....1om|} +}....1om|} { {{ {
p1nf "1om 1s W1fhouf May!\n"
} e1se {
p1nf "1om 1s busy...\n"
}
1om 1s busy...
Haxonen, ecnn onee rouno c]opmynnponart, uero rpeyercx, nonyunrcx nyxnt pesyntrar:
$fexf = "May+1om "
1f 1f 1f 1f {$fexf = {$fexf = {$fexf = {$fexf = m|{<!May m|{<!May m|{<!May m|{<!May\ \\ \+}1om|} +}1om|} +}1om|} +}1om|} { {{ {
p1nf "1om 1s W1fhouf May\n"
} e1se {
p1nf "1om 1s busy...\n"
}
1om 1s busy...
Crp.135
Fnana 7
Ropnporpauum

Kopo1ko
Paofa c nopnporparraru
Hex nonporpammt, no cyrn, ecrt nonnomenne craporo nosynra nporpammncron: pase-
nx n nnacrny. Honporpammt nosnonxmr pasnrt xo na uacrn oospnmoro pasmepa, uro
necxontxo ynpomaer nponecc nporpammnponannx. Hpenonoxnm, nanpnmep, rpeyercx
naneuarart na uncna, ntnox nx uncen, xoropte mentme ecxrn, coomenne o omnxe:
$va1ue = 10
1f 1f 1f 1f {$va1 {$va1 {$va1 {$va1ue > 10} { ue > 10} { ue > 10} { ue > 10} {
p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue.\ \\ \n" n" n" n"
} e1se { } e1se { } e1se { } e1se {
p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11. \ \\ \n" n" n" n"
} }} }
$va1ue = 12
1f {$va1ue > 10} { 1f {$va1ue > 10} { 1f {$va1ue > 10} { 1f {$va1ue > 10} {
p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue. p1nf "va1ue 1s $va1ue.\ \\ \n" n" n" n"
} e1se { } e1se { } e1se { } e1se {
p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11. p1nf "va1ue 1s foo sma11.\ \\ \n" n" n" n"
} }} }
va1ue 1s foo sma11.
va1ue 1s 12.
Onaxo moxno cenart nyume c+xonomnrt mecro, ynaxonan nonropxmmnecx noxn ycnon-
ntx oneparopon n nne nonporpammt. Hasonem raxym nonporpammy, nanpnmep, printif:
sub p1nf1fO {
my $1nfena1va1ue = sh1ff{0}
1f {$1nfena1va1ue > 10} {
p1nf "va1ue 1s $1nfena1va1ue.\n"
} e1se {
p1nf "va1ue 1s foo sma11.\n"
}
}
Hepeannte nonporpamme napamerpt sanomnnamrcx, a norom no mepe naonocrn ns-
nnexamrcx ns cnennantnoro maccnna _. Bect ocranmncx xo +ro yxe snaxomt nam
ycnonnt oneparop. urot ncnontsonart nonporpammy, e nao nepeart rpeyemte
snauennx. Pesyntrar, xoneuno, nonyuaercx ponno raxnm xe, xax n n npetymem npnmepe.
$va1ue = 10 $va1ue = 10 $va1ue = 10 $va1ue = 10
p1nf1fO{$va1ue} p1nf1fO{$va1ue} p1nf1fO{$va1ue} p1nf1fO{$va1ue}
$va1ue = 12 $va1ue = 12 $va1ue = 12 $va1ue = 12
Crp.136
p1nf p1nf p1nf p1nf1 11 1fO{$va1ue} fO{$va1ue} fO{$va1ue} fO{$va1ue}
va1ue 1s foo sma11.
va1ue 1s 12.
Honporpammt moryr raxxe nosnpamart snauennx. Tax, n cneymmem cnyuae c nomomtm
xomant return nosnpamaercx cymma nyx nepeanntx nennunn:
sub addem
{
{$va1ue1, $va1ue2} = 0
efun $va1ue1 efun $va1ue1 efun $va1ue1 efun $va1ue1 + + + + $va1ue2 $va1ue2 $va1ue2 $va1ue2
}
p1nf "2 + 2 = " . addem{2,2} . "\n"
2 + 2 = 4
Hocuasua. B opveux sirax npoepa++upoeauu qvuruu noooeprueammc omoe.iuo om noonpoepa++.
Ho e Perl noonpoepa++i +oevm eosepauami sua:euu, u o. po.u qvuru ue eeoomc noonpoepa++i
cneua.iuoeo muna. Te+ ca+i+ c.oea qvuru u noonpoepa++a e Perl e.mmc cuuouu+a+u.
Bor nxparne xax paoramr nonporpammt onn nosnonxmr pasnnart xo na nonyanronomnte
xycxn, xoroptm nepeamrcx onn annte, a oparno nosnpamamrcx pyrne. Taxax pasnnxa xoa
enaer nporpammt onee nerxnmn nx nanncannx n conponoxennx. 3auacrym +ro eme n cnntno
coxpamaer pasmep nporpamm. Tenept paccmorpnm nce onee erantno.
Henocpepc1neuume pemeuun
OLneneuue nopnporparr
Oxnnenne moxno ncnontsonart, urot coomnrt Perl o cymecrnonannn nonporpammt
n yrounnrt npn +rom rnnt nepeanaemtx aprymenron n nosnpamaemoro snauennx. Oxn-
nenne (declaration) nonporpammt ornnuaercx or onpeenennx (deIinition) npn onpee-
nennn saacrcx xo, cocrannxmmn reno nonporpammt.
B ornnune or pyrnx xstxon nporpammnponannx, n Perl nepe ncnontsonannem oxnnxrt
nonporpammt ue uaoo. Hcxnmuennem xnnxercx ntson nonporpamm (]ynxnn) es xpyr-
ntx cxoox, oxnartnammnx cnncox napamerpon (nanpnmep, cnncountx oneparopon). B
+rom cnyuae nepe rem, xax ncnontsonart nonporpammy, neoxonmo ee oxnnrt.
Honporpamma moxer trt oxnnena onnm ns cneymmnx cnocoon:
sub uun
sub uun {npo1o1un}
sub uun {bnox}
sub uun {npo1o1un} {bnox}
(n nocnennx nyx cnyuaxx nmecre c onncannem nonporpammt nponsnonrcx ee onpee-
nenne, ro ecrt saaercx xo, cocrannxmmn reno nonporpammt).
Hpn oxnnennn nonporpammt moxno yxasart ee npomomun no nemy Perl onpeenxer
napamerpt nonporpammt. Hexoropte nporpammncrt npenounramr ncnontsonart npo-
rornnt, xax cnoco nponepxn npannntnocrn xoa, onee nopono o +rom paccxast-
naercx n cneymmem pasene Hcnontsonanne nporornnon.
Paspemaercx nmnoprnponart nonporpammt ns naxeron Perl:
use naxe1 qW{uun1 uun2 uun3}
Crp.137
(Hcnontsonanne ncenoxantuex qw/.../ (cm. ran. 2.3 n rnane 2) camt npocro cnoco
cosart cnncox ns crpox-nmen ]ynxnn, saxnmuenntx n xantuxn. Fonee nopono o xo-
mane use paccxastnaercx n rnane 13.)
nx nonporpamm moxno ncnontsonart nmte nmena. Onaxo cneyer nomnnrt, uro Perl
pesepnnpyer nmena, cocrannennte nennxom ns sarnanntx yxn, nx nexnno ntstnaemtx
Perl nonporpamm naxeron rnna BEGIN nnn END.
urot ornnuart nmena nonporpamm or pyrnx rnnon anntx, nepe xaxtm nmenem
nopasymenaercx cnmnon . B ontmnncrne cnyuaen ero moxno onycrnrt - ecnn Perl pac-
nosnaer ns xonrexcra, uro nmeer eno c nonporpammo, on camocroxrentno nocrannxer
n nauane nmenn cnmnon . Hanpnmep, ecnn nt npncnonnn cnoe nonporpamme nmx
count, ro moxere ntstnart ee xax count(l,2) nnn count(l,2) (cm. pasen Btson nonpo-
rpamm anee n +ro rnane).
Hcnonusoeauue npofofunoe
Hexoropte nporpammncrt nmxr ncnontsonart nporornnt, xax cpecrno nponepxn npa-
nnntnocrn ntsona nonporpamm, nanpnmep, nx nponepxn roro, uro n xauecrne napa-
merpa nmecro maccnna ne nepeaercx cxanxpnax nepemennax n r. . urot oxnnrt npo-
rornn, nao nepeuncnnrt n nyxnom nopxxe cnmnont, coornercrnymmne npe]nxcam ap-
rymenron: $ nx cxanxpon, nx maccnnon n r. . (ran. 7.1).
Ecnn n nporornne n xauecrne ]opmantnoro napamerpa yxasant cnmnont nnn , ro +ror
napamerp nornomaer nce nocneymmne. To ecrt maccnn nnn x+m moryr trt rontxo no-
cnennm napamerpom nonporpammt:
sub NAML {$$0}
3ro osnauaer, uro npn ntsone nonporpammt nepntmn nymx napamerpamn yyr cxanxpt,
sa xoroptmn cneyer cnncox cxanxpntx oexron, opasyemt no nsnecrntm, npannnam:
NAML $sca1a1, $sca1a2, $1sfag1, $1sfag2, $1sfag3
Ecnn nt xornre, urot npn ntsone nonporpammt aprymenr n n camom ene naunnancx c
npe]nxca nnn , a ne npecrannxn coo cnncox cxanxpntx aprymenron, neoxonmo
samnrnrt +rn cnmnont c nomomtm oparno xoco ueprt:
sub 5u8NAML {\0}
Tenept moxno ntstnart nonporpammy, yxastnax e nepemennym-maccnn n xauecrne aprymenra:
5u8NAML 0aay
Hpn xenannn moxno onpeennrt nx nonporpammt neoxsarentnte napamerpt. Cnncox
neoxsarentntx napamerpon orenxercx or oxsarentntx rouxo c sanxro (cm. ran. 7.1).
Hocuasua. Hpomomuni e.umm ua uumepnpemaum ei.oea nonpoepa++m, mo.iro roeoa npu eisoee
o. u+euu noonpoepa++i ue vrasau npequrc &.
1abnua 7.1. 1abnua 7.1. 1abnua 7.1. 1abnua 7.1. ax coaa1u npo1o1un nonporpauuu
Onucauue Onucauue Onucauue Onucauue 8uaoa nonporpauuu 8uaoa nonporpauuu 8uaoa nonporpauuu 8uaoa nonporpauuu
sub uun{$} uun $ag1
sub uun{$$} uun $ag1, $ag2
sub uun{$$$} uun $ag1, $ag2 unu uun $ag1, $ag2, $ag3
sub uun{0} uun $ag1, $ag2, $ag3, $ag4
sub uun{x} uun $key1 => $va11, $key2=>$va12
sub uun{$0} uun $AkG, $ag1, $ag2, $ag3
Crp.138
sub uun{$x} uun $AkG, $key1 => $va11, $key2=>$va12
sub uun{\0} uun 0aay
sub uun{\x} uun x{$ccunxa-ua-hash}
sub uun{} uun uenouueuoaauuan-nonporpauua
sub uun{"} uun "ag1
sub uun{} uun
Onpepeneuue npofofunoe
Onpeenenne ornnuaercx or onncannx rem, uro n nem npnnonrcx xo, cocrannxmmn reno
nonporpammt. urot onpeennrt nonporpammy, ncnontsyercx xnmuenoe cnono sub:
sub uun {bnox}
sub uun {npo1o1un} {bnox}
Hanpnmep, rpeyercx saart nonporpammy printhello, npocro ntnoxmym coomenne
Hello! (oparnre nnnmanne, uro reno nonporpammt saxnmueno n ]nrypnte cxoxn n
], xorx n cocronr ns ono xomant):
sub p1nfhe11o
{
p1nf "he11o!\n"
}
Tenept moxno ntsnart +ry nonporpammy:
p1nfhe11o
he11o!
Fonee nopono nponeypa ntsona paccmarpnnaercx n cneymmem pasene Btson no-
nporpamm.
Busoe nopnporparr
Ecnn nonporpamma onpeenena, ee moxno ntsnart c xonxperntmn aprymenramn n xaue-
crne napamerpon:
uun {cnucox-apryueu1oa}
B Perl ntnonnnrt ntson nonporpammt moxno anexo ne onnm cnocoom. Hanpnmep,
npn ncnontsonannn xpyrntx cxoox ne oxsarentno crannrt npe]nxc :
uun {cnucox-apryueu1oa}
Ecnn nonporpamma tna onncana nnn +xcnoprnponana ns naxera, ro saono moxno onyc-
rnrt n xpyrnte cxoxn:
uun cnucox-apryueu1oa
Hpn ntsone nonporpammt, nepeanaemte e aprymenrt nomemamrcx n cnennantnt
maccnn _. Ecnn nonporpamma ntstnaercx c npe]nxcom , no es cnncxa napamerpon,
ro n xauecrne nocnenero e nepeaercx rexymee coepxnmoe maccnna _. 3ro nonesno n
rom cnyuae, xora ona nonporpamma ntstnaercx ns pyro, npnuem e rpeyercx nepe-
art re xe napamerpt, xoropte ncnontsonannct npn ntsone nepno nonporpammt. (O-
parnre nnnmanne, uro npn ncnontsonannn npe]nxca Perl ne ntnonnxer nponepxy coor-
nercrnnx nporornna nonporpammt cnncxy ]axrnuecxn nepeanaemtx nennunn.)
Aprymenrt, nepeanaemte nonporpamme, opasymr ennt ynn]nnnponannt cnncox.
Crp.139
Ecnn nepeamrcx na maccnna, nx coepxnmoe yer oenneno n ennt maccnn n o-
anneno x cnncxy snauenn, nepeanaemtx nonporpamme. Ecnn nt xornre nepeanart n
xauecrne napamerpon maccnnt n coxpanxrt nx crpyxrypy, nepeanare nx no cctnxe (cm.
pasen Hepeaua napamerpon no cctnxe anee n +ro rnane.)
-feuue apryreufoe, nepepauuux nopnporparre
ocryn x aprymenram, nepeanntm nonporpamme, ocymecrnnxercx uepes cnennantnt
maccnn _, n xoropt sanocxrcx +rn aprymenrt. Hanpnmep, ecnn nepeant na napamer-
pa, nonporpamma moxer oparnrtcx x nnm xax $_0] n $_1].
Hpenonoxnm, uro nt xornre cnoxnrt na uncna n naneuarart pesyntrar. nx +ro nenn
cosaercx nponeypa addem, xoropym moxno ntsnart, xax addem(2,2). Hocmorpnm, xax
addem nonyuaer snauennx uepes maccnn _:
sub addem {
$ $$ $va1ue va1ue va1ue va1ue1 1 1 1 = = = = $|0 $|0 $|0 $|0] ] ] ]
$va1ue2 = $|1] $va1ue2 = $|1] $va1ue2 = $|1] $va1ue2 = $|1]
p1nf "$va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2}
}
addem{2,2}
2 + 2 = 4
urot nsnneut napamerpt ns maccnna _, moxno raxxe ncnontsonart ]ynxnnm shift:
sub addem {
$va1ue1 = sh1ff 0 $va1ue1 = sh1ff 0 $va1ue1 = sh1ff 0 $va1ue1 = sh1ff 0
$va1ue2 $va1ue2 $va1ue2 $va1ue2 = = = = sh1ff 0 sh1ff 0 sh1ff 0 sh1ff 0
p1nf "$va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2}
}
addem{2,2}
2 + 2 = 4
Haxonen, n xauecrne rpertero meroa, urot nonyunrt snauenne napamerpon sa onn pas,
moxno ncnontsonart npncnoenne cnncxom:
sub addem {
{$va1ue1, $va1ue {$va1ue1, $va1ue {$va1ue1, $va1ue {$va1ue1, $va1ue2} 2} 2} 2} = = = = 0 0 0 0
p1nf "$va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2}
}
addem{2,2}
2 + 2 = 4
Hcnonusoeauue nepereuuoro ucna naparefpoe
Perl ynpomaer nepeauy nonporpamme nepemennoro uncna napamerpon, nocxontxy onn
nomemamrcx n maccnn _. urot onpeennrt, cxontxo napamerpon tno nepeano no-
nporpamme, ocrarouno nponepnrt nnny +roro maccnna (ro ecrt nepemennym $#_).
urot nanpxmym paorart c aprymenramn, sanecenntmn n maccnn _, moxno nc-
nontsonart nnxn foreach. On ntnonnnr nonnt nepeop nepeanntx napamerpon nesann-
cnmo or roro, cxontxo nx tno saano:
sub addem
{
$sum = 0
foeach $e1emenf {0} foeach $e1emenf {0} foeach $e1emenf {0} foeach $e1emenf {0}
Crp.140
{ $sum += $e1emenf }
p1nf o1n{" + ", 0} . " = " . $sum . "\n"
}
addem{2, 2, 2}
2 + 2 + 2 = 6
Hcnonusoeauue suaeuu no yronauuk
Hocxontxy nponeypa moxer nonyuart nepemennoe uncno aprymenron, nnora rpeyercx
oecneunrt snauennx no ymonuannm nx onymenntx napamerpon. 3ro moxno cenart c
nomomtm oneparopa [[. Oneparop [[ (nornuecxoe n), no-nepntx, npenounraer ne sa-
rpyxart cex nnmne paoro n ne ntuncnxer nropo aprymenr, ecnn yxe nepnt coor-
nercrnyer snauennm ucmuua. Bo-nroptx, on nosnpamaer ne nornuecxne snauennx .ori n
ucmuua, a nepnt ns nyx aprymenron, xoropt ue .ori:
sub addem {
{$va1ue1, $va1ue2} = 0
$va1ue2 $va1ue2 $va1ue2 $va1ue2 ||= ||= ||= ||= 1 1 1 1
p1nf $va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2}
}
Tax xax nocne npncnoennx cnncxom snauenne $value2 ocranoct neonpeenenntm, a c rou-
xn spennx Perl neonpeenennoe snauenne coornercrnyer ycnonnm .ori, ro cxanxpy
$value2 yer npncnoeno ncnontsyemoe no ymonuannm snauenne 1:
addem{2}
2 + 1 = 3
Onaxo +ror mero nopasymenaer, uro n xauecrne nroporo napamerpa ne yer saano
uncno nont nnn nycrax crpoxa. Hpannntnee ocymecrnnrt nponepxy xonnuecrna +nemenron
maccnna _ (+ro snauenne $#_) n xnnom nne. 3ro nosnonnr onpeennrt, cxontxo xe ap-
rymenron saano npn ntsone nonporpammt:
sub addem
{ $va1ue1 = sh1ff 0
1f {$# > 0} 1f {$# > 0} 1f {$# > 0} 1f {$# > 0} { {{ {
$va1ue2 = sh1ff 0 $va1ue2 = sh1ff 0 $va1ue2 = sh1ff 0 $va1ue2 = sh1ff 0
} e1se { } e1se { } e1se { } e1se {
$va1ue2 = 1 $va1ue2 = 1 $va1ue2 = 1 $va1ue2 = 1
} }} }
p1nf "$va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2}
}
addem{2}
2 + 1 = 3
3uaeuun, eosepaaerue nopnporparraru (qyukuunru)
3aart nosnpamaemoe snauenne n ntrn ns nonporpammt moxno raxxe c nomomtm xo-
mant return
1
. (B nexoroptx xstxax nporpammnponannx ]ynxnnn nosnpamamr snauenne,
a nonporpammt +roro enart ne moryr. Onaxo n Perl ]ynxnnx n nonporpamma +ro
ono n ro xe.) Cymecrnenno: snauenne, nosnpamaemoe oneparopom return, ntuncnxercx n
rom xonrexcre (cxanxpnom nnn cnncxonom), n xoropom ntstnaercx nonporpamma. Ha-

1
Ecnn n rene nonporpammt ner xomant return, npn ntxoe ns nonporpammt nosnpamaercx nocnenee ntuncnen-
noe snauenne. Hpu+e:. peo.
Crp.141
npnmep, nor xax nepeart nonporpamme na napamerpa n nepnyrt nx cymmy:
sub addem
{
{$va1ue1, $va1ue2} = 0
efun $va1ue1+$va1ue2 efun $va1ue1+$va1ue2 efun $va1ue1+$va1ue2 efun $va1ue1+$va1ue2
}
p1nf "2 + 2 = " . addem{2, 2} . "\n"
2 + 2 = 4
Touno rax xe nt moxere nepnyrt ne ono snauenne, a nx cnncox:
sub gefva1
{
efun efun efun efun 1 11 1, , , , 2 22 2, , , , 3 33 3, , , , 4 44 4, , , , 5 55 5, , , , 6 66 6, , , , 7 77 7, , , ,
}
Ecnn nonporpamma nosnpamaer cnncox snauenn, ro moxno, nanpnmep, ncnontsonart ero
npn npncnoennn snauennx maccnny:
0aay 0aay 0aay 0aay = = = = gefva1ues gefva1ues gefva1ues gefva1ues
p1nf o1n{", ", 0aay}
1, 2, 3, 4, 5, 6, 7,
Onaxo ecnn n xauecrne nosnpamaemoro snauennx yxasano necxontxo cnncxon, maccnnon
nnn x+me, nce onn yyr ynaxonant n onn ontmo cnncox. Ho+romy snauenne, nos-
npamaemoe nonporpammo, moxno npncnonrt rontxo onomy maccnny. Hanpnmep, cne-
ymmax xomana npocro ne cpaoraer:
{0aay1, 0aay2} = gefva1ues
Hpn ee ntnonnennn nce snauennx, nosnpamaemte nonporpammo getvalues, nomemamrcx n
maccnn array1, a maccnn array2 ocraercx neonpeenenntm. urot cnpannrtcx c +ro
nponemo, oparnrect x paseny Hepeaua napamerpon no cctnxe anee n +ro rnane.
Ynpaeneuue onacfuk eupurocfu (knkeeue cnoea fy u local)
Ho ymonuannm nepemennte Perl xnnxmrcx rnoantntmn. 3ro snaunr, uro nt moxere o-
pamartcx x nnm ns nmoro mecra nporpammt. (Ha camom-ro ene, onn rnoantnt rontxo n
pamxax rexymero naxera, no o naxerax nt ysnaere ns rnant 15.) axe ecnn nepemennax
onpeenena nnyrpn rena nonporpammt, ona cranonnrcx rnoantno n x ne moxno o-
pamartcx nocne ntxoa ns nonporpammt. Hanpnmep, n cneymmem ]parmenre xoa mt
nerxo nonyuaem ocryn x nepemenno inner, naxoxct sa npeenamn nonporpammt, n
xoropo ona onpeenena n n xoropo e npncnoeno snauenne:
sub p1nfem {
$1nne = sh1ff 0
p1nf $1nne
}
p1nfem "he11o!\n"
p1nf "/" . $1nne
he11o!
/he11o!
(xax nerxo nnert, nropoe Hello! nonyueno n pesyntrare npxmoro opamennx x nepe-
menno, es ntsona nonporpammt).
Ecnn t na +rom nce n xonuanoct, Perl t tcrpo cran neynpannxemtm ns-sa orpomnoro
uncna rnoantntx nepemenntx, memammnx pyr pyry n nponecce paort nporpammt.
Crp.142
Onaxo moxno orpannunrt nepemennym npeenamn nonporpammt, ecnn saart ee oo-
.acmi euou+ocmu (scope).
Knmuenoe cnono my orpannunnaer nepemennym rexymnm noxom yt ro onnount
nox, ycnonnt oneparop, oneparop nnxna, nonporpamma, xomana eval nnn ]an, no-
xnmuaemt xomanamn do, require nnn use. Hepemennte, onncannte c xnmuentm cnonom
my, nmemr orpannuennym .ercu:ecrvm onacrt nnnmocrn. B ornnune or nnx nepemen-
nte, onncannte c xnmuentm cnonom local, nmemr orpannuennym ouua+u:ecrvm onacrt
nnnmocrn. Pasnnune mexy nnmn cocronr n rom, uro nepemennte c nnamnuecxo ona-
crtm nnnmocrn ocrynnt raxxe nonporpammam, xoropte ntstnamrcx ns rexymero
noxa. Hepemennte c nexcnuecxo onacrtm nnnmocrn nnnt ncxnmunrentno nnyrpn
roro noxa, n xoropom onn onncant. (Fonee nopono o nnamnuecxo onacrn nnnmo-
crn moxno ysnart ns pasena Cosanne npemenntx nepemenntx (xnmuenoe cnono local)
anee n +ro rnane.)
Ecnn nocne xnmuenoro cnona my nepeuncneno onee ono nepemenno, cnncox onxen
trt saxnmuen n xpyrnte cxoxn. Bce +nemenrt cnncxa onxnt trt onycrnmtmn ne-
ntmn snauennxmn, ro ecrt nx moxno yxastnart n neno uacrn oneparopa npncnannannx.
Kpome roro, nexcnuecxym onacrt nnnmocrn moxno oxnnxrt rontxo nx nepemenntx,
nmena xoroptx cocrannent ns yxn, nn]p n cnmnona nouepxnnannx. Tem camtm cnenn-
antnte (ncrpoennte) nepemennte Perl rnna $_ oxnnxrt c ncnontsonannem xnmuenoro
cnona my nentsx, saro nx raxnx nepemenntx moxno saanart xnmuenoe cnono local, uro
nosnonxer samnrnrt snauenne nepemenno or nsmenenn, nponsnonmtx nnyrpn no-
nporpammt (noponee cm. pasen Cosanne npemenntx nepemenntx (xnmuenoe cnono
local) anee n +ro rnane). Haxonen, oxnnenne my ecrnnrentno rontxo nx cxanxp-
ntx nepemenntx, maccnnon n x+me oxnnrt raxnm opasom, nanpnmep, nonporpam-
my nentsx (n uacrnocrn, noromy, uro nonporpamma ne xnnxercx nentm snauennem).
B cneymmem npnmepe onacrt nnnmocrn nepemenno $inner orpannunnaercx renom
nonporpammt: ona oxnnena c xnmuentm cnonom my. Tenept ocryn x ne sa npeena-
mn nonporpammt sanpemen (n uacrnocrn, xomana print ntnonr nycrym crpoxy):
sub p1nfem
{
my my my my $1nne = sh1ff 0 $1nne = sh1ff 0 $1nne = sh1ff 0 $1nne = sh1ff 0
p1nf $1nne
}
p1nfem "he11o!\n"
p1nf "/" . $1nne
he11o!
/
Bor necxontxo npnmepon c ncnontsonannem xnmuenoro cnona my:
my $va1ab1e1
my {$va1ab1e1, $va1ab1e2}
my $va1ab1e1 = 5
Hpeynpexeuue. Ec.u ei ooe.eme c no+ouim r.m:eeom c.oea my uecro.iro nepe+euuix, mo oo.rui
sar.m:umi ux e rpve.ie crooru. Hua:e r.m:eeoe c.oeo my ovoem omuocumic mo.iro r nepeo us nepe:uc.eu-
uix e cnucre nepe+euuo rar, uanpu+ep, e c.eovmue ro+auoe.
my $va1ab1e1, $va1ab1e2 = 5
Hepemennte, oxnnennte c xnmuentm cnonom my, ne oxsarentno naxoxrcx nnyrpn
noxa. Hanpnmep, ecnn oxnnenne naxonrcx n sarononxe nnxna nnn ycnonnoro oneparo-
pa, ro raxne nepemennte npnoperamr nexcnuecxym onacrt nnnmocrn n npeenax +roro
Crp.143
oneparopa. B uacrnocrn, nepemennax, oxnnennax xax my n ycnonnn ycnonnoro oneparo-
pa, nmeer nexcnuecxym onacrt nnnmocrn no ornomennm x ycnonnxm n ncem noxam, co-
crannxmmnm oneparop:
$fesfva1ue = 10
1f {{my $va1ab1e1 = 10} > $fesfva1ue} {
p1nf "va1ue, ", $va1ab1e1, ", 1s geafe fhan fhe fesf va1ue.\n"
} e1s1f {$va1ab1e1 < $fesfva1ue} {
p1nf "va1ue, ", $va1ab1e1, ", 1s 1ess fhan fhe fesf va1ue.\n"
} e1se {
p1nf "va1ue, ". $va1ab1e1, ", 1s equa1 fo fhe fesf va1ue.\n"
}
va1ue, 10, 1s equa1 fo fhe fesf va1ue.
Tpeoeauue onsafenuuo nekcuecko onacfu eupurocfu
Hparma use strict vars n nporpamme npennctnaer nporpammncry xnno yxastnart nx nm-
o nepemenno, xnnxercx nn ona noxantno nnn rnoantno. To ecrt nmoe ynomnnanne
nmo nepemenno, naunnax c nparmt n o xonna noxa nnn rexyme onacrn nnnmocrn,
onxno ornocnrtcx nno x nepemenno, onncanno c nexcnuecxo onacrtm nnnmocrn, nn-
o x rnoantno nepemenno c xnntm yxasannem nmenn naxera, x xoropomy ona ornocnrcx.
Cospauue epereuuux nepereuuux (knkeeoe cnoeo local)
Kpome nepemenntx c nexcnuecxo onacrtm nnnmocrn, cosammnxcx c nomomtm xnm-
uenoro cnona my, nt moxere raxxe opasontnart nepemennte c ouua+u:ecro onacrtm
nnnmocrn c nomomtm xnmuenoro cnona local
1
. 3ro xnmuenoe cnono cosaer npemen-
nym xonnm rnoantno nepemenno. Bnyrpn nnaunuecxon onacrn nnnmocrn paora
ner c npemenno xonne, n nsmenennx, nnocnmte n nepemennym, ne cxastnamrcx na oc-
nonno nepcnn nepemenno. Hpn ntxoe ns nnamnuecxo onacrn nnnmocrn npemennax
xonnx ynnuroxaercx, n rnoantnax nepemennax noccranannnnaer npexnee snauenne.
nnamnuecxax onacrt nnnmocrn ornnuaercx or nexcnuecxo rem, uro ornocnrcx ne
rontxo x rexymemy noxy, no n xo ncem ntstnaemtm ns +roro noxa nonporpammam. Ha-
npnmep, ecnn nepemennax $_ oxnnxercx xax noxantnax, sarem e npncnannaercx nexoro-
poe snauenne n ntstnaercx xomana print es napamerpon, ro print naneuaraer coepxn-
moe npemenno xonnn, a ne rnoantno nepemenno $_. Hpn ntxoe ns rexymero noxa
npemennax xonnx yer ynnuroxena, n rora ntsont xomant print es napamerpon y-
yr ornocnrtcx x ncrnnno cnennantno nepemenno $_.
Otuno nmecro cnenn]nxaropa local pexomenyercx ncnontsonart my. Hnora, onaxo,
local nosnonxer cenart nemn, neocrynnte nx my. Hanpnmep, moxer nonaonrtcx ce-
nart npemennym xonnm cnennantno nepemenno Perl, nnn nsmennrt orentnt +nemenr
maccnna nnn x+m-rannnt, nnn nopaorart noxantno c ecxpnnropamn ]anon nnn ]op-
maramn Perl. Bce +rn ]ynxnnn ocrynnt ncxnmunrentno uepes cnenn]nxarop local.
Oparnre nnnmanne, uro xnmuenoe cnono local cosoaem ue nonym nepemennym, a ncero
nnmt xonnm cymecrnymme (xax npannno) rnoantno nepeuennon, c xoropo nt yere
paorart n antnemem. Bor necxontxo npnmepon ncnontsonannx xnmuenoro cnona local:

1
Hacroxrentno ne pexomenyercx nontsonartcx +ro nosmoxnocrtm npn cosannn nporpamm na Perl. Hpu+e:. peo.
Crp.144
1oca1 $va1ab1e1
1oca1 {$va1ab1e1, $va1ab1e2}
1oca1 $va1ab1e1 = 5
1oca1 "IlLLhANDLL
Hrax, onncarent local cosaer xonnn nepeuncnenntx n nem +nemenron, enaer nepemennte
noxantntmn nx rexymero noxa, xomant eval, xomant do, a raxxe nx nmo nonpo-
rpammt (n ncex ee nnoxenntx nonporpamm), xoropax yer ntsnana ns +roro noxa. Kax n n
cnyuae my, npn nepeuncnennn onee ono nepemenno neoxonmo saxnmunrt nx n xpyr-
nte cxoxn. Bce +nemenrt, nepeuncnxemte c local, onxnt trt npannntntmn nentmn
snauennxmn, ro ecrt nx moxno yxastnart cnena or oneparopa npncnannannx.
Rocfonuuue (cfafueckue) nepereuuue
Hnora nao cenart rax, urot nepemennax nnyrpn nonporpammt coxpanxna cnoe sna-
uenne mexy ntsonamn. Onaxo ecnn onpeennrt nepemennte c xnmuentm cnonom my, ro
nx snauennx nepemenntx yyr cpactnartcx npn xaxom nxoe n nonporpammy. B cne-
ymmem npnmepe nepemennax $count cpactnaercx n nont
1
n cneom ynennunnaercx na
ennnny npn xaxom nxoe n nonporpammy, rax uro na ntxoe nmecro xenaemo nocne-
onarentnocrn narypantntx uncen 1, 2, 3, nonyuamrcx uertpe ennnnt:
sub 1ncemenfcounf {
my $counf
efun ++$counf
}
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
1
1
1
1
Ecnn moxno tno cenart nepemennym $count crarnuecxo, xax n xstxe C, +ro pemnno
t namy nponemy (crarnuecxne nepemennte coxpanxmr cnoe snauenne mexy ntsonamn
]ynxnn). K coxanennm, Perl ne noepxnnaer crarnuecxne nepemennte nanpxmym: rno-
antnte nepemennte xnnxmrcx crarnuecxnmn no ymonuannm, a nepemennte, oxnnen-
nte nnyrpn nonporpamm, nnxora raxontmn ne tnamr.
Onaxo cymecrnyer rpmx, c nomomtm xoroporo +ry nponemy moxno oorn. He-
pemennte c nexcnuecxo onacrtm nnnmocrn ne cpactnamrcx o rex nop, noxa onn na-
xoxrcx n npeenax nnnmocrn (rounee, cncrema anromarnuecxo copxn mycopa ne rpora-
er nx o rex nop, noxa na nepemennym xort xro-ro cctnaercx nonporpamma, cctnxa n
r. .). Ho+romy axe ecnn nepemennax ne nnna, +ro eme ne snaunr, uro ona ynnuroxena.
B cneymmem npnmepe mt nomemaem onpeenenne my nne rena nonporpammt, saxnmunn
n nonporpammy, n onpeenenne nepemenno n ]nrypnte cxoxn. B pesyntrare $count
naunnaer necrn cex, xax crarnuecxax nepemennax:
{ {{ {

1
Ha camom ene nepemennax $count, xoneuno xe, ne cpactnaercx n nont, a cranonnrcx neonpeenenno. Hpocro c rouxn
spennx oneparopa ++ neonpeenennoe snauenne n nont pannonpannte nennunnt. Boome, oneparopt anronpnpamennx
n anroymentmennx ++ n -- ymemr nnrepnpernponart xax apn]mernuecxne mnorne nxonte annte, xoropte otunte
apn]mernuecxne oneparopt (+, -, , /) couryr sa omnxy. (uyrt noponee o +rom cnocrne oneparopon ++ n -- paccxasano
n rnane , n pasene Anronpnpamenne n anroymentmenne.) Hpu+e:. nepee.
Crp.145
my $counf
sub 1ncemenfcounf {
efun ++$counf
}
} }} }
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
1
2
3
4
3ect ncnontsonan ror ]axr, uro nonporpamma, axe oxnnennax nnyrpn noxa, xnnxer-
cx rnoantno. Ho+romy ona ocrynna, n rom uncne n nne oxnartnammero ee noxa, rora
xax noxantnax nepemennax $count ner. Hpn nxoe n nox n pesyntrare ntnonnennx xo-
mant my nx nepemenno $count ntenxercx namxrt, no npn ntxoe ns noxa namxrt ne
ocnooxaercx net na +ry nepemennym nce eme cctnaercx nonporpamma increment-
count. B pesyntrare nonporpamma nce panno nmeer eno c ono n ro xe onacrtm na-
mxrn, ne ocnooxax ee npn ntxoe n ne pasmemax n ne anntx nnont npn nxoe.
Bect +ror ]parmenr xoa moxno nomecrnrt n nox BEGIN, ntnonnxemt npn sarpysxe
nporpammt. 3ro rapanrnpyer ntenenne namxrn no nocroxnnte nepemennte nepe
nauanom paort cnenapnx, a ne n nponecce ero ntnonnennx:
sub sub sub sub 8LGlN 8LGlN 8LGlN 8LGlN
{ {{ {
my $counf = 0
sub 1ncemenfcounf {
efun ++$counf
}
} } } }
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
p1nf 1ncemenfcounf . "\n"
1
2
3
4
Oparnre nnnmanne, uro, xora npn nxoe n nox ntenxercx namxrt no nepemennym, no
npn ntxoe ns noxa +ra namxrt ne ocnooxaercx (no npnunne sanxrocrn nepemenno), npn
nonom nxoe n nox namxrt ne yer nonropno ntenxrtcx. Kax pesyntrar, npexnee snauenne
nocroxnno nepemenno ne yer norepxno ns-sa ee nonropno nnnnnannsannn, npn nt-
nonnennn nporpammt rnna:
$f1ag =1
LA8LL1:
{
my $counf = 0
sub 1ncemenfcounf {
efun ++$counf
}
}
fo {1..4} {p1nf 1ncemenfcounf, "/"}
1f {$f1ag} {
$f1ag = 0
Crp.146
gofo LA8LL1
}
1/2/3/4/5/6/7//
Pekypcueuu eusoe nopnporparr
B Perl nt moxere ntstnart nonporpammt pexypcnnno, ro ecrt nonporpamma moxer co-
epxart (npxmo nnn xocnenno) ntsont camo cex. Hpnntunt npnmep pexypcnnntx
onepann +ro ntuncnenne ]axropnana (nanpnmep, 5! 54321). He yem orcry-
nart or +ro rpannnn.
Mt pasnnaem nponeypy ntuncnennx ]axropnana na necxontxo pexypcnnntx maron. Ha
xaxom mare snauenne, nepeannoe n xauecrne napamerpa, ymnoxaercx na pesyntrar nt-
uncnennx ]axropnana c aprymenrom, ymentmenntm na ennnny. Tontxo ecnn nonpo-
rpamma ntstnaercx c napamerpom, panntm ennnne, yer nosnpameno snauenne ennnna,
es yrnynenn n antnemne ntuncnennx:
sub facfo1a1 {
my $va1ue = sh1ff 0
1f {$va1ue == 1} {
efun $va1ue
} e1se {
efun $va1ue " facfo1a1{$va1ue-1}
}
}
$esu1f = facfo1a1{6}
p1nf $esu1f
720
Kax nnnre, +ra nonporpamma moxer ntstnart pexypcnnno cama cex. (Ha camom ene,
nmenno +ro ona n enaer, ecnn rontxo ee ne nonpocnnn ntuncnnrt ]axropnan ennnnt.)
Bnoeuuue nopnporparru
B Perl paspemaercx ncnontsonart raxxe n nnoxennte nonporpammt (ro ecrt onpeenxrt
nonporpammt nnyrpn nonporpamm). B namem npnmepe mt onpeenxem nonporpammy
outer n nonporpammy inner nnyrpn nee. Oparnre nnnmanne, uro inner nnna ne rontxo
nnyrpn outer, no n nne ee:
sub oufe {
sub 1nne { p1nf "lns1de fhe 1nne subouf1ne.\n" }
1nne
}
oufe
1nne
lns1de fhe 1nne subouf1ne.
lns1de fhe 1nne subouf1ne.
Repepaa naparefpoe no ccunke
Ecnn nonporpamme nepeamrcx maccnnt n x+mn, ro nx +nemenrt oennxmrcx n onn
cnncox nmecre c ocrantntmn napamerpamn. Ecnn nt xornre nepeart na n onee maccn-
non nnn x+me, coxpannn npn +rom nx nnnnnyantnocrt, ro onxnt nepeanart cci.ru
Crp.147
na maccnnt nnn x+mn (o cctnxax noponee paccxastnaercx n rnane ).
B cneymmem npnmepe cosamrcx na maccnna:
0a = {1, 2, 3}
0b = {4, 5, 6}
Mt xornm cosart nonporpammy addem, xoropax cxnatnaer nonapno +nemenrt nyx
maccnnon nesanncnmo or nx nnnt. urot onrtcx +roro, mt ntstnaem nonporpammy
addem, nepeanax e cctnxn na maccnnt:
0aay = addem{\0a, \0b}
Tenept nnyrpn nonporpammt addem nao oparnrtcx x cctnxam na maccnnt, a sarem op-
rannsonart nnxn no uncny nx +nemenron, nosnpamax n xauecrne pesyntrara maccnn, coep-
xamn nonapnte cymmt +nemenron nxontx maccnnon (+ror xo craner nonxrnee nocne
roro, xax nt npourere rnany ):
sub addem {
my {$ef1, $ef2} = 0
fo {$1oop1ndex = 0 $1oop1ndex <= $#{$ef1} $1oop1ndex++} {
$esu1f|$1oop1ndex] = 0{$ef1}|$1oop1ndex] + 0{$ef2}|$1oop1ndex] }
efun 0esu1f
}
Bor xax c nomomtm nonporpammt addem nponcxonr cnoxenne +nemenron nyx maccnnon:
0aay = addem {\0a, \0b}
p1nf o1n{ , , 0aay}
5, 7, 9
Texnnxa nepeaun cctnox nosnonxer nenocpecrnenno cctnartcx na +nemenrt anntx, ne-
peanaemte nonporpamme. 3ro osnauaer, uro moxno mon]nnnponart napamerpt, nepea-
naemte nonporpamme. B Perl cxanxpt u mar nepeamrcx no cctnxe, no+romy nx mon]n-
xannn coepxnmoro nepemenntx nnyrpn nonporpammt ne rpeyercx nepeanart na nnx
cctnxn n xnnom nne.
Repepaa sanucu fanuuu cureonoe (fun pauuux typeglob)
Hepeaua rnna anntx typeglob o neannero npemenn tna enncrnenntm cnocoom ne-
peart cctnxy na nepemennym cpecrnamn Perl. o cnx nop +ro nyumn cnoco nepea-
nart n xauecrne napamerpon raxne annte, xax ecxpnnropt ]anon. Hocxontxy typeglob
+ro nonnax sannct rannnt cnmnonon, ro npn nepeaue anntx +roro rnna peantno ne-
peamrcx cctnxn na nce rnnt anntx, xpanxmnecx n nporpamme no anntm nmenem.
Paccmorpnm, xax c nomomtm rnna typeglob nmecro cctnox peannsonart nonporpammy
nonapnoro cnoxennx +nemenron maccnna, paccmorpennym n npetymem pasene:
0a = {1, 2, 3}
0b = {4, 5, 6}
sub addem {
1oca1 {"aay1, "aay2} = 0
fo {$1oop1ndex = 0 $1oop1ndex <= $#aay1 $1oop1ndex++} {
$esu1f|$1oop1ndex] = $aay1|$1oop1ndex] + $aay2|$1oop1ndex] }
efun 0esu1f
}
0aay = addem {\0a, \0b}
p1nf o1n{ , , 0aay}
5, 7, 9
Crp.148
Hpn nepeaue ecxpnnropon ]anon n xauecrne napamerpa moxno nepeart coepxnmoe
sanncn rannnt cnmnonon typeglob nanpnmep, n ]opme STDUT. Onaxo nyume ne-
peanart cci.ru na sanncn rannnt cnmnonon, noromy uro raxo xo yer paorart n n
rom cnyuae, ecnn yer axrnnnsnponana nparma use strict refs (Hpae+i +ro npexrn-
nt, nepeanaemte xomnnnxropy. annax nparma nponepxer cnmnonnuecxne cctnxn, onee
noponax nn]opmannx moxer trt naena n rnane .) B cneymmem npnmepe mt nepe-
aem nonporpamme cctnxy na sannct STDUT rannnt cnmnonon:
sub p1nfhe11o {
my $hand1e = sh1ff
p1nf $hand1e "he11o!\n"
}
p1nfhe11o {\"51DOu1}
he11o!
Rpoeepka koufekcfa eusoea qyukuuu: qyukuun antarray
Honporpammt moryr nosnpamart xax cxanxpnte snauennx, rax n cnncxn. 3ro snaunr, uro
nosnpamaemoe snauenne moxer sanncert or xonrexcra, n xoropom ntsnana nonporpamma.
Ecnn nt xornre camocroxrentno opaartnart cnncxont n cxanxpnt xonrexcrt, nam
nyxen nncrpymenr, xoropt cxaxer, n xaxom xonrexcre ntstnaercx nonporpamma. Ta-
xnm nncrpymenrom xnnxercx ]ynxnnx wantarray.
Ona nosnpamaer snauenne ucmuua, ecnn pesyntrar paort nonporpammt yer nnrep-
npernponartcx n cnncxonom xonrexcre, n snauenne .ori n npornnnom cnyuae. B npnmepe
nnxe c nomomtm ]ynxnnn wantarray nponepxercx xonrexcr ntsona ]ynxnnn swapxy, n n
sanncnmocrn or xonrexcra nosnpamaercx cxanxp nnn cnncox (]ynxnnx swapxy samenxer n
rexcrontx crpoxax nce nxoxennx narnncxo yxnt x na narnncxym y):
sub sWapxy {
my 0dafa = 0
fo {0dafa} {
s/x/y/g
}
efun efun efun efun Wanfaay Wanfaay Wanfaay Wanfaay 0 0 0 0dafa dafa dafa dafa : $ : $ : $ : $dafa dafa dafa dafa|0] |0] |0] |0]
}
A nor xax ntrnxnr ntson ]ynxnnn swapxy n cnncxonom xonrexcre, xora mt nepeaem
e na nxo cnncox rexcrontx crpox n oxnaem nonyunrt opaorannt cnncox:
$a = "xy"
$b = "xxx"
{$a, $b} = sWapxy{$a, $b}
p1nf "$a\n"
p1nf "$b\n"
yy
xxx
Cospauue ecfpaueaerux qyukuu
Ecnn nx ]ynxnnn saan nporornn (), ona moxer ecmpaueamic n xo xomnnnxropom Perl.
Bcrpannaemax (inline) ]ynxnnx onrnmnsnpyercx nx ontme cxopocrn ntuncnenn, o-
naxo ona onxna nounnxrtcx cnennantntm orpannuennxm n cocroxrt ns xoncranrt nnn
cxanxpa c nexcnuecxo onacrtm nnnmocrn. (B nocnenem cnyuae na cxanxp ne onxno
Crp.149
trt nnxaxnx ononnnrentntx cctnox.) Kpome roro, na ncrpannaemym ]ynxnnm nentsx
cctnartcx, yxastnax npe]nxc nnn xnmuenoe cnono do, raxne ntsont ]ynxnn nnxo-
ra ne npeopasymrcx Perl no ncrpannaemte.
Hanpnmep, nor raxne ]ynxnnn yyr ncrpannartcx Perl:
sub e {} {2.712}
sub e {} {exp 1.0}
Oe nosnpamamr uncno e ocnonanne narypantnoro norapn]ma. Hepnax ]ynxnnx saaer
ero n nne xoncranrt, a nropax ncnontsyer ncrpoennym ]ynxnnm Perl exp. (Kcrarn,
]ynxnnx exp nosnpamaer onee rounoe snauenne nx ocnonannx narypantnoro norapn]ma,
uem xoncranra, yxasannax n nepno ]ynxnnn.)
3areeuue ecfpoeuuux qyukuu. Rceeponakef CORE
Ho samemennem nonporpammt nonnmamr nonoe onpeenenne cymecrnymme nonpo-
rpammt. Paspemaercx samemart nmte nonporpammt, n rom uncne n ncrpoennte n Perl,
no rontxo n rom cnyuae, ecnn nonporpamma yxe nmnoprnponana ns moynx. B npornnnom
cnyuae oxnnennx nonporpammt yer neocrarouno.
Onaxo nx oxnnennx nonporpammt, xoropax samemaer nnocnecrnnn nmnoprnpyemym
nonporpammy, nt moxere ncnontsonart nparmy subs. Honporpammt, utn nmena nepe-
uncnent n subs, yyr samemart ncrpoennte ]ynxnnn Perl. Paccmorpnm npnmep, n xoro-
pom mt samemaem ]ynxnnm Perl exit, sacrannxx ee cnpamnnart, ecrnnrentno nn nont-
sonarent xouer ntrn ns nporpammt:
use subs ex1f use subs ex1f use subs ex1f use subs ex1f
sub ex1f sub ex1f sub ex1f sub ex1f
{
p1nf "Do you ea11y Wanf fo ex1f"
$ansWe = <>
1f {$ansWe = /y/1} {COkL::ex1f}
}
Wh11e {1} {
ex1f
}
3ect, nx roro urot na camom ene ntrn ns nporpammt, xora nontsonarent saxouer
+ro cenart, mt ncnontsyem ncenonaxer E, ntstnax ]ynxnnm E::exit. (B Perl
moxno yxastnart n xnnom nne, ns xaxoro naxera epercx ra nnn nnax ]ynxnnx, ncnont-
syx cnmnont :: n xauecrne pasennrenx.) Hcenonaxer E coepxnr ncrpoennte ]ynx-
nnn Perl n ncxonom nne, n axe samemax ony ns nnx, nce panno c nomomtm E
moxno ntsnart ncxonym nepcnm.
Henoureuoeauuue nopnporparru
Perl nosnonxer cosanart estmxnnte (anonnmnte) nonporpammt. Hanpnmep, n cne-
ymmem cnyuae mt cosaem cctnxy na nonporpammy (cctnxn nopono pasnpamrcx n
cneymme rnane), ne npncnannax nonporpamme nmenn:
$codeef = sub {p1nf "he11o!\n"}
Oparnre nnnmanne, uro +ry xomany rpeyercx sanepmnrt rouxo c sanxro, xorx npn
otunom onpeenennn nonporpamm ona ne rpeyercx. Xorx y nonporpammt n ner nme-
Crp.150
nn, ee moxno ntsnart c npe]nxcom (saxnmunn cctnxy n ]nrypnte cxoxn):
{$codeef}
he11o!
Cospauue fanuu pucnefepusauuu nopnporparr
Tannna ncneruepnsannn nonporpamm (subroutine dispatch table) +ro cosanaemax
nontsonarenem crpyxrypa anntx (maccnn nnn x+m), coepxamax cctnxn na nonporpam-
mt. Moxno yxasart, xaxym nonporpammy rpeyercx ntsnart, yxasan nnexc (n cnyuae
maccnna) nnn xnmu (n cnyuae x+ma). Ecnn ecrt annte, xoropte nyxno opaartnart ne-
cxontxnmn onornnno ntstnaemtmn nonporpammamn, ro sauacrym nonesnee oxastnaer-
cx ocryn ne no nmenn, a no nnexcy nnn xnmuy.
Paccmorpnm cneymmn npnmep. V nac ecrt ne nonporpammt: ona nepenonr rpayct
Hentcnx n rpayct uapenrera, pyrax conepmaer oparnym nponeypy:
sub cfof # cenf1gade fo Iahenhe1f
{ $va1ue = sh1ff{0}
efun 9 " $va1ue / 5 + 32 }
sub ffoc # Iahenhe1f fo cenf1gade
{ $va1ue = sh1ff{0}
efun 5 " {$va1ue - 32} / 9 }
urot nomecrnrt ]ynxnnn n rannny ncneruepnsannn, mt sanomnnaem cctnxn na no-
nporpammt (cctnxn paccmarpnnamrcx n cneymme rnane):
$fempconvef|0] = \ffoc
$fempconvef|1] = \cfof
Tenept moxno c nomomtm nnexca ntnpart, xaxym nmenno nonporpammy ntstnart:
p1nf "eo cenf1gade 1s " . {$fenpconvef|1]}{0} . " Iahenhe1f.\n"
eo cenf1gade 1s 32 Iahenhe1f.
Hocuasua. Umooi nepeoami napa+empi eisieae+o maru+ oopaso+ nonpoepa++e, uaoo no+ecmumi
cnucor napa+empoe e rpve.ie crooru, c.eovmuue sa cci.ro ua noonpoepa++v.
Crp.151
Fnana 8
Ccmnku n PerI

Kopo1ko
Cci.ru nont ]ynamenrantnt rnn anntx n Perl, nnepnte nneennt nx nepcnn 5.
Honeenne cctnox n snaunrentno mepe nanomnnaer noneenne yxasarene n raxnx xst-
xax, xax C. Kax cneyer ns nasnannx, cctnxa cctnaercx na +nemenr anntx. urot o-
partcx o nacroxmnx anntx, nao pasi+euoeami (dereIerence) cctnxy.
C nomomtm cctnox moxno cosanart n Perl cnoxnte crpyxrypt anntx (npnmept yyr n
rnane 13). Cctnxn ncnontsymrcx nx norpxcammero uncna npnemon nporpammnponannx n
uacrnocrn, nx cosannx anonnmntx maccnnon, x+me, nonporpamm n manonon ]ynxnn
(nce +rn remt paccmarpnnamrcx n anno rnane). Ecnn nonporpamme nepeaercx onee o-
noro maccnna nnn x+ma, xoropte npn +rom nao coxpannrt xax nnnnnyantnte crpyxrypt
anntx, es cctnox ne oornct. (B npornnnom cnyuae +nemenrt ncex maccnnon n x+me
cnnnamrcx n onn nnnnt cnncox.) Hpn cosannn oexra n ncnontsonannn cranaprno
rexnnxn opamennx x xoncrpyxropy xnacca cam xoncrpyxrop, xax npannno, nosnpamaer cctn-
xy na oexr, a ne cam oexr. Cneonarentno, cctnxn +ro eme n ]ynamenrantnt nncr-
pymenr oexrno-opnenrnponannoro nporpammnponannx n Perl.
B Perl nmemrcx na rnna cctnox: xecrxne (hard reIerences) n cnmnonnuecxne (symbolic reI-
erences). B nauane rnant npnneen xparxn osop +rnx nyx rnnon.
Xecfkue ccunku
Hycrt nmeercx nepemennax c nmenem $variable1:
$va1ab1e1 = 5
urot cosart xecrxym cctnxy na nee, ncnontsyercx oneparop oparnax xocax uepra:
$va1ab1e1 = 5
$ $$ $efeence efeence efeence efeence = == = \ \\ \$ $$ $va1ab1e va1ab1e va1ab1e va1ab1e1 1 1 1
Tenept nepemennax $reference coepxnr cctnxy na $variable1 (no cctnxo nonnmaercx
apec nepemenno n namxrn n rnn nepemenno). Cctnxn raxoro rnna nastnamrcx recm-
ru+u. Cxanxpnte nepemennte Perl moryr xpannrt xecrxne cctnxn n xauecrne snauenn.
urot pasi+euoeami cctnxy, ncnontsyercx oneparop $ cm. cneymmn npnmep, n xo-
ropom snauenne, na xoropoe cctnaercx nepemennax $reference, nonyuaercx c nomomtm
pastmenonannx uepes $:
$va1ab1e1 = 5
$efeence = \$va1ab1e1
p1nf $$efe p1nf $$efe p1nf $$efe p1nf $$efe ence ence ence ence
Pastmenonanne cctnxn nosnonxer nonyunrt ncxonte annte:
$va1ab1e1 = 5
$efeence = \$va1ab1e1
p1nf $$efeence
Crp.152
5
uro nonyunrcx, ecnn nao nponepnrt snauenne cctnxn B +rom cnyuae mt ynnnm ]axrn-
uecxn apec n rnn nepemenno $variable1 n onacrn anntx nnrepnperaropa Perl:
$va1ab1e1 = 5
$efeence = \$va1ab1e1
p1nf $$efeence
5CALAk{Oxa57d4}
Tax ntrnxnr xecrxax cctnxa n Perl.
Cureonueckue ccunku
Cu+eo.u:ecra cci.ra ne coepxnr apec n rnn +nemenra anntx, rontxo ero u+. Bep-
nemcx x nepemenno $variable1 ns npetymero npnmepa:
$va1ab1e1 = 5
Ee nmx moxno npncnonrt pyro nepemenno $variablename (oparnre nnnmanne, npn
saannn nmenn onycxaercx pastmenontnammn npe]nxc $):
$va1ab1e1 = 5
$ $$ $va1ab1ename va1ab1ename va1ab1ename va1ab1ename = " = " = " = "va1ab1e va1ab1e va1ab1e va1ab1e1" 1" 1" 1"
Pastmenonanne nmenn nepemenno npenpamaer +ro nmx n cctnxy na annte, xpanxmnecx
n ncxono nepemenno. 3ror nponecc n nastnaercx cosannem cu+eo.u:ecro cci.ru.
Bor xax +ror mero paoraer c oneparopom $:
$va1ab1e1 = 5
$va1ab1ename = "va1ab1e1"
p1nf p1nf p1nf p1nf "$$va1ab1ename "$$va1ab1ename "$$va1ab1ename "$$va1ab1ename\ \\ \n" n" n" n"
5
Bmecro oneparopa $ nx pastmenonannx cctnox moxno raxxe ncnontsonart oneparop-
crpenxy ->.
Onepafop-cfpenka
Oneparop-crpenxa pyrax nonynxpnax ]opma oneparopa pastmenonannx cctnox. On
ncnontsyercx co cctnxamn na maccnn, x+m, nonporpammy n conponoxaercx nnexcom,
xnmuom, cnncxom aprymenron:
0aay = {1, 2, 3}
$aayefeence = \0aay
p1nf $aayefeence p1nf $aayefeence p1nf $aayefeence p1nf $aayefeence- -- ->|0] >|0] >|0] >|0]
1
Hoponee o oneparope-crpenxe paccxastnaercx anee n +ro rnane.
Auouuruue raccueu, xsu-fanuuu, nopnporparru
B Perl moxno cosanart maccnnt, x+mn n nonporpammt, nx ocryna x xoroptm ncnontsy-
mrcx cctnxn, a ne nmena. Hanpnmep, npnmep ns npetymero pasena coxpamaercx, ecnn
cpasy cosart anonnmnt maccnn n npncnonrt cctnxy na nero nepemenno $arrayreference:
$aayefeence = |1, 2, 3]
Crp.153
urot oparnrtcx x coepxnmomy maccnna, norpeyercx oneparop pastmenonannx:
$aayefeence = |1, 2, 3]
p1nf p1nf p1nf p1nf $$ $$ $$ $$aayefeence aayefeence aayefeence aayefeence|0] |0] |0] |0]
1
C nomomtm xoncrpyxnn rnna anonnmntx maccnnon cosamrcx cnoxnte crpyxrypt an-
ntx rnna maccnna maccnnon. B +ro rnane emoncrpnpymrcx noonte npnemt (a raxxe
mnoroe pyroe, nmemmee ornomenne x cctnxam). Oparnmcx x noponocrxm.
Henocpepc1neuume pemeuun
Cospauue ccunku
nx cosannx cctnxn ncnontsyercx oneparop oparnax xocax uepra (\). Cctnxn, cosan-
nte c ero nomomtm, nastnamrcx recmru+u. Kpome roro, cctnxy moxno nsnneut npxmo
ns rannnt cnmnonon Perl - +ra nn]opmannx coepxnrcx n pasene Kax nsnneut cctnxy
ns rannnt cnmnonon anee n +ro rnane. Kpome roro cymecrnymr cnmnonnuecxne cctn-
xn - nopono o nnx paccxastnaercx n pasene Cosanne cnmnonnuecxnx cctnox.
Oneparop oparnax xocax uepra moxer ncnontsonartcx nx cxanxpntx nepemenntx,
maccnnon, x+me, nonporpamm nnn xoncranr. Hanpnmep, nor rax cosaercx xecrxax
cctnxa na snauenne-xoncranry:
$efeence = \"he11o!"
Touno rax xe cosamrcx cctnxn nx nepemenntx, maccnnon, x+me, nonporpamm, n r. .
(oparnre nnnmanne, uro cctnxn xpanxrcx n cxanxpntx nepemenntx):
$sca1aefeence = \$myva1ab1e
$aayefeence = \0myaay
$hashefeence = \xmyhash
$codeefeence = \mysubouf1ne
$g1obefeence = \"myname
urot nonyunrt snauenne nepemenno uepes cctnxy, ncnontsyercx oneparop pa-
stmenonannx:
$efeence = \"he11o!"
p1nf $$efeen p1nf $$efeen p1nf $$efeen p1nf $$efeence ce ce ce
he11o!
Moxno ncnontsonart crontxo yponne nnoxennocrn, cxontxo nam xouercx. Hanpnmep, n
cneymmem npnmepe cosaercx cctnxa na cctnxy, na cctnxy, na cctnxy:
$efeence4 = \\\\"he11o!"
A renept ona pastmenontnaercx c nomomtm oneparopa $:
p1nf p1nf p1nf p1nf $$$$$ $$$$$ $$$$$ $$$$$efeence efeence efeence efeence
he11o!
Hnrepecnoe cnocrno Perl cocronr n rom, uro cctnxn cosamrcx anromarnuecxn, n momenr
pastmenonannx, n npenonoxennn, uro onn cymecrnymr:
$$efeence = 5
p1nf "$$efeence\n"
5
Crp.154
Kax nerxo samernrt, n +rom npnmepe cctnxa (ro ecrt apec n namxrn) ncnontsyercx o roro,
xax ona peantno noxnnnact. Hocne ntnonnennx npnneennoro ntme xoa cctnxa ecrnn-
rentno nosnnxner - +ror nponecc nastnaercx n Perl ca+oorue.euue+ (autoviviIication):
p1nf "$efeence\n"
5CALAk{0xa0b14}
B cneymmem npnmepe cctnxn ncnontsymrcx nx nepeaun nonporpamme n xauecrne napa-
merpon nyx maccnnon. Honporpamma no+nemenrno cxnatnaer nx (npenonarax, uro maccnnt
nmemr onnaxonym nnny), nosnpamax n xauecrne pesyntrara maccnn. Hepeaua cctnox nmecro
camnx maccnnon npeornpamaer oennenne nx coepxnmoro n onn nepasnnunmt cnncox:
0a = {1, 2, 3}
0b = {4, 5, 6}
sub addem
{
my {$efeence1, $efeence2} = 0
fo {$1oop1ndex = 0 $1oop1ndex <= $#$efeence1 $1oop1ndex++} {
$esu1f|$1oop1ndex] = 0$efeence1|$1oop1ndex] + 0$efeence2|$1oop1ndex] }
efun 0esu1f
}
0aay = addem{0a, 0b}
p1nf o1n {, , 0aay}
5, 7, 9
Ccunku ua auouuruue raccueu
B nauane +ro rnant yxe cosanancx maccnn es nmenn, nasnannt namn auouu+ui+. nx
+ro nenn ncnontsonancx reneparop anonnmntx maccnnon (anonymous array composer)
napa xnaparntx cxoox:
$aayefeence = |1, 2, 3]
3ra xoncrpyxnnx nosnpamaer cctnxy na anonnmnt maccnn, xoropax npncnannaercx cxanxp-
no nepemenno $arrayreference. o +nemenron maccnna moxno opartcx, pastmenonan
cctnxy, no nmx maccnna nx +ro nenn ncnontsonart nentsx - ero nonpocry ne cymecrnyer:
$aayefeence = |1, 2, 3]
p1nf p1nf p1nf p1nf $$ $$ $$ $$aayefeence aayefeence aayefeence aayefeence|0] |0] |0] |0]
1
nx pastmenontnamr cctnxn na estmxnnt maccnn moxno raxxe ncnontsonart onepa-
rop-crpenxy:
$aayefeence = |1, 2, 3]
p1nf p1nf p1nf p1nf $ $$ $aayefeence aayefeence aayefeence aayefeence- -- ->|0] >|0] >|0] >|0]
1
Hocuasua. Poooma onepamopa-cmpe.ru noopoouo onucieaemc oa.ee e pasoe.e Pasi+euoeauue cci.or
c no+ouim onepamopa-cmpe.ru
Anonnmnte maccnnt amr nosmoxnocrt nporpammncram na Perl ncnontsonart nonesnt
rpmx, cocroxmn n nnrepnonnponannn nnyrpt crpoxn, orpannuenno nontmn xantuxa-
mn, pesyntrar ntsona nonporpammt nnn pesyntrar ntuncnennx. B cneymmem npnmepe
ncrpoennax ]ynxnnx Perl uc nepenonr n nepxnn perncrp crpoxy:
p1nf "0{|uc{he11o p1nf "0{|uc{he11o p1nf "0{|uc{he11o p1nf "0{|uc{he11o}]} fhee. }]} fhee. }]} fhee. }]} fhee. \ \\ \n" n" n" n"
hLLLO fhee.
Pasepemcx, uro enaer +ra xoncrpyxnnx. Perl opaartnaer xoncrpyxnnm ] xax nox.
Crp.155
B nponecce ero ntuncnennx cosaercx cctnxa na anonnmnt maccnn ns onoro-
enncrnennoro +nemenra pesyntrara ntsona ]ynxnnn uc. Hpn pastmenonannn cctnxn
na anonnmnt maccnn pesyntrar nnrepnonnpyercx nnyrpt crpoxn.
Ccunku ua auouuruue xsuu
Moxno cosart cctnxy na x+m es nmenn on raxxe nastnaercx auouu+ui+. nx +ro nenn
ncnontsyercx reneparop anonnmntx x+me (anonymous hash composer) -napa ]nrypntx cxoox:
$hashefeence = {
Name => 1omm1e,
lD => 1234,
}
Mt cosann anonnmnt x+m, oannnn n nero ne napt xnmu/snauenne n nomecrnnn
cctnxy na x+m n cxanxpnym nepemennym $hashreference. Tenept moxno ncnontsonart ee
xax otunt x+m, no cnauana nao pastmenonart cctnxy:
p1nf p1nf p1nf p1nf $$ $$ $$ $$hashefeence hashefeence hashefeence hashefeence { {{ {Name Name Name Name} }} }
1omm1e
nx +ro nenn moxer cnyxnrt n oneparop-crpenxa:
$hashefeence = { Name => 1omm1e, lD => 1234, }
p1nf $hashefeence p1nf $hashefeence p1nf $hashefeence p1nf $hashefeence- -- ->{Name} >{Name} >{Name} >{Name}
1omm1e
Hocuasua. Paooma onepamopa-cmpe.ru noopoouo onucieaemc oa.ee e pasoe.e Pasi+euoeauue cci.or
c no+ouim onepamopa-cmpe.ru.
Ccunku ua auouuruue nopnporparru
Moxno cosart cctnxy na nonporpammy es nmenn raxax nonporpamma nastnaercx
auouu+uo. nx +ro nenn ncnontsyercx reneparop anonnmntx nonporpamm (anonymous
subroutine composer) xnmuenoe cnono sub:
$codeefeence = sub {p1nf he11o!\n}
Oparnre nnnmanne, uro n xonne xomant npnmnoct oannrt rouxy c sanxro, rora xax
n cnyuae otuntx nonporpamm +roro enart ne nao. urot ntsnart nonporpammy, na-
o pastmenonart cctnxy n oannrt npe]nxc :
$ $ $ $codeefeence codeefeence codeefeence codeefeence
he11o!
Taxxe moxno nepeart anonnmno nonporpamme napamerpt:
$codeefeence = sub {p1nf sh1ff}
{$codeefeence}{ "he11o! {$codeefeence}{ "he11o! {$codeefeence}{ "he11o! {$codeefeence}{ "he11o!\ \\ \n nn n"} "} "} "}
he11o!
Hpn xenannn nx pastmenonannx cctnxn na nonporpammy moxno ncnontsonart onepa-
rop-crpenxy (noponee o +rom cm. pasen Pastmenonanne cctnox c nomomtm oneparo-
pa-crpenxn anee n +ro rnane):
$codeefeence = sub {p1nf sh1ff}
$codeefeence $codeefeence $codeefeence $codeefeence - -- -> >> > {"he11o! {"he11o! {"he11o! {"he11o!\ \\ \n nn n"} "} "} "}
he11o!
Crp.156
Kak useneu ccunky us fanuuu cureonoe
3anncn n rannne cnmnonon Perl +ro x+mn, xoropte coepxar apeca ncex nme-
nonanntx +nemenron rexymero naxera. Ipynnonomy nmenn name coornercrnyer x+m
*name, nnexcnponannt xnmuamn SAA, HASH, DE n r. . 3ro snaunr, uro xo-
ra nam rpeyercx cctnxa na +nemenr anntx, nt moxere nsnneut ee npxmo ns rannnt
cnmnonon, a ne ncnontsonart oneparop \:
$sca1aefeence = "name{5CALAk}
$aayefeence = "name{AkkA}
$hashefeence = "name{hA5h}
$codeefeence = "name{CODL}
$1oefeence = "name{lO}
$g1obefeence = "name{GLO8}
Hanpnmep, urot nonyunrt cctnxy na nepemennym c nmenem $variable1 n c ee nomomtm
ntnecrn snauenne +ro nepemenno, moxno ncnontsonart xo:
$va1ab1e1 = 5
$sca1aefeence = "va1ab1e1{5CALAk} $sca1aefeence = "va1ab1e1{5CALAk} $sca1aefeence = "va1ab1e1{5CALAk} $sca1aefeence = "va1ab1e1{5CALAk}
p1nf $$sca1aefeence
5
Touno rax xe moxno nonyunrt cctnxy na nonporpammy:
sub p1nfem
{ p1nf "he11o!\n" }
$codeefeence = "p1nfem{CODL} $codeefeence = "p1nfem{CODL} $codeefeence = "p1nfem{CODL} $codeefeence = "p1nfem{CODL}
$codeefeence $codeefeence $codeefeence $codeefeence
he11o!
Koncrpyxnnx nameI] nosnpamaer ecxpnnrop noroxa nnoa n/nnn ntnoa ro ecrt e-
cxpnnrop ]ana, coxer, ecxpnnrop xaranora.
Hocuasua 1. He.is no.v:umi cci.rv na oecrpunmop nomora eeooa/eieooa c no+ouim onepamopa .
Hocuasua 2. ocmvn r cci.re :epes sanucu e mao.ue cu+eo.oe saeucum om moeo, ucno.isoea.c .u coom-
eemcmevmuu o.e+eum oauuix (mo ecmi cvuecmevem .u coomeemcmevmuu cu+eo.). Ec.u cu+eo.a, o. ro-
mopoeo ea+ uvrua cci.ra, ue cvuecmevem, e+ecmo cci.ru ei no.v:ume sua:euue undef. Pauuue eepcuu Perl
eosepaua.u cci.rv ua, crare+, auouu+ui cra.p, ec.u eipareuue euoa newscalar]SCALAR} sanpauuea-
.oci oo uuuua.usauu nepe+euuo $newscalar, oouaro o. Perl eepcuu 5 +ro ue mar.
Pasureuoeauue ccunok
nx pastmenontnannx cctnxn (ro ecrt ocryna x anntm, na xoropte ona yxastnaer) nc-
nontsyercx oneparop $. B +ro rnane yxe npnnonnnct npnmept ero paort:
$va1ab1e1 = 5
$efeence = \$va1ab1e1
p1nf p1nf p1nf p1nf $$ $$ $$ $$efe efe efe efe ence ence ence ence
5
Oneparop $ ncnontsyercx nese, re ecrt nenrn]nxaropt nnn nx naopt. Bor necxontxo
npnmepon pastmenonannx asontx rnnon anntx Perl:
$sc1a = $$sca1aefeence
0aay = 0$aayefeence
xhash = x$hashefeence
$codeefeence{$agumenf1, $agumenf2}
"g1ob = "$g1obefeence
Crp.157
Kax yxe tno noxasano, c nomomtm oneparopa $ moxno raxxe ononpemenno pastmenont-
nart necxontxo yponne cctnox:
$efeence4 = \\\\"he11o!"
p1nf $$$$$efeence
he11o!
Kpome npocrtx cnyuaen, moxno oannxrt nnexc, xora pastmenonannm nonepraercx
cctnxa na maccnn:
{0aay = {1, 2, 3}
$aayefeence = \0aay
p pp p1nf $$a 1nf $$a 1nf $$a 1nf $$a ayefeence ayefeence ayefeence ayefeence|0] |0] |0] |0]
1
Touno rax xe n cnyuae pastmenonannx cctnxn na x+m-rannny moxno yxastnart xnmu:
xhash = { Name => 1omm1e, lD => 1234, }
$hashefeence = \xhash
p1nf p1nf p1nf p1nf $$ $$ $$ $$hashefe hashefe hashefe hashefe ence ence ence ence{ {{ {Name Name Name Name} } } }
1omm1e
Hpn pastmenontnannn cctnxn na nonporpammy moxno yxasart cnncox aprymenron:
sub p1nfem
{
p1nf sh1ff
}
$codeefeence = \p1nfem
$ $$ $codeefeence codeefeence codeefeence codeefeence - -- -> {" > {" > {" > {"he11o he11o he11o he11o! !! !\ \\ \n nn n"} "} "} "}
he11o!
Hpxmax cctnxa nnora samenxercx nosnpamammnm ee noxom. Bor xax npnneennte nt-
me npnmept ntrnxxr n cnyuae npnmenennx noxon:
$sc1a = ${$sca1aefeence}
0aay = 0{$aayefeence}
xhash = x{$hashefeence}
{$codeefeence}{$agumenf1, $agumenf2}
"g1ob = "{$g1obefeence}
H nocnenee. 3anncn rannnt cnmnonon (rnn anntx typeglob) moryr pastmenontnartcx
rouno rax xe, xax n cctnxn, nocxontxy onn coepxar cctnxn na nce rnnt anntx, acconnn-
ponannte c +rnm nmenem. Hpn pastmenonannn cctnxn c nomomtm npe]nxca ncera nao
yxastnart, xaxoro rnna annoe nt pacunrtnaere nonyunrt na ntxoe. 3ror xe camt npe-
]nxc nomoraer Perl nonxrt, xaxo ns +nemenron anntx, acconnnponanntx c saanntm nme-
nem, nt nmeere n nny npn pastmenontnannn rnna anntx typeglob. Hanpnmep, n cneym-
mem xoe rpeyercx nonyunrt cxanxpnym nepemennym (pastmenontnammn npe]nxc $):
$va1ab1e1 = 5
p1nf p1nf p1nf p1nf ${ ${ ${ ${va1 va1 va1 va1ab1e ab1e ab1e ab1e1} 1} 1} 1}
5
To xe camoe mt nonyuaem, ecnn rpeyercx maccnn (pastmenontnammn npe]nxc ):
0aay = {1, 2, 3}
p1nf p1nf p1nf p1nf o1n o1n o1n o1n{", ", 0{" {", ", 0{" {", ", 0{" {", ", 0{"aay aay aay aay}} }} }} }}
1, 2, 3, 4
Pasureuoeauue ccunok c norouk onepafopa-cfpenku
Crp.158
Hpn paore c maccnnamn, x+mamn n nonporpammamn nx npocroro pastmenonannx cct-
nox moxno ncnontsonart oneparop-crpenxy. Hanpnmep, nor rax oneparop-crpenxa paora-
er n couerannn co cctnxo na maccnn:
$aayefeence = |1, 2, 3]
p1nf p1nf p1nf p1nf $ $$ $aayefeence aayefeence aayefeence aayefeence- -- ->|0] >|0] >|0] >|0]
7
Moxno raxxe cosanart maccnnt maccnnon, xax n cneymmem npnmepe. 3ect cosaercx
anonnmnt maccnn, cocroxmn ns nyx pyrnx anonnmntx maccnnon:
$aayefeence = ||1, 2, 3], |4, 5, 6]]
urot cctnartcx na +nemenrt maccnna maccnnon, ncnontsyercx cneymmn cnnraxcnc:
$aayefeence = ||1, 2, 3], |4, 5, 6]]
p p p p1nf 1nf 1nf 1nf $ $$ $aayefeence aayefeence aayefeence aayefeence- -- ->|1]|1] >|1]|1] >|1]|1] >|1]|1]
5
(Oparnre nnnmanne na orcyrcrnne oneparopa-crpenxn mexy nymx napamn xnaparntx
cxoox. Hoponee +ror nonpoc paccmarpnnaercx anee n pasene Kora moxno onycxart
oneparop-crpenxy,)
Hocuasua. Bo.ee noopoouo o +accueax +accueoe paccrasieaemc e e.aee 13.
Bt moxere raxxe ncnontsonart oneparop-crpenxy npn paore co cctnxamn na x+m-rannnt:
$hashefeence->{key} = "1h1s 1s fhe fexf."
p1nf $hashefeence->{key}
1h1s 1s fhe fexf.
(B +rom npnmepe nx cosannx +nemenra, na xoropt cctnaercx nepemennax $hashreference,
mt nonaraemcx na nponecc ca+oorue.enu (autoviviIication), o xoropom yxe paccxastnanoct
ntme n pasene Cosanne cctnxn.)
A nor xax ncnontsyercx oneparop-crpenxa npn cctnxe na nonporpammy:
sub p1nfem
{ p1nf sh1ff }
$codeefeence = \p1nfem
$ $$ $codeefe codeefe codeefe codeefe ence ence ence ence- -- ->{" >{" >{" >{"he11o he11o he11o he11o! !! !\ \\ \n nn n"} "} "} "}
he11o!
B omem cnyuae cnena or oneparopa-crpenxn moxer croxrt nmoe ntpaxenne, nos-
npamammee cctnxy:
$dafasef|$foday]->{p1ces}->|1234] = "\$4999.99"
Korpa rouo onyckafu onepafop-cfpenky
Oneparop-crpenxy n Perl neoxsarentno crannrt mexy xnaparntmn n xpyrntmn cxoxa-
mn. Ho+romy npnmep ns npetymero pasena
$dafasef|$foday]->{p1ces}->|1234] = "\$4999.99"
moxno sanncart xax:
$dafasef|$foday]{p1ces}|1234] = "\$4999.99"
Perl paspemaer onycxart oneparop-crpenxy mexy cxoxamn, npexe ncero, sarem, urot
nosnonnrt paorart c maccnnamn maccnnon n cenart nx noxoxnmn na mnoromepnte mac-
cnnt pyrnx xstxon nporpammnponannx. Hpnneem npnmep:
Crp.159
0aay = {|1, 2], |3, 4]}
p1nf p1nf p1nf p1nf $ $$ $aay aay aay aay|1]|1] |1]|1] |1]|1] |1]|1]
4
Kak onpepenufu fun ccunku c norouk onepafopa re
Oneparop ref nomoraer onpeennrt, na +nemenr xaxoro rnna cctnaercx cctnxa. Cnnraxcnc
ntsona ref ntrnxnr rax:
ef aupaeuue
ef
3ror oneparop nosnpamaer snauenne ucmuua (nenynenoe snauenne), ecnn eipareuue +ro
cctnxa, n .ori n npornnnom cnyuae. Ecnn ne yxasano ntpaxenne, ro oneparop ref ncnontsy-
er cnennantnym nepemennym $_. 3nauenne, nosnpamaemoe xax ucmuua, orpaxaer rnn +ne-
menra, na xoropt cctnaercx cctnxa. Bcrpoennte rnnt Perl coornercrnymr snauennxm:
EF HASH
SAA DE
AA GB
Bor npnmep, n xoropom oneparop ref npnmenxercx x cctnxe na cxanxp:
$va1ab1e1 = 5
$sca1aef = \$va1ab1e1
p1nf {ef $sca1aef} p1nf {ef $sca1aef} p1nf {ef $sca1aef} p1nf {ef $sca1aef}
5CALAk
Cospauue cureonueckux ccunok
ecrxax cctnxa coepxnr apec +nemenra n namxrn n ero rnn. Cnmnonnuecxax cctnxa nme-
cro apeca coepxnr nmx +nemenra. Tem camtm nocnenxx sacrannxer Perl ncxart nepemen-
nym c saanntm nmenem nmecro roro, urot nanpxmym oparnrtcx no nyxnomy apecy.
B cneymmem npnmepe cosaercx cnmnonnuecxax cctnxa na nepemennym $variable1, n sa-
rem +ra cctnxa ncnontsyercx nx ocryna x ncxono nepemenno:
$va1ab1e1 = 0
$ $$ $va1ab1ename va1ab1ename va1ab1ename va1ab1ename = = = = " "" "va1ab1e va1ab1e va1ab1e va1ab1e1" 1" 1" 1"
$$ $$ $$ $$va va va va1 11 1ab1ename ab1ename ab1ename ab1ename = 5 = 5 = 5 = 5
p1nf "$va1ab1e1\n"
5
(Oparnre nnnmanne, uro cnmnonnuecxax cctnxa coepxnr nmx nepemenno es pastme-
nontnammero npe]nxca $.)
Kax n n cnyuae xecrxnx cctnox, ncnontsonanne cnmnonnuecxnx cctnox c eme ne cymecr-
nymmnmn +nemenramn anntx npnnonr x cosannm +rnx +nemenron. B cneymmem npn-
mepe nepemennax $variable1 noxnnnact rontxo nocne roro, xax na nee cocnannct:
$va1ab1ename = "va1ab1e1"
${$va1ab1ename} = 5 ${$va1ab1ename} = 5 ${$va1ab1ename} = 5 ${$va1ab1ename} = 5
p1nf "$va1ab1e1\n"
5
Touno rax xe moxno cosanart cnmnonnuecxne cctnxn na +nemenrt anntx rnna maccn-
non nnn x+m-rannn:
Crp.160
$aayname = $aayname = $aayname = $aayname = "aay1" "aay1" "aay1" "aay1"
$aayname->|1] = 5
p1nf "$aay1|1]\n"
5
C nomomtm cnmnonnuecxnx cctnox moxno cctnartcx axe na nonporpammt:
$subouf1nename $subouf1nename $subouf1nename $subouf1nename = = = = "subouf1ne1" "subouf1ne1" "subouf1ne1" "subouf1ne1"
sub subouf1ne1
{ p1nf "he11o!\n" }
$subouf1nename{}
he11o!
C nomomtm cnmnonnuecxnx cctnox moxno cctnartcx na rnoantnte n noxantnte nepemen-
nte rexymero naxera. Hepemennte c nexcnuecxo onacrtm nnnmocrn (ro ecrt onncannte c
xnmuentm cnonom my) ne coepxarcx n rannne cnmnonon n no+romy na nnx nentsx cctnart-
cx c nomomtm cnmnonnuecxnx cctnox. Hanpnmep, n cneymmem ]parmenre xoa nmecro sna-
uennx nepemenno, na xoropym yxastnaer cnmnonnuecxax cctnxa, ntnonrcx nycrax crpoxa
(Perl ne moxer narn nepemennym n nnrepnpernpyer ee snauenne xax undef):
my $va1ab1e1 = 10
$va1ab1ename = "va1ab1e1" # ye1 npobneua
p1nf "1he va1ue 1s $$va1ab1ename\n"
# hpuaeeuuuu aue xo auac1 uenonuuu peaynu1a1:
1he va1ue 1s
3anpef cureonueckux ccunok
Ouent cnoxno ncnontsonart xecrxne cctnxn nenontno, no nnonne nosmoxno nc-
nontsonart cnmnonnuecxym cctnxy (ro ecrt nmx nepemenno, na xoropym cctnamrcx) n
pesyntrare omnxn. urot sanpernrt ncnontsonanne cnmnonnuecxnx cctnox, cneyer
nxnmunrt n cnenapn nparmy:
use sf1cf efs
o oxonuannx noxa c raxo nparmo Perl paspemaer ncnontsonart rontxo xecrxne cctn-
xn. Onaxo, ecnn nt xornre paspemnrt cnmnonnuecxne cctnxn nnyrpn nnoxennoro noxa,
saare pyrym nparmy:
no sf1cf efs
Hcnonusoeauue ccunok ua raccueu kak ccunok ua xsuu
Haunnax c nepcnn Perl 5.005 ram, re onxna naxonrtcx cctnxa na x+m, paspemaercx nc-
nontsonart cctnxy na maccnnt (no xpane mepe, +ro moxno enart n ontmnncrne cny-
uaen). Hntmn cnonamn, npn onpeenenno oprannsannn maccnna x ero +nemenram moxno
opamartcx no nmenam.
Hocuasua. Jmo uoeoe u orcnepu+euma.iuoe ceocmeo Perl. Ouo +orem us+euumic e ovovue+.
urot ncnontsonart cctnxy na maccnn n xauecrne cctnxn na x+m, neoxonmo oannrt n
maccnn ononnnrentnym nn]opmannm: ycranonnrt coornercrnne mexy xnmuamn x+ma n
nnexcamn maccnna. 3ra nn]opmannx sanocnrcx n nyneno +nemenr maccnna n cneymmem
]opmare:
{key1 => 1ndex1, key2 => 1ndex2, key3 => 1ndex3, ...}
Crp.161
B cneymmem npnmepe cosaercx cctnxa na anonnmnt maccnn, x xoropomy oannena
nn]opmannx o x+m-xnmuax c nmenamn first n second:
$aayef = |{f1sf=>1, second=>2}, "he11o", "fhee"]
Tenept na +nemenrt maccnna moxno cctnartcx c nomomtm xnmue, uro nnnmcrpnpyer
cneymmn npnmep:
$aayef = |{f1sf=>1, second=>2}, "he11o", "fhee"]
p1nf $aayef p1nf $aayef p1nf $aayef p1nf $aayef- -- ->{f1sf} . >{f1sf} . >{f1sf} . >{f1sf} . " " . " " . " " . " " . $ $$ $a aa a ayef ayef ayef ayef- -- ->{ >{ >{ >{second second second second} } } }
he11o fhee
Kak cospafu sarukauue onacfu eupurocfu e ycfoueyk qopry
3a+irauue (closure) anonnmnax nonporpamma, nmemmax ocryn x nepemenntm, nexcnue-
cxax onacrt nnnmocrn xoroptx nxonna n onacrt nnnmocrn nonporpammt n momenr ee
xomnnnxnnn. Taxax nonporpamma coxpanxer nyxnte nepemennte n cnoe onacrn nnnmo-
crn (a nmenno, ne aer ynnuroxnrt nx npn anromarnuecxo copxe mycopa), axe ecnn ntst-
naercx nosnee. 3amtxannx nosnonxmr nepeanart nonporpamme annte n momenr ee onpe-
enennx rax, urot nanexamnm opasom nnnnnannsnponart nnyrpennne nepemennte.
Paccmorpnm npnmep, xoropt nomoxer nonxrt, o uem ner peut. B cneymmem ]parmenre
xoa cosaercx nonporpamma printem, xoropax nosnpamaer cctnxy na anonnmnym no-
nporpammy. Hpn ntsone nocnenxx nonyuaer ocryn x crpoxe, ncxono naxonnmecx
nnyrpn printem, axe ecnn +ra crpoxa anno ntmna ns onacrn nnnmocrn. Tem camtm
anonnmnax nonporpamma neuaraer xax rexcr, nepeannt e n xauecrne napamerpa, rax n
rexcr, nsnauantno nepeananmncx printem:
sub p1nfem { my $sf1ng1 = sh1ff
efun sub { my $sf1ng2 = sh1ff
p1nf "$sf1ng1 $sf1ng2\n"} }
Tenept n crpoxy $string1 nonporpammt printem sanocnrcx rexcr Hello, a cctnxa na
anonnmnym nonporpammy sannctnaercx n nepemennym $hello:
$he11o = p1nfem{"he11o"}
B pesyntrare npn ntsone anonnmno nonporpammt c nexoe crpoxo n xauecrne apry-
menra ra coxpanxer ocryn raxxe n x ncxono crpoxe n moxer ntnecrn oe:
$he11o{"foday."}
$he11o{"fhee."}
he11o foday.
he11o fhee.
Taxnm cnocoom nonporpamma nnnnnannsnpyercx o nauana paort c ne. Onaxo no-
onte samtxannx nosmoxnt rontxo nx nepemenntx c nexcnuecxo onacrtm nnnmo-
crn. B cneymmem pasene npnnonrcx ononnnrentnax nn]opmannx o samtxannxx.
Cospauue qyukuu ua ocuoee uanouoe
3amtxannx moxno ncnontsonart nx cosannx uao.ouoe qvuru, nosnonxmmnx cosa-
nart n nacrpannart nontsonarentcxne ]ynxnnn n nponecce paort cnenapnx.
Paccmorpnm npnmep. Mt yem ncnontsonart manon nx cosannx rpex nontx ]ynxnn
printHello, printHi n printGreetings. Onn yyr ntnonrt coornercrnenno crpoxn
Crp.162
Hello, hi n Greetings. Haunem c roro, uro sanomnnm +rn crpoxn n maccnne:
0geef1ngs = {"he11o", "h1", "Geef1ngs"}
anee mt ntnonnnm nnxn foreach no +nemenram maccnna c ncnontsonannem nexcnuecxo
nepemenno n xauecrne nnexca (urot ncnontsonart samtxannx, neoxonmt nexcnue-
cxne nepemennte cm. npetymn pasen). Ha xaxo nrepannn nnxna cosaercx ne-
nonmenonannax ]ynxnnx, ncnontsymmax ouepeno +nemenr maccnna greetings, n sa-
nnct rannnt cnmnonon (rnn anntx typeglob) nx +ro ]ynxnnn:
foeach my $fem {0geef1ngs}
{ {{ { "{"p1nf" . $fem} "{"p1nf" . $fem} "{"p1nf" . $fem} "{"p1nf" . $fem} = sub {p1nf "$fem = sub {p1nf "$fem = sub {p1nf "$fem = sub {p1nf "$fem\ \\ \n"} n"} n"} n"} } }} }
Tenept moxno ntstnart cosannte na ocnone manona nontsonarentcxne ]ynxnnn
printHello, printHi n printGreetings:
p1nfhe11o{}
p1nfh1{}
p1nfGeef1ngs{}
he11o
h1
Geef1ngs
Tax n paoramr manont ]ynxnn. Ecnn t mt npocro sanomnnnn cctnxn na anonnmnte
nonporpammt n noxoxmnx cxanxpntx nepemenntx:
foeach my $fem {0geef1ngs}
{ {{ { ${"p1nf" . $fem} = sub {p1nf "$fem ${"p1nf" . $fem} = sub {p1nf "$fem ${"p1nf" . $fem} = sub {p1nf "$fem ${"p1nf" . $fem} = sub {p1nf "$fem\ \\ \n"} } n"} } n"} } n"} }
ro mornn ntstnart +rn nonporpammt uepes pastmenontnannx cctnox, a ne xax ncrnn-
nte nonporpammt:
$p1nfhe11o{}
$p1nfh1{}
$p1nfGeef1ngs{}
he11o
h1
Geef1ngs
Crp.163
Hac1u II
Bc1poeuume pecypcm

Fnana 9
Bc1poeuume nepeueuume

Kopo1ko
Perl paoraer co mnoxecrnom ncrpoenntx nepemenntx. O nexoroptx ns nnx peut yxe mna
nanpnmep, o esycnonnom ]anopnre cpen cnennantntx nepemenntx Perl nepemen-
no $_, ncnontsyemo no ymonuannm mnornmn ]ynxnnxmn:
Wh11e {$ = <>} { p1nf $ }
Hocxontxy $_ ncnontsyercx no ymonuannm n nx urennx noroxa nnoa, n nx ntnoa,
npnneennt ntme xo connaaer c
Wh11e {<>} {p1nf}
Perl coepxnr mnoro ncrpoenntx nepemenntx, n n +ro rnane yer paccxasano o ncex.
Hocuasua. B omo e.aee +uoeo +amepua.a, cneuqu:uoeo o. Unix, nocro.irv +uoeue ecmpoeuuie nepe-
+euuie oi.u eeeoeui o. v:ema uuoueuova.iuix ocooeuuocme u+euuo omo onepauouuo cucme+i.
Hexoropte ncrpoennte nepemennte npenasnauent rontxo nx urennx (raxne cnyuan orona-
pnnamrcx npn onncannn nepemenno cnennantno). Hpn nontrxe npncnoennx raxo nepemen-
no nonoro snauennx Perl ntacr coomenne o omnxe. C pyro cropont, ncnontsonart
nexoropte nepemennte n nonce ne pexomenyercx (a nmenno, xorx onn n ocrynnt nx npo-
rpammncra, opamenne x nnm moxer ntsnart nponemt) na raxne cnyuan raxxe opama-
ercx ocooe nnnmanne.
Hocuasua. Jma e.aea e.emc rpamru+ cnpaeo:uuro+ no ece+ ecmpoeuui+ nepe+euui+ Perl. Kar pesv.i-
mam, ueromopie nepe+euuie cci.ammc ua ceocmea, romopie onucieammc e omo ruuee oa.iue. Bo.ee
moeo, ueromopie nepe+euuie cci.ammc ua ceocmea, romopie e omo ruuee .uoo eoooue ue oocvroammc,
.uoo oo ux cvuecmeoeauuu mo.iro vno+uuaemc e marux c.v:ax sa oono.uume.iuo uuqop+aue npu-
oemc oopamumic r oorv+eumauu, conpoeoroamue Perl. (Jmo osua:aem, :mo npoepa++ucmv e noeceouee-
uo nparmure uem ueooxoou+ocmu ucno.isoeami coomeemcmevmuue ceocmea Perl.)
Hrax, na +rom nneenne sanepmaercx. Hocne snaxomcrna co cnpanouno nn]opmanne ns
nyx nocneymmnx pasenon moxno nepexonrt x ocnonno uacrn rnant, onnctnamme
ncrpoennte nepemennte Perl.
Crp.164
Paseepuyfue ureua ecfpoeuuux nepereuuux
Ho ontme uacrn ncrpoennte nepemennte Perl nasnant ouent xparxo n saraouno na-
npnmep, $] nnn $<. Onaxo, xax npannno, nx nnx nmemrcx +xnnnanenrnte pasnepnyrte
nmena, nanomnnammne n mnemonnuecxo (nx nnaemmnx anrnncxnm xstxom) ]opme,
uro nmenno enaer ra nnn nnax nepemennax. Pasnepnyrte nmena ncrpoenntx nepemenntx
moxno ncnontsonart, ecnn nomecrnrt n nauane cnenapnx cnennantnym nparmy:
use Lng11sh
Ona nosnonxer ncnontsonart nx ncrpoenntx nepemenntx, nepeuncnenntx n ran. 9.1,
anrnncxne +xnnnanenrt. Vurnre, uro nexoropte ncrpoennte nepemennte nmemr ne-
cxontxo mnemonnuecxnx +xnnnanenron.
1abnua 9.1. 1abnua 9.1. 1abnua 9.1. 1abnua 9.1. aaaepuy1ue xauaaneu1u uueu ac1poeuuux nepeueuuux
hepeueuuan hepeueuuan hepeueuuan hepeueuuan aaaepuy1an aaaepuy1an aaaepuy1an aaaepuy1an opua opua opua opua
$ $PO51MA1Ch
$- $IOkMA1LlNL5LLI1
$! $O5LkkOk
$LkkNO
$" $Ll515LPAkA1Ok
$# $OIM1
$$ $PkOCL55lD
$PlD
$x $IOkMA1PAGLNuM8Lk
$ $MA1Ch
${ $kLALGkOuPlD
$GlD
$} $LIILC1lvLGkOuPlD
$LGlD
$" $MuL1lPLLMA1ChlNG
$, $Ou1Pu1IlLLD5LPAkA1Ok
$OI5
$. $lNPu1LlNLNuM8Lk
$Nk
$/ $lNPu1kLCOkD5LPAkA1Ok
$k5
$: $IOkMA1LlNL8kLAChAkAC1Lk5
$ $5u85CklP15LPAkA1Ok
$5u85LP
$ $ChlLDLkkOk
$0 $LvALLkkOk
$\ $Ou1Pu1kLCOkD5LPAkA1Ok
$Ok5
$] $PLkLvLk5lON
$ $IOkMA11OPNAML
$A $ACCuMuLA1Ok
$D $DL8uGGlNG
$ $L1LNDLDO5LkkOk
$I $551LMIDMA
$l $lNPLACLLDl1
$L $IOkMA1IOkMILLD
$0 $O5NAML
$P $PLkLD8
$1 $8A5L1lML
$W $WAkNlNG
$ $LLCu1A8LLNAML
$ $AkG
$ $PkLMA1Ch
$| $Ou1Pu1Au1OILu5h
$ $IOkMA1NAML
$+ $LA51PAkLNMA1Ch
$< $kLALu5LklD
$ulD
$= $IOkMA1LlNL5PLkPAGL
$> $LIILC1lvLu5Lk lD
$LulD
$O $PkOGkAMNAML
$AkGv he1 cuuouuua
$nn he1 cuuouuua
xLNv he1 cuuouuua
xlNC he1 cuuouuua
x5lG he1 cuuouuua
0 he1 cuuouuua
0AkGv he1 cuuouuua
0lNC he1 cuuouuua

Hacfpoka ecfpoeuuux nepereuuux ua koukpefuue peckpunfopu qanoe
Mnorne ncrpoennte nepemennte paoramr c rexymnm ecxpnnropom ]ana (onee no-
pono o +rom paccxastnaercx n rnane 12). Onaxo npn nannunn nparmt
use I11ehand1e
n nauane cnenapnx nepemennte moxno nacrpannart na paory c xonxperntm ecx-
pnnropom ]ana. nx +ro nenn moxno npnmennrt +emooi, xoropte ntnonnxmrcx c no-
Crp.165
momtm xoncrpyxnn nna
ue1o ecxpun1op aupaeuue
nnn, ncnontsyx nemnoro pyro cnnraxcnc, xax
ecxpun1op->ue1o{aupaeuue}
Merot npnnoxrcx n ran. 9.2.
1abnua 9.2. 1abnua 9.2. 1abnua 9.2. 1abnua 9.2. e1ou, uac1pauaak|ue ac1poeuuue nepeueuuue ua pabo1y c xouxpe1uuu ecxpun1opou auna
hepeueuuan hepeueuuan hepeueuuan hepeueuuan 8uaoa 8uaoa 8uaoa 8uaoa ue1oa ue1oa ue1oa ue1oa
$- fomaf11nes1eff hANDLL LPk
$x fomafpagenumbe hANDLL LPk
$, oufpuff1e1dsepaafo hANDLL LPk
$. 1npuf11nenumbe hANDLL LPk
$/ 1npufecodsepaafo hANDLL LPk
$: fomaf11nebeakchaacfes hANDLL LPk
$\ oufpufecodsepaafo hANDLL LPk
$ fomaffopname hANDLL LPk
$L fomaffomfeed hANDLL LPk
$| aufof1ush hANDLL LPk
$ fomafname hANDLL LPk
$= fomaf11nepepage hANDLL LPk
Henocpepc1neuume pemeuun
$ - cfpoka, cnepykan sa coenapeuuer
Cnnonnm: POSTMATCH
Hepemennax $ coepxnr ]parmenr saannoro nontsonarenem rexcra, xoropt cneyer sa
]parmenrom, conocrannenntm manony n nponecce nocnene onepannn noncxa nnn sament.
Hpnmep:
$fexf = "ea1ynoW1afe"
$fexf = /noW/
p1nf "Pemafch: p1nf "Pemafch: p1nf "Pemafch: p1nf "Pemafch: \ \\ \"$ "$ "$ "$ \ \\ \" " . " " . " " . " " . " "" "Mafch: Mafch: Mafch: Mafch: \ \\ \"$ "$ "$ "$\ \\ \" " . " " . " " . " " . "Posfmafch: "Posfmafch: "Posfmafch: "Posfmafch: \ \\ \"$ "$ "$ "$` `` `\ \\ \" "" "\ \\ \n" n" n" n"
Pemafch: "ea1y" Mafch: "noW" Posfmafch: "1afe"
3ra nepemennax ocrynna rontxo nx urennx.
$- - ucno cfpok, ocfaeuuxcn ua cfpauuue
Cnnonnm: FORMATLINESLEFT
Hepemennax $- coepxnr uncno crpox, xoropte ocrannct na crpannne rexymero noroxa ntnoa.
$ - fekyan ouuka
Cnnonnmt: OSERROR, ERRNO
Hepemennax $! ntnonr nomep rexyme omnxn n uncnonom xonrexcre n rexcronym crpo-
xy coomenne o omnxe npn paore n crpoxonom xonrexcre. Hpnmep:
Crp.166
use I11e::Copy
# honu1xa cxonupoaa1u uecy|ec1ayk|uu aun
copy {"nonex1sfenf.p1", "neW.p1"}
p1nf $!
No such f11e o d1ecfoy
$ - paspenufenu none raccueoe npu uufepnonupoeauuu
Cnnonnm: LISTSEPARATOR
3ra nepemennax nenrnuna nepemenno $ (ona paccmarpnnaercx anee), sa rem nc-
xnmuennem, uro ornocnrcx ne x maccnnam, ntnonmtm no xomane print, a x maccnnam,
nnrepnonnpyemtm nnyrpt crpox. A nmenno, $" coepxnr cnmnon, ncnontsyemt xax pas-
ennrent none maccnnon, xora Perl nnrepnonnpyer coepxnmoe maccnna n crpoxy, sa-
xnmuennym n nonte xantuxn. Ho ymonuannm ncnontsyercx npoen. Hpnmep:
0aay = {1, 2, 3}
$"=,
$fexf = "0aay"
p1nf $fexf
1,2,3
$ - qopraf eueopa ucen c nnaeake foko
Cnnonnm: OFMT
Hepemennax $# saaer ]opmar no ymonuannm nx ntnoa uncen c nnanamme rouxo c
nomomtm xomant print. Hpnmep:
$p1 = 3.14159265359
$# = x.6g
p1nf "$p1\n"
3.14159
Hpeynpexeuue. Hcno.isoeauue nepe+euuo $# ue pero+euovemc.
$$ - upeufuqukafop npouecca Perl
Cnnonnmt: PROCESSID, PID
Hepemennax $$ coepxnr nenrn]nxarop nponecca nnrepnperaropa Perl, ntnonnxmmero
rexymn cnenapn.
$ - fekyan cfpauuua eueopa
Cnnonnm: FORMATPAGENUMBER
Hepemennax $ coepxnr nomep rexyme crpannnt nx rexymero noroxa ntnoa.
$ - coenapeuue c uanouor noucka
Cnnonnm: MATCH
Crp.167
Hepemennax $ coepxnr ]parmenr rexcra, conocrannennt manony npn nocnene
onepannn noncxa nnn sament. Hpnmep:
$fexf = "ea1ynoW1afe"
$fexf = /noW/
p1nf "Pemafch: p1nf "Pemafch: p1nf "Pemafch: p1nf "Pemafch: \ \\ \"$ "$ "$ "$ \ \\ \" " . " " . " " . " " . "Mafc "Mafc "Mafc "Mafch: h: h: h: \ \\ \"$ "$ "$ "$\ \\ \" " . " " . " " . " " . "Posfmafch: "Posfmafch: "Posfmafch: "Posfmafch: \ \\ \"$ "$ "$ "$\ \\ \" "" "\ \\ \n" n" n" n"
Pemafch: "ea1y" Mafch: "noW" Posfmafch: "1afe"
3ra nepemennax npenasnauena rontxo nx urennx.
$( - peanuuu upeufuqukafop rpynnu nonusoeafene (real GD)
Cnnonnmt: REALGROUPID, GID
Hepemennax $( coepxnr peantnt nenrn]nxarop rpynnt nontsonarene (real GID) nx re-
xymero nponecca. 3ra nn]opmannx nonesna rontxo n cpee Unix. Ecnn onepannonnax cncre-
ma noepxnnaer unencrno n necxontxnx rpynnax ononpemenno, nepemennax $( coepxnr
cnncox rpynn, n xoropte nxonr nontsonarent, or nmenn xoroporo sanymen cnenapn.
$)- fekyu upeufuqukafop rpynnu nonusoeafene (eectie GD)
Cnnonnmt: EFFECTIEGROUPID, EGID
Hepemennax $) coepxnr rexymn nenrn]nxarop rpynnt nontsonarene (eIIective GID) nx
rexymero nponecca. 3ra nn]opmannx nonesna rontxo n cpee Unix. Ecnn onepannonnax cnc-
rema noepxnnaer unencrno n necxontxnx rpynnax ononpemenno, nepemennax ( coepxnr
cnncox rpynn, n xoropte nxonr nontsonarent, or nmenn xoroporo sanymen cnenapn.
Hocuasua. B :e+ om.u:ue mervue epvnni om pea.iuo? Hpoepa++a +orem oimi sanvueua .moi+ no.isoea-
me.e+ us opveo epvnni, uo npaea oocmvna oua no.v:aem om epvnni e.aoe.ia qa.a, mo ecmi mervue epvnni.
$ - coenapeuue c uanouor noucka
Cnnonnm: MULTIPLEMATCHING
Hepemennax $ nosnonxer ntnonnxrt noncx n mnorocrpountx crpoxax, ro ecrt crpoxax,
coepxamnx cnmnont nono crpoxn \n. Ecnn nepemennax $ ycranonnena n 1, ro meracnm-
nont manona noncxa n $ conocrannxmrcx nosnnnxm nepe n nocne nnyrpennnx cnmno-
non nono crpoxn, ecnn n 0 (snauenne no ymonuannm) - rontxo ]nsnuecxomy nauany n xon-
ny nccneyemoro rexcra. Hpnmep:
$fexf = "hee \n1s fhe \nfexf."
$fexf = /1s/
p1nf $ # Coanaeuue ue uaueuo
$" = 1 $" = 1 $" = 1 $" = 1
$fexf = /fexf/
p1nf $ # Coanaeuue uaueuo
fexf
Hpeynpexeuue. Paooma c nepe+euuo $ e Perl ue npueemcmevemc. . omo e.u ooi:uo ucno.isv-
mmc +oouquramopi m u s.
$, - paspenufenu none eueopa
Crp.168
Cnnonnmt: OUTPUTFIELDSEPARATOR, OFS
Hepemennax $, coepxnr pasennrent none nx oneparopa print. Cneymmn npnmep
noxastnaer, xax ncnontsonart +ry nepemennym:
$, =
p1nf 1, 2, 3
123
$. - fekyu uorep cfpoku eeopa
Cnnonnmt: INPUTLINENUMBER, NR
Hepemennax $. coepxnr rexymn nomep crpoxn nnoa nx nocnenero orxptroro ecxpnn-
ropa ]ana.
$/ - paspenufenu exopuux sanuce
Cnnonnmt: INPUTRECORDSEPARATOR, RS
Hepemennax $/ coepxnr pasennrent nxontx sannce. Hpn urennn anntx uepes ecxpnn-
rop ]ana coepxnmoe nepemenno $/ ntcrynaer n xauecrne orpannunrenx ouepeno sann-
cn. Ho ymonuannm $/ coepxnr cnmnon nono crpoxn \n. Paccmorpnm cneymmn nnrepec-
nt npnmep. Otuno coepxnmoe ]ana cunrtnaercx nocrpouno. Onaxo ecnn cenart ne-
pemennym $/ neonpeenenno, ro moxno npouecrt nect mnorocrpount ]an sa onn pas:
undef $/ undef $/ undef $/ undef $/
open hANDLL, "f11e.fxf"
$fexf = <hANDLL>
p1nf $fexf
hees fexf fom a f11e.
$: - rapkep pasueku cfpoku
Cnnonnm: FORMATLINEBREACHARACTERS
Hepemennax $: coepxnr rexcr mapxep pasnnxn crpoxn. Hocne ntnoa coepxnmoro
$: Perl nmeer npano pasnnart crpoxy, urot sanonnnrt nonx ]opmara.
$ - paspenufenu uupekcoe
Cnnonnmt: SUBSCRIPTSEPARATOR, SUBSEP
Hepemennax $; nosnonxer +mynnponart mnoromepnte maccnnt c nomomtm x+ma. 3ra ne-
pemennax coepxnr rexcr, ntcrynammn n ponn pasennrenx nnexcon npn nepeaue x+-
my nnexcon, saanntx n crnne mnoromepntx maccnnon. Conoxynnt xnmu, nonyuam-
mncx npn oennennn nnexcon n pasennrene n ennym crpoxy, cnyxnr xnmuom oc-
ryna x +nemenry. Ho ymonuannm +ra nepemennax coepxnr sanxrym. Hnxe npnnoxrcx
na +xnnnanenrntx ntpaxennx:
$hash{x,y,}
$hash{o1n{$, x, y, }}
Crp.169
Bor eme onn npnmep ncnontsonannx x+ma n ponn maccnna sect n xauecrne nnexca
saan xnmu 1,1,1:
$hash{"1$1$1"} = "he11o!"
p1nf $hash{1,1,1}
he11o!
Onaxo nmecro x+ma npenournrentnee paorart c ncrnnntmn mnoromepntmn maccn-
namn (cm. rnany 1) onn tcrpee nx ocryna n +]]exrnnnee n ncnontsonannn namxrn.
$? - cfafyc nocnepue cucferuo onepauuu
Cnnonnm: CHILDERROR
Hepemennax $ coepxnr craryc (nosmoxno, nomep cncremno omnxn), nosnpamennt
nocnennm saxptrtm xananom, ntsonom nnemne xomant n crpoxe, orpannuenno o-
parntmn anocrpo]amn, nnn opamennem x ncrpoenntm cncremntm oneparopam Perl.
$ - ouuka eunonueuun qyukuuu eal
Cnnonnm: EALERROR
Hepemennax $ coepxnr coomenne o omnxe, xoropax nosnnxna (ecnn nosnnxna) n npo-
necce nocnenero opamennx x ]ynxnnn eval. Ecnn omnox ne tno, +ra nepemennax nycra.
$ - asoeu uupekc raccueoe
Cnnonnm: ner.
Hepemennax $ coepxnr uncno, npncnannaemoe nepnomy nnexcy maccnna. Ho ymonua-
nnm ona panna nynm, no nt moxere npncnonrt e pyroe snauenne.
Hpeynpexeuue. Hcno.isoeauue nepe+euuo $[ ue npueemcmevemc e Perl.
$ - paspenufenu euxopuux sanuce
Cnnonnmt: OUTPUTRECORDSEPARATOR, ORS
Hepemennax $\ coepxnr cnmnon nnn crpoxy cnmnonon, xoropte oneparop print ncnontsyer
xax pasennrent ntxontx sannce. Otuno +ro nycrax crpoxa, no ee moxno nepeonpeennrt:
$ $$ $\ \\ \ = = = = " "" "LND LND LND LND OI OI OI OI Ou1Pu1 Ou1Pu1 Ou1Pu1 Ou1Pu1" " " "
p1nf "he11o!"
he11o! LND OI Ou1Pu1
$ - eepcun Perl
Cnnonnm: PERLERSION
Hepemennax $] coepxnr nepcnm nnrepnperaropa Perl, no xoroptm ntnonnxercx cnena-
pn. Hpnmep:
p1nf $]
Crp.170
5. 00502
$ - fekyu qopraf konoufufyna cfpauuuu
Cnnonnm: FORMATTOPNAME
Hepemennax $ coepxnr rexymn ]opmar xononrnryna crpannnt nx rexymero noroxa ntnoa.
$A - uakonufenu koraupu rite
Cnnonnm: ACCUMULATOR
Hepemennax $A npecrannxer coo naxonnrent nx xomant write. Hocne opamennx x
ee ]opmary xomana write ntneer coepxnmoe naxonnrenx.
$D - fekyue qnaru ofnapku
Cnnonnm: DEBUGGING
Hepemennax $D coepxnr snauenne rexymnx ]naron, ynpannxmmnx ornaxo cnenapnx Perl.
$E - uuqoprauun o ouuke, cneuuquuan pnn onepauuouuo cucferu
Cnnonnm: EXTENDEDOSERROR
Hepemennax $ coepxnr ononnnrentnym nn]opmannm o omnxe, nosnpamaemym n
cnyuae paort no ynpannennem xonxperno onepannonno cncremt. B ontmnncrne
onepannonntx cncrem, sa ncxnmuennem MS, OS/2, Win32 n nnrepnperaropa MacPerl nx
xomntmrepon Macintosh, nepemennte $ n $! coepxar onn n ror xe rexcr. Bor npn-
mep, noxastnammn ocoennocrn onepannonno cpet Windows:
use I11e::Copy
# honu1xa cxonupoaa1u uecy|ec1ayk|uu aun
copy {"nonex1sfenf.p1", "neW.p1"}
p1nf $!
p1nf $L p1nf $L p1nf $L p1nf $L
No such f11e o d1ecfoy
1he sysfem cannof f1nd fhe f11e spec1f1ed
$F - rakcuranuuoe konuecfeo peckpunfopoe qanoe
Cnnonnm: SSTEMFDMAX
Hepemennax $F coepxnr maxcnmantnoe xonnuecrno ecxpnnropon ]anon nx onepa-
nnonno cncremt Unix (otuno 2).
$H - qnaru npoeepku cuufakcuca
Cnnonnm: ner.
Hepemennax $H coepxnr rexymn naop npannn nponepox cnnraxcnca Perl, saanntx c
Crp.171
nomomtm xomant use strict n pyrnx nparm.
$ - pacuupeuue qanoe pnn pepakfupoeauun no recfy
Cnnonnm: INPLACEEDIT
Hepemennax $I coepxnr pacmnpenne ]anon, nx xoroptx paspemeno peaxrnponanne
na mecre (cm. onncanne xnmua -i n rnane 1). Ecnn raxo ]an orxptr xax norox nnoa, n
nero moxno ntnonrt annte, no n nenxx esonacnocrn n momenr orxptrnx ]ana Perl
cosaer ero pesepnnym xonnm. urot sanpernrt peaxrnponanne na mecre, nao ce-
nart nepemennym $I neonpeenenno (xomana undef)
$L - cureon nporoua cfpauuuu
Cnnonnm: FORMATFORMFEED
Hepemennax $ coepxnr cnmnon, xoropt Perl ncnontsyer npn ntnoe ]opmara nx
oosnauennx xonna crpannnt. 3nauenne no ymonuannm cnmnon \f.
$M - yqep narnfu ua kpauu cnya
Cnnonnm: ner.
Hepemennax $M pesepnnpyer namxrt, xoropax moxer ncnontsonartcx Perl, xora ero
nnyrpennne pecypct ncuepnant. Boome ronopx, Perl ne opaartnaer cnryannm neoc-
rarxa namxrn. Onaxo ecnn npn xomnnnxnnn nnrepnperaropa tn saan xnmu
DPE_EMEGEM_SB, ro moxno sapesepnnponart na +ror cnyua ononnn-
rentnt y]ep namxrn (cxaxem, n 1M), ntnonnnn xomany:
$ = | {2 "" 20}
$O - urn onepauuouuo cucferu
Cnnonnm: OSNAME
Hepemennax $O coepxnr nmx onepannonno cncremt, nx xoropo npenasnauen an-
nt nnrepnperarop Perl. B cnyuae onepannonno cncremt Unix, nx xoropo nnrepnpera-
rop otuno conpaercx camocroxrentno, +ra nepemennax nomnmo nmenn onepannonno
cncremt moxer coepxart noxantnoe nmx xomntmrepa (ne nmemmee, xax nerxo nonxrt,
ornomennx x onepannonno cncreme). Bepcnn Perl, cnennantno copannte nx ro nnn
nno onepannonno cncremt, xax npannno, onee naexnt
1
:
p1nf $O
5W1n32

1
Boome ronopx, naexnocrt oecneunnaercx xomnnnxropom, a ne ycnonnxmn copxn. Ecnn nt cunraere cex npo-
]eccnonanom, ro pyunax copxa Perl moxer trt nx nac onee npenournrentntm napnanrom, npn +rom, xpome
ncero npouero, moxno saecrnonart ononnnrentnte nosmoxnocrn. Hpu+e:. peo.

Crp.172
$P - noppepka ofnapku
Cnnonnm: PERLDB
Hepemennax $P coepxnr ]narn, onnctnammne nnyrpennmm xon]nrypannm pexnma
ornaxn. Bor onncanne snauenn nexoroptx nron:
Bit 0 ornaxa nxoa/ntxoa ns nonporpammt;
Bit 1 paspemaer nomaronym ornaxy (crpouxa sa crpouxo);
Bit 2 orxnmuaer onrnmnsannm nx ornaxn;
Bit 3 coxpanxer annte nx nnrepaxrnnno nponepxn nepemenntx;
Bit 4 coxpanxer nomep crpoxn, n xoropo onpeenxercx nonporpamma;
Bit 5 naunnaer ornaxy c nxnmuenntm pexnmom nomaronoro ntnonnennx.
$R - pesynufaf euucneuun yfeeppeuun e fene uanoua
Cnnonnm: ner.
Hepemennax $ coepxnr pesyntrar ycnemnoro conocrannennx rexcra n manona nx
nocnenero ntuncnennoro yrnepxennx (assertion) manona xomant noncxa nnn sament
rexcra (cm. pasent Mnnmte cnmnont n perynxpntx ntpaxennxx n ononnnrentnte
xoncrpyxnnn n perynxpntx ntpaxennxx n rnane ). B cneymmem npnmepe yrnepxenne
(...]) n rene manona ncnontsyercx nx ntnonnennx nexoroporo xoa Perl n cepenne pe-
rynxpnoro ntpaxennx c nocneymmnm ntnoom pesyntrara, nosnpamaemoro +rnm xoom n
sanecennoro n nepemennym $:
$fexf = "fexf"
$fexf = /x{{$va1ab1e1 = 5}}/
p1nf p1nf p1nf p1nf $ $ $ $k kk k
5
$S cocfonuue uufepnpefafopa
Cnnonnm: ner.
Hepemennax $S coepxnr rexymee cocroxnne nnrepnperaropa. Ecnn ona panna ucmuue,
ro nnrepnperarop ntnonnxer xo nnyrpn xomant eval, n npornnnom cnyuae nepemennax
coepxnr .ori.
$T - epern sanycka cueuapun ua eunonueuue
Cnnonnm: BASETIME
Hepemennax $T coepxnr npemx, xora cnenapn tn sanymen na ntnonnenne. Bpemx
nsmepxercx n cexynax, npomemnx c 1 xnnapx 1970 roa (cranaprnoe npemx cncremt
Unix). Hpnmep:
p1nf $1
90917645
$W - peur eueopa npepynpepakux cooeuu
Crp.173
Cnnonnm: WARNING
Hepemennax $ coepxnr rexymee cocroxnne nepexnmuarenx, ynpannxmmero ntnoom
npeynpexammnx coomenn (cm. onncanne xnmua -w n rnane 1). Ecnn npn crapre saan
xnmu -w, ona coepxnr snauenne ucmuua.
p1nf $W # aanycxa1u xax xpe15 -W Wan.p1
1
$X - urn nporparru-uufepnpefafopa
Cnnonnm: EXECUTABLENAME
Hepemennax $X coepxnr nonnt nyrt x nnrepnperaropy Perl, ntnonnxmmemy rexymn
cnenapn:
p1nf $
/us/b1n/pe15
$_ - apryreuf no yronauuk
Cnnonnm: arg
Hepemennax $_ ncnontsyercx mnornmn oneparopamn n ]ynxnnxmn Perl n xauecrne napamerpa,
ecnn onn ntsnant es yxasannx aprymenra. Hanpnmep, nx oneparopa Hnxna while n onepa-
ropa neuarn print $_ ncnontsyercx n xauecrne napamerpa no ymonuannm, no+romy xo Perl
Wh11e {$ = <>} { p1nf $ }
enaer ro xe camoe, uro n xo
Wh11e {<>} { p1nf }
pyrne xomant n ]ynxnnn, xax yxe yxastnanoct n +ro xnnre, raxxe ncnontsymr nepe-
mennym $_ (nanpnmep, oneparopt m/.../ n s/.../.../, ]ynxnnn chop n chomp), ecnn nontso-
narent ne saan pyroro aprymenra.
$ - cfpoka, cnepykan nepep coenapeuuer
Cnnonnm: PREMATCH
Hepemennax $ coepxnr uacrt saannoro nontsonarenem rexcra, xoropt cneyer nepe
]parmenrom, conocrannenntm manony n nponecce nocnene onepannn noncxa nnn sament.
Hpnmep:
$fexf = "ea1ynoW1afe"
$fexf = /noW/
p pp p1nf "Pemafch: 1nf "Pemafch: 1nf "Pemafch: 1nf "Pemafch: \ \\ \"$ "$ "$ "$\ \\ \" " . " " . " " . " " . "Mafch: "Mafch: "Mafch: "Mafch: \ \\ \"$ "$ "$ "$\ \\ \" " . " " . " " . " " . "Posfmafch: "Posfmafch: "Posfmafch: "Posfmafch: \ \\ \"$` "$` "$` "$`\ \\ \" "" "\ \\ \n" n" n" n"
Pemafch: "ea1y" Mafch: "noW" Posfmafch: "1afe"
3ra nepemennax npenasnauena rontxo nx urennx.
$ -- ynpaeneuue yqepor eueopa
Cnnonnm: OUTPUTAUTOFLUSH
Crp.174
Ecnn nepemennax $[ ycranonnena n snauenne ucmuua, Perl nemenenno ntnonr annte n re-
xymn norox ntnoa, ne ncnontsyx npomexyrount y]ep-naxonnrent. B momenr, xora $[
ycranannnnaercx n snauenne ucmuua, Perl ntnonr n norox ntnoa rexymee coepxanne y-
]epa n yer enart ro xe camoe nx xaxo nocneymme xomant write nnn print.
Otuno +ra nepemennax ncnontsyercx npn paore c xanaramn, urot ne saepxnnart ne-
pectnxy anntx. B cneymmem npnmepe nepemennax $[ cnxstnaercx c xonxperntm xana-
nom, a ee snauenne ycranannnnaercx n ucmuuv c nomomtm meroa ns ran. 9.2:
p1pe {kLADLk, Wkl1Lk}
aufof1ush aufof1ush aufof1ush aufof1ush Wkl1Lk Wkl1Lk Wkl1Lk Wkl1Lk 1 1 1 1
To xe camoe moxno cenart c nomomtm xomant
p1pe {kLADLk, Wkl1Lk}
Wkl1Lk Wkl1Lk Wkl1Lk Wkl1Lk- -- -> >> >aufof1ush aufof1ush aufof1ush aufof1ush{1} {1} {1} {1}
$ - urn fekyero qoprafa ofefoe
Cnnonnm: FORMATNAME
Hepemennax $ coepxnr nmx rexymero ]opmara, ncnontsyemoro nx renepannn oruera
nx rexymero noroxa ntnoa.
$ - qparreuf coenapeuun
Cnnonnm: LASTPARENMATCH
Hepemennax $+ coepxnr ]parmenr rexcra, conocrannennoro manony n nponecce no-
cnene onepannn noncxa nnn sament. On coornercrnyer nocnene opaoranno nape
xpyrntx cxoox nnyrpn rena manona. Hpnmep:
$fexf = "here 1s fhe fexf."
$fexf = 1s fhe {\W+}/
p1nf p1nf p1nf p1nf $+ $+ $+ $+
fexf
3ra nepemennax npenasnauena rontxo nx urennx.
$ - peanuuu upeufuqukafop nonusoeafenn (Real User D)
Cnnonnmt: REALUSERID, UID
Hepemennax $< coepxnr peantnt nenrn]nxarop nontsonarenx nx rexymero nponecca.
3ra nn]opmannx otuno nmeer cmtcn rontxo nx onepannonno cncremt Unix. Hpnmep:
p1nf $<
166
$ - fekyu pasrep cfpauuuu
Cnnonnm: FORMATLINESPERPAGE
Hepemennax $ coepxnr pasmep crpannnt n crpoxax rexcra (no ymonuannm 0) nx re-
xymero noxa ntnoa. Hpnmep:
Crp.175
p1nf $=
60
$ - fekyu upeufuqukafop nonusoeafenn (Eectie User D)
Cnnonnmt: EFFECTIEUSERID, EUID
Hepemennax $> coepxnr rexymn nenrn]nxarop nontsonarenx nx rexymero nponecca.
3ra nn]opmannx otuno nmeer cmtcn rontxo nx onepannonno cncremt Unix. Hpnmep:
p1nf $>
166
$0 - urn nporparru
Cnnonnm: PROGRAMNAME
Hepemennax $0 coepxnr nmx cnenapnx, sanymennoro na ntnonnenne. Hpnmep:
p1nf $0
sc1pf.p1
$RGV - urn exopuoro qana
Cnnonnm: ner.
Hepemennax $AGV coepxnr nmx ]ana, ncnontsyemoro nx nnoa uepes STDOUT c
nomomtm xomant <>. Hanpnmep, ecnn cnenapn sanymen c nomomtm xomant
xpe1 ead.p1 f11e.fxf
ro nepemennax $AGV coepxnr nmx, nepeannoe cnenapnm n xomanno crpoxe:
$fexf = <>
p1nf $AkGv
f11e.fxf
$nn - nn- qparreuf coenapeuun
Cnnonnm: ner.
Hymeponannte nepemennte nna $nn nocne ouepeno ycnemno onepannn noncxa nnn same-
nt coepxar conocrannennte manony ]parmenrt rexcra. Hocnenne coornercrnymr rpynnam
xpyrntx cxoox, yxasanntx nnyrpn rena manona n nponymeponanntx no nopxxy cneonannx
c yuerom nnoxennocrn (onee nopono o +rom paccxasano n rnane ). B cneymmem npnmepe
nymeponannte nepemennte ncnontsymrcx nx nsmenennx nopxxa cnon n crpoxe:
$fexf = "yes o no"
$fexf = s/{\W+} {\W+} {\W+}/$3 $2 $1/
p1nf $fe p1nf $fe p1nf $fe p1nf $fexf xf xf xf
no o yes
ENV - nepereuuue okpyeuun
Crp.176
Cnnonnm: ner.
Hepemennax ENV npecrannxer coo x+m n coepxnr snauennx nepemenntx cpet,
saanntx na momenr sanycxa cnenapnx. Knmuamn otuno cnyxar nmena nepemenntx cpe-
t (no, noome ronopx, cocran xnmue sanncnr or onepannonno cncremt). Hanpnmep,
nx onepannonno cncremt Unix moxno oxnart cneymmnx snauenn:
Wh11e {$key, $va1ue} = each{xLNv}
{ p1nf "$key => $va1ue\n"}
5hLLL => /b1n/csh
hOML => /home/usename
...
NC - nopknkaerue qanu
Cnnonnm: ner.
Hepemennax IN npecrannxer coo x+m, n xoropom nx xaxoro ]ana, no-
xnmuennoro c nomomtm xoman do nnn require, nmeercx sannct. Knmuom xnnxercx nmx
]ana, a snauennem nyrt x nemy. (3ror x+m ncnontsyercx n camnm Perl nx nponepxn,
ne noxnmuen nn yxe coornercrnymmn ]an.)
SG - opaofuku cufyauu
Cnnonnm: ner.
Hepemennax SIG npecrannxer coo x+m, n xoropom xpanxrcx opaorunxn pasnnuntx
cnryann, nosnnxammnx n Perl. Hanpnmep, rax moxno orxnmunrt ntno npeynpexammnx
coomenn:
1oca1 $5lG{WAkN} = sub {}
_ - apryreufu, nepepauuue nopnporparre
Cnnonnm: ner.
uopmantnte napamerpt nonporpamm opaartnamrcx c nomomtm maccnna , n xoropt
n momenr ntsona nonporpammt sanocnrcx cnncox aprymenron, nepeanntx nontsonarenem.
B saauy nonporpammt nxonr nsnneut napamerpt ns +roro maccnna, uro n noxasano n cne-
ymmem npnmepe:
sub addem {
$va1ue1 = sh1ff $va1ue1 = sh1ff $va1ue1 = sh1ff $va1ue1 = sh1ff 0 0 0 0
$va1ue2 $va1ue2 $va1ue2 $va1ue2 = = = = sh1ff sh1ff sh1ff sh1ff 0 0 0 0
p1nf "$va1ue1 + $va1ue2 = " . {$va1ue1+$va1ue2} . "\n"
}
addem {2, 2}
2 + 2 = 4
RGV - apryreufu, nepepauuue e koraupuo cfpoke
Cnnonnm: ner.
Crp.177
Maccnn AGV coepxnr cnncox aprymenron, nepeanntx cnenapnm n momenr ero sa-
nycxa. onycrnm, cnenapn sanymen na ntnonnenne xomano
xpe1 sc1pf.p1 a b c d
Ecnn ntnecrn +nemenrt maccnna AGV, nor uro mt ynnnm:
p1nf o1n{", ", 0AkGv}
a, b, c, d
Hocuasua. Oopamume euu+auue, :mo nepei apev+eum uaxooumc e o.e+eume +accue $ARCJ[j. Ho-
omo+v $#ARCJ omo ro.u:ecmeo apev+eumoe oes eouuui.
NC - nyfu noucka nopknkaerux qanoe
Cnnonnm: ner.
Maccnn IN coepxnr cnncox xaranoron, n xoroptx Perl nmer ]ant, noxnmuaemte
xomanamn do, require n use. Hpnmep:
p1nf o1n{",\n", 0lNC}
/us/1oca1/11b/pe15/sun/5.00502,
/us/1oca1/11b/pe15/,
/us/1oca1/11b/pe15/s1fepe1/sun,
/us/1oca1/11b/pe15/s1fepe1,
Crp.178
Fnana 10
Bc1poeuume qyukuuu: opao1ka pauumx

Kopo1ko
Perl coepxnr mnoxecrno ncrpoenntx ]ynxnn. Hexoropte yxe ocyxannct n +ro xnn-
re, nanpnmep ]ynxnnx push, oannxmmax x maccnny annte:
push {0aay, "one"} push {0aay, "one"} push {0aay, "one"} push {0aay, "one"}
push {0aay, "fWo"} push {0aay, "fWo"} push {0aay, "fWo"} push {0aay, "fWo"}
push {0aay, "fhee"} push {0aay, "fhee"} push {0aay, "fhee"} push {0aay, "fhee"}
p1nf 0$aay|1]
one
B +ro rnane paccmarpnnamrcx ncrpoennte ]ynxnnn Perl, npenasnauennte nx paort c
anntmn n nx opaorxn (cma nxoxr ]ynxnnn nx paort co crpoxamn, coprnponxn
anntx, maremarnuecxne ]ynxnnn, ]ynxnnn nx paort c maccnnamn n x+mamn n mnoroe
pyroe). Bcrpoennte ]ynxnnn, ncnontsyemte nx onepann nnoa/ntnoa n nsanmoe-
crnnx mexy nponeccamn, yyr paccmorpent n rnane 11. Bcrpoenntm ]ynxnnxm, npe-
nasnauenntm nx paort c ]anamn, nocnxmena rnana 12. Haxonen, moynn n ]ynxnnn,
nxoxmne n cranaprnym nnnorexy moyne, paccmarpnnamrcx n rnane 13.
Hocuasua. Ec.u ei euu+ame.iuo noc+ompume a.qaeumui cnucor qvuru e oorv+eumauu, npu.a-
eamuec r Perl, mo c+oreme oouapvrumi ueromopie no.esuie qvuruu, ue onucauuie e omo e.aee.
uynxnnn, paccmarpnnaemte n +ro rnane (sa ncxnmuennem ]ynxnn POSIX -Portable Op-
erating System InterIace), xnnxmrcx ncrpoenntmn n Perl, rax uro c nnmn moxno paorart
es ononnnrentntx nnnorex n ]anon. Hocxontxy no xoy nsnoxennx ontmnncrno
npnneenntx sect ]ynxnn yxe neonoxparno ncnontsonanoct, ononnnrentnoro nne-
ennx ne rpeyercx. Hrax, mt nemenenno nepexonm x eranxm.

Henocpepc1neuume pemeuun
abs - aconkfuoe suaeuue
uynxnnx abs nosnpamaer aconmrnoe snauenne aprymenra. Ecnn ne yxasan aprymenr sua-
:euue, ncnontsyercx nepemennax $_:
abs auaueuue
abs
Hpnmep:
p1nf abs -5
5
Crp.179
atan2 - apkfaureuc
uynxnnx atan2 nosnpamaer apxranrenc uacrnoro /X. Bosnpamaemoe snauenne nexnr n
nanasone or - o +:
afan2 ,
B Perl ner ncrpoenno ]ynxnnn, ntuncnxmme ranrenc (xorx raxax ]ynxnnx nxnmuena n
naxer POSIX +ro PSI::tan, cm. anee pasen uynxnnn POSIX).
Hocuasua. Bi eceeoa +oreme pasoe.umi cuuvc ua rocuuvc, :mooi no.v:umi maueeuc.
chop - ypaneuue kouua cfpoku
uynxnnx chomp yanxer cnmnont nono crpoxn (otuno \n), pacnonoxennte n xonne
rexcrono crpoxn. Ona paoraer co crpoxamn nnn cnncxamn. Ecnn ne yxasan aprymenr, nc-
nontsyercx nepemennax $_:
chomp nepeueuuan
chomp cnucox-nepeueuuux
chomp
B xauecrne aprymenron ]ynxnnn onxnt ncnontsonartcx nente snauennx, ro ecrt cnnrax-
cnuecxne xoncrpyxnnn, onycrnmte cnena or oneparopa npncnannannx. uynxnnx nosnpamaer
uncno yanenntx cnmnonon. Ecnn yxasan cnncox nepemenntx, ro xonen crpoxn yanxercx y
xaxo ns nnx, no nosnpamaemoe ]ynxnne snauenne coornercrnyer rontxo nocnene.
Otuno chomp ncnontsyercx nx yanennx cnmnona xonna crpoxn y nneennoro rexcra:
Wh11e {<>} {
chomp chomp chomp chomp
p1nf
}
B xauecrne cnmnona xonna crpoxn ncnontsyercx coepxnmoe cnennantno nepemenno $/.
chop- ypaneuue nocnepuero cureona cfpoku
]ynxnnx chop yanxer nocnenn cnmnon crpoxn nnn cnncxa crpox. B xauecrne snauennx
nosnpamaercx yanennt cnmnon. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
chop nepeueuuan
chop cnucox-nepeueuuux
chop
B xauecrne aprymenron ]ynxnnn onxnt ncnontsonartcx nente snauennx, ro ecrt cnn-
raxcnuecxne xoncrpyxnnn, onycrnmte cnena or oneparopa npncnannannx. Ecnn yxasan
cnncox nepemenntx, ro nocnenn cnmnon yanxercx y xaxo nepemenno, no nosnpa-
maemoe ]ynxnne snauenne coornercrnyer rontxo nocnene. Hpnmep:
Wh11e {<>} {
chop chop chop chop
p1nf
}
Kax npannno, n noontx cnryannxx pexomenyercx ncnontsonart ]ynxnnm chomp, a ne
chop, rax xax ona esonacnee: yanxercx nmenno cnmnon xonna crpoxn, a ne nmo no-
cnenn cnmnon.
Crp.180
chr - npeopasoeauue ucna e cureon
uynxnnx chr nosnpamaer cnmnon, xo ASCII xoroporo saan n xauecrne aprymenra ]ynx-
nnn. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
ch uucno
ch
Hpnmep:
p1nf ch 65

cos - kocuuyc
uynxnnx cos ntuncnxer xocnnyc aprymenra, saannoro n pananax. Ecnn ne yxasan apry-
menr, ncnontsyercx nepemennax $_:
cos aupaeuue
cos
urot ntuncnnrt apxxocnnyc, moxno nocnontsonartcx ]ynxnne PSI::acos ns naxera
POSIX (cm. anee pasen uynxnnn POSIX).
each - napa knk/suaeuue us xsua
uynxnnx each nocneonarentno nepenpaer sanncn n x+me. B xonrexcre cnncxa ]ynxnnx
nosnpamaer napy (r.m:, sua:euue) nx ouepeno sanncn. B cxanxpnom xonrexcre ona nos-
npamaer ouepenoe snauenne xnmua:
each x
Bor npnmep npnmenennx ]ynxnnn each:
$hash{sandW1ch} = g111ed
$hash{d1nk} = of bee
Wh11e {{$key, $va1ue} = each{xhash}} Wh11e {{$key, $va1ue} = each{xhash}} Wh11e {{$key, $va1ue} = each{xhash}} Wh11e {{$key, $va1ue} = each{xhash}}
{ {{ {
p1nf p1nf p1nf p1nf "$key => $va1ue "$key => $va1ue "$key => $va1ue "$key => $va1ue\ \\ \n" n" n" n"
} }} }
d1nk => oof bee
sandW1ch => g111ed
Hpn nepnom opamennn x x+my each sanomnnaer rexymn +nemenr (rounee, enaer nomerxy
no nnyrpennem npecrannennn x+ma) n npn nocneymmnx opamennxx nepexonr x cneym-
memy +nemenry, noxa ne oer o xonna x+ma. Ho ocrnxennn nocnene sanncn nosnpama-
ercx neonpeenennoe snauenne (nnrepnpernpyemoe xax .ori), uro moxer trt ncnontsonano
n ycnonntx oneparopax n oneparopax nnxna: uepeyre ntsont ]ynxnnn each npnmennrent-
no x onomy nnn necxontxnm x+mam n onom nnn necxontxnx mecrax cnenapnx nocxontxy
nomerxa rexyme sanncn cnxsana c x+mem, a ne c nnyrpennnm cocroxnnem ]ynxnnn nnn c
rouxo xoa, n xoropo ona ntstnaercx, ntsont each yyr paorart npannntno.
eal - kornunupoeauue u eunonueuue koraup Perl
uynxnnx eval ncnontsyercx nx xomnnnxnnn n ntnonnennx xoman Perl n nponecce pao-
Crp.181
rt cnenapnx:
eva1 aupaeuue
eva1 {bnox}
eva1
Ecnn n xauecrne aprymenra saano ntpaxenne (xoropoe onxno nmert rnn rexcrono
crpoxn), ro nocne ero ntuncnennx nponsnonrcx cnnraxcnuecxn ananns, xomnnnxnnx n
ntnonnenne xoman Perl, coepxamnxcx n saanno crpoxe. B ornnune or crpoxn nox
xomnnnnpyercx rontxo onn pas a nmenno, n momenr xomnnnxnnn xoa camoro cnena-
pnx. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_.
Hpnmep:
eva1 {p1nf "he11o " p1nf "fhee.\n"}
he11o fhee.
Coomenne o omnxe, ecnn ona nosnnxaer, sannctnaercx n nepemennym $.
eists -- npoeepka knka e xsue
uynxnnx exists nponepxer, cymecrnyer nn n annom x+me yxasannt xnmu:
ex1sfs aupaeuue
Btpaxennem onxno trt nenoe snauenne, coornercrnymmee opamennm x +nemenry
x+ma. Hpnmep:
$hash{fu1f} = app1e
$hash{sandW1ch} = hambuge
$hash{d1nk} = bubb1y
1f {ex1sfs{$hash{"vegefab1e"}}} 1f {ex1sfs{$hash{"vegefab1e"}}} 1f {ex1sfs{$hash{"vegefab1e"}}} 1f {ex1sfs{$hash{"vegefab1e"}}}
{ p1nf "L1emenf ex1sfs." }
e1se
{ p1nf "L1emenf does nof ex1sfs." }
L1emenf does nof ex1sfs
ep - euucneuue skcnoueuuuanuuo qyukuuu
uynxnnx exp ntuncnxer pesyntrar nosneennx ocnonannx narypantntx norapn]mon (ro
ecrt uncna e2.71212...) n saannym crenent. Ecnn ne yxasan aprymenr, ncnontsyercx
nepemennax $_:
exp aupaeuue
exp
Hpnmep:
p1nf exp 1
2.7121245905
he - npeopasoeauue uecfuapuafepuuoro ucna
uynxnnx hex npeopasyer crpoxy, npecrannxmmym coo mecrnanarepnunoe uncno, x
nenomy uncny. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
hex aupaeuue
hex
Crp.182
Hpnmep:
p1nf hex "10"
76
inde - nonoeuue nopcfpoku
uynxnnx index nosnpamaer nepnym naennym nosnnnm, na xoropo n crpoxe pacnono-
xena yxasannax nocrpoxa:
1ndex c1poxa, noc1poxa, noauun
1ndex c1poxa, noc1poxa
Honcx nocrpoxn naunnaercx c saanno nosnnnn. Ecnn coornercrnymmn aprymenr
onymen, noncx neercx c nauana crpoxn. Ecnn nocrpoxa ne naena, ]ynxnnx index nos-
npamaer snauenne -1 (nnn snauenne, na ennnny mentme asonoro nnexca maccnnon, xpa-
nnmoro n nepemenno $, ecnn ono ne nont). Hpnmep:
$fexf = "hee 1s fhe fexf!"
p1nf 1ndex $fexf, "fexf" p1nf 1ndex $fexf, "fexf" p1nf 1ndex $fexf, "fexf" p1nf 1ndex $fexf, "fexf"
12
int - uenan acfu ucna
uynxnnx int ntuncnxer nenym uacrt ntpaxennx, saannoro n xauecrne aprymenra. Ecnn
ne yxasan aprymenr, ncnontsyercx nepemennax $_:
1nf aupaeuue
1nf
Vceuenne o neno uacrn neercx n nanpannennn nynx. uynxnnx ne oxpyrnxer, a ycexaer
snauenne o nenoro, no+romy nx oxpyrnennx nennunn nmecro int nyxno ncnontsonart
ncrpoennte ]ynxnnn sprintf n printf nnn ]ynxnnn PSI::floor n PSI::ceil ns naxera
POSIX. Hpnmep:
p1nf 1nf 1.99999
1
oin - npeopasoeauue cnucka e cfpoky
uynxnnx oin npeopasyer cnncox n rexcronym crpoxy, ncrannxx mexy +nemenramn cnn-
cxa saannt n nepnom aprymenre pasennrent:
o1n aupaeuue, cnucox
uynxnnx nosnpamaer n xauecrne pesyntrara crpoxy. Btpaxenne onxno trt rexcrono
crpoxo nnn npeopasontnartcx n nee. Ha cam cnncox paora ]ynxnnn ne nnnxer.
Hpnmep:
0aay = {1, 2, 3, 4, 5, 6, 7, , 9, 10}
p1nf p1nf p1nf p1nf o1n o1n o1n o1n {"/", 0 {"/", 0 {"/", 0 {"/", 0aay aay aay aay} } } }
1/2/3/4/5/6/7//9/10
keys - cnucok knke xsua
Crp.183
B cnncxonom xonrexcre ]ynxnnx keys nosnpamaer cnncox, cocroxmn ns ncex xnmue
x+ma. B cxanxpnom xonrexcre ]ynxnnx nosnpamaer uncno xonnuecrno xnmue:
keys x
Hpnmep:
$hash{sandW1ch} = sa1am1
$hash{d1nk} = of bee
foeach $key {keys xhash} foeach $key {keys xhash} foeach $key {keys xhash} foeach $key {keys xhash}
p1nf $hash{$key} . p1nf $hash{$key} . p1nf $hash{$key} . p1nf $hash{$key} . " "" "\ \\ \n nn n" " " "
} }} }
oof bee
sa1am1
lc - npeopasoeauue yke k uuuery perucfpy
]ynxnnx lc nosnpamaer crpoxy, nepeannym n xauecrne napamerpa, npeopasonanno x
nnxnemy perncrpy, ro ecrt samenxer n ntxono crpoxe sarnannte yxnt na crpounte.
Ecnn aprymenr onymen, ncnontsyercx nepemennax $_:
1c aupaeuue
1c
Hpnmep:
p1nf 1c hLLLO!
he11o!
cirst - npeopasoeauue nepeo ykeu k uuuery perucfpy
uynxnnx lcfirst nosnpamaer ncxonym crpoxy, n xoropo nepnax yxna (ecnn +ro yxna)
npeopasonana x nnxnemy perncrpy (ro ecrt sarnannax yxna samenxercx na crpounym).
Ecnn y ]ynxnnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
1cf1sf aupaeuue
1cf1sf
Hpnmep:
p1nf 1cf1sf hLLLO!
hLLLO!
length - pnuua cfpoku
uynxnnx length nosnpamaer nnny (n arax) cnoero aprymenra. B xauecrne aprymenra onxno
croxrt ntpaxenne, npeopasyemoe n xoneunom cuere x cxanxpy -rexcrono crpoxe. Ecnn ne
yxasan aprymenr, ]ynxnnx nosnpamaer nnny crpoxn, coepxamecx n nepemenno $_:
1engfh aupaeuue
1engfh
Hpnmep:
$fexf = "hee 1s fhe fexf."
p1nf p1nf p1nf p1nf 1engfh $fexf 1engfh $fexf 1engfh $fexf 1engfh $fexf
17
Crp.184
log - uafypanuuu norapuqr
uynxnnx log ntuncnxer narypantnt norapn]m aprymenra, xoropt onxen trt unc-
nom. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
1og aupaeuue
1og
ap - eunonuufu koraupy pnn kaporo snereufa cnucka
uynxnnx map ntuncnxer ntpaxenne nnn ntnonnxer nox xoman nx xaxoro +nemenra
cnncxa. B xauecrne pesyntrara ona nosnpamaer cnncox nonyuenntx snauenn n cnncxonom
n uncno +nemenron cnncxa n cxanxpnom xonrexcre:
map {bnox}, cnucox
map aupaeuue, cnucox
B cneymmem npnmepe mt ncnontsyem ]ynxnnm uc nx npeopasonannx +nemenron mac-
cnna x nepxnemy perncrpy:
0aay = {a, b, c, d, e, f}
0aay 0aay 0aay 0aay = = = = map{uc, 0aay} map{uc, 0aay} map{uc, 0aay} map{uc, 0aay}
p1nf o1n{", ", 0aay}
A, 8, C, D, L, I
B momenr ntnonnennx noxa xoman nnn ntuncnennx ntpaxennx ouepeno +nemenr
cnncxa sanocnrcx n cnennantnym nepemennym Perl $_. (B uacrnocrn, no+romy rax xopomo
paoramr conmecrno c map ]ynxnnn, ncnontsymmne $_ xax aprymenr no ymonuannm.)
Btuncnennx nponoxrcx n cnncxonom xonrexcre n pesyntrare ono snauenne ns nxo-
noro cnncxa moxer oannxrt x ntxonomy cnncxy nn onoro, onn nnn necxontxo +ne-
menron. Tax, xomana
xhash = map {gefkey{$} => $} 0aay
- +ro npocro coxpamennt (n onee tcrpt) napnanr nnxna
xhash = {}
foeach $ {0aay} { xhash{gefkey{$}} = $ }
Hocuasua. Hocro.irv nepe+euua $ eicmvnaem e po.u cuuouu+a o.e+euma cnucra, +oruo us+eumi
exoouo cnucor c no+ouim o.ora ro+auo. Oouaro, ec.u e po.u cnucra eicmvnaem ue nepe+euua-+accue,
pesv.imami +oevm orasamic uenpeocrasve+i+u.
oct - npeopasoeauue eocurepuuoro ucna
uynxnnx oct ntnonnxer npeopasonanne rexcrono crpoxn, npecrannxmme ns cex noct-
mepnunoe uncno, x nenomy uncny. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
ocf aupaeuue
ocf
Hpnmep:
p1nf ocf "10"

ord - npeopasoeauue cureona e kop
Crp.185
]ynxnnx ord npeopasyer nepnt (mo.iro nepnt!) cnmnon crpoxn, saanno n xauecrne
ee aprymenra, n uncno (ro ecrt n xo cnmnona cornacno rannne ASCII). Ecnn ne yxasan
aprymenr, ncnontsyercx nepemennax $_:
od aupaeuue
od
Hpnmep:
p1nf od A
65
pack - ynakoeka suaeuu
uynxnnx pack nonyuaer na nxoe cnncox snauenn n ynaxontnaer nx n nnapnym crpyx-
rypy (cxanxp), xoropym n nosnpamaer n xauecrne pesyntrara:
pack abnou, cnucox
nx pacnaxonxn ncnontsyercx ]ynxnnx unpack c ro xe crpoxo-manonom, uro yxast-
nanact npn ynaxonxe.
3ect n xauecrne manona ncnontsyercx ue perynxpnoe ntpaxenne, a crpoxa (no-
cneonarentnocrt yxn), xoropax saaer nopxox n rnn snauenn:
sanonnenne nonuntmn nynxmn o cneymme aconmrno nosnnnn,
a rexcronax crpoxa, ononnxemax nonuntmn nynxmn,
A rexcronax crpoxa, ononnxemax npoenamn,
b crpoxa nron, ynopxouennax no nospacrannm crapmnncrna nron (ananornuna pe-
syntrary paort ]ynxnnn vec),
crpoxa nron, ynopxouennax no ytnannm crapmnncrna nron,
c onoaronoe nenoe (cnmnon) co snaxom,
C onoaronoe nenoe (cnmnon) es snaxa,
d uncno c nnanamme rouxo nono rounocrn n ecrecrnennom ]opmare,
f uncno c nnanamme rouxo opnnapno rounocrn n ecrecrnennom ]opmare,
h mecrnanarepnunax crpoxa, n xoropo cnepna nyr mnamne mecrnanarepnunte
nn]pt, saxonponannte uernepxamn nron,
H mecrnanarepnunax crpoxa, n xoropo cnepna nyr crapmne mecrnanarepnunte
nn]pt, saxonponannte uernepxamn nron,
i nenoe snauenne co snaxom (cocronr no xpane mepe ns 32 nr, peantnax nnna on-
peenxercx xomnnnxropom C),
I nenoe snauenne es snaxa (cocronr no xpane mepe ns 32 nr, peantnax nnna on-
peenxercx xomnnnxropom C),
l nnnnoe nenoe co snaxom (cocronr ponno ns 32 nr n moxer ornnuartcx or rnna
anntx long xomnnnxropa C),
nnnnoe nenoe es snaxa (cocronr ponno ns 32 nr n moxer ornnuartcx or rnna
anntx long xomnnnxropa C),
Crp.186
n xoporxoe nenoe, ncnontsyemoe npn cerentx omenax (crapmne nrt nyr n xonne,
coepxnr ponno 1 nr),
N nnnnoe nenoe, ncnontsyemoe npn cerentx onepannxx (crapmne nrt nyr n xon-
ne, coepxnr ponno 32 nr),
p yxasarent na crpoxy, sanepmammymcx nynt-repmnnaropom,
P yxasarent na crpyxrypy (ro ecrt crpoxy ]nxcnponanno nnnt),
q -nrnoe nenoe co snaxom (ocrynno rontxo ecnn cncrema noepxnnaer raxne
uncna n Perl tn copan c coornercrnymmnmn xnmuamn),
-nrnoe nenoe es snaxa (ocrynno rontxo ecnn cncrema noepxnnaer raxne
uncna n Perl tn copan c coornercrnymmnmn xnmuamn),
s xoporxoe nenoe co snaxom (n Perl cocronr ns 1 nr),
S xoporxoe nenoe es snaxa (n Perl cocronr ns 1 nr),
u crpoxa, saxonponannax no anropnrmy uuencode,
v xoporxoe nenoe, ncnontsyemoe na xomntmrepax AX (mnamne nrt nyr n xon-
ne, coepxnr ponno 1 nr),
V nnnnoe nenoe, ncnontsyemoe na xomntmrepax AX (mnamne nrt nyr n xon-
ne, coepxnr ponno 32 nr),
w nenoe, saxonponannoe no cranapry BER (ISO Basic Encoding Rules). Fart
npecrannxmr coo nn]pt uncna, sanncannoro n cncreme cuncnennx c ocnonannem 12.
Konnuecrno ar mnnnmantno neoxonmoe nx npecrannennx annoro uncna. Crap-
mn paspx ner nepntm. V ncex nn]p, xpome ro, xoropax nepectnaercx nocnene, -
nr ycranonnen n ennnny,
x nyneno ar,
nxono ar onn x onomy,
rexcronax crpoxa, sanepmaemax nynt-repmnnaropom n ononnxemax nonuntmn
nynxmn.
3a cnmnonom (xpome cnmnonon a, A, b, , h, H, P, ) moxer cneonart uncno, noxastnammee,
cxontxo pas neoxonmo nonropnrt coornercrnymmn cnenn]nxarop. nx neonpeenennoro
uncna nonropenn nmecro xonxpernoro uncna moxno ncnontsonart cnmnon . Hanpnmep:
p1nf pack {"ccc", , 9, 90}

p1nf pack {"c3", 65, 66, 67}
A8C
p1nf pack {"c"", 6, 69, 70, 71}
DLIG
(Fonee erantnte npnmept moryr trt naent n oxymenrannn Perl.) Vmnoxnrent, cne-
ymmn sa cnmnonamn a, A, , yxastnaer na uncno ar, ornonmoe no coornercrnym-
mee snauenne (ro ecrt +rn cnmnont npnnoxr x ntopxe onoro snauennx ns cnncxa, ne-
cmorpx na conyrcrnymmn ymnoxnrent). Hpn pacnaxonxe A yanxer xnocronte npoent
n nynente art, yanxer nce nocne nepnoro nynenoro ara, a nepectnaer annte
onn x onomy. Ananornuno, cnmnont b n nopoxamr crpoxy ns yxasannoro uncna n-
ron, a h n H crpoxy ns yxasannoro uncna :emeepor nron.
Crp.187
Cnenn]nxaropt p n P coxpanxmrcx xax snauennx yxasarene, n nontsonarent onxen
npocnenrt, urot onn ne cctnannct na npemennte nepemennte n coornercrnymmne o-
nacrn namxrn ne nsmenxnnct o momenra pacnaxonxn. Kpome roro, nonropnrent, cneym-
mn sa cnmnonom P, yxastnaer xonnuecrno ar, orneennoe nx crpyxrypt anntx, a ne
uncno nonropenn cnmnona P n ]opmare. Ecnn snauenne, na xoropoe cctnamrcx yxasare-
nn, xnnxercx neonpeenenntm (undef), nocrannxercx nycro yxasarent.
Hente, ynaxonannte n ]opmarax i, I, l, , s, S ne xnnxmrcx nepenocnmtmn mexy nponecco-
pamn n onepannonntmn cncremamn, nocxontxy coxpanxmr nopxox nron n aron, ecrecr-
nennt nx coornercrnymmero nponeccopa (cm. raxxe anee pasen vec nexrop nentx
snauenn es snaxa). Tax, nponeccopt Intel, Alpha n AX xnnxmrcx ocrpoxoneuntmn
1
,
Motorola, mk/k, PPC, Spare, HP PA, Power, Cray rynoxoneuntmn, a nponeccopt
MIPS paoramr xax ocrpoxoneunte na xomntmrepax ]npmt Digital n xax rynoxoneunte
na xomntmrepax Silicon Graphics. nx nepenocnmoro ]opmara ynaxonxn nao ncnontsonart
cnenn]nxaropt n, N (rynoxoneunt ]opmar) n v, V (ocrpoxoneunt ]opmar). Perl nc-
nontsyer annte c nnanamme rouxo nono rounocrn nx nnyrpennnx ntuncnenn. Ho-
+romy snauenne, cnepna ynaxonannoe, a sarem pacnaxonannoe c ]opmarom f, moxer trt ne-
nenrnuntm camomy cee. Kpome roro, npn ynaxonxe Perl ncnontsyer npecrannenne uncna,
ecrecrnennoe nx anno mamnnt, rax, annte, ynaxonannte na onom xomntmrepe, ne
oxsarentno yyr npounrant npannntno na pyrom xomntmrepe.
pop - useneeuue pauuux us raccuea
uynxnnx pop nosnpamaer nocnenee snauenne n maccnne, yxopaunnax ero na onn +ne-
menr. Ecnn ne yxasan aprymenr, ncnontsyercx maccnn _:
pop uaccua
pop
Paccmorpnm npnmep (oparnre nnnmanne, uro nepnax xomana cosaer maccnn array,
ecnn on eme ne cymecrnyer):
push 0aay, 5
p1nf pop 0 p1nf pop 0 p1nf pop 0 p1nf pop 0a aa a ay ay ay ay
5
4yukuuu POS
Haoparopnx xomntmrepntx cncrem Hannonantnoro nncrnryra cranapron n rexnonorn
(the National Institute oI Standards and Technology Computer Systems Laboratory
NIST/CSL) n copyxecrne c pyrnmn oprannsannxmn cosana cranapr POSIX Portable
Operating System InterIace. POSIX +ro ontmax nnnorexa cranaprnsnponanntx C-
noontx ]ynxnn, noxptnammnx cranaprnte norpenocrn nporpammnponannx, or a-
sontx maremarnuecxnx ntuncnenn o nponnnyro paort c ]anamn.

1
Tepmnnt rynoxoneunt (big-endian) n ocrpoxoneunt (little-endian), ncnontsyemte sect, sanmcrnonant ns Hyre-
mecrnn Iynnnnepa xonarana Cnn]ra n ro nx uacrn, re onnctnaercx otua nnnnnyron ecrt xypnnte xna c rynoro
nnn ocrporo xonna. Tax, nenoe uncno 0x75321 (ecxrnunoe 2271501) sannctnaercx n nne naopa aron 0x12,
0x3, 0x5, 0x7 npn ocrpoxoneunom ynopxounnannn n xax 0x7, 0x5, 0x3, 0x12 npn rynoxoneunom ynopxounna-
nnn. Taxax repmnnonornx (big-endian, little-endian, middle-endian) noxnnnact eme n 70-x rr., xora Perl n n nomnne ne tno,
n xnnxercx nxnaom amepnxancxnx nporpammncron n mexynapont xomntmrepnt xapron. Hpu+e:. nepee. u peo.

Crp.188
Moynt Perl POSIX npeocrannxer ocryn x npaxrnuecxn ncem cranaprntm ]ynxnnxm
POSIX nepcnn 1003.1 ncero oxono 250 ]ynxnn. 3rn ]ynxnnn ne xnnxmrcx ncrpoen-
ntmn noono ocrantntm ]ynxnnxm +ro rnant, onaxo, nocxontxy moynt POSIX oec-
neunnaer nporpammncram ontme nosmoxnocre, uem ncrpoennte ]ynxnnn Perl, onn onn-
cant sect. Moynt POSIX noxnmuaercx c nomomtm xomant use:
use PO5l # obaau1u ack bubnuo1exy PO5l
use PO5l qW/yuxun/ # obaau1u ouy yuxuk
(Bo nropom napnanre ncenoxantuxn qw/.../ (cm. ran. 2.3 n rnane 2) nomoramr cosart
cnncox ns crpox-nmen ]ynxnn, saxnmuenntx n xantuxn. Fonee nopono o xomane use
paccxastnaercx n rnane 13.)
Hanpnmep, nnxe ntuncnxercx ranrenc / c nomomtm ]ynxnnn tan moynx POSIX (+ra
]ynxnnx ne nmeer nonnxa cpen ncrpoenntx ]ynxnn Perl, onaxo nx ntuncnennx
snauennx / mt ncnontsyem ncrpoennym ]ynxnnm Perl atan2): use POSIX;
p1nf P p1nf P p1nf P p1nf PO5l::fan{afan2 {1, 1}} O5l::fan{afan2 {1, 1}} O5l::fan{afan2 {1, 1}} O5l::fan{afan2 {1, 1}}
1
push - poaeneuue pauuux k raccuey
uynxnnx push oannxer nonoe snauenne nnn cnncox snauenn n xonen maccnna n ynenn-
unnaer ero nnny na uncno oannenntx +nemenron:
push uaccua, cnucox
Paccmorpnm npnmep (oparnre nnnmanne, uro xomana push cosaer maccnn array, ecnn
on eme ne cymecrnyer):
push 0aay, 5
p1nf p1nf p1nf p1nf pop pop pop pop 0 00 0aay aay aay aay
5
rand - cnyauoe ucno
uynxnnx rand nosnpamaer ouepenoe cnyuanoe uncno, xoropoe nexnr n nanasone or
nynx o snauennx, saannoro xax napamerp ]ynxnnn rand. Ecnn ne yxasan napamerp,
]ynxnnx ncnontsyer snauenne 1:
and aupaeuue
and
Hpn xaxom opamennn x ]ynxnnn nosnpamaercx nonoe snauenne. nx nnnnnannsannn
nocneonarentnocrn cnyuantx uncen ncnontsyercx ]ynxnnx srand (cm. nnxe onnca-
nne +ro ]ynxnnn). Ecnn ]ynxnnx rand ntsnana es npemecrnymmero ntsona srand, n
nepnt pas ]ynxnnx srand ntstnaercx anromarnuecxn. Hpnmep:
p1nf and, "\n", and, "\n", and, "\n"
0.676220703125
0.55670166015625
0.6707006359375
reerse - nepecfaeufu cnucok e opafuor nopnpke
Crp.189
uynxnnx reverse eper cnncox, saannt xax nxono napamerp, nepecrannxer ero +ne-
menrt n oparnom nopxxe n nosnpamaer n xauecrne pesyntrara nonyunnmncx cnncox:
evese cnucox
Hpnmep:
0aay = {1, 2, 3}
p1nf o1n{", ", evese 0aay} p1nf o1n{", ", evese 0aay} p1nf o1n{", ", evese 0aay} p1nf o1n{", ", evese 0aay}
3, 2, 1
rinde - nonoeuue nopcfpoku
uynxnnx rindex nosnpamaer nocnenmm nosnnnm, na xoropo n crpoxe pacnonoxena yxa-
sannax nontsonarenem nocrpoxa. B ornnune or ]ynxnnn index, ona naunnaer noncx c
xonna crpoxn, a ne c nauana:
1ndex c1poxa, noc1poxa, noauun
1ndex c1poxa, noc1poxa
Honcx nocrpoxn naunnaercx nepeo yxasanno nosnnne n nanpannennn x nauany crpoxn.
Ecnn nosnnnx ne yxasana, noncx neercx c xonna crpoxn. Ecnn nocrpoxa ne naena,
]ynxnnx rindex nosnpamaer snauenne -1 (nnn snauenne, na ennnny mentme asonoro nn-
exca maccnnon, xpannmoro n nepemenno $, ecnn ono ne nont). Hpnmep:
$fexf = "hee 1s fhe fexf!"
p1nf p1nf p1nf p1nf 1ndex 1ndex 1ndex 1ndex $ $$ $fexf fexf fexf fexf, " , " , " , "fexf fexf fexf fexf" " " "
12
scalar - qopcupoeauue ckannpuoro koufekcfa
uynxnnx scalar ]opcnpyer nnrepnperannm ntpaxennx, saannoro n xauecrne ee aprymen-
ra n cxanxpnom xonrexcre:
sca1a aupaeuue
Ananornuno ]ynxnnn, xoropax ]opcnponana t cnncxont xonrexcr, n Perl ner. B cne-
ymmem npnmepe noxasana ]ynxnnx scalar sa paoro (oparnre nnnmanne uro nosnpama-
ercx nocnenn +nemenr cnncxa):
0aay = {1, 2, 3}
p1nf p1nf p1nf p1nf sca1a sca1a sca1a sca1a 0 00 0a aa a ay ay ay ay
3
shit - useneeuue nepeoro snereufa raccuea
uynxnnx shift nsnnexaer nepnt +nemenr maccnna, nosnpamax ero n xauecrne pesyntrara,
cnnraer ocrantnte +nemenrt n ymentmaer nnny maccnna na ennnny:
sh1ff uaccua
sh1ff
Ecnn ne yxasan aprymenr n ]ynxnnx paoraer n nexcnuecxo onacrn nnnmocrn nonpo-
rpammt n ]opmara, ncnontsyercx maccnn _. Ecnn xe nt naxonrect n onacrn nnnmo-
crn ]ana nnn n nexcnuecxo onacrn nnnmocrn xoncrpyxnn eval, BEGIN, END nnn
INIT, ncnontsyercx maccnn AGV.
Crp.190
sin - cuuyc
uynxnnx sin ntuncnxer cnnyc aprymenra, saannoro n pananax. Ecnn ne yxasan apry-
menr, ncnontsyercx nepemennax $_:
s1n aupaeuue
s1n
urot ntuncnnrt apxcnnyc, moxno nocnontsonartcx ]ynxnne PSI::asin ns naxera
POSIX (cm. panee pasen uynxnnn POSIX).
sort - copfupoeka cnucka
uynxnnx sort coprnpyer cnncox n nosnpamaer pesyntrar:
sof nonporpauua cnucox
sof |bnox] cnucox
sof cnucox
Ecnn ne saant nn nonporpamma, nn nox xoman, ro ]ynxnnx coprnpyer cnncox n coorner-
crnnn co cranaprntm nopxxom rexcrontx crpox. Ecnn yxasano nmx nonporpammt, ona
onxna nosnpamart nenoe uncno nno mentmee, nno pannoe, nno ontmee nynx n sann-
cnmocrn or pesyntrara cpannennx nyx nepeanntx e +nemenron cnncxa. Bt moxere raxxe
saart nox xoman n xauecrne ncrpannaemo no mecry nonporpammt. Paccmorpnm necxont-
xo npnmepon:
0aay = {, b, a, x, y, c}
p1nf o1n {", ", sof 0aay} . "\n"
a, b, c, x, y,
p1nf o1n {", ", sof {$a cmp $b} 0aay} . "\n"
a, b, c, x, y,
p1nf o1n {", ", sof {$b cmp $a} 0aay} . "\n"
, y, x, c, b, a
0aay = {1, 5, 6, 7, 3, 2}
p1nf o1n {", ", sof {$a <=> $b} 0aay} . "\n"
1, 2, 3, 5, 6, 7
p1nf o1n {", ", sof {$b <=> $a} 0aay} . "\n"
7, 6, 5, 3, 2, 1
splice - sareua cpesa raccuea
uynxnnx splice yanxer ns maccnna nocneonarentno pacnonoxennte +nemenrt n, ecnn
+ro rpeyercx, oannxer nmecro nnx nonte:
sp11ce uaccua, cue|euue, nuua, cnucox
sp11ce uaccua, cue|euue, nuua
sp11ce uaccua, cue|euue
Hapamerp c+eueuue saaer nnexc nepnoro yanxemoro +nemenra maccnna, napamerp o.u-
ua uncno yanxemtx +nemenron. Ecnn napamerp o.uua ne saan, yanxmrcx nce +nemen-
rt o xonna maccnna. Ecnn ononnnrentno saan cnucor +nemenron, nocne onepannn ya-
nennx +rn +nemenrt oannxmrcx n maccnn, naunnax c nnexca, oosnauennoro napamer-
pom c+eueuue, cnnrax nmemmnecx n maccnne +nemenrt n ynennunnax ero nnny.
Pesyntrar paort ]ynxnnn sanncnr or xonrexcra. B cnncxonom xonrexcre nosnpamaercx cnn-
cox yanenntx +nemenron. B cxanxpnom xonrexcre nosnpamaercx nocnenn yanennt +ne-
Crp.191
menr. Ecnn nn onn +nemenr ne yanen, nosnpamaercx neonpeenennoe snauenne undef.
B cneymmem npnmepe x maccnny oannxercx nont +nemenr "three" nocne roro, xax n
nero sannctnamrcx +nemenrt one n two:
0aay = {"one", "fWo"}
sp11ce sp11ce sp11ce sp11ce {0aay, 2, 0, "fhee"} {0aay, 2, 0, "fhee"} {0aay, 2, 0, "fhee"} {0aay, 2, 0, "fhee"}
p1nf o1n {", ", 0aay}
one, fWo, fhee
split - pasueka cfpoku ua cnucok cfpok
uynxnnx split npenpamaer crpoxy n maccnn crpox, pasnnax ee n nosnnnxx, coornercr-
nymmnx saannomy manony:
sp11f /abnou/, 1exc1, nuuu1
sp11f /abnou/, 1exc1
sp11f /abnou/
sp11f
Ecnn manon (perynxpnoe ntpaxenne) saan, Perl paccmarpnnaer ]parmenrt rexcra, co-
nocrannxemte c manonom, xax pasennrenn none. Camn pasennrenn, xax npannno, nc-
xnmuamrcx ns pesyntrara. Ecnn rexcr ne saan, ncnontsyercx cnennantnax nepemennax
Perl $_. Ecnn ne saan manon, crpoxa $_ pasnnaercx no npoentntm cnmnonam, npn-
uem nauantnte npoentnte cnmnont nrnopnpymrcx. Hpnmep:
0aay = sp11f /|,\s]\s"/, "he11o, my f1ends!"
p1nf o1n {"/ ", 0aay}
he11o/my/f1ends!
Ecnn napamerp .u+um saan n xnnxercx nonoxnrentntm uncnom, ]ynxnnx ocra-
nannnnaercx, xora ocrnrnyro yxasannoe xonnuecrno ]parmenron (n +rom cnyuae nocne-
nxx crpoxa cnncxa coepxnr neopaorannt xnocr). Ecnn napamerp .u+um ne saan
nnn panen nynm, ro +ro coornercrnyer pexnmy no ymonuannm, xora pasnenne neercx o
xonna crpoxn n xonnente nycrte crpoxn ne nxnmuamrcx n cnncox. Ecnn xe napamerp .u-
+um orpnnarentnoe uncno, xonnente nycrte crpoxn nxoxr n pesyntrar.
Ecnn, onaxo, ncnontsyercx npncnoenne cnncxom, Perl yer pasnnart crpoxy na ]parmenrt
o rex nop, noxa ne npentcnr uncno nmemmnxcx n ero pacnopxxennn nepemenntx. Hanpn-
mep, n cneymmem cnyuae nexnno nopasymenaercx, uro napamerp .u+um panen uertpem:
{$1og1n, $passWod, $ema1nde} = sp11f /:/, $fexf
(3ro nosnonxer +xonomnrt npemx ntnonnennx nx npnnoxenn, y xoroptx ono xpnrnuno.)
Otuno pasennrent ne nxnmuaercx n crpoxn, ns xoroptx cocrannen cnncox. Onaxo ecnn
manon coepxnr ]parmenrt, saxnmuennte n xpyrnte cxoxn, ro coornercrnymmne no-
crpoxn oannxmrcx x pasnennm:
p1nf o1n {"|", sp11f{/\s"|,-]\s"/, "1 - 10, 20"}}
1|10|20
p1nf o1n {"|", sp11f{/\s"{|,-]}\s"/,"1 - 10, 20"}}
1|-|10|,|20
Kax npannno, nauantnte nycrte crpoxn nxnmuamrcx n cnncox, a xoneunte ner. Ecnn
nx napamerpa .u+um saano orpnnarentnoe snauenne, xonnente crpoxn raxxe nxoxr n
pesyntrar. Ecnn n xauecrne manona saano snauenne , nponsnonrcx pasnenne no rpa-
nnnam npoentntx cnmnonon, npnuem neymne npoentnte cnmnont nrnopnpymrcx.
Crp.192
Hoxoxn na nero manon / / pasoter nxono rexcr no ncem npoenam, noponn maccy
nycrtx crpox. Banon /\s+/ npaxrnuecxn ananornuen manony , sa rem ncxnmuennem,
uro npn nannunn neymnx npoenon ntxono cnncox yer naunnartcx c nycro crpoxn.
Banont, conocrannxemte nycro crpoxe (//, /\s/ n r. .), raxxe cnennantntm opasom o-
paartnamrcx ]ynxnne split onn pasnnamr nxonym crpoxy na orentnte cnmnont:
p1nf o1n {"-", sp11f{//,"he11o"}}
h-e-1-1-o
B cnncxonom xonrexcre ]ynxnnx split nosnpamaer cnncox crpox, nonyuenntx n pesyntrare
pasnennx. B cxanxpnom xonrexcre ona nosnpamaer uncno pasnenn, a pesyntrar pasne-
nnx sanocnrcx n maccnn _. Moxno npnnynrentno sanonnnrt maccnn _ pesyntrarom
pasnennx raxxe n n cnncxonom xonrexcre, ecnn orpannunrt manon nonpocnrentntmn
snaxamn. Taxoe nexnnoe sanonnenne maccnna _ ne npnnercrnyercx n Perl, rax xax xon-
]nnxryer c nepeaue napamerpon nonporpammam.
B xauecrne manona moxno saanart ntpaxenne, ntuncnxemoe no npemx paort nporpammt:
$va1ab1e1 = "\s"|+"/=-]"
0aay = sp11f /$va1ab1e1/, "2 + 2 = 4"
p1nf o1n {", ", 0aay}
2, 2, 4
Ecnn ntpaxenne onxno xomnnnnponartcx n nponecce ntnonnennx cnenapnx rontxo onn
pas, saare mon]nxarop o (cm. rnany n onncanne oneparopon m/.../ n s/.../.../):
$va1ab1e1 = "\s"|+"/=-]"
0aay = sp11f /$va1ab1e1/o, "2 + 2 = 4"
p1nf o1n {", ", 0aay}
2, 2, 4
Kax n n cnyuae oneparopon m/.../, s/.../.../ n tr/.../.../, nx ]ynxnnn split n xauecrne orpann-
unrenx manona ner neoxonmocrn ncnontsonart xocym uepry.
sprint - qoprafupoeauue cfpoku
uynxnnx sprintf cosaer ntxonym crpoxy, nponsnox nx saannoro cnncxa snauenn npone-
ypy nnrepnonxnnn n coornercrnnn c yxasanntmn nontsonarenem cnenn]nxaropamn ]opmara:
sp1nff opua1, cnucox
B omem cnyuae xaxomy +nemenry cnncxa coornercrnyer cno cnenn]nxarop ]opmara n
crpoxe qop+am. Cymecrnymr cneymmne cnenn]nxaropt:
snax nponenra,
c cnmnon c saanntm xoom,
d nenoe co snaxom n ecxrnuno sanncn,
e uncno c nnanamme rouxo, manrncco n nopxxom,
ro xe camoe, uro e, no nx oosnauennx nopxxa ncnontsyercx , a ne e
f uncno c nnanamme rouxo n ]nxcnponannom ]opmare es yxasannx nopxxa,
g uncno c nnanamme rouxo nno n ]opmare f, nno n ]opmare e (n sa-
nncnmocrn or roro, moxno nn xoppexrno sanncart snauennx uncna n ]opmare f)
G ro xe camoe, uro g, no nx oosnauennx nopxxa ncnontsyercx , a ne e
Crp.193
n sanomnnaer n cneymme nepemenno (+nemenre cnncxa) uncno yxe nt-
neenntx cnmnonon,
o nenoe es snaxa n noctmepnuno sanncn,
p yxasarent (apec snauennx xax mecrnanarepnunoe uncno),
s crpoxa,
u nenoe es snaxa n ecxrnuno sanncn,
x nenoe es snaxa n mecrnanarepnuno sanncn,
X ro xe camoe, uro x, no c sarnanntmn narnncxnmn yxnamn n xauecrne mecr-
nanarepnuntx uncen.
nx conmecrnmocrn c pannnmn nepcnxmn Perl ncnontsyer raxxe:
D ro xe camoe, uro n ld,
F ro xe camoe, uro n lf,
i ro xe camoe, uro n d,
O ro xe camoe, uro n lo,
U ro xe camoe, uro n lu.
Kpome roro, mexy cnmnonom nponenra n yxno, saamme rnn ]opmara, moxno nc-
nontsonart cneymmne ononnnrentnte cnenn]nxaropt:
- ntpannnnart no nenomy xpam nonx,
# oannrt npe]nxc 0 nx noctmepnunoro n npe]nxc 0x nx mecrnanarepnunoro
uncna, ecnn ono ornnuno or nynx,
+ nx nonoxnrentnoro uncna oannrt npe]nxc +,
npooe. nx nonoxnrentnoro uncna oannrt npoen n xauecrne npe]nxca,
0 ncnontsonart nynn, a ne npoent nx ntpannnnannx no npanomy xpam,
h nnrepnpernponart nenoe snauenne xax rnn short nnn unsigned short xstxa C,
l nnrepnpernponart nenoe snauenne xax rnn long nnn unsigned long xstxa C,
V nnrepnpernponart nenoe snauenne xax cranaprnt nenouncnennt rnn Perl (ne
nmeer ananoron n xstxe C),
:uc.o mnnnmantnt pasmep nonx,
.:uc.o nx uncen c nnanamme rouxo saaer uncno snaxon nocne ecxrnuno rouxn;
nx nentx uncen saaer mnnnmantnoe uncno paspxon; nx crpox saaer maxcnmantnym
nnny crpoxn.
(nx onoro asonoro cnenn]nxaropa moxno yxastnart necxontxo ononnnrentntx.)
ecnn nmecro uncna yxasana snesouxa , ro n xauecrne uncna yer ncnontsonano ouepe-
noe snauenne ns nxonoro cnncxa anntx. Ono onxno trt nentm nonoxnrentntm unc-
nom. Hcxnmuennem xnnxercx mnnnmantnt pasmep nonx: nx nero orpnnarentnoe snaue-
nne nnrepnpernpyercx xax ononnnrentnt cnenn]nxarop -, ro ecrt xax ntpannnnanne
no nenomy xpam nonx.
Bor necxontxo npnmepon (oparnre nnnmanne, uro n nepnom npnmepe uncno oxpyrnxercx):
Crp.194
$va1ue = 1234.5679
p1nf sp1nff "=x.4f\n", $va1ue
=1234.5679
p1nf sp1nff "=x.5f\n", $va1ue
=1234.5679
p1nf sp1nff "=x6.6f\n", $va1ue
=1234.56790
p1nf sp1nff "W=x+.4e\n", $va1ue
W=x1.2346ex003
srt - keappafuu kopeuu
uynxnnx sqrt ntuncnxer xnaparnt xopent cnoero aprymenra. Ecnn ne yxasan aprymenr,
ncnontsyercx nepemennax $_:
sqf aupaeuue
sqf
Hpnmep:
p1nf sqf 144
12
srand - uuuuuafop reuepafopa cnyauux ucen
uynxnnx srand nnnnnannsnpyer paory reneparopa cnyuantx uncen. Ecnn yxastnart
e onn n ror xe aprymenr, ]ynxnnx rand yer renepnponart onn n re xe nocneona-
rentnocrn. Ecnn aprymenr ne yxasan, ncnontsyercx snauenne, ocnonannoe na rexymem
npemenn n nenrn]nxarope nponecca:
sand aupaeuue
sand
substr - nopcfpoka fekcfoeo cfpoku
uynxnnx substr nosnpamaer nocrpoxy rexcrono crpoxn, nepeanno e n xauecrne aprymenra:
subsf 1exc1, cue|euue, nuua, aaueua
subsf 1exc1, cue|euue, nuua
subsf 1exc1, cue|euue
Hocrpoxa naunnaercx c cnmnona, nmemmero saannoe c+eueuue or nauana crpoxn Ecnn
c+eueuue orpnnarentno, nocrpoxa naunnaercx c xonna crpoxn n nnraercx or xonna x naua-
ny. Ecnn ne saana o.uua, nosnpamaercx nect rexcr o xonna ncxono crpoxn. Ecnn o.uua
orpnnarentna, yxasannoe xonnuecrno cnmnonon ntunraercx ns nnnt ocranmerocx o xonna
crpoxn rexcra. Ecnn saan napamerp sa+eua, ro n xauecrne mercma onxno trt yxasano
nenoe snauenne (ro ecrt aprymenr nmemmn npano naxonrtcx n neno uacrn oneparopa
npncnannannx), a ]ynxnnx substr ne npocro nepner nocrpoxy, no n samennr n ncxonom rex-
cre ee na nont ]parmenr. Hpnmept:
$fexf = "hee 1s fhe fexf."
p1nf subsf {$fexf, 12} . "\n"
fexf.
p1nf subsf {$fexf, 12, 4} . "\n"
fexf
p1nf subsf {$fexf, 12, 4, "Wod"} . "\n"
Crp.195
p1nf $fexf
fexf
hee 1s fhe Wod.
tie - epern e cekyupax c 1 nueapn 1970 ropa
uynxnnx time nosnpamaer xonnuecrno cexyn, npomemnx c momenra nexoroporo xnmue-
noro cotrnx:
f1me
Fontmnncrno nnrepnperaropon Perl ncnontsymr n xauecrne xnmuenoro momenra
00:00:00 1-ro xnnapx 1970 roa ennoro npemennoro xanenapx (ro ecrt momenr, c xoropo-
ro orcunrtnaer npemx onepannonnax cncrema Unix). Onaxo, nanpnmep, nx MacOs xnm-
uentm momenrom xnnxercx 00:00:00 1-ro xnnapx 190 roa
1
.
uc - npeopasoeauue yke k eepxuery perucfpy
uynxnnx uc nosnpamaer crpoxy, nepeannym e n xauecrne napamerpa, npeopasonanno x
nnxnemy perncrpy, ro ecrt samenxx n ntxono crpoxe crpounte yxnt na sarnannte.
Ecnn aprymenr onymen, ncnontsyercx nepemennax $_:
uc aupaeuue
uc
Hpnmep:
p1nf uc "he11o!"
hLLLO!
ucirst - npeopasoeauue nepeo ykeu k eepxuery perucfpy
uynxnnx ucfirst nosnpamaer ncxonym crpoxy, n xoropo nepnax yxna (ecnn +ro yxna)
npeopasonana x nepxnemy perncrpy (ro ecrt crpounax yxna samenxercx na sarnannym).
Ecnn y ]ynxnnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
ucf1sf aupaeuue
ucf1sf
Hpnmep:
p1nf ucf1sf "he11o!"
he11o!
unpack - ynakoeka suaeuu
uynxnnx unpack nonyuaer nnapnym crpyxrypy, ynaxonannym ]ynxnne pack (cm. nt-
me), a nosnpamaer cnncox pacnaxonanntx snauenn:
unpack abnou, aupaeuue

1
Kcrarn, snauennx, renepnpyemte mktime na mamnne, paoramme no ynpannennem Windows, nenpannntno nnrepnpern-
pymrcx n Unix, n naoopor. Hpn +rom, onaxo, pasnte napnanrt Unix (Solaris, FreeBSD n Linux) amr nnonne nepenocn-
mte pesyntrart. Hpu+e:. peo.

Crp.196
B xauecrne manona ncnontsyercx crpoxa, saananmaxcx npn ntsone ]ynxnnn pack. B xa-
uecrne snauennx nosnpamaercx xonrpontnax cymma. Hanpnmep:
$sf1ng = pack {"ccc", , 9, 90}
p1nf o1n{", ", unpack " ccc ",$sf1ng} p1nf o1n{", ", unpack " ccc ",$sf1ng} p1nf o1n{", ", unpack " ccc ",$sf1ng} p1nf o1n{", ", unpack " ccc ",$sf1ng}
, 9, 90
B cneymmem npnmepe mt pacnaxontnaem mecrnanarepnunoe snauenne, ynaxonannoe
]ynxnne vec (cm. ee onncanne nnxe) n crpoxy, cocroxmym ns nyne n ennnn:
vec {$dafa, 0, 32} = 0x11
$b1fsf1ng = unpack{"8"", $dafa} $b1fsf1ng = unpack{"8"", $dafa} $b1fsf1ng = unpack{"8"", $dafa} $b1fsf1ng = unpack{"8"", $dafa}
p1nf $b1fsf1ng
00000000000000000000000000010001
unshit - poaeneuue nepeoro snereufa e raccue
uynxnnx unshift oannxer n nauano maccnna +nemenr nnn cnncox, cnnrax ocrantnte
+nemenrt n ynennunnax nnny maccnna na uncno oannenntx +nemenron:
unsh1ff uaccua, cnucox
Hpnmep:
0aay = {4, 5, 6}
unsh1ff 0aay, 1, 2, 3 unsh1ff 0aay, 1, 2, 3 unsh1ff 0aay, 1, 2, 3 unsh1ff 0aay, 1, 2, 3
p1nf o1n{", ", 0aay}
1, 2, 3, 4, 5, 6
alues - cnucok suaeuu xsua
B cnncxonom xonrexcre ]ynxnnx values nosnpamaer cnncox, cocroxmn ns ncex snauenn (ne
xnmue!), coepxamnxcx n x+me. B cxanxpnom xonrexcre ona nosnpamaer uncno - xonnuecrno
snauenn:
va1ues x
Hpnmep:
$hash{sandW1ch} = ham and cheese
$hash{d1nk} = d1ef co1a
foeach $va1ue {va1ues xhash} { foeach $va1ue {va1ues xhash} { foeach $va1ue {va1ues xhash} { foeach $va1ue {va1ues xhash} {
p1nf "$va1ue p1nf "$va1ue p1nf "$va1ue p1nf "$va1ue\ \\ \n" } n" } n" } n" }
d1ef co1a ham and cheese
ec - eekfop uenux suaeuu es suaka
uynxnnx vec paccmarpnnaer crpoxy, nepeannym e n xauecrne napamerpa, xax onomep-
nt nront maccnn (nastnaemt eermopo+), cocrannennt ns pacnonoxenntx pyr sa
pyrom nentx es snaxa. B xauecrne pesyntrara nosnpamaercx nronoe none nyxno nn-
nt, naunnammeecx c yxasannoro cmemennx:
vec aupaeuue, cue|euue, nuua-nonn
Hapamerp o.uua-no. yxastnaer uncno nron, sapesepnnponannoe no xaxt +nemenr
nexropa. On onxen trt crenentm noxn or 1 o 32. Ecnn none ntxonr sa pamxn crpo-
xn, ono ononnxercx nynxmn. Hpnmep:
Crp.197
$fexf = "A8CD"
p1nf vec{$fexf,0,}, \n # 1effe A
p1nf vec{$fexf,1,}, \n # 1effe 8
p1nf vec{$fexf,2,}, \n # 1effe C
p1nf vec{$fexf,3,}, \n # 1effe D
p1nf vec{$fexf,4,}, \n # auxo aa rpauuy c1poxu
65
66
67
6
O
Bt raxxe moxere npuceaueami nonte snauennx xoncrpyxnnn vec. B +rom cnyuae, urot
orennrt cnncox napamerpon or onepannn npncnannannx, napamerpt ]ynxnnn vec onx-
nt trt saxnmuent n xpyrnte cxoxn. Hpnmep:
$fexf = "A8CD"
vec vec vec vec{$ {$ {$ {$fexf fexf fexf fexf, 2, } , 2, } , 2, } , 2, } = = = = 65 65 65 65
p1nf "$fexf\n"
A8AD
Hpncnoenne xoncrpyxnnn vec(...) nonoro snauennx moxer ne rontxo mon]nnnponart co-
epxnmoe crpoxn, no n nsmenxrt ee pasmep:
$fexf = "A8CD"
vec{$fexf, 4, } vec{$fexf, 4, } vec{$fexf, 4, } vec{$fexf, 4, } = = = = 69 p1nf "$fexf 69 p1nf "$fexf 69 p1nf "$fexf 69 p1nf "$fexf\ \\ \n" n" n" n"
vec{$fexf, 6, } vec{$fexf, 6, } vec{$fexf, 6, } vec{$fexf, 6, } = = = = od "I" p1nf "$fexf od "I" p1nf "$fexf od "I" p1nf "$fexf od "I" p1nf "$fexf\ \\ \n" n" n" n"
A8CDL
A8CDL I
Hopxox cneonannx nron sanncnr or roro, xax oprannsonana namxrt xomntmrepa. Cne-
ymmn pesyntrar nonyuen nx nnar]opmt Win32 (nponeccop Intel):
$fexf = "A8CD"
fo $1oop1ndex {0..7} {
p1nf vec{$fexf, $1oop1ndex, 4}, "/"
}
1/4/2/4/3/4/4/4
B +rom cnyuae apeca namxrn nospacramr cnpana naneno:
1nd{} 1nd{} 1nd{} 1nd{} 3 33 3 2 22 2 1 11 1 0
1nd{4} 1nd{4} 1nd{4} 1nd{4} 7 77 7 6 66 6 5 55 5 4 44 4 3 33 3 2 22 2 1 11 1 0
cha cha cha cha "D" "C" "8" ""
he he he hex{} x{} x{} x{} 0x44 0x43 0x42 0x41
hex hex hex hex{4} {4} {4} {4} 4 4 4 3 4 2 4 1
b1n b1n b1n b1n 0100 0100 0100 0011 0100 0010 0100 0001
Oparnre nnnmanne, uro ecnn nt paccmarpnnaere nonx, cocroxmne ns -mn nr, ro npn
paore co crpoxamn nopxox oprannsannn namxrn ne cymecrnenen. 3ro nnno ns cneym-
mero pncynxa, re apeca namxrn nospacramr cnpana naneno (xorx, nanpnmep, na yponne
nyxarontx cnon pasnnna yer cymecrnenno):
1nd 1nd 1nd 1nd{} {} {} {} 0 00 0 1 11 1 2 22 2 3 33 3
1nd 1nd 1nd 1nd{4} {4} {4} {4} 0 00 0 1 11 1 2 22 2 3 33 3 4 44 4 5 55 5 6 66 6 7 77 7
cha cha cha cha "" "8" "C" "D"
hex hex hex hex{} {} {} {} 0x41 0x42 0x43 0x44
hex hex hex hex{4} {4} {4} {4} 4 4 4 3 4 2 4 1
b1n b1n b1n b1n 0100 0001 0100 0010 0100 0011 0100 0100
Cneymmn npnmep noxastnaer, xax pacneuarart nonrno (ro ecrt n nne uncna c ocno-
nannem na) mecrnanarepnunoe snauenne:
Crp.198
$dafa = ""
vec{$dafa,0,} = 0x8
p1nf vec {$dafa, 3, 1} p1nf vec {$dafa, 3, 1} p1nf vec {$dafa, 3, 1} p1nf vec {$dafa, 3, 1}
p1nf vec {$dafa, 2, 1} p1nf vec {$dafa, 2, 1} p1nf vec {$dafa, 2, 1} p1nf vec {$dafa, 2, 1}
p1nf vec p1nf vec p1nf vec p1nf vec {$dafa, 1, 1} {$dafa, 1, 1} {$dafa, 1, 1} {$dafa, 1, 1}
p1nf p1nf p1nf p1nf vec vec vec vec {$ {$ {$ {$dafa dafa dafa dafa, 0, 1} , 0, 1} , 0, 1} , 0, 1}
1011
(Oparnre nnnmanne, uro o roro, xax ntnonnxercx npncnoenne xoncrpyxnnn vec(...), n
cneymme crpoxe cnenapnx onxna trt nnnnnannsnponana nepemennax $data. B ornn-
une or cctnox na ]ynxnnm vec ne pacnpocrpanxercx nponecc aemoorue.euu (autoviviIi-
cation), onncannt n rnane .)
nx ro xe nenn (ro ecrt urot npeopasonart nront nexrop n crpoxy ns nyne n en-
nnn) onee npaxrnuno ncnontsonart ]ynxnnm unpack:
$fexf = "A8CD"
0b1fsdecomposed = sp11f{//, unpack{"b"", $fexf}}
$b1fsp1nfed = unpack "8"", $fexf
p1nf 0b1fsdecomposed . "\n"
p1nf "$b1fsp1nfed\n"
10000010010000101100001000100010
01000001010000100100001101000100
B nepnom cnyuae mt pacnaxontnaem nrt, naunnax c camoro mnamero n xonuax camtm
crpamnm. Bo nropom nopxox pacnaxonxn npxmo npornnononoxnt. 3ro aer nonu-
noe uncno n npnntunom nne, no moxer oxasartcx ne camo yono ]opmo, ecnn npe-
nonaraercx ononnnrentnt ananns pesyntrara.
Crp.199
Fnana 11
Bc1poeuume qyukuuu:
nnop/nmnop u uexnpo-
ueccume nsauuopec1nun

Kopo1ko
Bno n ntno n Perl ne orpannunnaercx rontxo paoro c xoncontm (ro ecrt nnoom c
xnannarypt n ntnoom na ncnne). Bt moxere paorart raxxe c ]anamn, pacnonoxen-
ntmn na ncxe, n omennnartcx nn]opmanne c pyrnmn nponeccamn. Bce +rn onepannn
nnoa/ntnoa ntnonnxmrcx c nomomtm ecxpnnropon ]anon (Iile handlers). Hocxontxy x
+ro reme ornocnrcx snaunrentnt oem marepnana, x conpamct pasnrt ero na ne
rnant: n anno rnane mt yem nmert eno c xoncontntm nnoom/ntnoom n nsanmo-
ecrnnem mexy nponeccamn (IPC interprocess communication), a cneymmax yer no-
cnxmena paore c ncxontmn ]anamn.
4oprafu Perl
Perl nxnmuaer nncrpymenr nx cosannx npocrtx orueron n rannn. 3ro qop+ami Perl.
uopmarnponanne ntxonoro rexcra nx orueron c nomomtm ]opmaron nsnauantno tno
ono ns ocnonntx saau Perl (ncnomnnre, uro nasnanne Perl nponcxonr or coxpame-
nnx ]past Practical Extraction and Reporting Language). Hcnontsyx ]opmart, moxno sa-
art cnoco ntnoa nn]opmannn na xoncont, nanpnmep ntponnxrt rexcr no npanomy, ne-
nomy xpam nnn no nenrpy, npoxonrponnponart mnpnny onpeenenntx none n nx nono-
xenne n ntxonom pacneuarxe n r. . (C nomomtm ]opmaron moxno raxxe ntnonrt nn-
]opmannm no nnemnn ]an.)
uopmart Perl xnnxmrcx onontno npnmnrnnntm nncrpymenrom nanpnmep, n nnx ner no-
epxxn crnne (style sheets). Onaxo onn uacro ncnontsymrcx n CGI-nporpammnponannn
(CGI Common Gateway InterIace) nx cosannx ]opmarnponannoro rexcra.
Hoono naxeram n nonporpammam, ]opmart nyxamrcx n oxnnennn (declaration).
urot oxnnrt ]opmar, nao saart ecxpnnrop ]ana, nx xoroporo cosaercx ]opmar,
nepeuncnnrt marnuecxne crpouxn, cocroxmne ns cnmnonon $, , <, [ n r. . (+rn cnmnont
yxastnamr, xaxo nmenno nt xornre nnert crpouxy oruera), sarem yxasart n crpoxe nme-
na ntnonmtx nepemenntx. Oxnnenne ]opmara saxanunnaercx rouxo (.). uopmarnpo-
nannte annte ntnoxrcx ]ynxnne write.
Paccmorpnm npnmep. Onn +nemenr anntx ntpannnnaercx no nenomy xpam, nropo +ne-
menr anntx no npanomy. nnna marnuecxo crpouxn onpeenxer nnny crpoxn nt-
noa. Hpn neuarn oneparopom write nonyuaem ]opmarnponannt ntno:
fomaf 51DOu1 =
0<<<<<<<<<<<0>>>>>>>>>>>
Crp.200
$fexf1, $fexf2
.
$fexf1 = "he11o"
$fexf2 = "fhee!"
W1fe # no yuonuauuk ucnonuaye1cn 51DOu1
he11o fhee!
Crporo ronopx, ]opmar cocronr ns crpox c rexcrom, crpox c manonamn, crpox anntx n
crpox c xommenrapnxmn. Hoponee o +rom paccxastnaercx n pasene uopmart: ]op-
mantnt cnnraxcnc.
Bsanmoecrnne nponeccon raxxe oemnax rema. (Kpome roro, e nocnxmeno ne rax
yx mano xnnr.) B +ro rnane mt naunem nnmt snaxomnrtcx c ne. nx nauana mt ysnaem,
xax ntstnart cncremnte ]ynxnnn c nomomtm xoman exec n syscall. 3arem nayunmcx sa-
nycxart nponecct n nepeanart nm annte, unrart nepeanaemte pyrnmn nponeccamn
annte n sanycxart ouepnne nponecct, nx xoroptx moxno ntnonnxrt nno/ntno an-
ntx. Mt raxxe nocmorpnm, xax ncnontsonart cerente coennennx nx nepectnxn nn-
]opmannn uepes Hnrepner. Mt axe nosnaxomnmcx c rexnonorne OLE cncremt Win-
dows, nosnonxmme omennnartcx nn]opmanne c nponeccamn. Onaxo ntme ronont ne
nptrnemt, ns-sa oemnocrn remt mt nce panno cmoxem pocnrt nnmt nonepxnocrnt
nsrnx na nponemt IPC (ro ecrt nsanmoecrnnx mexy nponeccamn). urot nonyunrt
onee nonnym nn]opmannm, nam npnercx oparnrtcx x oxymenrannn Perl.

Henocpepc1neuume pemeuun
print - neafu cnucka pauuux
uynxnnx print ncnontsyercx nx ntnoa (neuart) n yxasannt ecxpnnrop ]ana cnncxa
anntx. Ecnn ecxpnnrop ]ana onymen, ncnontsyercx STDUT nnn norox ntnoa no
ymonuannm. (urot nasnaunrt norox ntnoa no ymonuannm, ncnontsyercx ]ynxnnx select
ee onncanne moxno narn n coornercrnymmem pasene rnant 12). Ecnn cnncox anntx ony-
men, ncnontsyercx cnennantnax nepemennax Perl $_:
p1nf ecxpun1op-auna cnucox
p1nf cnucox
p1nf
Ha nporxxennn +ro xnnrn mt ne pas ncrpeuannct c ]ynxnne print n yxe onontno ne-
nnoxo c ne snaxomt. uynxnnx print nosnpamaer snauenne ucmuua, ecnn onepannx neuarn
npomna ycnemno. Xorx o cnx nop mt ncnontsonann ee rontxo co cranaprntm noroxom
ntnoa STDUT, ee moxno ncnontsonart n nx neuarn nn]opmannn n pyrne ]ant (xax
mt ynnnm n cneymme rnane). Hpnmep:
$a = "he11o" $b = " fo" $c = " you" $d = " fom" $e = " Pe1!"
p1nf $a, $b, $c, p1nf $a, $b, $c, p1nf $a, $b, $c, p1nf $a, $b, $c, $d, $e $d, $e $d, $e $d, $e
he11o fo you fom Pe1!
print - neafu qoprafupoeauuoro cnucka pauuux
uynxnnx printf ncnontsyercx nx ]opmarnponannoro ntnoa (neuarn) n yxasannt ecx-
pnnrop ]ana cnncxa anntx. Ecnn ecxpnnrop ]ana onymen, ncnontsyercx STDUT
Crp.201
nnn norox ntnoa no ymonuannm:
p1nff ecxpun1op-auna opua1, cnucox
p1nff opua1, cnucox
uynxnnx printf ananornuna print sa rem ncxnmuennem, uro ona ntnonr ]opmarnponannte
annte. annte, nonexamne ]opmarnponannm, yxastnamrcx napamerpom cnucor. Kax
nmenno ]opmarnpymrcx annte, yxastnaer crpoxa qop+am. uopmarnponanne anntx
]ynxnne printf ananornuno ]opmarnponannm anntx uynxnne sprintf, onncanno n coor-
nercrnymmem pasene npetyme rnant. Ho cyrn, ]ynxnnx printf ananornuna xomane
p1nf ecxpun1op-auna sp1nff {opua1,cnucox}
sa ncxnmuennem roro, uro ona ncnontsyer pasennrent ntxontx sannce, xpanxmncx n
cnennantno nepemenno Perl $\. Ho+romy noponee ysnart o rom, xax oprannsonana n
xax paoraer crpoxa ]opmarnponannx qop+am, moxno ns pasena sprintI ]opmarnpo-
nanne crpoxn rnant 10.
Hecxontxo npnmepon ncnontsonannx ]ynxnnn printf:
$va1ue = 1234.5679
p1nff "x.4f\n", $va1ue
1234.5679
p1nff "x.5f\n", $va1ue
1234.5679
p1nff "x6.6f\n", $va1ue
1234.56790
p1nff "x+.4e\n", $va1ue
x1.2346ex003
(Ecnn nama nporpamma uyncrnnrentna x noxantntm nacrpoxam (ro ecrt ecnn onepannonnax
cncrema noepxnnaer noxantnte ycranonxn n n nporpamme ecrt nparma use locale), ro ]op-
marnponanne ecxrnuno rouxn onpeenxercx snauennem LCNUMERIC nacrpoex locale.)
-feuue exopuoro nofoka
nx urennx cranaprnoro nxonoro noroxa anntx STDIN nt moxere ncnontsonart nt-
paxenne <>, xax +ro enaercx n cneymmem cnyuae:
Wh11e Wh11e Wh11e Wh11e {<>} {<>} {<>} {<>}
{ p1nf }
Ho ymonuannm ntpaxenne sanocnr snauenne n cnennantnym nepemennym Perl $_. Ec-
nn nt xornre sanecrn snauenne n pyrym nepemennym, ee nao yxasart n xnnom nne:
Wh11e Wh11e Wh11e Wh11e {$ {$ {$ {$1npuf 1npuf 1npuf 1npuf = <>} = <>} = <>} = <>}
{ p1nf $1npuf }
Btpaxenne xnnxercx xparxo ]opmo nx STDIN. Ecnn nt xornre ncnontsonart
nx nnoa pyro ecxpnnrop ]ana, ero nao saart xax oecrpunmop-qa.a~.
getc - cufafu opuuouu cureon
uynxnnx getc nosnpamaer cneymmn cnmnon, cunrannt ns ecxpnnropa ]ana. Ecnn
ecxpnnrop ]ana ne yxasan, ncnontsyercx STDIN:
gefc ecxpun1op-auna
gefc
Crp.202
Mnornx nporpammncron pasouapontnaer nenosmoxnocrt ncnontsonannx ]ynxnnn getc nx
ney]epnsonannoro nnoa (ro ecrt cnmnon sa cnmnonom, no mepe nnoa nontsonarenem), ecnn
rontxo cncrema cnennantno nx +roro ne nacrpannanact. Hpn otuntx ocroxrentcrnax
]ynxnnx getc oxnaer, noxa nontsonarent ne naxmer xnannmy Bno, n rontxo norom na-
unnaer nepeanart nneennte cnmnont. Onaxo n nexoroptx nepcnxx Unix y]epnsannm
moxno orxnmunrt xax, nanpnmep, n cneymmem xoe, xoropt nonropxer na +xpane n +xo-
pexnme nnonmte nontsonarenem cnmnont, noxa nx uncno ne ocrnrner ecxrn:
sysfem "sffy cbeak </dev/ffy >1"
fo {$1oop1ndex = 0 $1oop1ndex <= 9 $1oop1ndex++} {
$cha $cha $cha $cha = = = = gefc{51DlN} gefc{51DlN} gefc{51DlN} gefc{51DlN}
p1nf $cha
}
(3ror npnmep ncnontsyer ]ynxnnm system nx opamennx x cncreme. Hoponee o +ro
nosmoxnocrn paccxastnaercx anee n pasene IPC: system nernnenne n ntnonnenne
cncremno xomant.)
rite - eueop qoprafupoeauuo sanucu
uynxnnx write neuaraer ]opmarnponannym sannct n ecxpnnrop ]ana c nomomtm ]opmara,
cnxsannoro c saanntm ecxpnnropom. Ecnn nocnenn onymen, ncnontsyercx STDOUT:
W1fe ecxpun1op-auna
W1fe aupaeuue
W1fe
Ecnn nmecro ecxpnnropa ]ana yxasano ntpaxenne, Perl ntuncnxer ero n nponecce paort
nporpammt (ntpaxenne onxno trt rexcrono crpoxo) n nnrepnpernpyer pesyntrar xax
nmx ecxpnnropa ]ana. B cneymmem npnmepe mt npncoennxem ]opmar x ntnonomy no-
roxy STDOUT, a sarem ncnontsyem ]ynxnnm write nx ntnoa or]opmarnponannoro rexcra:
fomaf 51DOu1 =
0<<<<<<<<<<<0>>>>>>>>>>> 0<<<<<<<<<<<0>>>>>>>>>>> 0<<<<<<<<<<<0>>>>>>>>>>> 0<<<<<<<<<<<0>>>>>>>>>>>
$fexf1, $fexf2
.
$fexf1 = "he11o"
$fexf2 = "fhee!"
W1fe
he11o fhee
Hocuasua. Ho+uume, :mo ei oo.rui sarau:ueami ooe.euue qop+ama mo:ro (.).
4oprafu: fekcf, eupoeueuuu no neeory kpak
urot ntponnxrt rexcr no nenomy xpam n ]opmarnpyemom none, ncnontsyre cnmnont <,
cneymmne sa cnmnonom ( naunnaer none, npenasnauennoe nx nocranonxn an-
ntx). Bnpnna nonx onpeenxercx xonnuecrnom cnmnonon < (cam cnmnon raxxe nxnm-
uaercx n mnpnny nonx). Hpnmep:
fomaf 51DOu1 =
0<<<<<<<<<<<<0<<<<<<<<<<<<0<<<<<<<<<<0<<<< 0<<<<<<<<<<<<0<<<<<<<<<<<<0<<<<<<<<<<0<<<< 0<<<<<<<<<<<<0<<<<<<<<<<<<0<<<<<<<<<<0<<<< 0<<<<<<<<<<<<0<<<<<<<<<<<<0<<<<<<<<<<0<<<<
$f1sfname, $1asfname, $lD, $exfens1on
.
$f1sfname = "Cay" $1asfname = "Ganf"
$lD = 1024 $exfens1on = x456
Crp.203
W1fe
Cary Ganf 1024 x456
4oprafu: fekcf, eupoeueuuu no npaeory kpak
urot n ]opmarnpyemom none ntponnxrt rexcr no npanomy xpam, ncnontsyre cnmnont
>, cneymmne sa cnmnonom ( naunnaer none, npenasnauennoe nx nocranonxn an-
ntx). Kax n n npetymem cnyuae, mnpnna nonx onpeenxercx xonnuecrnom cnmnonon >
(xax n pantme, cam cnmnon raxxe nxnmuaercx n mnpnny nonx). Hpnmep:
fomaf 51DOu1 =
0 00 0>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>> >> >
$fexf
.
$fexf = "he11o!"
W1fe
he11o!
4oprafu: ueufpupoeauuu fekcf
Ecnn nt xornre n ]opmarnpyemom none ntponnxrt rexcr no nenrpy, urot saart mnpnny
nonx, nmecro cnmnonon > n < ncnontsyre cnmnon [. Hpnmep:
fomaf 51DOu1 =
0|||||||||||||||||||||||||| 0|||||||||||||||||||||||||| 0|||||||||||||||||||||||||| 0||||||||||||||||||||||||||
$fexf
.
$fexf = "he11o!"
W1fe
he11o!
4oprafu: neafu ucen
urot saart none nx ntnoa uncnonoro snauennx, ncnontsyre cnmnont # n couerannn
c neoxsarentntm cnmnonom rouxn (.). Hanpnmep, nnxe pesepnnpyercx nyxnoe uncno sna-
xon nx ntnoa uncna nn:
$p1 = 3.1415925359
fomaf 51DOu1 =
0. ## 0. ####### 0. ## 0. ####### 0. ## 0. ####### 0. ## 0. #######
$p1, $p1
.
W1fe
3. 14 3. 1415926
Oparnre nnnmanne, uro cnmnon # ne moxer croxrt na nepno nosnnnn n crpoxe: ncrpeuax n
rene ]opmara crpoxy, naunnammymcx c cnmnona #, Perl paccmarpnnaer ee xax xommenrapn.
Hocuasua. Ec.u ei xomume eieecmu :uc.o oes omce:euu paspooe, ucno.isvme cneuquramop no.
. (Hpu omo+ maroe no.e oo.ruo oimi eouucmeeuui+ no.e+, saoauui+ e +aeu:ecro cmpore.)
4oprafu: qoprafupoeauuu ruorocfpouu eueop
Mnorocrpounte ]opmart ne npecrannxmr nponemt n Perl. Hpocro yxaxnre n ]opmare
Crp.204
crontxo marnuecxnx crpox n crpox c nepemenntmn, cxontxo nam nao, ne satn sanep-
mnrt ]opmar rouxo. B cneymmem npnmepe ntnonrcx cpasy necxontxo crpox rexcra:
fomaf 51DOu1 =
0< 0< 0< 0<<<<<<<<<< <<<<<<<<< <<<<<<<<< <<<<<<<<<0< 0< 0< 0<<<<<<<<<<< <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<
$fexf1, $fexf1, $fexf1, $fexf1, $fexf2 $fexf2 $fexf2 $fexf2
0 00 0<<<<<<<<<< <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<0 00 0<<<<<<<<<< <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<< << <
$fexf3 $fexf3 $fexf3 $fexf3, , , , $fexf4 $fexf4 $fexf4 $fexf4
.
$fexf1 = "he11o"
$fexf2 = "fhee!"
$fexf3 = "hoW ae"
$fexf4 = "fh1ngs"
W1fe
he11o fhee
hoW ae fh1ngs
4oprafu: qoprafupoeauuu ruorocfpouu eueop c eupesko uyuoro fekcfa
urot pasnrt nnnnym crpoxy rexcra na necxontxo none, nmecro cnmnona . ncnont-
syre n +rom cnyuae rexcr, coornercrnymmn nonm, ntpesaercx ns nauana crpoxn,
npnnncanno +romy nonm (n pesyntrare coepxnmoe crpoxn menxercx). uncno ntpesae-
mtx cnmnonon coornercrnyer mnpnne nonx. B cneymmem npnmepe mt ntpesaem ns o-
no n ro xe crpoxn cnona spancrnyre, nanncannte na pasntx xstxax:
$: = ""
fomaf 51DOu1 =
Lng11sh: Lng11sh: Lng11sh: Lng11sh: <<<<< <<<<< <<<<< <<<<<
$fexf $fexf $fexf $fexf
Geman: Geman: Geman: Geman: <<<<<<<<<< <<<<<<<<<< <<<<<<<<<< <<<<<<<<<<
$fexf $fexf $fexf $fexf
Iench: Iench: Iench: Iench: <<<<<<<< <<<<<<<< <<<<<<<< <<<<<<<<
$fexf $fexf $fexf $fexf
.
$fexf = "he11o!Gufen 1ag!8onou!"
W1fe
Lng11sh: he11o!
Geman: Gufen 1ag!
Iench: 8onou!
Cnennantnax nepemennax Perl $:, xoropym mt cenann nycro crpoxo, coepxnr cnmno-
nt, npnnynrentno pasenxmmne ]parmenrt (+ro necxontxo cnnxaer pncx omnnrtcx n
nnnax ]parmenron, oennenntx n ony crpoxy, n oneruaer noncx omnox). A nmen-
no, ecnn eme o ocrnxennx nyxno nnnt ncrpernncx onn ns cnmnonon, nxoxmnx n
crpoxy $:, ro ]parmenr optnaercx nemenenno Hanpnmep:
fomaf 51DOu1 =
1esf = {<<} {<<} {<<}
$fexf, $fexf, $fexf
.
$: $: $: $: = == = / / / /
$fexf = "111222333" W1fe
$fexf = "11/22/33" W1fe
$fexf = "1/2/3/4" W1fe
$fexf = "111/222/333" W1fe
1esf: {111} {222} {333}
1esf: {11/} {22/} {33/}
1esf: {1/ } {2/ } {3/ }
Crp.205
1esf: {111} {/ } {222}
B uacrnocrn, n npnneennom uyrt ntme npnmepe mt mornn saart $: "!". Ho ymonua-
nnm nepemennax $: coepxnr na cnmnona - crpoxy \n-.
4oprafu: ueqoprafupoeauuu ruorocfpouu eueop
Ecnn n xauecrne marnuecxo crpoxn ]opmara ncnontsyercx xoncrpyxnnx , ro saannt
rexcr yer ntneen es ncxxoro ]opmarnponannx. B uacrnocrn, cnmnont nono crpoxn, yxa-
sannte n rexcre, npnneyr x romy, uro na ntxoe noxnnrcx necxontxo crpox. Hpnmep:
fomaf 51DOu1 = 0"
$fexf
.
$fexf = "hee\n1s\nfhe\nfexf."
W1fe
hee
1s
fhe
fexf.
4oprafu: eueop konoufufyna (saronoeka cfpauuuu)
Ecnn nmx ]opmara npecrannxer coo nmx ecxpnnropa ]ana, x xoropomy oannen
cy]]nxc _TOP, ro raxo ]opmar nrpaer pont xononrnryna npn ntnoe n coornercrnym-
mn ]an (ro ecrt +ror rexcr ntnonrcx n nauane xaxo crpannnt). B cneymmem npn-
mepe cosaercx xononrnryn nx anntx, ntnonmtx n cranaprnt ntxono norox:
fomaf 51DOu11OP = fomaf 51DOu11OP = fomaf 51DOu11OP = fomaf 51DOu11OP =
Lmp1oyes Lmp1oyes Lmp1oyes Lmp1oyes
I1sf Name Lasf Name lD I1sf Name Lasf Name lD I1sf Name Lasf Name lD I1sf Name Lasf Name lD Lxfens1on Lxfens1on Lxfens1on Lxfens1on
--------------------------------------------- --------------------------------------------- --------------------------------------------- ---------------------------------------------
. .. .
fomaf 51DOu1 =
0<<<<<<<<<<<<<<<0<<<<<<<<<<<<<0<<<<<0<<<<
$f1sfname, $1asfname, $lD, $exfens1on
.
$f1sfname = "Gay" $1asfname = "Ganf"
$lD = 1024 $exfens1on = x456
W1fe
Lmp1oyes
I1sf Name Lasf Name lD Lxfens1on
---------------------------------------------
Gay Ganf 1024 x456
Hocuasua. Ko.oumumv. +orem cooeprami no. oauuix, us+eumuuec npu eieooe raroo cmpauui,
uanpu+ep, uo+ep cmpauui u.u uuoerc, coe+eueuui c onepamopo+ aemonpupaueuu ++.
4oprafu: eueop uuuero konoufufyna
K coxanennm, Perl ne coepxnr cpecrn, nosnonxmmnx anromarnuecxn cosanart nnxnne
xononrnrynt (Iooters). Ecnn nnxnn xononrnryn nmeer nepemennym ntcory (cm. anee
pasen uopmart: ]opmantnt cnnraxcnc), ro +ro ouennntm opasom npenxrcrnyer
nopmantnomy ntnoy na neuart ocnonno uacrn crpannnt, ocoenno ecnn pasmep xonon-
Crp.206
rnryna menxercx n pesyntrare ecrnnx ]opmara. (Hanpnmep, ntuncnenne ntpaxenn,
coornercrnymmnx nonxm ]opmara, moxer nsmenxrt nepemennte, or xoroptx sanncnr xo-
nnuecrno crpox nnxnero xononrnryna.)
Onaxo, ecnn xononrnryn nmeer nocroxnnt pasmep, ero moxno ntnecrn npyunym, nponepnn
nepe ntnonnennem ouepeno xomant write nepemennym Perl $- (FOR-
MATLINESLEFT), noxastnammym, cxontxo nesanonnenntx crpox ocranoct na crpannne.
Moxno uepeonart xomant write n print npn ntnoe anntx n onn n ror xe norox ntno-
a, no n raxom cnyuae npnercx xoppexrnponart nepemennym $- npyunym, urot ona npa-
nnntno orpaxana xonnuecrno nesanonnenntx crpox, ocranmnxcx na crpannne.
4oprafu: cneuuanuuue nepereuuue Perl
B Perl nmeercx necxontxo nepemenntx, cnxsanntx c paoro c ]opmaramn:
$ - rexymn ]opmar,
$ - rexymn ]opmar xononrnryna,
$ - uncno crpox na crpannne,
$[ - ecnn nmeer snauenne ucmuua, ncnontsyercx mrnonennt ntno, mnnyx y]ep ntnoa,
$ - crpoxa, ntnonmax nepe nauanom ouepeno crpannnt, sa ucr.m:euue+ nepno.
B cneymmem npnmepe mt saaem nont ]opmar nx rexymero xanana ntnoa, ncnontsyx
nepemennym $:
fomaf sfandadfomaf -
0|||||||||||||||||||||||||||||||||||||||||||
$fexf
.
$fexf = "he11o!"
0 = sfan 0 = sfan 0 = sfan 0 = sfandadfo dadfo dadfo dadfo maf maf maf maf
W1fe
he11o!
4oprafu: qopranuuu cuufakcuc
uopmar, oxnnxemt c nomomtm xnmuenoro cnona format, sa xoroptm cneyer nmx
(otuno connaammee c nmenem ecxpnnropa ]ana) n snax panencrna, cocronr ns crpox c
rexcrom, manonon (noneepxarene), crpox anntx n crpox c xommenrapnxmn. Hmx
]opmara moxer orcyrcrnonart n raxom cnyuae nopasymenaercx nmx STDUT. Onnca-
nne ]opmara saxanunnaercx crpoxo, n xoropo cronr onnounax rouxa.
Crpoxa xommenrapnx naunnaercx c cnmnona # n nepno nosnnnn. Ona npocro nr-
nopnpyercx Perl n nnxax ne nnnxer na paory ]opmara.
Crpoxa c rexcrom ntnonrcx onn x onomy, npnuem moxno yxastnart nopx cxontxo
yrono crpox c rexcrom, ne nepemexax nx crpoxamn anntx. Crpoxo c rexcrom cunraercx
crpoxa, n xoropo ner cnennantntx cnmnonon nnn , oosnauammnx nonx anntx. Ec-
nn nt xornre ncnontsonart +rn cnmnont ne xax mapxept none anntx, a xax otunte
cnmnont, npnercx ncnontsonart rpmx a nmenno, yxasart none anntx, xoropomy co-
ornercrnyer rexcronax xoncranra c nyxntm cnmnonom:
fomaf 51DOu1 =
1hee ae 0>>>>>> p1eces 0 0###.## do11as pe each
$va1ue, "0", $p1ce
Crp.207
Crpoxa c manonamn (nastnaemax namn nnora marnuecxo crpoxo) coepxnr xpome rex-
cra mapxept n , oosnauammne nocrannxemte (]opmarnpyemte) nonx. 3a mapxepamn
n otuno cneymr cnmnont <, > nnn [, oosnauammne nnny nonx n ntpannnnanne no-
crannxemoro n nero rexcra. Ecnn none npenasnaueno nx uncnonoro snauennx, nmecro +rnx
cnmnonon ncnontsyercx cnmnon (nosnnnx nx nn]pt) n, nosmoxno, ecxrnunax rouxa, yxa-
stnammne Perl, xax nmenno nao ntnecrn uncnonoe snauenne. Ocoym pont nrpaer none
- ono onxno trt enncrnenntm n crpoxe c manonamn; raxoe none yxastnaer Perl, uro
rexcr nocrannxemoro snauennx onxen ntnonrtcx xax ecrt, es ononnnrentnoro ]op-
marnponannx.
Hocne crpoxn c manonamn oxsarentno yxastnaercx crpoxa c anntmn, nepeuncnxmmax
nocrannxemte annte (nepemennte n ntpaxennx) n rom xe camom xonnuecrne n nopxxe, n
xoropom n crpoxe c manonamn nyr noneepxarenn. Ecnn nocrannxemoe snauenne ntxonr
sa pamxn saxnnennoro nonx, ono ycexaercx o nyxno nnnt. Btpaxennx, coornercrnymmne
nonxm anntx, pasenxmrcx sanxrtmn. (3anxrym nocne nocnenero ntpaxennx, ecrecrnenno,
yxastnart ne nao.)
Crpoxa c anntmn opaartnaercx Perl n cnncxonom xonrexcre, rax uro nepemennax-cnncox
(nanpnmep, nmx maccnna) npenpamaercx cpasy n necxontxo nocrannxemtx snauenn. Konn-
uecrno anntx n crpoxe onxno coornercrnonart xonnuecrny none n npemecrnymme e
crpoxe c manonamn (ono moxer trt ontme, uem nyxno, no ne moxer trt mentme).
Crpoxa c anntmn moxer sannmart necxontxo ]nsnuecxnx crpox n +rom cnyuae cnncox
snauenn-anntx saxnmuaercx n xpyrnte cxoxn, n orxptnammax xpyrnax cxoxa +ro
nepnt cnmnon, ornnunt or npoena, xoropt yxasan n nepno crpoxe c anntmn.
uopmantno ronopx, ner neoxonmocrn ntpannnnart annte ornocnrentno mapxepon n ,
xax n npnnonmtx namn panee npnmepax. Onaxo raxoe crpyxrypnponanne oneruaer unrae-
mocrt nporpammt n n cnny +roro ncxuecxn pexomenyercx nx npnmenennx.
Kaxax crpoxa c manonamn, panno xax n crpoxa c rexcrom, coornercrnyer ponno ono
crpoxe ntnoa (ncxnmuennem xnnxercx crpoxa c xoncrpyxnne , a raxxe crpoxn c cnmno-
nom nnn cnmnonamn , o xoroptx paccxastnaercx nnxe). Ecnn nocrannxemoe snauenne
(rexcr) coepxnr cnmnont nono crpoxn, ro nect rexcr nrnopnpyercx, naunnax c cnmnona \n,
ecnn rontxo on ne nocrannxercx nmecro xoncrpyxnnn . Koncrpyxnnx , yxastnaemax n
xauecrne crpoxn c manonamn, npnnonr x romy, uro coornercrnymmn rexcr ntnonrcx
xax ecrt c nepexoom na nonym crpoxy nx xaxoro cnmnona \n, saannoro nnyrpn rexcra.
Mapxep nonx opaartnaercx ocotmn opasom. Ecnn raxomy nonm anntx co-
nocrannxercx neonpeenennoe snauenne, a camo none saaercx c nomomtm cnmnonon #, ono
sanonnxercx npoenamn es ntnoa coomennx o omnxe (n npornnnom cnyuae none ]opma-
rnpyercx otuntm opasom). C pyrnmn rnnamn ntpannnnannx (<, >, n [) conocrannennoe
nonm snauenne cnyxnr ncrounnxom, or xoroporo orpesamrcx ]parmenrt neoxonmo nn-
nt. Hntmn cnonamn, raxomy nonm onxna conocrannxrtcx cxanxpnax nepemennax, coepxa-
max rexcronym crpoxy.
Otuno ona n ra xe nepemennax cnyxnr ncrounnxom nx necxontxnx none, oosnauenntx
mapxepom . Coepxnmoe raxo nepemenno npecrannxer coo naop rexcrontx snauenn
neoxonmo nnnt, coennenntx ncrtx, xoropte yyr nocneonarentno orpesartcx n
nponecce ntnoa.
Hocuasua. Ho.e cmporu c uao.oua+u c +aprepo+ ^ +orem saeepuamic mpe+ mo:ra+u (mercmo+
...). B maro+ c.v:ae, ec.u pas+ep mercma, ocmaeuemc e nepe+euuo, npeeicum pas+ep no., ovovm
eieeoeui mpu mo:ru, norasieamuue, :mo mercm c.uuro+ o.uuui, :mooi v+ecmumic e no.e eieooa.
Crp.208
Ec.u re o.uua mercma coomeemcmevem o.uue no., saeepuamuue mpu mo:ru ue eieoomc. (Tarvm rou-
cmpvrum vooouo ucno.isoeami, roeoa ei ompesaeme om nepe+euuo noc.eouu qpae+eum mercma.)
Mapxep nonx n couerannn c cnmnonom nosnonxer nonyuart nepemennoe uncno crpox nt-
noa npn opaorxe ono crpoxn c manonamn. Ecnn n rene crpoxn nmeercx cnmnon - (same-
nxemt npn ntnoe na npoen), ro crpoxa nrnopnpyercx, ecnn nocne ]opmarnponannx none
ona cocronr ncxnmunrentno ns npoenon. Ecnn n rene crpoxn nmemrcx na raxnx cnmnona
nopx (xoncrpyxnnx ), ro crpoxa yer nonropxrtcx o rex nop, noxa ne ncuepnamrcx co-
nocrannxemte nm annte. (3ro ne snaunr, uro nentsx ncnontsonart nonx c mapxepom n co-
uerannn c cnmnonom . Onaxo n +rom cnyuae conocrannxemte nonxm snauennx onxnt
trt nno ntsonamn ]ynxnn, xoropte pano nnn nosno naunnamr nosnpamart nycrte nnn
neonpeenennte snauennx, nno +nemenramn maccnnon c anronpnpamennem nnexca, nno
nmtmn pyrnmn ntpaxennxmn c noontmn cnocrnamn.) Hpnmep:
fomaf 51DOu1 =
5ubecf: 0<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$subecf
lndex: 0<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<
$1ndex, $desc1pf1on
Ass1gned fo: 0<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<
$pogamme, $desc1pf1on
<<<<<<<<<<<<<<<<<<<<<<<<<
$desc1pf1on
<<<<<<<<<<<<<<<<<<<<<<<<<
$desc1pf1on
<<<<<<<<<<<<<<<<<<<<<<<...
$desc1pf1on
Hmena ]opmaron, n ornnune or cxanxpon, maccnnon, x+me n r. ., ne nmemr cnennantnoro
npe]nxca. uopmart onaamr cocrnenno onacrtm nmen, no+romy nmx ]opmara moxer
connaart c nmenem merxn, ]ynxnnn, ecxpnnropa ]ana nnn naxera. 3ro snaunr, uro paspe-
maercx nmert ]opmar MyName n merxy Myname, npnuem +rn nmena ne yyr nepexptnartcx.
(Hanomnnm, uro Perl xonrexcrno-uyncrnnrentnt xstx nporpammnponannx, ro ecrt cno-
co nnrepnperannn roro nnn nnoro nmenn onpeenxercx xonrexcrom, n xoropom +ro nmx nc-
nontsyercx.) Kax npannno, nmx ]opmara connaaer c nmenem ecxpnnropa ]ana, cnxsannoro
c ]opmarom (pexnm no ymonuannm), no+romy ouent uacro ecxpnnrop ]ana n conyr-
crnymmn emy ]opmar ntrnxxr xax onn oexr. Ha camom ene +ro ne rax, n nontsonarent
moxer oennxrt ]opmart n ecxpnnropt ]anon c pasntmn nmenamn (cm. npnmep ns
npetymero pasena).
uopmart, xax n nonporpammt, nmemr rnoantnym onacrt nnnmocrn n xnnxmrcx crarnue-
cxnmn oexramn. Ho+romy ecnn nt onpeennre ]opmar c nmenem STDUT, ntsonnre ne-
cxontxo pas xomany write, a sarem nepeonpeennre ]opmar STDUT, ro, no-nepntx, nony-
unre or Perl npeynpexammee coomenne Format STDOUT redeIined at ... line ... na +rane
xomnnnxnnn cnenapnx, a no-nroptx, c ynnnennem onapyxnre, uro xomant write ncnont-
symr nocnenn napnanr ]opmara, a ne ror, xoropt tn onpeenen n nauane nporpammt.
(Hpnmep cment ]opmaron n nponecce ntnoa npnneen n npetymem pasene.)
uopmart xnnxmrcx crarnuecxnmn oexramn. 3ro osnauaer, uro, noome ronopx, nentsx re-
nepnponart ]opmar na nery no npemx ntnonnennx nporpammt n ncnontsonart n nem mexa-
nnsm nnrepnonxnnn, xax +ro enaercx n cnyuae manonon xoman m/.../ n s/.../.../. Onaxo +ro
orpannuenne moxno oorn, cxomnnnnponan ]opmar c nomomtm ]ynxnnn eval. Hpnmep:
$co1s = 30:
$fomaf = "fomaf 51DOu1 =\n"
Crp.209
. "" . {"<" x $co1s} . "\n"
. $enfy . "\n"
. {" " x } . "" . {"<" x {$co1s-}} . "\n"
. {" " x } . $enfy . "\n"
. ".\n"
p1nf $fomaf 1f $Debugg1ng
eva1 eva1 eva1 eva1 $ $$ $fomaf fomaf fomaf fomaf
$enfy =<>
W1fe
annt xo nnamnuecxn cosacr ]opmar c nyxntm xonnuecrnom xononox ntnoa, pan-
ntm $cols+1:
fomaf 51DOu1 =
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$enfy
<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$enfy
arn - eueop npepynpepakux cooeuu
o cnx nop na nporxxennn nce rnant mt nmenn eno co cranaprntmn noroxamn an-
ntx STDIN n STDUT. A xax paorart c noroxom anntx STDE, no ymonuannm cnx-
sanntm c xoncontm Onn ns cnocoon ntnoa n norox STDE, npenasnauennt nx
ntnoa coomenn o omnxax, ncnontsonart xomany warn:
Wan cnucox
Hocuasua. pveo cnocoo omo, ecmecmeeuuo, ne:amami uenocpeocmeeuuo e nomor eieooa S1DERR c
no+ouim qvuruu print.
uynxnnx warn ntnonr coomenne n norox STDE, no, n ornnune or ]ynxnnn die (xo-
ropax raxxe ntnonr coomenne n STDE), ona ne npnnonr nn x sanepmennm npo-
rpammt, nn x nosnnxnonennm omnxn. Ecnn ntsnart ee es aprymenron, nt nonyunre
cranaprnoe npeynpexenne:
Wan
Wan1ng: somefh1ng 1s Wong af sc1pf.p1 11ne 1.
Ecnn nt npncnonre snauenne nepemenno $, ro coomenne yer nntm coepxamncx n
nepemenno $ rexcr yer ntneen nmecre c nexoroptm oanountm coomennem n xonne:
$0 = "Ovef1oW eo"
Wan
Ovef1oW eo . . .caughf af sc1pf.p1 2.
Bor npnmep, n xoropom ]ynxnnx warn ncnontsyercx c aprymenramn:
Wan "5omefh1ng", " 1s", " offen", " 1n", " Denmak"
5omefh1ng 1s offen 1n Denmak af scnpf.p1 11ne 1.
Oparnre nnnmanne, uro ecnn saart nycro opaorunx npeynpexammnx cnrnanon
(xax +ro enaercx, paccxastnaercx n pasene IPC: xax nepecnart cnrnan pyromy nponec-
cy anee n +ro rnane), coomennx ntneent ne yyr:
1oca1 $5lG{WAkN} = sub {}
PC: eec - cucferuu eusoe
Crp.210
uynxnnx exec ntnonnxer cncremnt ntson:
exec cnucox
exec nporpauua cnucox
Ecnn n xauecrne aprymenra yxasan cnncox snauenn es nmenn nporpammt, ro cnncox
(otuno +ro rexcronte crpoxn n nepemennte) oennxercx n ennym crpoxy, xoropax n
nepeaercx cncreme nx ntnonnennx, xax ecnn t ona tna napana nontsonarenem n xo-
manno crpoxe. Ecnn n xauecrne nepnoro napamerpa ner nmx nporpammt (nanpnmep,
xomannoro nnrepnperaropa), ro cncrema ntsoner na ntnonnenne coornercrnymmym npo-
rpammy n nepeacr e ocranmnecx aprymenrt n xauecrne napamerpon.
uynxnnx exec ntnonnxer cncremnym xomany, no uuroeoa ne nosnpamaer ynpannenne nasa,
ro ecrt paora cnenapnx npeptnaercx. (Ecnn nt xornre nepnyrt ynpannenne cnenapnm Perl,
ncnontsyre ]ynxnnm system.) uynxnnx nosnpamaer snauenne .ori n nepeaer ynpannenne
cneymme xomane Perl rontxo n onom-enncrnennom cnyuae a nmenno, xora cncrem-
no xomant, xoropym nt xornre ntnonnnrt, ne cymecrnyer (n xora ona ntnonnxercx na-
npxmym, a ne uepes xomannt nnrepnperarop). Hocxontxy ncnontsonanne exec nmecro sys-
tem rnnnunax omnxa, ro npn saannom ]nare -w Perl yer ntnonrt npeynpexammee
coomenne, ecnn ]ynxnnx exec ntstnaercx cama no cee, a cneom sa ne ne cneyer ona ns
]ynxnn warn, exit, die nnn xe xonen noxa. Ecnn nt oecmeume.iuo xornre ncnontsonart
]ynxnnm exec, +ro nyxno enart, nanpnmep, cneymmnm opasom:
exec {"de1 ".bak"} o p1nf 51DLkk "Cou1dnf f1nd DLL"
Cnncox ocrynntx xoman sanncnr or onepannonno cncremt. Cneymmn npnmep yer
paorart n cnyuae Windows n Unix:
exec "echo he11o!"
he11o!
Otuno nt es nponem moxere ntsnart ]ynxnnm exec, yxasan e cncremnym xomany n
conyrcrnymmne aprymenrt. Onaxo oparnre nnnmanne, uro exec opaartnaer cnon ap-
rymenrt onontno cnoxntm opasom n nx pasntx onepannonntx cncrem +ro moxer
nponcxonrt no-pasnomy.
Bor, nanpnmep, xax +ro nponcxonr nx Unix. Ecnn cnncox aprymenron coepxnr necxontxo
+nemenron (nnn ecnn aprymenr +ro maccnn, coepxamn necxontxo +nemenron), ]ynxnnx
exec ntstnaer cncremnym ]ynxnnm execvp c +rnm cnncxom n xauecrne napamerpa. Ecnn
]ynxnnx nonyuaer enncrnennt cxanxpnt aprymenr nnn maccnn, cocroxmn ns onoro
+nemenra, Perl nponepxer aprymenr na nannune meracnmnonon (nanpnmep, cnmnonon, nepena-
npannxmmnx nno/ntno nnn xe oennxmmnx necxontxo xoman n onn xanan). Ecnn onn
naent, ro Perl ntstnaer xomannt nnrepnperarop no ymonuannm. B cnyuae xe orcyrcr-
nnx meracnmnonon Perl pasnnaer crpoxy na xomnonenrt n nepeaer nx execvp.
PC: syste - eefeneuue u eunonueuue cucferuo koraupu
uynxnnx system ntnonnxer re xe ecrnnx, uro n exec, sa onnm manentxnm nc-
xnmuennem. A nmenno, rexymn nponecc cnepna paseeme.emc, nopoxax ouepnn
nponecc, xoropomy n nepeapecyercx ntson xomant onepannonno cncremt, a sarem
]ynxnnx system xer, noxa +ror nponecc sanepmnrcx:
sysfem cnucox
sysfem nporpauua cnucox
Crp.211
Te xe camte npeocrepexennx, xoropte ronopnnnct ornocnrentno nepeaun ]ynxnnn exec cnn-
cxa aprymenron, ocramrcx cnpanennntmn n nx ]ynxnnn system (cm. npetymn pasen).
PC: kak npoufafu pauuue, nepepauuue ppyro nporparro
Hpenonoxnm, uro y nac nmeercx nporpamma c nmenem printem, xoropax neuaraer cnono
Hello!:
p1nf he11o!
Moxem nn mt npounrart, uro naneuarana nama nporpamma, naxoxct n pyro npo-
rpamme a, +ro moxno cenart, ncnontsyx ntno uepes raua. (pipe). Orxptnax ]an, nt
yxastnaere, uro n xauecrne ncrounnxa anntx nao ncnontsonart annte, nanpannxemte
nporpammo printem n cranaprnt norox ntnoa (npn +rom, ecrecrnenno, cncrema cama
onxna sanycrnrt nporpammy printem, urot nonyunrt ncxomte annte):
open open open open{ {{ {IlLLhANDLL IlLLhANDLL IlLLhANDLL IlLLhANDLL, , , , " "" "p1nfem p1nfem p1nfem p1nfem / // /"} "} "} "}
B +rom npnmepe, urot nepenanpannrt norox ntnoa nporpammt printem n norox nnoa
name nporpammt, mt ncnontsonann ]ynxnnm open (ona paccmarpnnaercx n cneymme
rnane). Kanant, nosnonxmmne coennxrt noroxn nnoa n ntnoa nyx pasntx nporpamm,
xnnxmrcx ]ynamenrantntm mexannsmom IPC. Ho-nnnmomy, nanonee yauno oprannso-
nano ncnontsonanne xananon n onepannonno cncreme Unix, xorx onpeenennax nx no-
epxxa nmeercx n n nepcnn Perl nx nnar]opmt Win32.
Cosan xanan, mt npocro unraem annte nporpammt printem ns ecxpnnropa ]ana, cos-
annoro npn ntsone ]ynxnnn open:
open{IlLLhANDLL, "p1nfem |"}
Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} { p1nf p1nf p1nf p1nf } } } }
c1ose {IlLLhANDLL}
he11o!
Oparnre nnnmanne, uro mt moxem ncnontsonart ]ynxnnm open npn cosannn xanana
nno nx urennx anntx ns ]ana, nno nx nx sanncn n ]an (cm. cneymmn pasen),
no ne nx roro n pyroro ononpemenno. urot ncnontsonart oa ecrnnx n ono n ro
xe nporpamme, ncnontsyre ]ynxnnm IPC::Open2 ns moynx Perl IPC.
PC: kak nepecnafu pauuue ppyro nporparre
onycrnm, y nac ecrt nporpamma readem, xoropax unraer norox anntx n neuaraer na +x-
pane nce, uro nonyuaer na nxoe:
Wh11e {<>} { p1nf }
Kax nepecnart +ro nporpamme annte Bt moxere cenart +ro, yxasan npn orxptrnn
]ana nmecro ero nmenn cnmnon [, croxmn nepe nmenem nporpammt:
open {IlLLhANDLL, "| eadem"}
uan, orxptrt c nomomtm ]ynxnnn open noontm opasom, nosnonxer nepectnart
nporpamme annte, sannctnaemte n norox ntnoa (ecxpnnrop ]ana):
open {IlLLhANDLL, "| eadem"}
p1nf IlLLhANDLL "hLLLO!" p1nf IlLLhANDLL "hLLLO!" p1nf IlLLhANDLL "hLLLO!" p1nf IlLLhANDLL "hLLLO!"
c1ose {IlLLhANDLL}
he11o!
Crp.212
Kax yxe tno cxasano n npetymem pasene, nt moxere ncnontsonart ]ynxnnm open, uro-
t cosart xanan nno nx urennx anntx ns ]ana nnn nno nx sanncn anntx n ]an (cm.
npetymn pasen), no ne nx roro n pyroro ononpemenno. urot ncnontsonart oa e-
crnnx n ono n ro xe nporpamme, ncnontsyre ]ynxnnm IPC::Open2 ns moynx Perl IPC.
PC: eueop pauuux e poepuu npouecc
C nomomtm ]ynxnnn open moxno cosart ouepnn nponecc n nonyuart or nero annte,
npocro cunrtnax nx ns nexoroporo ecxpnnropa ]ana. nx +roro nao orxptrt ]an,
yxasan ]ynxnnn open nun -:
1f {open{ChlLDhANDLL, "|-"}} ...
3ra xomana cosaer nont ecxpnnrop ]ana HIDHANDE nx ouepnero nponecca n
ntnonnxer nernnenne rexymero nponecca (ro ecrt nopoxaer ouepnn nponecc). Kax ouep-
nn, rax n ponrentcxn nponecct ncnontsymr onn n ror xe xo Perl, no xomana open
(HIDHANDE, "[-") n ouepnem nponecce yer nosnpamart nont (ro ecrt .ori), rax
uro ycnonnt oneparop iI nomoxer nsexart ecxoneuno nenouxn ouepnnx nponeccon (n
onpeennrt, naxonmcx nn mt n ponrentcxom nponecce nnn n ouepnem).
Ecnn mt naxonmcx n ponrentcxom nponecce, ro moxno nepecnart ouepnemy nponeccy ne-
xoropte annte, a sarem saxptrt ero:
1f {open{ChlLDhANDLL, "|-"}} {
p1nf ChlLDhANDLL, "hee 1s fhe fexf." p1nf ChlLDhANDLL, "hee 1s fhe fexf." p1nf ChlLDhANDLL, "hee 1s fhe fexf." p1nf ChlLDhANDLL, "hee 1s fhe fexf."
c1ose c1ose c1ose c1ose { {{ {ChlLDhANDLL ChlLDhANDLL ChlLDhANDLL ChlLDhANDLL} } } }
} ...
C pyro cropont, ecnn mt naxonmcx n ouepnem nponecce, ro annte, nonyuennte or
ponrentcxoro nponecca, neuaramrcx uepes cranaprnt norox nnoa:
1f {open{ChlLDhANDLL, "|-"}} {
p1nf ChlLDhANDLL, "hee 1s fhe fexf."
c1ose {ChlLDhANDLL}
} e1se {
$1npuf = $1npuf = $1npuf = $1npuf = <> <> <> <>
p1nf $1npuf p1nf $1npuf p1nf $1npuf p1nf $1npuf
ex1f ex1f ex1f ex1f
}
Tenept nocmorpnm na pesyntrar n namem npnmepe ouepnn nponecc ntnonr rexcr,
nonyuennt nm or ponrentcxoro nponecca:
hee 1s fhe fexf.
PC: eueop pauuux e popufenucku npouecc
B npetymem pasene mt n ouepnem nponecce unrann annte, nonyuennte or pon-
rentcxoro nponecca. Touno rax xe c nomomtm ]ynxnnn open nt moxere cosart ouep-
nn nponecc, orxptrt xanan cnxsn mexy ouepnnm n ponrentcxnm nponeccamn n ntno-
nrt annte n ponrentcxn nponecc ns ouepnero. Bce, uro nx +roro nyxno cenart,
npn orxptrnn ]ana saart nmx -[:
1f {open{ChlLDhANDLL, "-|"}} ...
Kora nt nepeaere ]ynxnnn open aprymenr -[, rexymn nponecc pasnernnxercx, nopo-
Crp.213
xax ouepnn nponecc. Oa nponecca ncnontsymr onn n ror xe xo Perl, no xomana
open (HIDHANDE, "-[") n ouepnem nponecce yer nosnpamart nont (ro ecrt
.ori), rax uro ycnonnt oneparop if nomoraer onpeennrt, naxonmcx nn mt n ponrent-
cxom nponecce nnn n ouepnem (n nsexart ecxoneuno nenouxn ouepnnx nponeccon).
Ecnn mt naxonmcx n ponrentcxom nponecce, ro cunrtnaem ns ouepnero nponecca
rexcr, neuaraem ero n saxptnaem ouepnn nponecc:
1f {open{ChlLDhANOLL, "-|"}} {
$1npuf = <ChlLDhANDLL>
p1nf p1nf p1nf p1nf $ $$ $1npuf 1npuf 1npuf 1npuf
c1ose c1ose c1ose c1ose { {{ {ChlLDhANDLL ChlLDhANDLL ChlLDhANDLL ChlLDhANDLL} } } }
} ...
C pyro cropont, xora mt naxonmcx n ouepnem nponecce, ro annte ponrentcxomy
nponeccy nepectnamrcx npocro c nomomtm xomant print, neuaramme n cranaprnt no-
rox ntnoa:
1f {open{ChlLDhANDLL, "-|"}} {
$1npuf = <ChlLDhANDLL>
p1nf $1npuf
c1ose {ChlLDhANDLL}
} e1se { } e1se { } e1se { } e1se {
p1nf "hee 1s fhe fexf." p1nf "hee 1s fhe fexf." p1nf "hee 1s fhe fexf." p1nf "hee 1s fhe fexf."
ex1f ex1f ex1f ex1f
}
B pesyntrare ponrentcxn nponecc ntneer annte, nonyuennte or ouepnero nponecca:
hee 1s fhe fexf.
PC: kak nepecnafu curuan ppyrory npoueccy
B onepannonno cncreme Unix nponecct nsanmoecrnymr pyr c pyrom, noctnax cue-
ua.i. Onpeennrt, xaxne cnrnant noepxnnamrcx xonxperno peannsanne Unix, mox-
no c nomomtm xomant kill -1:
xk111 -1
huP lN1 ul1 lLL 1kAP A8k1 LM1 IPL lLL 8u5 5LGv 55
PlPL ALkM 1LkM ukG 51OP 151P CON1 ChLD 11lN 11Ou lO
CPu I5 v1ALkM PkOI WlNCh LO51 u5k1 u5k2
Perl nosnonxer nepexnartnart +rn cnrnant. nx +roro x cnrnany nao npncoennnrt opa-
orunx. 3ro moxno cenart c nomomtm cnennantno nepemenno Perl -x+ma SIG: nx xa-
xoro cnrnana n +rom x+me ecrt xnmu, a snauennem xnmua cnyxnr nonporpamma, opaart-
nammax cnrnan.
Paccmorpnm npnmep. B namem cnyuae mt pasnernnxem nponecc n cosaem ouepnn nponecc.
Mt raxxe sacrannxem ouepnn nponecc nepecnart ponrentcxomy cnrnan INT. Hrax, cosa-
em ouepnn nponecc:
1f {open{ChlLDhANDLL, "|-"}} ...
3arem mt oannxem opaorunx cnrnana INT anonnmnym nonporpammy, xoropax ntno-
nr coomenne, xora mt nonyuaem cnrnan:
1f {open{ChlLDhANDLL, "|-"}} {
$5lG{lN1} = sub {p1nf "Gof fhe message.\n"}
...
Crp.214
ouepnemy nponeccy norpeyercx nenrn]nxarop ponrentcxoro nponecca (nen-
rn]nxarop rexymero nponecca xpannrcx n cnennantno nepemenno Perl $$). Ho+romy mt
nepectnaem ouepnemy nponeccy nyxnym nn]opmannm:
1f {open{ChlLDhANDLL, "|-"}} {
$5lG{lN $5lG{lN $5lG{lN $5lG{lN1} = sub {p1nf "Gof fhe message. 1} = sub {p1nf "Gof fhe message. 1} = sub {p1nf "Gof fhe message. 1} = sub {p1nf "Gof fhe message. \ \\ \n"} n"} n"} n"}
p1nf ChlLDhANDLL "$$"
c1ose {ChlLDhANDLL}
...
B ouepnem nponecce mt sanomnnaem nenrn]nxarop ponrentcxoro nponecca n nepemenno
$parentid n noctnaem ponrentcxomy nponeccy cnrnan INT c nomomtm ]ynxnnn kill:
1f {open{ChlLDhANDLL, "|-"}} {
$5lG{lN1} = sub {p1nf "Gof fhe message.\n"}
p1nf ChlLDhANDLL "$$" p1nf ChlLDhANDLL "$$" p1nf ChlLDhANDLL "$$" p1nf ChlLDhANDLL "$$"
c1ose {ChlLDhANDLL}
} e1se {
chomp{$paenf1d = <>} chomp{$paenf1d = <>} chomp{$paenf1d = <>} chomp{$paenf1d = <>}
k111 lN1 k111 lN1 k111 lN1 k111 lN1 => => => => $paenf1d $paenf1d $paenf1d $paenf1d
ex1f
}
A nor n pesyntrar: ponrentcxn nponecc neuaraer coomenne o nonyuennn cnr-I nana or
ouepnero nponecca:
Gof fhe message.
PC: ucnonusoeauue cokefoe
1

Coxert nosnonxmr cnxstnartcx c pyrnmn xomntmrepamn uepes Hnrepner nnn noxantnte
cern. 3ra rema cnnmxom omnpna, urot nopono paccmorpert ee n onom pasene. B
+ro xnnre mt nnmt nemnoro npnorxpoem omnpnt mnp cerentx coennenn.
Ho+romy nmecro erantntx pasxcnenn x npocro npnney npnmep, n xoropom nporpamma-
xnnenr nepectnaer annte nporpamme-cepnepy uepes Cert, ncnontsyx nporoxon UDP. Hpn
+rom xorx nt moxere sanycxart nporpammy-xnnenra c nmo mamnnt, noxnmuenno x Hn-
repnery (nanpnmep, c omamnero nepconantnoro xomntmrepa), nporpamma-cepnep onxna
paorart na mamnne nponaepa.
Hama nporpamma-xnnenr naunnaercx c nmnoprnponannx moynx I::Socket cranaprnoro
naxera I, nxoxmero n cocran Perl:
use lO::5ockef
3arem mt cosaem coxer c nomomtm ntsona meroa I::Socket::INET->new. Mt nepea-
em meroy n xauecrne napamerpon ncnontsyemt nporoxon (UDP), nopr, uepes xoropt
mt xornm nonyunrt ocryn x cepnepy, n nmx mamnnt nponaepa (ISP):
use lO::5ockef
$sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1- -- ->neW{Pofo >neW{Pofo >neW{Pofo >neW{Pofo => => => => udp udp udp udp, ,, ,
PeePof PeePof PeePof PeePof => => => => 4321, 4321, 4321, 4321,
PeeAdd PeeAdd PeeAdd PeeAdd => => => => sevename sevename sevename sevename. .. .com com com com} } } }
(B xauecrne nomepa nopra n namem npnmepe cronr nponsnontnoe snauenne 321. B onepann-

1
3ror, a raxxe nocneymmn pasent ncnontsymr nonxrnx n cnnraxcnuecxne xoncrpyxnnn oexrno-opnenrnponannoro
nporpammnponannx, xoropte noxa ne paccmarpnnannct n anno xnnre. Ecnn nt yxe snaere, uro raxoe naxert, moynn, xnac-
ct, merot n oexrt n xstxe Perl, unrare cmeno. Ecnn nt +roro eme ne snaere nponycrnre +rn na pasena n nepnn-
rect x nnm nosxe, xora osnaxomnrect c rnanamn 13-1. Hpu+e:. nepee.
Crp.215
onno cncreme Unix noprt mentme 102 sapesepnnponant nx cncremnoro nontsonannx.)
Ham ocranoct nepecnart annte na cepnep. Mt ocymecrnnxem +ro c nomomtm meroa send:
use lO::5ockef
$sockef = lO::5ockef::lNL1->neW{Pofo => udp,
PeePof => 4321,
PeeAdd => sevename.com}
$sockef $sockef $sockef $sockef- -- ->send{he11o!} >send{he11o!} >send{he11o!} >send{he11o!}
Ocranoct nanncart nporpammy-cepnep. Ona yxe onxna paorart, xora nporpamma-
xnnenr noctnaer coomenne. Ho+romy nporpamma-cepnep naunnaercx c nmnoprnponannx
moynx n neuarn coomennx Waiting...:
use lO::5ockef
p1nf "Wa1f1ng ...\n"
Mt cosaem cerenoe coennenne nx nporpammt-cepnepa c nomomtm meroa
I::Socket::INET->new, yxastnax xax napamerpt nporoxon UDP n nopr c nomepom 321
(ror xe camt, xoropt ncnontsyer nporpamma-xnnenr):
use lO::5ockef p1nf "Wa1f1ng ...\n"
$sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1 $sockef = lO::5ockef::lNL1- -- ->neW{Pofo >neW{Pofo >neW{Pofo >neW{Pofo => => => => udp, Loca1Pof udp, Loca1Pof udp, Loca1Pof udp, Loca1Pof => => => => 4321} 4321} 4321} 4321}
urot nonyunrt annte or xnnenra, mt ncnontsyem mero recv oexra socket. Hpn nt-
sone meroa mt yxastnaem, uro xornm nonyunrt maxcnmym 12 ar. Mero recv yer
oxnart annte n ntneer nx cpasy no nonyuennn:
use lO::5ockef
p1nf "Wa1f1ng ..,\n"
$sockef = lO::5ockef::lNL1->neW{Pofo => udp, Loca1Pof => 4321}
$sockef->ecv{$fexf, 12}
p1nf "Gof fh1s message: $fexf\n"
Tenept, nocne roro xax yer sanymena nporpamma-xnnenr, nporpamma-cepnep naneuaraer:
Wa1f1ng ...
Gof fh1s message: he11o!
PC: ucnonusoeauue fexuonoruu Win32 OLE utoation
1

Ono ns rnnnuntx npnmenenn IPC na Windows-mamnnax +ro ncnontsonanne cepne-
pon OLE Automation (nx raxxe nastnamr eme xomnonenramn). Hporpamma ActivePerl
(nnar]opma Win32) noepxnnaer OLE Automation. urot ncnontsonart +ry rexnono-
rnm, neoxonmo cosart n nporpamme oexr c nomomtm cepnepa OLE Automation (na-
npnmep, MicrosoIt Excel). 3arem moxno ncnontsonart ero merot.
Hpn nsanmoecrnnn nponeccon n cncreme Windows nnyrpennee npecrannenne rnnon
anntx cranonnrcx nponemo npecrantre cee, x npnmepy, uro nama nporpamma na-
nncana na C n nt nsanmoecrnyere c nporpammo, nanncanno na Pascal. (Ecnn nt
snaere oa xstxa, ro nerxo coopasnre, uro, nanpnmep, nnyrpennee npecrannenne crpox y
xomnnnxropa Borland Pascal n xomnnnxropa isual C npnnnnnnantno pasnnuno.) urot
cnpannrtcx c nponemo nepexonponxn nnyrpennero npecrannennx, naxer Perl Ior
Win32 onpeenxer naop cranaprntx eapuaumuix rnnon anntx. 3rn rnnt, nepeunc-
nennte n ra. 11.1, oecneunnamr yont cnoco xpanennx, no cyrn, nernnnsnponanntx

1
Hpetymn pasen tn nnmt napocxom paccxasa, uro xe raxoe cerente coennennx n xax c nnmn paorart n Perl. Touno
rax xe +ror pasen ne onee xax napocox roro, xax n Perl ncnontsyercx rexnonornx Win32 OLE Automation ]npmt Micro-
soIt. Hpu+e:. nepee.
Crp.216
anntx Perl. Tannna 11.1 noxastnaer rnnt anntx OLE, n xoropte npeopasymrcx
nnyrpennne annte Perl nepe nepectnxo cepnepy OLE Automation.
Hanpnmep, snauenne, xpannmoe n Perl xax nenoe uncno, yer npeopasonano n VT_I4 rn-
na anntx, uncno c nnanamme rouxo (nono rounocrn) n VT_I8 n r. . Haxer Perl
Ior Win32 saornrcx o +rom anromarnuecxn. urot cosart oexr OLE Automation, na-
o noxnmunrt moynt Perl OLE:
use OLL
Tenept nt moxere ncnontsonart ]ynxnnm reatebect, urot cosart oexr OLE
Automation. Paccmorpnm neontmo npnmep. 3ect mt yem ncnontsonart nporpammy
MicrosoIt Excel nx roro, urot cnoxnrt 2 2 (noncrnny npannntnoe ncnontsonanne an-
no nporpammt ) n ntnecrn pesyntrar. nx nauana coxpannm +rn na snauennx xax $op-
erand1 n $operand2 (oparnre nnnmanne, uro mt coxpanxem nx xax crpoxn):
use OLL
$opeand1 = 2
$opeand2 = 2
Tenept cosanm c nomomtm ]ynxnnn reatebect oexr OLE Automation exce-
lobect. 3ro ]ynxnnn nt nepeaere snauenne E (coepxnrcx n nmnoprnponannom mo-
yne) n yxastnaere c nomomtm crpoxn nna cepeep.r.acc rnn oexra, xoropt xornre
cosart. (3ect cepeep +ro saperncrpnponannoe n Windows nmx cepnepa OLE Automa-
tion, a r.acc onpeenennt nx annoro cepnepa xnacc cosanaemoro oexra.) Taxnm
opasom, mt cosanm +nexrponnym rannny Excel c nomomtm xomant:
use OLL
$opeand1 = 2
$opeand2 = 2
$exce1obecf $exce1obecf $exce1obecf $exce1obecf = = = = C C C CeafeObecf OLL Lxce1.5heef eafeObecf OLL Lxce1.5heef eafeObecf OLL Lxce1.5heef eafeObecf OLL Lxce1.5heef
(Boome ronopx, cepnept OLE Automation otuno noepxnnamr mnoxecrno xnaccon.
Ecnn yaua c namn, oxymenrannx namero cepnepa onnctnaer, xaxne nmenno merot
moxno ncnontsonart n c xaxnmn nmenno xnaccamn. oxymenrannx Excel raxym nn]opma-
nnm coepxnr nor nouemy on tn n namem npnmepe.)
Bt cosann oexr n renept cnoono moxere ncnontsonart ero merot. B cneymmem
]parmenre xoa $operand1 sarpyxaercx n xuexy +nexrponno rannnt (1,1), $operand2
n xuexy (2,1), a sarem c nomomtm ]opmynt Excel oa aprymenra cxnatnamrcx, a pe-
syntrar nomemaercx n xuexy (3,1):
use OLL
$opeand1 = 2
$opeand2 = 2
$exce1obecf = CeafeObecf OLL Lxce1.5heef
$exce1obecf $exce1obecf $exce1obecf $exce1obecf- -- ->Ce11s{1,1} >Ce11s{1,1} >Ce11s{1,1} >Ce11s{1,1}- -- -> > > > {va1ue} {va1ue} {va1ue} {va1ue} = = = = $opeand1 $opeand1 $opeand1 $opeand1
$exce1obecf $exce1obecf $exce1obecf $exce1obecf- -- ->Ce11s{2,1} >Ce11s{2,1} >Ce11s{2,1} >Ce11s{2,1}- -- -> {va > {va > {va > {va1ue} 1ue} 1ue} 1ue} = = = = $opeand2 $opeand2 $opeand2 $opeand2
$exce1obecf $exce1obecf $exce1obecf $exce1obecf- -- ->Ce11s{3,1} >Ce11s{3,1} >Ce11s{3,1} >Ce11s{3,1}- -- -> {Iomu1a} > {Iomu1a} > {Iomu1a} > {Iomu1a} = = = = =k1C1 =k1C1 =k1C1 =k1C1 + k + k + k + k2 22 2C CC C1 1 1 1
Bce, uro ocranoct cenart, +ro ntnecrn cymmy n sanepmnrt paory
1
:
use OLL
$opeand1 = 2
$opeand2 = 2
$exce1obecf = CeafeObecf OLL Lxce1.5heef
$exce1obecf->Ce11s{1,1}->{va1ue} = $opeand1

1
Hocxontxy npnnoxennx MS OIIice nmxr nrnopnponart xomany quit, nosmoxno, npnercx raxxe naxart rpn xnannmn
CtrlAltDel n c nomomtm noxnnnmerocx menm sanepmnrt paory npnnoxennx Excel. Hpu+e:. peo.
Crp.217
$exce1obecf->Ce11s{2,1}->{va1ue} = $opeand2
$exce1obecf->Ce11s{3,1}->{Iomu1a} = =k1C1 + k2C1
$sum $sum $sum $sum = = = = $exce1obecf $exce1obecf $exce1obecf $exce1obecf- -- ->Ce11s{3, 1} >Ce11s{3, 1} >Ce11s{3, 1} >Ce11s{3, 1}- -- -> {va1ue} > {va1ue} > {va1ue} > {va1ue}
$exce1obecf $exce1obecf $exce1obecf $exce1obecf- -- ->u1f{} >u1f{} >u1f{} >u1f{}
p1nf "Accod1ng fo M1cosoff Lxce1 p1nf "Accod1ng fo M1cosoff Lxce1 p1nf "Accod1ng fo M1cosoff Lxce1 p1nf "Accod1ng fo M1cosoff Lxce1, , , , ", ", ", ",
"$opeand1 + $opeand2 "$opeand1 + $opeand2 "$opeand1 + $opeand2 "$opeand1 + $opeand2 = = = = $s $s $s $sum. um. um. um.\ \\ \n" n" n" n"
Accod1ng fo M1cosoff Lxce1, 2x2=4

1abnua 1abnua 1abnua 1abnua 1unu auuux OLL Aufomaf1on
1un auuux 1un auuux 1un auuux 1un auuux OLL OLL OLL OLL C1auap1uuu 1un auuux C1auap1uuu 1un auuux C1auap1uuu 1un auuux C1auap1uuu 1un auuux
OLL::v18OOL yneacxuu 1un OLL
OLL::v1851k C1poxa OLL {cha" a c1une nauxa C}
OLL::v1C eueuuu 1un OLL
OLL::v1DA1L a1a OLL
OLL::v1l2 enoe co auaxou {2 bau1a}
OLL::v1l4 enoe co auaxou {4 bau1a}
OLL::v1k4 8e|ec1aeuuoe {4 bau1a}
OLL::v1k 8e|ec1aeuuoe { bau1}
OLL::v1ul1 Cuuaon {ouobau1oaoe enoe bea auaxa}
Crp.218
Fnana 12
Bc1poeuume qyukuuu: pao1a c qanauu

Kopo1ko
B +ro rnane paccxastnaercx o cpecrnax paort c ]anamn Perl. Ocooe nnnmanne yenxer-
cx ]ynxnnxm, xoropte otuno paoramr c ]nsnuecxnmn (ro ecrt pacnonoxenntmn na ncxe)
]anamn, nx nmenamn n xaranoramn. 3ro ouent naxnax n oemnax rema; xorx nonnt osop
nenosmoxen, annax rnana aer nennoxym asy, xoropax nosnonnr unrarenm trt ocrarouno
xomnerenrntm n annom nonpoce. (Ona ns npnunn nnymnrentnoro oema +ro remt ro,
uro mnorne ]ynxnnn ynnpymr pyr pyra. enns Perl ncera ecrt necxontxo cnocoon
cenart uro-nno npoxnnxer cex n onacrn paort c ]anamn c ocoenno nonnoro.)
Hocxasxa. Hpomueuuru Unix oo.rui cpasv vcuumi, :mo paooma c qa.a+u e Perl ocuoeiea.aci usua-
:a.iuo u+euuo ua qa.oeo cucme+e omo onepauouuo cucme+i. Bsir no-nperue+v e sua:ume.iuo
cmeneuu npuoeprueaemc nperue cmpvrmvpi, noooepruea npaea oocmvna r qa.a+ Unix, cu+eo.u:e-
crue cci.ru u m. o. Umooi npoeepumi, rarue u+euuo qvuruu noooeprueammc e eaue onepauouuo
cucme+e, nompeovmmc ueromopie orcnepu+eumi c rourpemuo eepcue Perl (ocooeuuo roeoa oe.o oo-
oem oo vcmauoeru npae oocmvna r eauu+ qa.a+).
Koe-fo o paofe c qanaru
Fontmnncrno nporpammncron snaxomo c npnnnnnamn, na xoroptx crponrcx paora c ]ana-
mn. urot paorart c anntmn, coepxamnmncx n ]ane, nporpamma orxptnaer ero, no-
nyuax coornercrnymmn oecrpunmop (Iile handle). 3ra nponeypa cosaer nomor nnoa nnn
ntnoa, n antnemne onepannn (urenne, sannct ]ana) cctnamrcx na norox uepes ecxpnn-
rop. Ho sanepmennn paort ]an saxptnaercx. B +ro rnane +ror nponecc paccmarpnnaercx
onee rnyoxo. Mt yem paorart ne rontxo c ecxpnnropamn, no n c ]ynxnnxmn, xoropte
ynpannxmr nenocpecrnenno ]anamn n xaranoramn, pacnonoxenntmn na ncxe. Kpome roro,
sect paccxastnaercx o cosannn n ncnontsonannn as anntx DBM (Unix), rax xax +ror ma-
repnan nonynxpen n cpee nporpammncron CGI-npnnoxenn. Heoxonmo sanomnnrt ne-
cxontxo cornamenn. Bo-nepntx, nmena ecxpnnropon ]anon n Perl, urot ornnunrt nx or
sapesepnnponanntx nenrn]nxaropon, otuno nanpamrcx sarnanntmn yxnamn. (ecxpnn-
ropt ]anon ne rpeymr pastmenontnammero npe]nxca rnna $. urot paorart c nnmn xax
c nepemenntmn, nanpnmep, urot cxonnponart nao oparnrtcx x sanncn rannnt cnm-
nonon c rem xe camtm nmenem.)
Hocuasua. Bcmpe:a u+ (uanpu+ep, AAME), romopoe +oruo uumepnpemupoeami rar oecrpunmop
qa.a, Perl oopauaemc r sanucu mao.ui cu+eo.oe AAME u use.eraem oecrpunmop qa.a. Hoomo+v
ec.u ei, uanpu+ep, eino.uume onepaum npuceoeuu AEWAAME AAME o. sanuce mao.ui cu+-
eo.oe, mo AEWAAME u AAME ovovm cci.amic ua oouu u mom re oecrpunmop.
Bo-nroptx, nao nomnnrt, uro opamenne x ]anam nanonee uacro npnnonr x omnxam, - no-
+romy nanonee naxnte onepannn cronr conponoxart xoncrpyxnne or die nnn uem-ro no-
ontm. Haxonen, yurnre, uro Unix ncnontsyer xocym uepry (/) nx pasenennx xaranoron n
nonnom nyrn x ]any. Ecnn nama onepannonnax cncrema (Windows nnn DOS) ncnontsyer o-
parnym xocym uepry (\), ne saytre, uro nx Perl n crpoxax, orpannuenntx nontmn xantuxa-
mn, +ro cnennantnt cnmnon n ero nao nanpart c nomomtm escape-nocneonarentnocrn (\\):
Crp.219
open {IlLLhANDLL, "fmp open {IlLLhANDLL, "fmp open {IlLLhANDLL, "fmp open {IlLLhANDLL, "fmp\ \\ \\ \\ \f11e.fxf"} f11e.fxf"} f11e.fxf"} f11e.fxf"}
o d1e {"Cannof open f11e.fxf"}
Wh11e {<IlLLhANDLL>} { p1nf }
3amertre raxxe, uro, nocxontxy Perl rax cnntno opnenrnponan nmenno na ]ant n paory
c nnmn, marepnan, ornocxmncx x anno rnane, moxno narn n n pyrnx pasenax xnnrn:
n rnane oneparopt paort c ]anamn rnna -, n rnane 9 -cnennantnte nepemennte
(nanpnmep, $/ pasennrent nxontx sannce, $, -pasennrent ntxontx sannce, $[
y]epnsannx ntnoa n r. .). Haxonen, n rnane 10 pasnpamrcx ]ynxnnn, ynaxontnammne
annte n sanncn ]nxcnponannoro pasmepa, uro yono nx ]anon c npxmtm ocrynom x
anntm (]ynxnnn pack, unpack n vec).
Haxonen, ne satnare, uro ono n ro xe ecrnne moxno ntnonnnrt pasntmn cnocoamn.
Ecnn nt ne moxere narn nyxnoro nncrpymenra cpen ncrpoenntx cpecrn paort c ]ana-
mn, ero moxno ortcxart n pyrom mecre. Hanpnmep, cpen ncrpoenntx ]ynxnn ner ]ynx-
nnn, nosnonxmme xonnponart ]ant, no n moyne I::File ecrt mero copy, npexpacno
noxoxmn nmenno nx +ro nenn. Haxonen, ecnn ne yaercx narn ro, uro nam nao cpen
snaxomtx nncrpymenron, nponeptre mnorouncnennte ]ynxnnn moynx POSIX.

Henocpepc1neuume pemeuun
open - ofkpufue qana
urot orxptrt ]an, ncnontsyre ]ynxnnm open:
open ecxpun1op, aupaeuue
open ecxpun1op
3ra ]ynxnnx orxptnaer ]an c saanntm nmenem n cosaer yxasannt ecxpnnrop ]a-
na. Hocne ee ntsona ecxpnnrop moxer ncnontsonartcx nx cctnox na ]an n camtx pas-
ntx onepannxx. Ecnn nmx ne saano, Perl ntraercx orxptrt ]an c nmenem, connaam-
mnm c nmenem ecxpnnropa.
uynxnnx open nosnpamaer nenynenoe snauenne (coornercrnyer ycnonnm ucmuua), ecnn ]an
ycnemno orxptr, n neonpeenennoe snauenne (coornercrnyer ycnonnm .ori), ecnn cenart
+roro ne yanoct. (Ecnn n xauecrne ]ana orxptnaercx raua., ro nosnpamaemoe snauenne n
cnyuae ycnemnoro sanepmennx paort ]ynxnnn +ro nenrn]nxarop ouepnero nponecca.)
Hmx ]ana moxer coepxart ononnnrentnte cnmnont, yxastnammne, xax nmenno cne-
yer orxptrt ero.
Ecnn nmx nmeer npe]nxc < nnn ne nmeer npe]nxca, ]an orxptnaercx nx urennx.
Ecnn nmx nmeer npe]nxc >, ]an orxptnaercx nx sanncn n nonnocrtm ounmaercx (ec-
nn on yxe cymecrnyer) nnn xe cosaercx nont ]an.
Ecnn nmx nmeer npe]nxc >>, ]an orxptnaercx nx sanncn, a annte onnctnamrcx n
ero xonen. Ecnn ]an ne cymecrnyer, cosaercx nont.
Ecnn nmx nmeer npe]nxc +<, ]an orxptnaercx nx urennx n sanncn. Ecnn ]an cyme-
crnyer, ero coepxnmoe coxpanxercx.
Ecnn nmx nmeer npe]nxc +>, ]an orxptnaercx n nx urennx n sanncn, onaxo ecnn on
Crp.220
yxe cymecrnyer, ro nonnocrtm ounmaercx.
Ecnn nmx nmeer npe]nxc [ nnn xe nepe nnm cronr cnmnon [, Perl paccmarpnnaer +ry
xoncrpyxnnm xax raua. eieooa, ro ecrt xax nmx nporpammt, xoropo na nxo yyr no-
anartcx ntnonmte cnenapnem annte (noponee cm. rnany H).
Ecnn nmx nmeer cy]]nxc [ nnn xe nocne nero cronr cnmnon [, Perl paccmarpnnaer +ry
xoncrpyxnnm xax raua. eeooa, ro ecrt xax nmx nporpammt, ntno xoropo yer cunrt-
nartcx cnenapnem n xauecrne anntx (noponee cm. rnany 11).
Ecnn n xauecrne nmenn ]ana saant xoncrpyxnnn [- nnn -[, nopoxaercx ouepnn npo-
necc, ]ynxnnx open nosnpamaer ero nenrn]nxarop, a onepannn nnoa (-[) nnn ntnoa ([-) y-
yr npnnonrt x omeny nn]opmanne c ouepnnm nponeccom (noponee cm. rnany 11).
Ecnn n xauecrne nmenn ]ana saan e]nc , ]ynxnnx orxptnaer cranaprnt norox
nnoa (otuno STDIN).
Ecnn n xauecrne nmenn ]ana saant cnmnont >-, ]ynxnnx orxptnaer cranaprnt
norox ntnoa (otuno STDUT).
Ecnn ntpaxenne naunnaercx c xoncrpyxnnn >, ro nmx sa ne nnrepnpernpyercx nno xax nmx
ecxpnnropa ]ana Perl (ecnn +ro rexcr), nno xax ecxpnnrop ]ana Unix (ecnn +ro uncno).
Hocuasua. Moruo ucno.isoeami cu+eo. & u noc.e roucmpvru >>, +>, +>>, < u +<.
Ecnn ntpaxenne naunnaercx c xoncrpyxnnn <&nn, re nn +ro uncno, ro Perl pac-
cmarpnnaer nn xax ecxpnnrop ]ana n opaartnaer ero xax ]ynxnnx fdopen xstxa C.
B cneymmem npnmepe ]an orxptnaercx nx sanncn, n n nero ntnonrcx nexoropt rexcr:
open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11 open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11 open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11 open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11e he11o.fxf"} e he11o.fxf"} e he11o.fxf"} e he11o.fxf"}
p1nf IlLLhANDLL, "he11o!"
c1ose {IlLLhANDLL}
he11o!
close - sakpufue qana
uynxnnx close saxptnaer orxptrt ]an nnn xanan no oxonuannn paort c nnm. Hpn
+rom n ]an nnn xanan nepectnamrcx nce annte, eme naxoxmnecx n y]epe ntnoa, a
ecxpnnrop ]ana ennnnnannsnpyercx, rax uro antnemne onepannn c nnm (xpome or-
xptrnx nonoro ]ana) cranonxrcx nenosmoxnt:
c1ose ecxpun1op
c1ose
Ecnn ecxpnnrop ]ana onymen, ]ynxnnx saxptnaer norox, xoropt ycranonnen xax
STDOUT (cm. onncanne ]ynxnnn select anee n +ro rnane). Ona nosnpamaer snauenne
ucmuua, ecnn yanoct ntnecrn ocrarox y]epa n ycnemno saxptrt ]an.
Ecnn saxptnaercx xanan, ]ynxnnx close oxnaer sanepmennx paort nponecca, cnxsanno-
ro c nnm, urot moxno nponepnrt ntno uepes xanan. (Ko sanepmennx nporpammt, c xo-
ropo nponcxonn omen anntmn, sannctnaercx n nepemennym Perl $.)
Hpnmep ncnontsonannx ]ynxnnn close:
open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11e he11o.fxf"}
p1nf IlLLhANDLL, "he11o!"
c1ose {IlLLhANDLL} c1ose {IlLLhANDLL} c1ose {IlLLhANDLL} c1ose {IlLLhANDLL}
he11o!
Crp.221
print - eueop e qan
uynxnnx print ntnonr cnncox n ]an, oosnauennt ecxpnnropom:
p1nf ecxpun1op cnucox
p1nf cnucox
p1nf
B cneymmem npnmepe (on yxe ncrpeuancx n +ro rnane) ]ynxnnx print sannctnaer nn-
]opmannm n ]an:
open {IlLLhANDLL, ">he11o.fxf"} o d1e {"Cannof open f11e he11o.fxf"}
p1nf IlLLhANDLL p1nf IlLLhANDLL p1nf IlLLhANDLL p1nf IlLLhANDLL "he11o!" "he11o!" "he11o!" "he11o!"
c1ose {IlLLhANDLL}
he11o!
uynxnnx nosnpamaer snauenne ucmuua npn ycnemnom sanepmennn. Ecnn ne saan ecx-
pnnrop, ntno nponsnonrcx n STDUT nnn n ecxpnnrop ]ana, ycranonnennt xax
STDUT (cm. onncanne ]ynxnnn select anee n +ro rnane). Ecnn cnncox onymen, nc-
nontsyercx nepemennax $_. Hocxontxy print paoraer co cnncxamn, n xoroptx xaxt
+nemenr anntx cunraercx orentno sannctm, moxno ntnonrt n ]an cnncxn cneym-
mnm opasom (oparnre nnnmanne, uro sect nepeonpeenxercx pasennrent ntxontx
sannce, xpannmt n nepemenno $,):
open {IlLLhANDLL, ">aay.daf"} o d1e {"Cannof open f11e aay.daf"}
$, = "\n"
0aay = {1, 2, 3}
p1nf IlLLhA p1nf IlLLhA p1nf IlLLhA p1nf IlLLhAN NN NDLL 0aay DLL 0aay DLL 0aay DLL 0aay
c1ose {IlLLhANDLL}
Tenept coepxnmoe ]ana array.dat npecrannxer coo:
1
2
3
Cneymmn xo orxptnaer rontxo uro sanncannt ]an (oparnre nnnmanne, uro n mac-
cnn array cunrtnamrcx cpasy nce crpoxn ]ana n yanxercx cnmnon nono crpoxn):
open {IlLLhANDLL, "<aay.daf"} o d1e {"Cannof open f11e aay.daf"}
chomp{0aay = <IlLLhANDLL>} chomp{0aay = <IlLLhANDLL>} chomp{0aay = <IlLLhANDLL>} chomp{0aay = <IlLLhANDLL>}
c1ose IlLLhANDLL
p1nf o1n {, , 0aay}
1, 2, 3
rite - sanucu e qan
Bmecro print nx sanncn n ]an moxno ncnontsonart ]ynxnnm write:
W1fe ecxpun1op
W1fe aupaeuue
W1fe
Mt yxe ncrpeuannct c +ro ]ynxnne n npetyme rnane (cm. ononnnrentnte no-
ponocrn n rnane 11). uynxnnx write ncnontsyercx nx ntnoa ]opmarnponanntx sann-
ce, a ne xax ]ynxnnx ntnoa omero nasnauennx (nx +ro nenn ncnontsyercx print). Ec-
nn yxasan ecxpnnrop ]ana, write ntnonr ouepenym ]opmarnponannym sannct nx
]opmara, npnnncannoro +romy ecxpnnropy. Ecnn yxasano ntpaxenne, ro ono onxno
trt rexcrono crpoxo, nnrepnpernpyemo xax nmx ecxpnnropa. Haxonen, ecnn write
Crp.222
ntstnaercx es napamerpon, ro ncnontsyercx STDUT nnn ecxpnnrop, xoropt ycra-
nonnen xax STDUT (cm. onncanne ]ynxnnn select anee n +ro rnane). B cneymmem
npnmepe n ]an format.txt ntnonrcx ]opmarnponannax sannct:
open {IlLLhANDLL, ">fomaf.fxf"} o d1e {"Cannof open f11e fomaf.fxf"}
fomaf IlLLhANDLL =
0<<<<<<<<<<<0>>>>>>>>>>>>
$fexf1, $fexf2
.
$fexf1 = "he11o"
$fexf2 = "fhee"
W1fe IlL W1fe IlL W1fe IlL W1fe IlLL LL LhANDLL hANDLL hANDLL hANDLL
c1ose {IlLLhANDLL}
he11o fhee!
binode - ycfauoeka peouuoro peura
Hexoropte onepannonnte cncremt (nanpnmep, DOS n Windows) pasnnuamr rexcronte n
nonunte ]ant (rounee, pexnmt paort c ]anamn). Hpn ntnoe cnmnonon nono
crpoxn n anromarnuecxn samenxercx napo +F (\r\n), a npn nnoe \r\n samenxercx
cnmnonom nono crpoxn \n. urot npn ntnoe ncnontsonart rontxo cnmnont \n, ncnont-
syercx ]ynxnnx binmode:
b1nmode ecxpun1op
Paccmorpnm npnmep ncnontsonannx Perl n MS-DOS. Hpn ntnoe crpoxn, sanepmammecx
cnmnonom \n, n ntxonom ]ane noxnnrcx \r\n (ASCII-xot 0x0d0x0a):
open {IlLLhANDLL, ">dafa.fxf"} o d1e {"Cannof open f11e dafa.fxf"}
p1nf IlLLhANDLL "he11o p1nf IlLLhANDLL "he11o p1nf IlLLhANDLL "he11o p1nf IlLLhANDLL "he11o\ \\ \nfhee! nfhee! nfhee! nfhee!\ \\ \n" n" n" n"
c1ose {IlLLhANDLL}
C:\debug dafa.fxf
-d
107A:0100 4 65 6C 6C 6I 0D 0A 74 - 6 65 72 65 21 0D 0A
he11o. . fhee!. . .
(nx npocmorpa ]ana n mecrnanarepnunom pexnme n annom cnyuae ncnontsonancx orna-
unx MS-DOS, xorx pyrne nporpammt, nanpnmep Norton Commander, ropaso yonee.)
Ecnn xe ntsnart ]ynxnnm binmode, ro n ]an nonayr rontxo cnmnont nono crpoxn:
open {IlLLhANDLL, ">dafa.fxf"} o d1e {"Cannof open f11e dafa.fxf"}
b1nmode IlLLhANDLL b1nmode IlLLhANDLL b1nmode IlLLhANDLL b1nmode IlLLhANDLL
p1nf IlLLhANDLL "he11o\nfhee!\n"
c1ose {IlLLhANDLL}
C:\debug dafa.fxf
-d
107A:0100 4 65 6C 6C 6I 0A 74 - 6 65 72 65 21 0A
he11o, fhee!. ..
Ynpaeneuue yqepusauue eueopa
Moxno sacrannrt Perl cpactnart na ncx (nnn pyro nocnrent) y]ep ntnoa nocne
xaxo onepannn print nnn write. nx +roro nepemenno Perl $[ nao npncnonrt nenyne-
noe snauenne:
$| = 1
Crp.223
(B npornnnom cnyuae nce, uro nanpannxercx n norox ntnoa, naxannnnaercx n y]epe (o-
nacrn namxrn) n sannctnaercx rontxo nocne sanonnennx y]epa nnn saxptrnx noroxa.)
Toro xe pesyntrara moxno onrtcx, ecnn ntsnart ]ynxnnm autoflush:
aufof1ush ecxpun1op aupaeuue
-feuue qanoe, nepepauuux epes koraupuyk cfpoky
Hmena ]anon, croxmne nocne nmenn cnenapnx n xomanno crpoxe, nepeamrcx xoy n
xauecrne noroxa nnoa:
x p1nfem f11e1.fxf f11e2.fxf
Ecnn nocne +ro xomant oparnrtcx x STDIN, ro nmecro nnoa c xonconn (xnannarypt)
mt nonyunm oennennoe coepxnmoe ]anon file1.txt n file2.txt:
Wh11e {<>} { p1nf }
hee 1s
a
f11e!
hee 1s
anofhe
f11e!
-feuue us peckpunfopa qana
Btpaxenne nna oecrpunmop~ nosnpamaer cneymmym crpouxy, cunrannym ns ]ana.
3ro nonesno, xora nao npounrart orxptrt ]an. Hanpnmep, cneymmn ]parmenr xo-
a unraer nect rexcr ns ]ana file.txt:
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} { Wh11e {<IlLLhANDLL>} {
p1nf
}
hee
1s
a
f11e!
Ecnn ecxpnnrop ]ana ne yxasan, ncnontsyercx STDIN.
read - feuue exopuux pauuux
uynxnnx read unraer annte ns ecxpnnropa ]ana:
ead ecxpun1op, cxannp, nuua, cue|euue
ead ecxpun1op, cxannp, nuua
(Oparnre nnnmanne: ]ynxnnx read ue e.emc napno nx ]ynxnnn write!) 3ra ]ynx-
nnx ntraercx npounrart ns ecxpnnropa ]anon uncno aron, yxasannoe napamerpom
o.uua, n sanncart nx n cxanxpnym nepemennym. Hapamerp c+eueuue (ecnn on saan) yxa-
stnaer, c xaxo nosnnnn or nauana cxanxpa (crpoxn) nao pasmemart cunrannte art.
Ecnn on ne saan nnn panen nynm, ro cxanxpno nepemenno npncnannaercx snauenne-
crpoxa, cunrannax ns ]ana, a npexnee coepxnmoe nepemenno repxercx. Ecnn xe saa-
no c+eueuue, ro rexymnm snauennem cxanxpno nepemenno onxna trt rexcronax
Crp.224
crpoxa, or xoropo epercx necxontxo nepntx aron, x nnm oannxmrcx art, cunran-
nte ns ]ana, n nce sanomnnaercx n xauecrne nonoro snauennx nepemenno. Ho+romy
nnna crpoxn moxer ymentmnrtcx nnn ynennunrtcx. Ecnn rexymee snauenne cxanxpa ne
xnnxercx rexcrono crpoxo, ono npeopasyercx x ]opmary rexcrono crpoxn na-
npnmep, uncno 123.5 npeopasyercx n crpoxy 123.5. Ecnn rexymax nnna crpoxn
mentme yxasannoro cmemennx, ona ononnxercx nonuntmn nynxmn. (nx +xcne-
pnmenrantno nponepxn nonesno nocnontsonartcx ]ynxnne vec ns rnant 10.) uynxnnx
read nosnpamaer xonnuecrno ycnemno cunranntx ar. 3nauenne nont yxastnaer na xo-
nen ]ana. 3nauenne undef ronopnr o rom, uro n nponecce urennx tnn omnxn. Hpnmep
(urenne ]ana ar sa arom c nocneymmnm ntnoom):
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
$fexf = ""
Wh11e {ead {IlL Wh11e {ead {IlL Wh11e {ead {IlL Wh11e {ead {IlLL LL LhANDLL, hANDLL, hANDLL, hANDLL, $neWfexf, 1}} { $neWfexf, 1}} { $neWfexf, 1}} { $neWfexf, 1}} {
$ $$ $fexf .= $neWfexf fexf .= $neWfexf fexf .= $neWfexf fexf .= $neWfexf
} }} }
p1nf $fexf
hee
1s
a
f11e!
readline - cufueauue cfpoku eeopa
uynxnnx readline unraer ns ]ana ony crpoxy (n cxanxpnom xonrexcre) nnn cnncox
crpox o xonna ]ana (n cnncxonom xonrexcre) n nosnpamaer pesyntrar. nx nenrn]nxa-
nnn ]ana e nepeaercx ntpaxenne, xoropoe onxno trt sannctm rannnt cnmnonon
(typeglob) c rem xe nmenem, uro n ecxpnnrop ]ana (nanpnmep, readline (STDIN);):
ead11ne aupaeuue
(Hpnunna, no xoropo nepeaercx sannct rannnt cnmnonon, onontno npocra: n Perl +ro
eouucmeeuui cnoco nepeart cctnxy na ecxpnnrop ]ana.) urot onpeennrt xonen
crpoxn, ]ynxnnx readline ncnontsyer nepemennym Perl $/. B cneymmem npnmepe ns
STDIN cunrtnaercx crpoxa n ntnonrcx na +xpane:
$1npuf = ead11ne {"51DlN}
p1nf $1npuf
hee 1s a 11ne of fexf.
getc - cufueauue opuuouoro cureona
uynxnnx getc nosnpamaer onnount cnmnon, nonyuennt ns ecxpnnropa ]ana. Ecnn
nocnenn onymen, ncnontsyercx STDIN:
gefc ecxpun1op-auna gefc
uynxnnx nosnpamaer cnmnon nnn neonpeenennoe snauenne no ocrnxennn xonna ]ana.
uynxnnx getc yxe ncnontsonanact n npetyme rnane. B cneymmem npnmepe c ee no-
momtm ]an unraercx noarno (onaxo +ro ne osnauaer, uro nno ne y]epnsyercx
noponee cm. rnany 11):
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
Wh11e {$cha Wh11e {$cha Wh11e {$cha Wh11e {$cha = = = = gefc gefc gefc gefc IlLLhA IlLLhA IlLLhA IlLLhAN NN NDLL} DLL} DLL} DLL}
{ p1nf $cha }
Crp.225
hee 1s a f11e!
seek - nouck sapauuo nosuuuu e qane
uynxnnx seek ncnontsyercx nx ycranonxn rexyme nosnnnn n ]ane n rouxy, c xoropo
naunercx cneymmax onepannx nnoa nnn ntnoa:
seek ecxpun1op, noauun, xax-cuu1a1u
B xauecrne rexyme nosnnnn oecrpunmopa ]ana ycranannnnaercx saannax nont-
sonarenem nosnnnx. Hapamerp nosuu saaercx n arax, a napamerp rar-c:umami yxast-
naer, xax ero nnrepnpernponart. Bosmoxnte snauennx:
0 ycranonnrt rexymym nosnnnm n snauenne nosuu (orcunrtnaemoe or nauana ]ana),
1 cnnnyrt rexymym nosnnnm na yxasannoe xonnuecrno aron (c nauana nnn c xonna
]ana n sanncnmocrn or snaxa nosuuu),
2 ycranonnrt rexymym nosnnnm n snauenne nosuu, orcunrtnaemoe or xonna ]ana
(snauenne napamerpa nosuu onxno trt orpnnarentntm nnn nynentm).
Oparnre nnnmanne, uro napamerp rar-c:umami oxsarenen.
Paccmorpnm npnmep. uan file.txt coepxnr rexcr:
1h1s 1s fhe fexf.
C nomomtm cneymme xomant rexymax nosnnnx ycranannnnaercx na nauano cnona
text, a sarem, naunnax c +ro nosnnnn, ntnonnxercx onepannx urennx:
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
seek IlLLhANDLL, 12, 0 seek IlLLhANDLL, 12, 0 seek IlLLhANDLL, 12, 0 seek IlLLhANDLL, 12, 0
Wh11e {<IlLLhANDLL>} {
p1nf
}
c1ose {IlLLhANDLL}
fexf.
uynxnnx seek uacro ncnontsyercx c ]anamn, pasnrtmn na sanncn ]nxcnponannoro pasme-
pa. C ee nomomtm nerxo nonyunrt ocryn x nmo sanncn n ]ane (raxo ocryn nastnaercx
np+i+ (random), n ornnune or noc.eooeame.iuoeo (sequential access), xoropt rpeyer cun-
rtnart nce npomexyrounte sanncn, npexe uem nt oeperect o nyxnoro mecra). urot
ocnyxnnart sanncn nocroxnno nnnt (rax xax nepemennte Perl otuno oexrt nepe-
+euuo nnnt), moxno ncnontsonart ]ynxnnn Perl pack, unpack, vec n px pyrnx.
tell - fekyan nosuuun e qane
uynxnnx tell nosnpamaer snauenne rexyme nosnnnn n ]ane, orcunrtnaemoe n arax or
nauana ]ana:
fe11 ecxpun1op
fe11
Ecnn napamerp oecrpunmop onymen, tell ncnontsyer nocnenn npounrannt ]an. B
cneymmem npnmepe c nomomtm ]ynxnnn seek ycranannnnaercx nosnnnx n ]ane, a sarem
tell coomaer ee snauenne:
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
Crp.226
seek IlLLhANDLL, 12, 0
p1nf fe11 IlLLhANDLL p1nf fe11 IlLLhANDLL p1nf fe11 IlLLhANDLL p1nf fe11 IlLLhANDLL
c1ose {IlLLhANDLL}
12
Hocuasua. C no+ouim qvuruu tell +oruo vsuami pas+ep qa.a, vcmauoeue mervuvm nosuum ua roue
qa.a.
seek IlLLhANDLL, 0, 2
$s1eoff11e = fe11 IlLLhANDLL
stat - uuqoprauun o qane
uynxnnx stat nosnpamaer cnncox ns rpnnanarn +nemenron, onnctnammnx cocroxnne ]ana:
sfaf ecxpun1op
sfaf aupaeuue
sfaf
uan saaercx c nomomtm ecxpnnropa ]ana nnn rexcrono crpoxn, coornercrnymme
ero nmenn. Ecnn aprymenr stat ne saan, ncnontsyercx nepemennax $_. 3nemenrt, onnct-
nammne cocroxnne ]ana, npecrannxmr coo cneymmne snauennx:
0 (dev) nomep ]anono cncremt,
1 (ino) nnexcnt ecxpnnrop (xapaxrepncrnxn ]ana n onepannonno cncremt Unix),
2 (mode) pexnmt ocryna ]ana (paspemena sannct, paspemeno urenne, ncnonnxemt
]an, xaranor n r. .),
3 (nlink) uncno xecrxnx cctnox na ]an n ]anono cncreme,
4 (uid) nenrn]nxarop nontsonarenx (User ID) nx nnaentna ]ana,
5 (gid) nenrn]nxarop rpynnt nontsonarenx (Group ID) nx nnaentna ]ana,
6 (rdev) nenrn]nxarop ycrpocrna nx cnennantntx ]anon,
7 (si:e) nonnt pasmep ]ana n arax,
8 (atime) npemx nocnenero opamennx x ]any,
9 (mtime) npemx nocnenero nsmenennx ]ana,
10 (ctime) npemx nocnenero nsmenennx nnexcnoro ecxpnnropa,
11 (blks:e) pasmep noxa no ymonuannm nx cranaprno cncremt nnoa/ntnoa,
12 (blocks) uncno noxon, orneenntx nx ]ana.
Hocuasua. Bpe+ omc:umieaemc e cervuoax om ua:a.a onoxu, mo ecmi om 1 ueap 1970 eooa (omo
cnpaeeo.ueo no:mu o. ecex onepauouuix cucme+, ue mo.iro o. Unix, sa ucr.m:euue+, uanpu+ep, Ma-
cOs). Kpo+e moeo, ue ece o.e+eumi cocmouu noooeprueammc opveu+u onepauouui+u cucme+a+u.
B cneymmem npnmepe c nomomtm ]ynxnnn stat ntnonrcx pasmep ]ana:
$f11ename = "f11e.fxf"
{$dev, $1no, $n11nk, $u1d, $g1d, $dev, $s1e,
$af1me, $mf1me, $cf1me, $b1ks1e, $b1ocks} = sfaf {$f11ename}
p1nf "$f11ename 1s $s1e byfes 1ong."
f11e.fxf 1s 20 byfes 1ong.
Ecnn n xauecrne ecxpnnropa ]ana ]ynxnnn stat saan cnmnon nouepxnnannx, ona nosnpa-
maer cnncox snauenn, coornercrnymmnx nocnene nponepxe ]ana c nomomtm ]ynxnnn
Crp.227
stat nnn oneparopon nponepxa ]ana (cm. pasen Oneparopt nponepxn ]anon rnant ).
4anoeue qyukuuu ropynn POS
Haoparopnx xomntmrepntx cncrem Hannonantnoro nncrnryra cranapron n rexnonorn
(the National Institute oI Standards and Technology Computer Systems Laboratory
NIST/CSL) n copyxecrne c pyrnmn oprannsannxmn cosana cranapr POSIX Portable
Operating System InterIace. 3ro omnpnax nnnorexa cranaprnsnponanntx C-noontx
]ynxnn, noxptnammnx cranaprnte norpenocrn nporpammnponannx, or asontx mare-
marnuecxnx ntuncnenn o nponnnyro paort c ]anamn.
Moynt Perl POSIX npeocrannxer ocryn npaxrnuecxn xo ncem cranaprntm ]ynxnnxm
POSIX nepcnn 1003.1 ncero oxono 250. Onn ne xnnxmrcx ncrpoenntmn, noono oc-
rantntm ]ynxnnxm +ro rnant, onaxo ynomnnamrcx sect, rax xax moynt POSIX oec-
neunnaer ontme nosmoxnocre, uem ncrpoennte ]ynxnnn Perl. Moynt POSIX noxnm-
uaercx xomano use:
use PO5l # obaau1u ack bubnuo1exy PO5l
use PO5l qW/yuxun/ # obaau1u ouy yuxuk
(Hcnontsonanne no nropom napnanre ncenoxantuex qw/.../ (cm. ran. 2.3 n rnane 2) camt
npocro cnoco cosart cnncox ns crpox-nmen ]ynxnn, saxnmuenntx n xantuxn. Fonee no-
pono o xomane use paccxastnaercx n rnane 13.) Hanpnmep, n cneymmem ]parmenre ]ynxnnx
fstat moynx POSIX nosnonxer nonyuart nn]opmannm o cocroxnnn ]ana n ntnecrn ero pas-
mep (oparnre nnnmanne, uro ]ynxnnx fstat ncnontsyer ecxpnnropt, a ne nmena ]anon):
use PO5l use PO5l use PO5l use PO5l
$f11ename = "f11e.fxf"
$desc1p $desc1p $desc1p $desc1p = = = = PO5l::open{$f11ename, PO5l::0kDONL} PO5l::open{$f11ename, PO5l::0kDONL} PO5l::open{$f11ename, PO5l::0kDONL} PO5l::open{$f11ename, PO5l::0kDONL}
{$dev, $1no, $n11nk, $u1d, $g1d, $ {$dev, $1no, $n11nk, $u1d, $g1d, $ {$dev, $1no, $n11nk, $u1d, $g1d, $ {$dev, $1no, $n11nk, $u1d, $g1d, $dev, $s1e, $af1me, dev, $s1e, $af1me, dev, $s1e, $af1me, dev, $s1e, $af1me,
$mf1me, $mf1me, $mf1me, $mf1me, $cf1me, $b1ks1e, $cf1me, $b1ks1e, $cf1me, $b1ks1e, $cf1me, $b1ks1e, $b1ocks} $b1ocks} $b1ocks} $b1ocks} = = = = PO5l::fsfaf {$desc1p} PO5l::fsfaf {$desc1p} PO5l::fsfaf {$desc1p} PO5l::fsfaf {$desc1p}
p1nf "$f11ename 1s $s1e byfes 1ong."
f11e.fxf 1s 7 byfes 1ong.
select - euop peckpunfopa qana pnn STDOUT
uynxnnx select nosnonxer ycranonnrt nnn nonyunrt ecxpnnrop ]ana, ncnontsyemt no
ymonuannm nx onepann ntnoa:
se1ecf ecxpun1op
se1ecf
Hepnax ]opma ycranannnnaer saannt ecxpnnrop ]ana n xauecrne noroxa ntnoa, xo-
ropt yer ncnontsonartcx sarem no ymonuannm. Bropax nosnonxer nonyunrt rexymn
ecxpnnrop ]ana no ymonuannm nx onepann ntnoa. B cneymmem npnmepe c nomo-
mtm nepenanpannennx cranaprnt norox ntnoa STDUT nepeapecyercx n nont
]an, a ]ynxnnx print, saannax es xnnoro yxasannx ecxpnnropa ]ana, ntnonr annte
n +ror ]an:
open {IlLLhANDLL, ">he11o.fxf} o d1e {"Cannof open f11e he11o.fxf"}
se1ecf se1ecf se1ecf se1ecf IlLLhANDLL IlLLhANDLL IlLLhANDLL IlLLhANDLL
p1nf "he11o!"
c1ose {IlLLhANDLL}
Crp.228
eo - npoeepka kouua qana
uynxnnx eof nosnonxer nponepnrt npn urennn anntx, ne ocrnrnyr nn yxe xonen ]ana:
eof ecxpun1op
eof {}
eof
3ra ]ynxnnx nosnpamaer snauenne ucmuua (uncno 1), ecnn nt naxonrect n xonne ]ana, sa-
annoro uepes ecxpnnrop, rounee, ecnn cneymmax onepannx urennx npnneer x cnryannn
xonen ]ana. (3nauenne 1 nosnpamaercx n n rom cnyuae, ecnn ecxpnnrop ]ana ne orxptr.)
Fes aprymenra ]ynxnnx eof ncnontsyer nocnenn npounrannt ]an. Honeenne ]ynxnnn n
rom cnyuae, xora ee aprymenr nycrax napa xpyrntx cxoox, paccmarpnnaercx nnxe.
B cneymmem npnmepe ]an cunrtnaercx noarno o rex nop, noxa ne yer ocrnrnyr ero
xonen:
open {IlLLhANDLL, "<f11e.fxf"} o d1e {"Cannof open f11e f11e.fxf"}
$fexf = ""
unf11 {eof{IlLLhANDLL} unf11 {eof{IlLLhANDLL} unf11 {eof{IlLLhANDLL} unf11 {eof{IlLLhANDLL} { { { {
ead {IlLLhANDLL, $neWfexf, 1}
$fexf .= $neWfexf
}
p1nf $fexf
hee 1s a f11e!
uynxnnx eof c nycro napo xpyrntx cxoox n xauecrne aprymenra ncnontsyercx npn ure-
nnn noroxa nnoa STDIN, n xauecrne xoroporo ncnontsyercx necxontxo saanntx n xo-
manno crpoxe ]anon. B cneymmem npnmepe ntnonrcx coepxnmoe nepnoro ]ana c
rexcrom Here is the text!, nroporo c ]paso Here is another text!, a sarem ntnonrcx
crpoxa And that is it!. Cnenapn sanycxaercx rax:
xpe1 -W p1nf1f.p1 f11e1.fxf f11e2.fxf
nor xo cnenapnx:
Wh11e {<>} {
p1nf
1 11 1f {eof{}} { f {eof{}} { f {eof{}} { f {eof{}} {
p1nf "And fhaf 1s 1f!" }
}
hee 1s fhe fexf!
hee 1s anofhe fexf!
And fhaf 1s 1f!
Fes napamerpon nnn c xnntm yxasannem ecxpnnropa STDIN ]ynxnnx eof cpaartnana
t, ne rontxo xora ncuepnan nect nxono norox anntx, no n nepe sanepmennem pao-
rt c xaxtm ]nsnuecxnm ]anom:
Wh11e {<>} {
p1nf
1f 1f 1f 1f {eof} { {eof} { {eof} { {eof} {
p1nf "-" x 30 }
1f {eof{}} { 1f {eof{}} { 1f {eof{}} { 1f {eof{}} {
p1nf "And fhaf 1s 1f!" }
}
hee 1s fhe fexf!
------------------------------
hee 1s anofhe fexf!
------------------------------
And fhaf 1s 1f!
Crp.229
Oparnre nnnmanne, uro xoncrpyxnnx while (<>) nosnonxer npounrart o xonna nect no-
rox nnoa, necmorpx na ro uro on cocronr ns nyx ]nsnuecxnx ]anon. Komana nos-
npamaer neonpeenennoe snauenne, ecnn ne moxer ntnonnnrt onepannm urennx anntx.
uynxnnx eof nosnpamaer snauenne .ori, xora cneom sa rexyme nosnnne nnoa an-
ntx naxonrcx xonen ]ana.
Hanocneox ormernm, uro ]ynxnnx eof ncnontsyercx cpannnrentno pexo, rax xax ]ynx-
nnn Perl, npenasnauennte nx paort c ]anamn, npexpacno conpxramrcx c ycnonntmn
oneparopamn n oneparopamn nnxna a nmenno, onn nosnpamamr snauenne .ori, xora
nosnnxaer omnxa nnn xora n ]ane ne ocraercx anntx nx urennx.
3anucu asu pauuux DBM
Onepannonnax cncrema Unix noepxnnaer ouent npocro n ncnontsonannn ]opmar as
anntx DBM. B craptx nepcnxx Perl nx paort c DBM npenasnauannct ]ynxnnn rnna
dbmopen n dbmclose. Onaxo renept nx ntrecnnna ]ynxnnx tie (c ne mt nosnaxomnmcx,
xora yem nsyuart oexrno-opnenrnponannoe nporpammnponanne n Perl):
f1e nepeueuuan, uun-xnacca, cnucox-napaue1poa
Ona cesieaem x+m n xpanxmymcx na ncxe asy anntx DBM. B cneymmem npnmepe x+m
hash cnxstnaercx c ]anom data.db (pacmnpenne .db coornercrnyer asam anntx DBM n
oannxercx anromarnuecxn npn cnxstnannn nepemenno n ]ana
1
), n x+m sanocnrcx nonoe
snauenne, n n pesyntrare mt nonyuaem asy anntx c nyxno nam nn]opmanne:
use Icnf1
use ND8MI11e
f1e xhash, "ND8MI11e", dafa, f1e xhash, "ND8MI11e", dafa, f1e xhash, "ND8MI11e", dafa, f1e xhash, "ND8MI11e", dafa, O OO OkDWk| kDWk| kDWk| kDWk|O OO OCkLA1| CkLA1| CkLA1| CkLA1|O OO OLCL, 0644 LCL, 0644 LCL, 0644 LCL, 0644
$hash{d1nk} = oof bee
unf1e xhash
(Mt noxnmunnn moynt Fcntl, urot ncnontsonart cnmnonnuecxne xoncranrt rnna
_D, _EAT n r. . Mncrnuecxoe uncno 0 coornercrnyer paspemennxm na oc-
ryn ]ana onee nopono o +rom paccxastnaercx anee npn onncannn ]ynxnnn chmod.)
Kax npouecrt coepxnmoe cosanno ast anntx, paccxastnaercx n cneymmem pasene.
Hocuasua. Basi oauuix qop+ama DBM noooeprueammc ue mo.iro onepauouuo cucme+o Unix uo u,
uanpu+ep, Win32. Hpocmo ucno.isvme e+ecmo ADBMFile +oov.i (u r.acc) SDBMFile
2

Kak npoufafu qan asu pauuux qoprafa DBM
urot npounrart cosannym n npetymem pasene asy anntx data.db, nao cnxsart
+ror ]an n x+m Perl n ntnecrn annte ns x+ma:
use Icnf1
use ND8MI11e

1
3ro yrnepxenne anropa ne nnonne ncrnnno n ocraercx nennxom na ero conecrn. DBM +ro cxopee cnoco xpanennx
anntx, a ne ]opmar ]ana xonxpernt rnn DBM (BD, GDBM, NDBM, SDBM) sanncnr or nepcnn Unix. B uacrno-
crn, no ast anntx SDBM cosamrcx na ]ana, npnuem onn nonyuaer pacmnpenne pag, a pyro dir, a ]an ast
anntx GDBM noome ne nonyuaer pacmnpennx, ecnn ero ne yxasart. Hpu+e:. peo.
2
uecrno ronopx, sect anrop ntpan ne camt nyumn npnmep naexnee tno t ncnontsonart ]opmar DB (Berkley DB),
xoropt peannsonan nx ncex nnar]opm, a raxxe nosnonxer cosanart nanonee tcrpte n xomnaxrnte ast anntx. Kpome
roro, neoxonmo npeynpenrt, uro x+m maccnnon nnn x+m x+me n DBM coxpannrt ne yacrcx (xorx nexoropte nontsona-
renn ntramrcx +ro cenart) nx +ro nenn cneyer ncnontsonart moynt MLDBM. Hpu+e:. peo.
Crp.230
f1e xhash, "ND8MI11e", dafa, OkDWk, 0644 f1e xhash, "ND8MI11e", dafa, OkDWk, 0644 f1e xhash, "ND8MI11e", dafa, OkDWk, 0644 f1e xhash, "ND8MI11e", dafa, OkDWk, 0644
p1nf $hash{d1nk}
unf1e xhash
oof bee
urot paorart c asamn anntx DBM, +rnx cneenn ocrarouno. Cnxxnre asy anntx c
nepemenno Perl (x+mem), n x+m moxno yer ncnontsonart xax asy anntx. (Tem ne menee
erantnoe snaxomcrno c xnaccamn n meroamn ns moynx NDBMFile n/nnn SDBMFile npx
nn oxaxercx nnmnnm, xorx, nanpnmep, ncnontsonanne DBFile xnnxercx, xax npannno, nan-
onee naexntm, ycrounntm n pexomenyemtm noxoom.) Koneuno, moxno ncnontsonart
n ropaso onee momnte ast anntx c nosmoxnocrxmn, anexo npenocxoxmnmn DBM.
Onaxo ecnn namn sanpoct ne cnnmxom nennxn, ocrarouno ast anntx DBM.
Hocuasua. Basi oauuix qop+ama DBM noooeprueammc ue mo.iro noo onepauouuo cucme+o Unix
uo u, uanpu+ep, noo Win32. Hpocmo ucno.isvme e+ecmo ADBMFile +oov.i (u r.acc) SDBMFile.
lock - nokupoeka qana
]ynxnnx flock nosnonxer sanoxnponart ]an, saannt ecxpnnropom, nx ocryna co
cropont pyrnx nponeccon:
f1ock ecxpun1op, xo-onepauu
3ra onepannx ynpannxer pexnmom ocryna x ]any co cropont pyrnx nponeccon n, n uacr-
nocrn, nosnonxer nsexart xon]nnxron c pyrnmn nporpammamn, unrammnmn ]an, noxa n
nero sannctnamrcx nonte annte. Xorx n cncreme Unix noxnponxa nnmt pero+euove+a
onepannx, nx pyrnx onepannonntx cncrem (nanpnmep, nx Windows NT) ona oosame.iua.
Cneymmne snauennx xnnxmrcx onycrnmtmn nx xoa onepannn (urot ncnontsonart cnm-
nonnuecxne nmena, nao noxnmunrt moynt Fcntl c nomomtm xomant use Fcntl):
_SH (1) pasenxemt ocryn x ]any,
_E (2) mononontnt ocryn x ]any,
_NB (4) - conmecrno c _SH nnn _E oecneunnaer tcrpt
nosnpar, ro ecrt ]ynxnnx flock nosnpamaer ynpannenne n rouxy ntsona, ne oxnaxct,
noxa pexnm ocryna x ]any axrnnnsnpyercx (nanpnmep, npnercx nooxart pasenxe-
moro ocryna, ecnn xaxo-ro nponecc yxe saxnarnn mononontnt ocryn x ]any),
_UN (8) cnnmaer pexnm noxnponxn (ro ecrt nocne ntnonnennx +ro one-
pannn nponecc repxer nce cnon npana na ocryn x ]any).
Paspemaercx xomnnnponart necxontxo (ne xon]nnxrymmnx) pexnmon c nomomtm one-
pannn nonrnoe nnn ([).
chod - usreueuue npae pocfyna k qany
uynxnnx chmod nsmenxer npana ocryna x ]any, xpanxmemycx na nocroxnnom nocnrene.
Hanonee naxnt npana na urenne n/nnn sannct, npncnoennte romy nnn nnomy nontsonarenm:
chmod cnucox-napaue1poa
Hepntm +nemenrom cnncxa onxno trt uncno, saammee npana ocryna, xax +ro npnnx-
ro n onepannonno cncreme Unix (+ro uncno yono saanart n noctmepnunom npecran-
nennn, - nanpnmep, 0 nnn 000). Ocrantntmn +nemenramn cnncxa xnnxmrcx nmena
Crp.231
]anon. uynxnnx nosnpamaer uncno ]anon, nx xoroptx onepannx tna ycnemno.
B cneymmem npnmepe ncnontsyercx ]an file.txt. Texymne npana ocryna x nemy ycra-
nonnent n 000. A nmenno, nx +roro ]ana xomana Unix ls -l ntnonr pesyntrar:
-W------- 1 use 1 Ocf 2 11:51 f11e/fxf
C nomomtm ]ynxnnn chmod moxno nsmennrt npana ocryna na 0:
chmod 0644, f11e.fxf
-W----- 1 use 1 Ocf 2 11:51 f11e/fxf
Bosmoxno, nama onepannonnax cncrema ne noepxnnaer noctmepnunte xot npan oc-
ryna. Hanpnmep, Windows noepxnnaer rontxo rpn arpnyra ]anon: A (apxnnnt), R
(rontxo nx urennx), H (cxptrt) n S (cncremnt). 3rn arpnyrt nsmenxmrcx c nomo-
mtm xomant MS-DOS attrib nnn, n cnyuae nnrepnperaropa ActiveState Perl Ior Win32,
]ynxnn in32::File::GetAttributes n in32::File::SetAttributes.
renae - nepeureuoeauue qanoe
uynxnnx rename nosnonxer nsmenxrt nmena ]anon:
ename c1apoe-uun, uoaoe-uun
Ona nosnpamaer ucmuuv (uncno 1), ecnn ]an yanoct nepenmenonart, n .ori (uncno 0) n
npornnnom cnyuae.
glob - nouck qanoe no uanouy
uynxnnx glob nosnpamaer cnncox ]anon, nmena xoroptx coornercrnymr yxasannomy
manony (manon cneyer nonnmart n cmtcne ]anono cncremt Unix, a ne xax pery-
nxpnoe ntpaxenne Perl
1
). Hmx ]ynxnnn nponcxonr or coornercrnymme xomant Unix,
xorx c ne mentmnm ycnexom +ra ]ynxnnx Perl paoraer n nx pyrnx onepannonntx cnc-
rem
2
. Ecnn ne yxasan aprymenr, ncnontsyercx nepemennax $_:
g1ob aupaeuue
g1ob
Hanpnmep, cneymmn xo ntnonr nonnt cnncox ]anon rexymero xaranora:
p1nf o1n{"\n", g1ob {"}}
Hoocrasra. Moruo eieecmu u+eua qa.oe c.eovmuu+ oopaso+.
Wh11e {<".fxf>} { p1nf }
e.o e mo+, :mo Perl ueeuo eisieaem qvurum glob u npeepauaem uao.ou vrasauuoeo e+ecmo oecr-
punmopa qa.a, e reasuqa., romopi cocmoum us cmpor, eosepauae+ix qvurue glob. Oouaro o. mo-
eo, :mooi noumi, rar paoomaem omom mpmr, npuoemc ooeo.iuo e.vooro sa.esmi e oorv+eumaum Perl.
unlink - ypaneuue qanoe

1
Xorx n annom cnyuae +ro nourn ono n ro xe perynxpnte ntpaxennx n Unix ncnontsymrcx na yponne cranaprntx
cpecrn OC. Hpu+e:. peo.
2
K coxanennm, +ra nannnax nepa anropa ne ntepxnnaer nponepxn npaxrnxo. Hcnontsonart annym ]ynxnnm otuno ne
pexomenyercx axe n Unix, ecnn ncnontsyercx oonouxa C shell, a n Windows nnn DOS ona moxer art omnounte pe-
syntrart, ecnn rontxo ne npnnxrt cnennantnte mept. -Hpu+e:. peo.

Crp.232
uynxnnx unlink (nasnanne nponcxonr or ononmenno xomant Unix, xoropym +ra
]ynxnnx nmnrnpyer) nosnonxer yanxrt ]ant, yxasannte n xauecrne ee aprymenra.
uynxnnx nosnpamaer uncno yanenntx ]anon. Ecnn aprymenr onymen, ncnontsyercx ne-
pemennax $_:
un11nk cnucox-aunoa
un11nk
C nomomtm rpmxa, onncannoro n npetymem pasene, moxno yanxrt ]ant c nomo-
mtm manona, ananornunoro manony glob. B cneymmem npnmepe mt yanxem nce ]a-
nt c pacmnpennem .old:
p1nf De1efed , un11nk {<".o1d>}, f11es.
De1efed 9 f11es.
uynxnnx unlink ne npenasnauena nx yanennx xaranoron. nx +ro nenn cneyer ncnontso-
nart rmdir (cm. nnxe). Xorx npn ocotx ycnonnxx ]ynxnnx unlink n cnocona yanxrt xaranorn
(cm. oxymenrannm Perl), +ro moxer oxasartcx onacntm nx ]anono cncremt.
opendir - ofkpufue kafanora
uynxnnx opendir orxptnaer xaranor n cosaer ecxpnnrop, xoropt ncnontsyercx onn-
ctnaemtmn nnxe ]ynxnnxmn readdir, telldir, seekdir, rewinddir n closedir:
opend1 ecxpun1op, aupaeuue
Karanor paccmarpnnaercx xax xnasn]an, n xoropom sanncxmn cnyxar nmena ]anon n
noxaranoron. Hontsonarent moxer cunrtnart nx noono crpoxam otunoro ]ana, a
raxxe ycranannnnart nosnnnm nauana urennx c nomomtm nepeuncnenntx ntme ]ynxnn.
ecxpnnrop ]ana, nnnnnannsnponannt ]ynxnne opendir xax xaranor, ne moxer nc-
nontsonartcx ]ynxnnxmn ]anonoro nnoa/ntnoa, noxa nt ne saxpoere ero ]ynxnne
closedir n ne orxpoere xax otunt ]an ]ynxnne open. uynxnnx nosnpamaer snauenne
ucmuua, ecnn xaranor yanoct orxptrt, n .ori n npornnnom cnyuae.
closedir - sakpufue kafanora
uynxnnx closedir saxptnaer xaranor, panee orxptrt c nomomtm ]ynxnnn opendir:
c1osed1 ecxpun1op
uynxnnx nosnpamaer snauenne ucmuua, ecnn ee paora sanepmnnact ycnexom, n .ori n
npornnnom cnyuae.
readdir - feuue copepuroro kafanora
uynxnnx readdir nosnonxer nonyunrt cnncox ]anon n noxaranoron n xaranore, panee
orxptrom c nomomtm opendir:
eadd1 ecxpun1op
Ho ymonuannm nmena ]anon n noxaranoron naunnamr ntnonrtcx c nepnoro coepxame-
rocx n xaranore nmenn, onaxo c nomomtm ]ynxnn seekdir n rewinddir (cm. anee) moxno
nsmennrt nopxox ntnoa. B cnncxonom xonrexcre readdir nosnpamaer cnncox ns ncex nmen
]anon, naunnax c rexyme nosnnnn n o xonna xaranora. B cxanxpnom xonrexcre ona nos-
Crp.233
npamaer ouepenoe nmx ]ana. Ecnn cnncox nmen ncuepnan, nosnpamaercx nycro cnncox n
cnncxonom xonrexcre n neonpeenennoe snauenne n cxanxpnom xonrexcre.
Hocuasua. Ec.u ei cooupaemeci ue npocmo eieecmu no.v:euui cnucor qa.oe, a eue u npoeepumi ux
cocmouue, mo cnepea noc+ompume onucauue qvuruu readdir e oorv+eumauu Perl soeci ecmi nooeoo-
uie ra+uu, o romopix no.esuo suami.
B cneymmem npnmepe ntnonrcx cnncox nmen ]anon n rexymem xaranore:
opend1 {DlkLC1Ok, "."} o d1e "Cannof open cuenf d1ecfoy."
p1nf o1n{"/", eadd1{DlkLC1Ok}}:
c1osed1 DlkLC1Ok
. /. . /16.p1/.PL/P.PL/v.PL/W.PL
seekdir - ycfauoeka fekye nosuuuu e kafanore
uynxnnx seekdir nosnonxer nsmenxrt rexymym nosnnnm n xaranore, orxptrom c nomo-
mtm ]ynxnnn opendir:
seekd1 ecxpun1op, noauun
3nauenne napamerpa nosuu onxno trt snauennem, nosnpamaemtm ]ynxnne telldir (cm.
nnxe).
telldir - feuue fekye nosuuuu e kafanore
uynxnnx telldir nosnpamaer rexymym nosnnnm n xaranore, orxptrom c nomomtm opendir (c
+ro nosnnnn yer naunnartcx ouepenax onepannx urennx, ntnonnxemax ]ynxnne readdir):
fe11d1 ecxpun1op
reinddir - ycfauoeka fekye nosuuuu ua uaano kafanora
]ynxnnx rewinddir ycranannnnaer nosnnnm, ncnontsyemym ]ynxnne readdir, na nauano
xaranora:
eW1ndd1 ecxpun1op
chdir - creua fekyero kafanora
Bt moxere nsmennrt rexymn xaranor c nomomtm ]ynxnnn chdir:
chd1 aupaeuue
chd1
]ynxnnx chdir nsmenxer rexymn xaranor na snauenne aprymenra (rexcrono crpoxo), ecnn
+ro nosmoxno. Ecnn ne yxasan napamerp, ro rexymnm cranonnrcx omamnn xaranor. uynx-
nnx nosnpamaer snauenne ucmuua, ecnn ee paora sanepmnnact ycnexom, n .ori n npornnnom
cnyuae.
B cneymmem npnmepe rexymn xaranor nsmenxercx na ponrentcxn (oosnauaemoe xax
.. xax n DOS, rax n n Unix) n ntnonrcx cnncox ]anon, xpanxmnxcx n nem:
chd1 ".." chd1 ".." chd1 ".." chd1 ".."
opend1 {DlkLC1Ok, "."} o d1e "Cannof open d1ecfoy."
p1nf o1n {", ", eadd1{DlkLC1Ok}}
Crp.234
c1osed1 DlkLC1Ok
., .., ma11, .a11as, .cshc, .1og1n, .p1an, .pof11e
kdir - cospauue uoeoro kafanora
uynnnx mkdir cosaer nont xaranor:
mkd1 aupaeuue, npaaa-oc1yna
Hont xaranor cosaercx c nmenem, yxasanntm nepntm aprymenrom (rexcrono crpoxo), ecnn
+ro nosmoxno. Bropo napamerp saaer npana ocryna x xaranory n nne noctmepnunoro uncna
(nx ]anono cncremt Unix cm. ntme onncanne ]ynxnnn chmod). uynxnnx nosnpamaer
snauenne ucmuua, ecnn ee paora sanepmnnact ycnexom, n .ori n npornnnom cnyuae.
B cneymmem npnmepe mt cosaem nont xaranor tmp, nepexonm n nero n sannctnaem
]an:
mkd1 "fmp", 0744 mkd1 "fmp", 0744 mkd1 "fmp", 0744 mkd1 "fmp", 0744
chd1 "fmp"
open {IlLLhANDLL, ">he11o.fxf"} o d1e "Cannof open f11e he11o.fxf."
p1nf IlLLhANDLL "he11o!"
c1ose {IlLLhANDLL}
Hocuasua. Bepcu Perl for Win32 ueuopupvem emopo napa+emp, mar :mo ei +oreme oes npoo.e+ cos-
oaeami rama.oeu, paooma e Windows.
rdir - ypanufu kafanor
uynxnnx rmdir yanxer xaranor:
md1 aupaeuue
md1
Paspemaercx yanxrt rontxo nycro xaranor. Ecnn ne saano nmx xaranora, ncnontsyercx
nepemennax $_. uynxnnx nosnpamaer snauenne ucmuua, ecnn ee paora sanepmnnact ycne-
xom, n .ori n npornnnom cnyuae (n nocnenem cnyuae n nepemennym $! nomemaercx co-
omenne o omnxe).
Crp.235

Hac1u III

Rporpauuuponauue
ua PerI

Fnana 13

C1aupap1ume uopynu

Kopo1ko
B +ro rnane mt nxparne nosnaxomnmcx c nosmoxnocrxmn, npeocrannxemtmn mnorounc-
nenntmn moynxmn Perl. Moynt Perl cocronr ns xoa, xoropt nanncan n coornercrnne c
onpeenenntmn rpeonannxmn. uynxnnn, coepxamnecx n moynxx, moxno ncnontsonart
n nmo nporpamme.
Hcnonusoeauue ropyne Perl
O npannnax nanncannx moyne Perl paccxastnaercx n rnane 15. Ceuac nanonee cymecr-
nenno ro, uro onn npeocrannxmr n pacnopxxenne paspaorunxa orpomnt oem yxe na-
nncannoro n ntnonnxmmero mnoxecrno nonesntx ]ynxnn xoa. Moynn naxoxrcx n
]anax c pacmnpennem .pm n moryr sarpyxartcx n nporpammy c nomomtm xomant use:
use uoynu cnucox
use uoynu
use uoynu {}
use uoynu aepcun cnucox
use uoynu aepcun
use uoynu aepcun {}
use aepcun
Ecnn saano nmx moynx n sa nnm cneyer cnncox ]ynxnn, ro sarpyxamrcx yxasannte
]ynxnnn. Ecnn cnncox ne yxasan, sarpyxamrcx nce ]ynxnnn moynx. Haxonen, ecnn yxa-
san nycro cnncox, ro nn ona ]ynxnnx ne sarpyxaercx, xorx nmena ]ynxnn n nepemen-
ntx, yxasannte n moyne, cranonxrcx nsnecrnt nporpamme.
Ecnn nocne nmenn moynx (nnn mexy nmenem moynx n cnncxom ]ynxnn) naxonrcx uncno,
ro nepe sarpysxo moynx nponepxercx ero rexymax nepcnx (ecnn n moyne npeycmorpen no-
mep nepcnn). Ecnn nepcnx moynx, ycranonnennoro n Bame cncreme, mname nepcnn, yxasan-
Crp.236
no n xomane use, nnrepnperarop npeptnaer paory c ntaue coomennx o omnxe.
Haxonen, ecnn nepnt nepeannt xomane napamerp uncno, ro Perl nponepxer rexy-
mym nepcnm nnrepnperaropa. Ecnn ona mentme saannoro uncna, ro nnrepnperarop npe-
xpamaer paory c ntaue coomennx o omnxe. Bor npnmep, n xoropom mt sarpyxaem
nce ]ynxnnn ns moynx SaIe:
use use use use 5afe 5afe 5afe 5afe
$safecompafmenf = neW 5afe
$safecompafmenf->pem1f{qW{p1nf}}
$esu1f = $safecompafmenf->eva1{"p1nf \"he11o!\""}
he11o!
A nor rax ns moynx POSIX sarpyxaercx rontxo ]ynxnnx strtime:
use use use use PO5l sfff1me PO5l sfff1me PO5l sfff1me PO5l sfff1me
p1nf sfff1me "hee 1s fhe dafe: xd/xm/x\n", 1oca1f1me
hee 1s fhe dafe: 30/10/1999
Hocuasua. Hauoo.ee npocmo cnocoo npeoopasoeami cnucor u+eu qvuru e cnucor cmpor ~ omo sar.m-
:umi ux e nceeooraei:ru qw/.../ (c+. mao.. 23 e e.aee 2). To ecmi roucmpvru use POSIX qw/strftime asc-
time/ oreuea.eumua roucmpvruu use POSIX ('strftime', 'asctime') (oopamume euu+auue ua omcvmcmeue
sanmix euvmpu roucmpvruu qw/.../).
Hnora nx cocranntx nmen, nxnmuammnx nmx naxera n nmx moynx, raxxe npnxonrcx
ncnontsonart pasennrent :: (ro ecrt xora rpeyercx nomoynt moynx). Hanpnmep, n
cneymmem cnyuae mt ncnontsyem nomoynt Copy moynx File, urot cxonnponart
]an no nontm nmenem:
use use use use I11e::Copy I11e::Copy I11e::Copy I11e::Copy
copy {"f11e.fxf", "f11e2.fxf"}
Hocuasua. Perl sa+euem pasoe.ume.i :: ua /, mar :mo u+ File::Copy, mo ecmi File/Copy, osua:aem,
:mo +i uue+ qa. Copy.pm e noorama.oee File rama.oea ouo.uomeru +oov.e.
Kpome sarpysxn moyne xomana use opaartnaer raxxe nparmt, ro ecrt cnennantnte
npexrnnt nx xomnnnxropa Perl. Mt yxe ncrpeuannct c nexoroptmn ns nnx, nanpn-
mep, ecnn nt nomecrnre xomany use strict vars n nporpammy, Perl yer rpeonart o-
xnnennx ncex rnoantntx nepemenntx.
B Perl nmeercx mnoxecrno cranaprntx moyne, n mt conpaemcx cenart n +ro rnane
xparxn osop nanonee nonynxpntx. Cxontxo moyne nxnmuaercx n nocranxy Perl
Bsrnxnnre na ran. 13.1.
Homnmo ocnonntx moyne Perl, nepeuncnenntx n ran. 13.1, n annom pasene npnno-
nrcx cnncox ocnonntx nparm, oomenntx n ran. 13.2. Hmena nparm ncnontsymr
crpounte yxnt, rora xax nmena moyne naunnamrcx c sarnanno yxnt.
Kpome cranaprntx moyne, nocrannxemtx c Perl, mnoxecrno ononnnrentntx moyne
moxno nonyunrt uepes CPAN n pyrne apxnnt (nanpnmep, ysen xomnannn ActiveState). 3rn
moynn oxnartnamr nonnt nanason saau or cerentx onepann o paort c nnrep]ec-
ntmn nnnorexamn rnna Tk. Bt moxere cxonnponart n ycranonnrt +rn moynn camocrox-
rentno nnn xe ncnontsonart nncrpymenr, noont Perl Package Manager (PPM), ro ecrt
roront cnenapn Perl nx ycranonxn naxeron
1
(on nxonr n cocran naxera ActivePerl). Bor, x
npnmepy, xaxne xomant nt moxere ncnontsonart c PPM nocne coennennx c Hnrepnerom:
help ntnecrn cnpanxy,

1
B Unix nx ycranonxn moyne otuno ncnontsyercx moynt CPAN. Hpu+e:. peo.
Crp.237
install naremi sarpysnrt n ycranonnrt yxasannte naxert,
query nonyunrt nn]opmannm o ycranonnenntx naxerax,
quit ntxo ns PPM,
remove naremi yanxer yxasannte naxert,
search nonyunrt nn]opmannm o ocrynntx (nx sarpysxn n ycranonxn) naxerax,
set saart n oropasnrt rexymne ycranonxn,
verify nponepnrt, coornercrnymr nn ycranonnennte naxert camo nocnene nepcnn.
Hanpnmep, urot sarpysnrt n ycranonnrt nonynxpnt moynt Tk, nao coennnrtcx c
Hnrepnerom, sanycrnrt PPM, a sarem nnecrn xomany install Tk. 3ra nponeypa cnntno
ynpomaer nponecc, xorx o nponemax, cnxsanntx c PPM, n nocnxmenntx Perl xon]epen-
nnxx UseNet nyr ecxoneunte cnopt.
Moynt Tk noepxnnaer ntson nponeyp nnnorexn Tk ns Perl n nosnonxer ncnontsonart
n nporpammax nnsyantnt rpa]nuecxn nnrep]ec. Tax xax +ror moynt cran cront nonynx-
pen cpen nporpammncron na Perl, mt paccmorpnm ero uyrt onee nopono (xorx on n ue
nxonr n cranaprnym nnnorexy moyne), npoemoncrpnponan necxontxo npnmepon, xax
oropasnrt oxno c xnonxamn, xnonxamn c sanncnmo ]nxcanne (radiobuttons) n xnonxamn c
nesanncnmo ]nxcanne (checkbox), menm, cnncxamn n pyrnmn nnrep]ecntmn +nemenra-
mn rpa]nuecxo nnnorexn Tk. (Oparnre nnnmanne, uro nnrep]ecnte +nemenrt nnno-
rexn Tk moryr ntrnxert no-pasnomy n pasnnuntx onepannonntx cncremax.)
Ha +rom saxonunm c nneennem. Tenept mt roront npncrynnrt x osopy moyne Perl.
1abnua 13.1. 1abnua 13.1. 1abnua 13.1. 1abnua 13.1. C1auap1uue uoynu Pe1
oynu hpeuaauaueuue hpeuaauaueuue hpeuaauaueuue hpeuaauaueuue
AnyD8MI11e Cpea nn paanuuuux baa auuux D8M
AufoLoade 3arpyaxa yuxuu no 1peboaauuk
Aufo5p11f aabuaxa naxe1a nn obnerueuun peuua aa1oaarpyaxu {aufo1oad1ng}
8enchmak 1ec1u cxopoc1u xoa ua 1ane aunonueuun
CPAN u1epeuc x apxuay CN {Compehens1ve Pe1 Ach1ve NefWok}
CPAN::I1sf11me Coaae1 xouurypauouuuu aun CPAN
CPAN::Nox 3anycx uoyneu CPAN bea ucnonuaoaauun cxoununupoaauuux aunoa {o1bop no
pacupeuuk auna}
Cap hpeynpeeuun ob oubxax
C1ass::5fucf Coaauue c1pyx1ypuux 1unoa auuux, noobuux auuuu nauxa C
Conf1g uopuaun o xouurypauu Pe1
CWd hy1u, coo1ae1c1ayk|uu 1exy|euy {paboueuy} xa1anory
D8I11e Onepauu c baaauu auuux D8M opua1a 8ek1ey D8
Deve1::5e1f5fubbe Coaauue aarnyex nn uoyneu c peuuou aa1oaarpyaxu
D1hand1e e1ou pabo1u c ecxpun1opauu xa1anoroa
DynaLoade 3arpyaxa bubnuo1ex nauxa C
Lng11sh cnonuaoaauue aurnuucxux uueu nn cneuanuuux nepeueuuux Pe1
Lnv oc1yn x auaueuunu nepeueuuux cpeu
Lxpofe e1o uunop1upoaauun uoyneu, ucnonuayeuuu no yuonuauuk
Lxfuf11s::Lmbed 8uepeuue Pe1 a npunoeuun, uanucauuue ua C/C++
Lxfuf11s::1nsfa11 xc1auoaxa aunoa
Lxfuf11s::L1b11sf honyueuue bubnuo1ex nn ux ucnonuaoaauun
Lxfuf11s::MMO52 3aueua noaeeuun uoynn Lxfuf11s::MakeMake, 1unuuuoro nn un1x, ua no-
aeeuue, 1unuuuoe nn O5/2
Lxfuf11s::MMun1x xc1auaanuaae1 noaeeuue uoynn Lxfuf11s::MakeMake, 1unuuuoe nn un1x
Lxfuf11s::MMvM5 3aueua noaeeuun uoynn Lxfuf11s::MakeMake, 1unuuuoro nn un1x, ua no-
aeeuue, 1unuuuoe nn vM5
Lxfuf11s::MakeMake Coaauue pacupeuuoro makef11e
Lxfuf11s::Man1fesf 3anucu auna c onucauueu naxe1a
Lxfuf11s::Mkboofsfap Coaauue auna aa1oaarpyaxu, ucnonuayeuoro uoyneu DynaLoade
Lxfuf11s::Mksym11sfs 3anucu auna c napaue1pauu pabo1u nn peax1opa canaeu
Crp.238
Lxfuf11s::fesf11b obaanne1 xa1anoru x nepeueuuou 0 00 0l ll lNC NC NC NC
Iafa1 Oubxu cuu1ak1cn xpu1uuecxuuu
Icnf1 3arpyaxa aaronoaouuoro auna nauxa C Icnf1 Icnf1 Icnf1 Icnf1. .. .h hh h, coepa|ero cuuaonuuecxue
uueua nn xouc1au1, ucnonuayeuux yuxunuu Pe1
I11e::8asename Cuu1axcuuecxuu paabop nonuoro uueuu auna
I11e::Check1ee 1ec1upoaauue aunoa no nonuouy epeay xa1anoroa
I11e::Compae Cpaaueuue aunoa
I11e::Copy onupoaauue aunoa
I11e::I1nd houcx aunoa no nonuouy epeay xa1anoroa
I11e::Pafh Coaauue u yaneuue xa1anoroa
I11e::sfaf u1epeuc nn yuxuu sfaf{}
I11eCache hoaaonne1 ouoapeueuuo o1xpu1u bonue aunoa
I11ehand1e e1ou nn pabo1u c ecxpun1opauu aunoa
I1nd81n houcx xa1anora, re uaxou1cn ceuapuu Pe1
GD8MI11e yuxuu u xnaccu nn pabo1u c baaauu auuux D8M {bubnuo1exa GD8M}
Gefopf::Long Obpabo1xa napaue1poa xouauuou c1poxu
Gefopf::5fd Obpabo1xa ouobyxaeuuux napaue1poa xouauuou c1poxu
l1N::Co11afe Cpaaueuue -bu1uux cxannpuux auuux a coo1ae1c1auu c uauouanuuuuu c1au-
ap1auu
lO 3arpyaxa uoyneu aaoa/auaoa
lO::I11e e1ou nn pabo1u c ecxpun1opauu aunoa
lO::hand1e e1ou nn pabo1u c ecxpun1opauu aaoa/auaoa
lO::P1pe e1ou nn pabo1u c xauanauu
lO::5eekab1e e1ou nn pabo1u c obex1auu aaoa/auaoa
lO::5e1ecf 8ubop cuc1euuux auaoaoa
lO::5ockef Obueu auuuuu uepea coxe1u
lPC::Open2 O1xpuaae1 npoecc ua u1euue u aanucu {ouoapeueuuo}
lPC::Open3 O1xpuaae1 npoecc ua u1euue, aanucu u obpabo1xy oubox {ouoapeueuuo}
Mafh::81gI1oaf Coaauue uucen c nnaaak|eu 1ouxou npouaaonuuou nuuu
Mafh::81g1nf Coaauue enux uucen npouaaonuuou nuuu
Mafh::Comp1ex abo1a c xounnexcuuuu uucnauu
Mafh::11g u1epeuc Mafh::Comp1ex nn 1purououe1puuecxux yuxuu
ND8MI11e yuxuu u xnaccu nn pabo1u c baaauu auuux D8M {bubnuo1exa ND8M}
Nef::P1ng O1npaaxa lCMP-naxe1oa ua yaneuuuu xounuk1ep
Nef::hosfenf u1epeuc x yuxunu gefhosf gefhosf gefhosf gefhosf" "" "
Nef::nefenf u1epeuc x yuxunu gefnef gefnef gefnef gefnef" "" "
Nef::pofenf u1epeuc x yuxunu gefpofo gefpofo gefpofo gefpofo" "" "
Nef::sevenf u1epeuc x yuxunu gefsev gefsev gefsev gefsev" "" "
Opcode 3anpe|ae1 uueuoaauuue nceaoxou
Pod::1exf hpeobpaaye1 oxyueu1auk a opua1e POD {P1a1n O1d Documenfaf1on} a op-
ua1upoaauuuu 1exc1 A5Cll
PO5l u1epeuc x yuxunu PO5l {c1auap1 lLLL 1003.1}
5D8MI11e yuxuu u xnaccu nn pabo1u c baaauu auuux D8M {bubnuo1exa 5D8M}
5afe 8unonueuue xoa a aa|u|euuou paaene
5each::D1cf houcx xnkua a cnoaape {auanor x-1abnuu}
5e1ecf5ave Coxpaueuue u aocc1auoaneuue ecxpun1opoa aunoa
5e1fLoade 3arpyaxa yuxuu ucxnkuu1enuuo no 1peboaauuk
5he11 8unonueuue xouau xouauuou obonouxu
5ockef 3arpyaxa auna nauxa C sockef sockef sockef sockef. .. .h hh h c cuuaonuuecxuuu uueuauu nn xouc1au1,
ucnonuayeuux yuxunuu obueua auuuuu uepea ce1u
5ymbo1 auunynupoaauue cuuaonauu Pe1
5ys:hosfname hoaaonne1 nonyuu1u uun xounuk1epa
5ys::5ys1og u1epeuc x auaoaau cuc1euuou yuxuu sys1og sys1og sys1og sys1og{3} {3} {3} {3}
1em::Cap u1epeuc ynpaaneuun 1epuuuanou
1em::Comp1efe u1epax1uauoe aanonueuue cnoa auuuuu
1em::keadL1ne u1epeuc nn naxe1a ead11ne
1esf::haness 8unonune1 ceuapuu Pe1 u aanucuaae1 c1a1uc1uxy
1exf::Abbev Coaae1 1abnuy coxpa|euuu
1exf::PaseWods Cuu1axcuuecxuu paabop 1exc1a
1exf::5oundex nropu1u 5oundex
Crp.239
1exf::1abs 3aueune1 1abynnuu ua nocneoaa1enuuoc1u npobenoa, u uaobopo1
1exf::Wap hepeuoc nuuuux c1pox
11e::hash Onpeeneuun nn canaauuux xeu
11e::kefhash Onpeeneuun nn canaauuux xeu co ccunxauu ua xnkuu
11e::5ca1a Onpeeneuun nn canaauuux cxannpuux nepeueuuux
11e::5ubsfhash Coaae1 1abnuy uxcupoaauuoro paauepa c anropu1uou xupoaauun xnkueu,
1axe uuek|ux uxcupoaauuuu paauep
11me::Loca1 8oaapa|ae1 apeun a coo1ae1c1auu c noxanuuuuu yc1auoaxauu u no puuauu-
cxouy uepuuauy
11me::gmf1me u1epeuc nn yuxuu gmf1me
11me::1oca1f1me u1epeuc nn yuxuu 1oca1f1me
11me::fm cnonuaye1cn uoynnuu 11me::gmf1me u 11me::1oca1f1me
uNlvLk5AL aaoauu xnacc nn acex xnaccoa
use::genf u1epeuc x yuxunu gefg gefg gefg gefg" "" "
use::pWenf u1epeuc x yuxunu gefpW gefpW gefpW gefpW" "" "

1abnua 13.2. 1abnua 13.2. 1abnua 13.2. 1abnua 13.2. C1auap1uue nparuu Pe1
hparua hparua hparua hparua hpeuaauaueuue hpeuaauaueuue hpeuaauaueuue hpeuaauaueuue
b11b cnonuaoaa1u euuc1annupoaauuyk MakeMake aepcuk naxe1a
d1agnosf1cs cnonuaoaa1u pacupeuuyk uaruoc1uxy npeynpeak|ux coob|euuu
1nfege cnonuaoaa1u enouucneuuyk apuue1uxy
1ess 3anpauaae1 y xoununn1opa uuuuuanuuyk peanuaauk cuu1axcuuecxou xouc1pyxuu
11b xnpaanne1 cnucxou ny1eu, a xo1opux Pe1 bye1 ucxa1u ceuapuu
1oca1e 3aae1 pabo1y yuxuu, uyac1au1enuuux x noxanuuuu uac1pouxauu, c 1exy|uuu ua-
c1pouxauu 1oca1e
ops Orpauuueuuoe ucnonuaoaauue uueuoaauuux nceaoxooa
ove1oad heperpyaxa onepauu Pe1
e aueune1 pabo1y perynnpuux aupaeuuu Pe1
s1gfap aapeae1 obpabo1xy {nepexaa1} curuanoa
sf1cf 3aae1 c1poryk npoaepxy ue anonue beaonacuux nporpauuuux xouc1pyxuu
subs 8uuyae1 exnapupoaa1u nonporpauuu
vms1sh cnonuaoaa1u noaeeuue, cneuuuuoe nn onepauouuou cuc1euu vM5
vas 8uuyae1 npeaapu1enuuoe exnapupoaauue uueu rnobanuuux nepeueuuux

Henocpepc1neuume pemeuun
Ter::Cap - paofa c fepruuanor
Moynt Term npenasnauen nx paort c repmnnanom. Hanpnmep, c nomomtm ]ynxnnn Tgoto
moxno nepemecrnrt xypcop n nmym rouxy. B cneymmem npnmepe on nepememaercx n crpoxy
5, xononxy 0, re n ntnonrcx cnono Perl. nx +roro nam norpeymrcx ]ynxnnn ns moyne
POSIX n Term (]ynxnnx moynx POSIX onpeenxer cxopocrt ntnoa na repmnnan).
Hepeem x npnmepy. Cnauana cosaercx oexr $termcap, coornercrnymmn repmnnany:
use PO5l
use 1em
$fem1os = PO5l::1em1os->neW
$fem1os->gefaff
$speed = $fem1os-gefospeed
$femcap = 1em::Cap $femcap = 1em::Cap $femcap = 1em::Cap $femcap = 1em::Cap- -- ->1gefenf {{1em => undef, O5PLLD >1gefenf {{1em => undef, O5PLLD >1gefenf {{1em => undef, O5PLLD >1gefenf {{1em => undef, O5PLLD => => => => $speed}} $speed}} $speed}} $speed}}
Tenept $termcap ncnontsyercx nx ouncrxn +xpana c nomomtm meroa Tputs n nepeme-
mennx xypcopa n nyxnoe mecro c nomomtm meroa Tgoto:
use PO5l
use 1em
Crp.240
$fem1os = PO5l::1em1os->neW
$fem1os->gefaff
$speed = $fem1os-gefospeed
$femcap = 1em::Cap->1gefenf {{1em => undef, O5PLLO => $speed}}
$femcap $femcap $femcap $femcap- -- ->1pufs{c1, 1, "51DOu1} >1pufs{c1, 1, "51DOu1} >1pufs{c1, 1, "51DOu1} >1pufs{c1, 1, "51DOu1}
$femcap $femcap $femcap $femcap- -- ->1gofo{cm, 40, 5, "51DOu1} >1gofo{cm, 40, 5, "51DOu1} >1gofo{cm, 40, 5, "51DOu1} >1gofo{cm, 40, 5, "51DOu1}
p1nf "Pe1\n"
Hrax, mt nonyunnn, uro rpeonanoct, xypcop nonan n rouxy (5,0).
Hocuasua. Jmom ceuapu ovoem paoomami mo.iro o. mep+uua.oe u.u o+v.mopoe mep+uua.oe, noo-
oeprueamuux vnpae.muue escape-noc.eooeame.iuocmu. . orou DOS, paoomamuux noo vnpae.euue+
Windows, ou paoomami ue ovoem.
Math - kornnekcuue ucna u ucna onuuo paspnpuocfu
Moynt Math cnyxnr nx ntnonnennx maremarnuecxnx onepann c xomnnexcntmn unc-
namn nnn uncnamn c ontmnm uncnom paspxon. B cneymmem npnmepe cosamrcx na
xomnnexcntx uncna (ro ecrt uncna, cocroxmnx ns nemecrnenno n mnnmo uacrn), sarem
onn cxnatnamrcx, a pesyntrar ntnonrcx na +xpan:
use Mafh::Comp1ex
$opeand1 = Mafh::Comp1ex->neW{1, 2}
$opeand2 = Mafh::Comp1ex->neW{3, 4}
$sum = $opeand1 + $opeand2
p1nf "5um = $sum\n"
5um = 4"61
A nor xax ncnontsyercx naxer BigInt, nosnonxmmn paorart c ontmnmn nentmn uncna-
mn (naxer BigFloat, nosnonxmmn paorart c ontmnmn uncnamn c nnanamme rouxo,
raxxe cymecrnyer). B cneymmem npnmepe cxnatnamrcx na ontmnx nentx uncna:
use Mafh::81glnf
$1nf1 = 1234567912345679
$1nf2 = 9765432197654321
p1nf "5um of 1nfeges = ", $1nf1 + $1nf2, "\n"
5um of 1nfeges = 1.11111111111111111ex1
Hocxontxy Perl ne paoraer c ontmnmn uncnamn nanpxmym, on npeopasyer nx n uncna c
nnanamme rouxo. Bor xax nonyunrt pacneuarxy pesyntrara n ]opmare nenoro uncna:
use Mafh::81glnf
$b1nf1 = Mafh::81glnf->neW{1234567912345679}
$b1nf2 = Mafh::81glnf->neW{9765432197654321}
p1nf "5um of 1nfeges = ", $b1nf1->badd{$b1nf2}, "\n"
5um of 1nfeges = 1111111111111111110
POS - qyukuuu Portable Operating Syste nterace
Kax mt yxe snaem ns npetymnx rnan, Haoparopnx xomntmrepntx cncrem Hannonant-
noro nncrnryra cranapron n rexnonorn (the National Institute oI Standards and Technol-
ogy Computer Systems Laboratory - NIST/CSL) n copyxecrne c pyrnmn oprannsannxmn
cosana cranapr POSIX - Portable Operating System InterIace. POSIX - +ro ontmax n-
nnorexa cranaprnsnponanntx C-noontx ]ynxnn, noxptnammne cranaprnte no-
rpenocrn nporpammnponannx.
Moynt Perl POSIX oecneunnaer ocryn nourn xo ncem ]ynxnnxm, cranaprnsnponanntm n
Crp.241
POSIX nepcnn 1003.1. Moynt POSIX noxnmuaercx x nporpamme xomano use:
use PO5l # obaau1u ack bubnuo1exy
use PO5l yuxun # obaau1u ouy yuxuk
use PO5l qW{yu1 yu2} # obaau1u uecxonuxo yuxuu
(Hcenoxantuxn qw/.../ (cm. ran. 2.3 n rnane 2), ncnontsonannte n rpertem napnanre, xn-
nxmrcx camtm npocrtm cnocoom cosart cnncox ns crpox-nmen ]ynxnn, saxnmuenntx
n xantuxn.)
Hanpnmep, nor rax ncnontsyercx ]ynxnnx strftime moynx POSIX, ]opmarnpymmax ary:
use PO5l sfff1me use PO5l sfff1me use PO5l sfff1me use PO5l sfff1me
p1nf sfff1me "hees fhe dafe: xd/xm/x\n", 1oca1f1me
hees fhe dafe: 30/10/1999
Ecnn nt eme ne snaere, xaxne ]ynxnnn coepxarcx n moyne POSIX, nmeer cmtcn norpa-
rnrt nexoropoe npemx, urot osnaxomnrtcx c nnmn. Bo mnornx cnyuaxx onn axe menee
nnar]opmosanncnmt, uem cam Perl.
Benchark - fecfupoeauue npouseopufenuuocfu
Moynt Benchmark moxer cnyxnrt nx onpeenennx nponsnonrentnocrn nporpammt, no-
ono cexynomepy, nxnmuaemomy npn crapre nporpammt n ntxnmuaemomy npn ee oxonua-
nnn. (B mnorosaauntx cncremax n nrpy nnora ncrynamr nocroponnne ]axropt, nanpnmep
ro, xax nnannponmnx ornecercx x nporpamme. B pesyntrare nsmepennoe npemx moxer oxa-
sartcx ontme ncrnnnoro npemenn ee paort.) B cneymmem npnmepe c nomomtm oexron
moynx Benchmark nsmepxercx, cxontxo npemenn sannmaer mnnnnon nrepann nnxna:
use 8enchmak
$f1mesfamp1 = neW 8enchmak
fo {$1oop1ndex= 0 $1oop1ndex < 1000000 $1oop1ndex++}
{ $va1ab1e1 = 1 }
$f1mesfamp2 = neW 8enchmak
$f1med1ffeence = f1med1ff{$f1mesfamp2, $f1mesfamp1}
p1nf "1he 1oop fook ", f1mesf{$f1med1ffeence}
1he 1oop fook 5 Wa11c1ock sees { 5.65 us x 0.00 sys = 5.65 CPu}
Tie - usrepeuue epereuu u npeopasoeauue qoprafoe epereuu
Moynt Time nosnonxer npeopasontnart noxantnoe npemx nnn npemx no Ipnnnnucxomy
mepnnany n cexynt, orcunrtnaemte or nacrynnennx +noxn (nx Unix n ontmnncrna
pyrnx onepannonntx cncrem nauano +noxn orcunrtnaercx or 01.01.1970). Hanpnmep, n
cneymmem ]parmenre xoa c nomomtm nomoynx Tim::Local ntuncnxercx xonnuecrno
cexyn or 01.01.1970 o 01.01.2000:
use 11me::Loca1
p1nf f1me1oca1{0, 0, 0, 1, 1, 2000}
94931200
Carp - cooeuun o ouukax c ykasauuer foku eusoea
Komant warn n die coomamr rontxo o rexymem mecre nosnnxnonennx omnxn-
Komant moynx Carp oprannsonant rax, uro n coomennn o omnxe yxastnaercx rouxa
Crp.242
ntsona ]ynxnnn. Tem camtm cosannte c nx nomomtm moynn yyr necrn cex xax
ncrpoennte ]ynxnnn, coomax o omnxe nontsonarenx, ntsnanmero nonporpammy c
nenepntmn napamerpamn, a ne o omnxe cosanmero ee nporpammncra. pyrnmn cno-
namn, nporpammncrt, ncnontsymmne nanncannte namn moynn, yyr nnert ne nomep
crpouxn nnyrpn name nonporpammt, a nomep crpouxn n cnoem ncxonom xoe, uro o-
nee naxno nx nx paort. Bor npnmep ncnontsonannx xoman ns moynx Carp:
use Cap
cap "1h1s 1s a Wan1ng!"
coak "1h1s 1s an eo!"
confess "5o 1ong!"
(Hepnax xomana ntnonr npeynpexammee coomenne n npoonxaer ntnonnenne xo-
a. Bropax xomana ntnonr coomenne o omnxe n npexpamaer ntnonnenne nporpam-
mt. Tpertx neuaraer coomenne o omnxe n crex ntsonon ]ynxnn, nocne uero npexpa-
maer ntnonnenne nporpammt.)
locale -- ucnonusoeauue nokanuuux uacfpoek kornukfepa
Moynt locale +ro na camom ene nparma, xoropax paspemaer uyncrnnrentntm x no-
xantntm nacrpoxam nonporpammam ns moynx POSIX
1
ncnontsonart +ry nn]opmannm.
Hparma nnnxer na raxne momenrt, xax ntop rouxn nnn sanxro npn ntnoe uncen c nna-
namme rouxo, cranaprnt ]opmar art n r. . urot orxnmunrt nnnxnne noxantntx
nacrpoex, ncnontsyre xomany no locale. B cneymmem npnmepe maccnn coprnpyercx n
coornercrnnn c noxantntmn nacrpoxamn:
use 1oca1e
0sofed = sof 0unsofed
File - onepauuu c qanaru
Moynt File oecneunnaer noepxxy paort c ]anamn. Hanpnmep, nomoynt
File::Copy coepxnr ne orcyrcrnymmne n cranaprno nnnorexe Perl ]ynxnnn: copy
(nx xonnponannx ]anon) n move (nx nepememennx ]ana n nont xaranor). B cne-
ymmem npnmepe ]an Iile.txt xonnpyercx n Iile2.txt, a ror nepememaercx n xaranor, pac-
nonoxennt na yponent ntme rexymero:
use I11e::Copy
copy { copy { copy { copy {"f11e.fxf", "f11e2.fxf"} "f11e.fxf", "f11e2.fxf"} "f11e.fxf", "f11e2.fxf"} "f11e.fxf", "f11e2.fxf"}
move move move move {" {" {" {"f11e f11e f11e f11e2. 2. 2. 2.fxf fxf fxf fxf", ".."} ", ".."} ", ".."} ", ".."}
Net - pocfyn k cefu Hufepuef
Moynt Net coepxnr nonporpammt nx paort c certm n yanenntmn xomntmrepamn.
B cneymmem npnmepe moynt Net::Ping ncnontsyercx nx nponepxn coennennx c ya-
nenntm xomntmrepom. Cnauana cosaercx oexr pingobect, xoropt meroom ping y-

1
Ha camom ene, .moi+ nonporpammam n ]ynxnnxm Perl, uyncrnnrentntm x noxantntm nacrpoxam (n uacrnocrn, +ro orno-
cnrcx x onepannxm coprnponxn, perynxpntm ntpaxennxm n r. .). B Windows noxantnte nacrpoxn saamrcx c nomomtm
]ynxnnn setlocale. B Unix noxant otuno ycranannnnaercx na cncremnom yponne, no moxno saart ee n n nporpamme. Hpu-
+e:. peo.

Crp.243
er ornpannxrt recronte naxert yxasannomy xomntmrepy Hpn +rom moxno ncnontsonart
pasnnunte nporoxont, nanpnmep TCP, ICMP nnn UDP. Btson meroa new, cosammero
oexr, nmeer ]opmar:
$p1ngobecf = NeW::P1ng->neW{|npo1oxon |, apeun-ouauun |, uucno-bau1oa]]]}
Bce napamerpt xnnxmrcx neoxsarentntmn:
nporoxon moxer npnnnmart snauennx tcp, udp nnn icmp (no ymonuannm ncnont-
syercx udp);
npemx oxnannx saaercx n cexynax;
uncno-ar saaer pasmep naxera, noctnaemoro yanennomy xomntmrepy (maxcnmym
panen 102).
Btson meroa, ntnonnxmmero ornpanxy naxera yanennomy xomntmrepy, nmeer neoxsa-
rentnt napamerp, saammn npemx oxnannx ornera:
$p1ngobecf->p1ng{uun-xounuk1epa |, apeun-ouauun]}
Ho oxonuannn paort oexr saxptnaercx c nomomtm meroa close. cneymmem npn-
mepe emoncrpnpymrcx nosmoxnocrn moynx Net::Ping:
use Nef::P1ng
$p1ngobecf = Nef::P1ng->neW{1cmp}
1f {$p1ngobecf->p1ng{youseve.com}}
{ p1nf "Cou1d each hosf." }
$p1ngobecf->c1ose{}
Cou1d each hosf.
Sae - esonacuoe eunonueuue kopa
Moynt SaIe nosnonxer ntnonnxrt xo n esonacnom pexnme sa cuer cosannx ocooro
pasena, ]ynxnnonnpymmero nesanncnmo or ocrantno uacrn nporpammt. Bt moxere
yxasart, xaxne ]ynxnnn paspemeno ntstnart n +rom pasene, c nomomtm meroa permit.
Hocxontxy n name npemx cexpernocrn yenxercx ontmoe nnnmanne, moynt SaIe nectma
paspoccx n nxnmuaer mnoxecrno ncrpoenntx meroon. B cneymmem npnmepe mt cosaem
nont pasen nx ntnonnennx xoa, paspemaem ncnontsonart n nem xomany print, a sa-
rem ntnonnnm nexoropt xo, ncnontsyx mero reval moynx SaIe:
use 5afe
$safecompafmenf = neW 5afe
$safecompafmenf->pem1f{p1nf}
$esu1f = $safecompafmenf->eva1{"p1nf \"he11o!\""}
he11o!
Tk - cpepcfea paofu c unuofeko Tk
Bsanmoecrnne mexy nnrepnperaropom Perl n rpa]nuecxo nnnorexo Tk xnnxercx
ouent nonynxpno remo, no+romy n name xnnre mt noxaxem necxontxo npnmepon roro,
xax ]ynxnnn Tk ntstnamrcx ns Perl. Ecnn nt nmeere ontr paort c Tcl/Tk, ontmax
uacrt xoa npnnonmtx npnmepon yer nx nac ouennno. (Ecnn ner, ro oxymenrannx
Perl/Tk nomoxer nam pasopartcx.) Fasont nponecc ouent npocr: moynt Tk noxnmua-
ercx c nomomtm xomant use Tk, cosaercx rnannoe oxno, sarem cosamrcx n ynaxont-
namrcx n nne oexron neoxonmte nnrep]ecnte +nemenrt Tk. Hpn nx cosannn yxa-
Crp.244
stnamrcx neoxonmte napamerpt. Hocne nnnnnannsannn +nemenron ntstnaercx ]ynx-
nnx Mainoop. Ona nepeaer xonrpont na nnrep]ecntmn oexramn moynm Tk. B pe-
syntrare noxnnxercx oxno c nnrep]ecntmn +nemenramn.
B necxontxnx nocneymmnx pasenax yyr npnneent npnmept paort c moynem Tk.
urot pasopartcx c nnmn, unrarenm norpeyercx nexoropt ontr nporpammnponannx
nnnorexn Tk.
Tk: kuonku u fekcfoeue uufepqecuue snereufu
Haunem paccmorpenne npnmepon ncnontsonannx ]ynxnn Tk n Perl c nporpammt, xoropax
cosaer oxno n nomemaer n nero xnonxy n rexcront +nemenr. Kora nt menxaere no xnonxe,
nporpamma ntnonr rexcr Hello! n rexcronom +nemenre. Pesyntrar noxasan na pnc. 13.1.
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$fopW1ndoW->Labe1{-fexf => 8uffon and fexf W1dgef examp1e}->pack{}
$fopW1ndoW->8uffon{-fexf => "C11ck Me!", -command => \d1sp1ay }->pack{-s1de => "1eff"}
$fexf1 = $fopW1ndoW->1exf{-W1dfh => 40, -he1ghf => 2}->pack{}
$fexf1->b1nd{<Doub1e-1>, \d1sp1ay}
sub d1sp1ay
{
$fexf1->1nsef{end, "he11o!"}
}
Ma1nLoop uc. 13.1. uc. 13.1. uc. 13.1. uc. 13.1. O1obpaeuue xuonxu 1k u 1exc1oaoro neueu1a
Tk: kuonku c saeucuro u uesaeucuro qukcauue
annt npnmep noxastnaer, xax ntnecrn xnonxn c sanncnmo (radiobuttons) nesanncnmo
(checkbuttons) ]nxcanne, a raxxe onpeennrt, xaxne nmenno xnonxn ntpant nontsona-
renem. Pesyntrar noxasan na pnc. 13.2.
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$fopW1ndoW->Labe1{-fexf => kad1o- and checkbuffon W1dgef examp1e }->pack{}
$fopW1ndoW->kad1obuffon{-fexf => "kad1o 1", -va1ue => "1", -command => sub
{ $fexf1->de1efe{1.0, end}
$fexf1->1nsef{end, "ou c11cked ad1o 1"} } }->pack{}
$fopW1ndoW->kad1obuffon{-fexf => "kad1o 2", -va1ue => "0", -command => sub
{ $fexf1->de1efe{1.0, end }
$fexf1->1nsef{end, "ou c11cked ad1o 2"} } }->pack{}
$fopW1ndoW->Checkbuffon{-fexf => "Check 1", -command => sub
{ $fexf1->de1efe{1.0, end}
$fexf1->1nsef{end, "ou c11cked check 1"} } }->pack{}
$fopW1ndoW->Checkbuffon{-fexf => "Check 2", -command => sub
{ $fexf1->de1efe{1.0 , end}
$fexf1->1nsef{end, "ou c11cked check 2"} } }->pack{}
$fexf1 = $fopW1ndoW->1exf{-W1dfh => 40, -he1ghf => 2}->pack{}
Ma1nLoop





uc. 13.2. uc. 13.2. uc. 13.2. uc. 13.2. uonxu c aaaucuuou uxcaueu u xuonxu c
ueaaaucuuou uxcaueu bubnuo1exu 1k
Crp.245
Tk:uufepqecuu snereuf cnucok
B +rom npnmepe ntnonrcx cnncox. Kora nontsonarent ntnpaer +nemenr cnncxa naxt no
nemy menxnyn, ero ntop oropaxaercx n rexcronom +nemenre. Pesyntrar noxasan na pnc. 13.3.
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$fopW1ndoW->Labe1{-fexf => L1sfbox W1dgef examp1e }->pack{}
$11sfbox1 = $fopW1ndoW->L1sfbox{"-W1dfh" => 25, "-he1ghf" => 5 }->pack{}
$11sfbox1->1nsef{end, "App1es", "8ananas", "Oanges", "Peas", "P1neapp1es"}
$11sfbox1->b1nd{<Doub1e-1>. \geffu1f}
$fexf1 = $fopW1ndoW->1exf{"-W1dfh" => 40, "-he1ghf" => 2 }->pack{}
sub geffu1f {
$fu1f = $11sfbox1->gef{acf1ve}
$fexf1->1nsef{end, "$fu1f"}
}
Ma1nLoop

uc. 13.3. uc. 13.3. uc. 13.3. uc. 13.3. u1epeucuuu neueu1 cnucox bubnuo1exu 1k
Tk: uufepqecuu snereuf ukana
B +rom npnmepe mt ntneem nnrep]ecnt +nemenr mxana nnnorexn Tk. Btpan-
noe nontsonarenem nonoxenne mxant oropaxaercx n rexcronom nnrep]ecnom +nemen-
re. Pesyntrar noxasan na pnc. 13..
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$fopW1ndoW->Labe1{-fexf => 5ca1e W1dgef examp1e }->pack{}
$fopW1ndoW->5ca1e{-o1enf => ho1onfa1, -fom => 0,
-fo => 200, -f1ck1nfeva1 => 40,
-1abe1 => 5e1ecf a va1ue:, -1engfh => 200,
-va1ab1e => \$va1ue, -command => \d1sp1ay }->pack{}
$fexf1 = $fopW1ndoW->1exf{"-W1dfh" => 40, "-he1ghf" => 2 }->pack{}
sub d1sp1ay {
$fexf1->de1efe{1.0, end}
$fexf1->1nsef{end, "$va1ue"}
}
Ma1nLoop

uc. 13.4. uc. 13.4. uc. 13.4. uc. 13.4. u1epeucuuu neueu1 xana bubnuo1exu 1k
Tk: uufepqecuue snereufu rpaquku (kauea)
B +rom npnmepe cosaercx nnrep]ecnt +nemenr xanna nnnorexn Tk n n nem pncy-
ercx ronyo onan. Pesyntrar noxasan na pnc. 13.5.
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$canvas1 = $fopW1ndoW->Canvas{-W1dfh => 200, -he1ghf => 200 }->pack{}
$canvas1->ceafe{ova1, 50, 50, 160, 160,-f111 => "b1ue"}
Ma1nLoop



13.5. 13.5. 13.5. 13.5. u1epeucuuu neueu1 pucoaauun xauaa bubnuo1exu 1k
Crp.246
Tk: uufepqecuue snereufu reuk
B +rom npnmepe mt cosaem crpoxy menm c nymx pasenamn: File n Edit. Kora nontsona-
rent ntnpaer nynxr menm (pnc. 13.), mt noxastnaem n rexcronom nnrep]ecnom +ne-
menre, xaxo nmenno nynxr ntpan (pnc. 13.7).





uc. 13.6. uc. 13.6. uc. 13.6. uc. 13.6. euk bubnuo1exu 1k uc. 13.7. uc. 13.7. uc. 13.7. uc. 13.7. eaynu1a1 aubopa nyux1a ueuk

use 1k
my $fopW1ndoW = Ma1nW1ndoW->neW{}
$menuba = $fopW1ndoW->Iame{}->pack{-s1de => fop,-f111 => x}
$f11emenu = $menuba->Menubuffon{-fexf => I11e}->pack{-s1de => 1eff}
$f11emenu->command{-1abe1 => Open, -command => sub {
$fexf->de1efe{1.0, end}
$fexf->1nsef{end, "ou c11cked Open."} }}
$f11emenu->sepaafo{}
$f11emenu->command{-1abe1 => Lx1f, -command => sub {ex1f}}
$ed1fmenu = $menuba->Menubuffon{-fexf => Ld1f }->pack{-s1de => 1eff}
$ed1fmenu->command{-1abe1 => 5each, -command => sub {
$fexf->de1efe{1.0, end}
$fexf->1nsef{end, "ou c11cked 5each."} }}
$ed1fmenu->command{-1abe1 => kep1ace, -command => sub {
$fexf->de1efe{1.0, end}
$fexf->1nsef{end, "ou c11cked kep1ace."} }}
$fopW1ndoW->Labe1{-fexf => Menu W1dgef examp1e.}->pack{}
$fexf = $fopW1ndoW->1exf{ -W1dfh => 40, -he1ghf => 3 }->pack{}
Ma1nLoop
Tk: okua puanora
B +rom npnmepe cosaercx oxno nanora, xoropoe noxnnxercx, xora nontsonarent menxa-
er na coornercrnymme xnonxe rnannoro oxna (pnc. 13.). Hontsonarent moxer nnecrn
rexcr n crpoxe nnoa oxna nanora, a nocne naxarnx xnonxn OK n rnannom oxne oropa-
xaercx nneennt rexcr (pnc. 13.9).
use 1k
$fopW1ndoW = Ma1nW1ndoW->neW{}
$d1a1og = $fopW1ndoW->D1a1og8ox{-f1f1e => D1a1og 8ox,-buffons => |O, Cance1]}
$enfy = $d1a1og->add{"Lnfy", -W1dfh => 40}->pack{}
$fopW1ndoW->Labe1{-fexf => D1a1og W1dgef examp1e.}->pack{}
$fopW1ndoW->8uffon{-fexf => 5hoW d1a1og box, -command => \shoW}->pack{}
$fexf1 = $fopW1ndoW->1exf{-W1dfh => 40,-he1ghf => 2}->pack{}
Ma1nLoop
sub shoW {
$esu1f = $d1a1og->5hoW
1f {$esu1f eq "O"} {
$fexf1->de1efe{1.0, end}
$fexf1->1nsef{end, $enfy->gef} }
}
uc. 13.. uc. 13.. uc. 13.. uc. 13.. Oxuo uanora bubnuo1exu 1k


uc. 13.9. uc. 13.9. uc. 13.9. uc. 13.9. auuue, aaeeuuue a oxue uanora
Crp.247
Fnana 14

C1pyk1ypm pauumx

Kopo1ko
Hanonee cymecrnennax nemt, orcyrcrnonanmax n Perl o noxnnennx nxro nepcnn, +ro
noepxxa cnoxntx crpyxryp anntx (ne tno axe mnoromepntx maccnnon, ne ronopx
yxe o onee cnoxntx crpyxrypax). Cnecrnne +roro, nenosmoxnocrt ncnontsonannx n
maccnnax onee onoro nnexca, tna ono ns camtx omyrnmtx norept nx nporpammn-
cron. B npexnne npemena npnxonnoct nmnrnponart mnoromepnte maccnnt cnocoom,
xoropt xorx n ne ntrnxen raxnm yx neyxnmxnm, no ne cranonnncx or +roro +]]exrnn-
nee. Hnexct paccmarpnnannct xax rexcronte crpoxn (xnmun x+me) n oennxnnct n
onn xnmu. Bor, nanpnmep, nmnrannx nymepnoro maccnna:
fo $oufe1oop {0..4} {
fo $1nne1oop {0..4} {
$aay{"$oufe1oop, $1nne1oop"} = 1
} }
Hocne cosannx noono crpyxrypt anntx nx ocryna x +nemenry raxoro maccnna
rpeonanoct nepeart xonxarenannm nnexcon:
p1nf $aay{0,0}
1
Tenept Perl nxnmuaer cymecrnennym noepxxy crpyxryp anntx, n rom uncne n mnoro-
mepntx maccnnon, no+romy ror xe xo moxno sanncart xax
fo $oufe1oop {0..4} {
fo $1nne1oop {0..4} {
$a $a $a $a ay|$oufe1oop]|$1nne1oop] = 1 ay|$oufe1oop]|$1nne1oop] = 1 ay|$oufe1oop]|$1nne1oop] = 1 ay|$oufe1oop]|$1nne1oop] = 1
} }
p1nf $aay|0]|0]
1
Onaxo +ra xoncrpyxnnx roxe onee xnrpoymna, uem moxer noxasartcx. Maccnnt n x+mn
Perl no-npexnemy no cymecrny onomepnt. Ho+romy na camom ene npnneennax xoncr-
pyxnnx +ro onomepnt maccnn ns cctnox, cctnammnxcx na pyrne onomepnte mac-
cnnt. To, uro Perl nosnonxer onycxart mexy napamn cxoox (xnaparntx nnn ]nrypntx,
no ne xpyrntx) oneparop pastmenonannx cctnxn ->, enaer nosmoxno sannct maccnna
xax t n nymepnom nne xoncrpyxnnx $array$i]$] connaaer c $array$i]->$].
Onaxo neoxonmo nomnnrt, uro na camom ene +ro nce-raxn maccnn cctnox. Hanpnmep,
ecnn nt nonpoyere ntnonnnrt xomany print array, ro ne ynnnre +nemenron nymep-
noro maccnna. Bmecro +roro Perl naneuaraer mecrnanarepnunte cctnxn na pyrne ono-
mepnte maccnnt:
AkkA{0xa56e4}AkkA{0xa57c}
AkkA{0xa5d0}AkkA{0xa5924}
AkkA{0xa597}
Crp.248
Hocxontxy nymepnt maccnn +ro maccnn ns cctnox na onomepnte maccnnt, ero
moxno cosart c nomomtm reneparopa anonnmntx maccnnon napt xnaparntx cxoox:
$aay|0] = |"app1es", "o $aay|0] = |"app1es", "o $aay|0] = |"app1es", "o $aay|0] = |"app1es", "o anges"] anges"] anges"] anges"]
$aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"]
$aay|2] = |"ham", "ch1cken"] $aay|2] = |"ham", "ch1cken"] $aay|2] = |"ham", "ch1cken"] $aay|2] = |"ham", "ch1cken"]
p1nf $aay|1]|1]
con
Bor pyro cnoco cosannx ananornunoro maccnna, npn xoropom maccnn cpasy nnnnna-
nnsnpyercx cnncxom, cocroxmnm ns cctnox na maccnnt:
0aay = 0aay = 0aay = 0aay ={ {{ { |"app1es", "o |"app1es", "o |"app1es", "o |"app1es", "o anges"], anges"], anges"], anges"],
|"aspaagus", "con", "peas"], |"aspaagus", "con", "peas"], |"aspaagus", "con", "peas"], |"aspaagus", "con", "peas"],
|" |" |" |"ham ham ham ham", " ", " ", " ", "ch1cken ch1cken ch1cken ch1cken"] } "] } "] } "] }
p1nf $aay|1]|1]
con
Ecnn n xauecrne nnemne napt ncnontsymrcx ne xpyrnte cxoxn, a xnaparnte, ro rem
camtm cosaercx cci.ra na anonnmnt maccnn onomepntx maccnnon. Hpn ocryne x ra-
xo xoncrpyxnnn neoxonm oneparop pastmenonannx:
$aay =| |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] ]
p1nf p1nf p1nf p1nf $ $$ $aay aay aay aay- -- ->|1] >|1] >|1] >|1]|1] |1] |1] |1]
con
Kpome sanomnnannx cctnox na anonnmnte maccnnt n nexoropom maccnne, npn cosannn
nymepnoro maccnna moxno ncnontsonart n raxo xo:
0{$aay|0]} 0{$aay|0]} 0{$aay|0]} 0{$aay|0]} = = = = {"app1es", "o {"app1es", "o {"app1es", "o {"app1es", "o anges"} anges"} anges"} anges"}
0{$aay|1]} 0{$aay|1]} 0{$aay|1]} 0{$aay|1]} = = = = {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"}
0{$aay|2]} = 0{$aay|2]} = 0{$aay|2]} = 0{$aay|2]} = {"ham", {"ham", {"ham", {"ham", "ch1cke "ch1cke "ch1cke "ch1cken"} n"} n"} n"}
p1nf $aay|1]|1]
con
Koncrpyxnnx $array0]] opaartnaercx ocrarouno xnrpo: Perl snaer, uro xon-
crpyxnnx ...] osnauaer pastmenonanne cctnxn na maccnn. Onaxo, nocxontxy cctnxn
$array0] ne cymecrnyer, Perl cosaer ee, sanonnxer +nemenramn cnncxa, croxmero n npa-
no uacrn oneparopa npncnannannx (eme onn npnmep saecrnonanno n Perl crparernn
aemoorue.euu), a snauenne cctnxn sanocnr n nepemennym $array0]. To xe camoe npo-
ncxonr c $array1] n $array2]. B pesyntrare cosaercx nymepnt maccnn (ro ecrt mac-
cnn ns cctnox na maccnnt). Oparnre nnnmanne, uro c raxo noranne cneyer trt oc-
ropoxntm: ecnn $array0] cvuecmevem n momenr ncnonnennx xoa, nce, na uro on yxast-
naer, yer norepxno n samemeno nontmn anntmn. Hocne cosannx maccnna na ero +ne-
menrt moxno cctnartcx c nomomtm nnexcon:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
fo $oufe1oop {0..$#aay} {
fo $1nne1oop {0..$#{aay|$oufe]}} {
p1nf p1nf p1nf p1nf $a $a $a $a ay|$oufe1oop]|$1nne1oop], " " ay|$oufe1oop]|$1nne1oop], " " ay|$oufe1oop]|$1nne1oop], " " ay|$oufe1oop]|$1nne1oop], " "
}
p1nf "\n"
}
app1es oanges
aspaagus con peas
ham ch1cken
Crp.249
Merot paort c onomepntmn maccnnamn no-npexnemy ecrnymr. Hnora +ro ynpo-
maer opamenne x nymepntx maccnnam. Bor, nanpnmep, xax c nomomtm nnexca nnxna n
]ynxnnn oin pacneuarart nymepnt maccnn, nocneonarentno ntnox onomepnte mac-
cnnt (oparnre nnnmanne, xax ncnontsyercx xoncrpyxnnx ] nx pastmenonannx cct-
nox na maccnnt):
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
fo $1oop1ndex {0..$#aay} fo $1oop1ndex {0..$#aay} fo $1oop1ndex {0..$#aay} fo $1oop1ndex {0..$#aay} { {{ {
p1nf o1n{", ", 0{$aay|$1oop1ndex]}} p1nf o1n{", ", 0{$aay|$1oop1ndex]}} p1nf o1n{", ", 0{$aay|$1oop1ndex]}} p1nf o1n{", ", 0{$aay|$1oop1ndex]}}, , , , " "" "\ \\ \n" n" n" n"
} }} }
app1es, oanges
aspaagus, con, peas
ham, ch1cken
Cneyer sanomnnrt ono: n noontx cnryannxx xo ncera paoraer c maccnnom maccnnon,
n +ror ]axr xnnxercx xnmuom xo ncem crpyxrypam anntx, ocyxammnmcx n +ro rnane.
Onn ocnonant na cctnxax n ue xnnxmrcx ]ynamenrantntmn rnnamn anntx Perl.
Ropckaska: ucnonusyfe koraupy use strict ars
Hnora cosanne crpyxryp anntx n pasopxa ncnontsyemtx npn +rom cctnox nponecc
sanyrannt. Kpane nonesno sect oxastnaercx nparma use strict vars onycrnm, na-
npnmep, uro n pesyntrare omnxn nmecro xpyrntx cxoox n xoe oxasannct xnaparnte:
use sf1cf vas
$aay = | $aay = | $aay = | $aay = |
|"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"]
] ] ] ]
p1nf $aay|0]|0]
Hnrepnperarop Perl npn xomnnnxnnn cnenapnx coomnr o omnxe npn opaorxe no-
cnene crpoxn, noromy uro n ne nexnno ntstnaercx eme ne onncannax nepemennax
array. 3ra omnxa nanomnnaer, uro nao nno samennrt xnaparnte cxoxn na xpyr-
nte, nno ncnontsonart $array xax cctnxy:
p1nf $aay->|0]|0]
(Ecnn xe no nropo crpoxe nmecro npe]nxca $ ncnontsonart , ro nepemenno array
yer npncnoen maccnn, cocroxmn ns onoro +nemenra cctnxn na anonnmnt maccnn.
uopmantno omnxn n cnenapnn ne yer, no npn ntnoe nmecro +nemenra maccnna nt no-
nyunre mecrnanarepnunoe snauenne yxasarenx.)
Homnmo maccnna maccnnon n x+ma x+me moxno cosanart cmemannte rnnt -nanpnmep,
maccnnt x+me n r. .:
$aay|1]|2] # uaccua uaccuaoa
$hash{key1}{key2} # x-1abnua ua x-1abnu
$aay|3]{key} # uaccua x-1abnu
$hash{key}|4] # x-1abnua uaccuaoa
Kax +rn, rax n pyrne cmemannte rnnt anntx paccmarpnnamrcx anee n ocnonno uacrn
rnant.
Crp.250
Henocpepc1neuume pemeuun
Cnouue sanucu: xpaueuue ccunok u ppyrux snereufoe
Bt moxere xpannrt +nemenrt pasnnuntx rnnon n crpyxrypax anntx Perl maccnnax n
x+m-rannnax. Hocxontxy x uncny +rnx +nemenron ornocxrcx n cctnxn na pyrne annte,
paspemaercx cosanart cnoxnte crpyxrypt anntx, n xoroptx +nemenrt cnxsant mexy co-
o yxasarenxmn. Hoonte crpyxrypt tnamr ouent nonesntmn, ecnn rpeyercx anromarn-
uecxoe ononnenne xonn ncxontx anntx, xpanxmnecx n pasntx mecrax crpyxrypt.
B xauecrne osopnoro npnmepa paccmorpnm cneymmn ]parmenr xoa. On noxastnaer,
uro n ono crpyxrype moxno xpannrt annte pasposnenntx rnnon, nxnmuax cctnxn na
pyrne oexrt (n rom uncne n cctnxn na nonporpammt). Oparnre nnnmanne na crpoxn
c reneparopamn anonnmntx maccnnon n x+me, cosammnx xonnn maccnnon n x+me, a
raxxe na crpoxn, re mt sanomnnaem cctnxn na yxe cymecrnymmne maccnnt n x+mn,
urot nenocpecrnenno paorart c +rnmn anntmn:
$sf1ng = "hee 1s a sf1ng"
0aay = {1, 2, 3}
xhash = {fu1f => app1es, vegefab1es => con}
sub p1nfem
{
p1nf sh1ff
}
$comp1ex = {
sf1ng => $sf1ng,
numbe => 3.1415926,
aay => |0aay],
hash => {xhash},
aayefeence => \0aay,
hashefeence => \xhash,
sub => \p1nfem,
anonymoussub => sub { p1nf sh1ff },
hand1e => \"51DOu1
}
p1nf $comp1ex->{sf1ng}, "\n"
p1nf $comp1ex->{numbe}, "\n"
p1nf $comp1ex->{aay}|0], "\n"
p1nf $comp1ex->{hash}{fu1f}, "\n"
p1nf ${$comp1ex->{aayefeence}}|0], "\n"
p1nf ${$comp1ex->{hashefeence}}{fu1f}, "\n"
$comp1ex->{sub}->{"5ubouf1ne ca11.\n"}
$comp1ex->{anonymoussub}->{"Anonymous subouf1ne.\n"}
p1nf {$comp1ex->{hand1e}} "P1nfed 1exf.", "\n"
hee 1s a sf1ng
3.1415926
1
app1es
1
app1es
5ubouf1ne ca11.
Anonymous subouf1ne.
P1nfed 1exf.
Crp.251
OLneneuue raccuea raccueoe
Maccnnt maccnnon (a raxxe maccnnt maccnnon maccnnon n r. .) samenxmr n Perl mnoro-
mepnte maccnnt anntx. Taxne crpyxrypt nennt rem, uro annte ynopxounnamrcx no
necxontxnm nnexcam nanpnmep, maccnn nmen cryenron yer nnexcnponartcx nen-
rn]nxaropom ID n nopxxontm nomepom rpynnt. Bor nanonee pacnpocrpanennt cno-
co oxnnennx maccnna maccnnon n onom npenoxennn (oparnre nnnmanne, uro mno-
romepnt maccnn oxnnxercx c nomomtm reneparopa anonnmnoro maccnna):
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
Cospauue raccuea raccueoe ua nefy
Maccnn maccnnon cosaercx ]parmenr sa ]parmenrom c nomomtm reneparopa anonnmnoro
maccnna ...]. On yer sanonnxrt +nemenrt maccnna onee nnsxnmn no yponnm nepapxnn
cctnxamn na onomepnte maccnnt:
$aay|0] = |"app1es", "o $aay|0] = |"app1es", "o $aay|0] = |"app1es", "o $aay|0] = |"app1es", "o anges"] anges"] anges"] anges"]
$aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"] $aay|1] = |"aspaagus", "con", "peas"]
$aay|2] $aay|2] $aay|2] $aay|2] = = = = |"ham", "ch1cken"] |"ham", "ch1cken"] |"ham", "ch1cken"] |"ham", "ch1cken"]
p1nf $aay|1]|1]
con
Bt moxere onrtcx roro xe pesyntrara, ncnontsyx npnntuxy Perl cosanart cctnxn
camocroxrentno (cm. ocyxenne n nauane +ro rnant):
0{$ 0{$ 0{$ 0{$a aa a ay ay ay ay|0]} |0]} |0]} |0]} = == = {"app1es", "o {"app1es", "o {"app1es", "o {"app1es", "o anges"} anges"} anges"} anges"}
0{$aay|1]} 0{$aay|1]} 0{$aay|1]} 0{$aay|1]} = = = = {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"} {"aspaagus", "con", "peas"}
0{$aay|2]} 0{$aay|2]} 0{$aay|2]} 0{$aay|2]} = == = {"ham", "ch1cken"} {"ham", "ch1cken"} {"ham", "ch1cken"} {"ham", "ch1cken"}
p1nf $aay|1]|1]
con
Hy n, ecrecrnenno, moxno cosanart n sanonnxrt maccnn maccnnon +nemenr sa +nemenrom:
fo $oufe1oop {0..4} {
fo $1nne1oop {0..4} {
$a $a $a $a ay|$oufe1oop]|$1nne1oop] ay|$oufe1oop]|$1nne1oop] ay|$oufe1oop]|$1nne1oop] ay|$oufe1oop]|$1nne1oop] = == = 1 1 1 1
}
}
p1nf $aay|0]|0]
1
Kpome roro, ]ynxnnx push nosnonxer sanocnrt n rnannt maccnn ouepenym cctnxy:
fo $oufe1oop {0..4} {
push $aay, |1, 1, 1, 1, 1] push $aay, |1, 1, 1, 1, 1] push $aay, |1, 1, 1, 1, 1] push $aay, |1, 1, 1, 1, 1]
}
p1nf $aay|0]|0]
1
B cneymmem npnmepe nx cosannx maccnna maccnnon ncnontsyercx cnncox, nos-
npamaemt nonporpammo, n reneparop anonnmnoro maccnna:
fo $1oop1ndex {0..4} {
$aay|$1oop1ndex] = | $aay|$1oop1ndex] = | $aay|$1oop1ndex] = | $aay|$1oop1ndex] = |eo11sf] eo11sf] eo11sf] eo11sf]
}
sub eo11sf { efun {0, 0, 0, 0, 0} }
p1nf $aay|0]|0]
Crp.252
0
K maccnny maccnnon ncera moxno oannrt nonym crpoxy:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
$aay|3] = |"ch1cken noo $aay|3] = |"ch1cken noo $aay|3] = |"ch1cken noo $aay|3] = |"ch1cken nood1e", d1e", d1e", d1e", "ch111"] "ch111"] "ch111"] "ch111"]
p1nf $aay|3]|0]
ch1cken nood1e
Hnn, ecnn nt npenounraere oannxrt +nemenrt n yxe cymecrnymmym crpoxy maccnna,
moxno cenart rax:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
push 0{$aay|0]}, "banana" push 0{$aay|0]}, "banana" push 0{$aay|0]}, "banana" push 0{$aay|0]}, "banana"
p1nf $aay|0]|2]
banana
Haxonen, c nomomtm ]ynxnnn splice moxno oannxrt, samenxrt n yanxrt crpoxn c npo-
nsnontntm nomepom:
# obaau1u c1poxy nepe c1poxou c uuexcou 1:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
sp11ce 0aay, 1 ,0 , |"chess", "checkes", "go"]
p1nf $aay|0]|0], ", ", $aay|1]|0], ", ", $aay|2]|0]
app1es, chess, aspaagus

# aaueuu1u ae c1poxu ua ouy:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
sp11ce 0aay, 0 ,2 , |"chess", "checkes", "go"]
p1nf $aay|0]|0], ", ", $aay|1]|0]
chess, ham

# aaueuu1u ouy c1poxy ua ae:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
sp11ce 0aay, 0 ,1 , |"chess", "checkes", "go"],|"caf", "dog", "mouse"]
p1nf $aay|0]|0], ", ". $aay|1]|0], ", ", $aay|1]|0]
chess, caf, aspaagus

# yanu1u cpeukk c1poxy:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
sp11ce 0aay, 1 ,0 p1nf $aay|0]|0], ", ", $aay|1]|0]
app1es, ham
Ecrecrnenno, ro xe camoe moxno npoentnart n c +nemenramn crpoxn, ro ecrt oannxrt
nx, samenxrt n yanxrt n pexnme npxmoro ocryna.
ocfyn k snereufar raccuea raccueoe
Crp.253
Bt moxere ntnonnxrt ocryn x maccnny maccnnon no+nemenrno:
fo $oufe1oop {0..4} {
fo $1nne1oop {0..4} {
$aay|$oufe1oop]|$1nne1oop] = 1
}
}
p1nf p1nf p1nf p1nf $ $$ $aay aay aay aay|0]|0] |0]|0] |0]|0] |0]|0]
1
Moxno raxxe ncnontsonart momt oneparopon n ]ynxnn Perl, npenasnauenntx nx pa-
ort c onomepntmn maccnnamn, urot onerunrt paory c mnoromepntmn maccnnamn.
Hanpnmep, n cneymmem npnmepe (npnneennom raxxe n nauane rnant) ]ynxnnx oin
oennxer necxontxo +nemenron n rexcronym crpoxy:
0aay = { |"app1es", "oanges"],
|"aspaagus", "con", "peas"],
|"ham", "ch1cken"] }
fo $aayef {0aay}
{
p1nf o1n{", ", 0{$aayef}}, " p1nf o1n{", ", 0{$aayef}}, " p1nf o1n{", ", 0{$aayef}}, " p1nf o1n{", ", 0{$aayef}}, "\ \\ \n" n" n" n"
}
app1es, oanges
aspaagus, con, peas
ham, ch1cken
OLneneuue xsua xsue
X+mn, cocrannennte ns pyrnx x+me, nonesnt npn paore c rexcrono mnoroyponneno
nn]opmannonno cncremo (nanpnmep, +xcneprno cncremo). B +rnx cnyuaxx rexcronte
xnmun ncnontsymrcx nx nocneonarentnoro npocmorpa pasnnuntx yponne crpyxrypt
anntx. B cneymmem npnmepe raxo x+m cosaercx sa onn pas:
xhash = {
fu1fs => {
favo1fe => "app1es",
second favo1fe => "oanges"
},
vegefab1es => {
favo1fe => "con",
second favo1fe => "peas",
1easf favo1fe => "fun1p"
},
meaf => {
favo1fe => "ch1cken",
second favo1fe => "beef"
}
}
p1nf $hash{fu1fs}{favo1fe}
app1es
Oparnre nnnmanne, uro n raxom x+me snauennxmn nx nap xnmu/snauenne ntcrynamr
pyrne x+mn (rounee, cctnxn na nnx). Kpome roro, nx xoncrpyxnn rnna ...]...], xax n
ncnontsyemtx n cnyuae maccnna maccnnon xoncrpyxnn nna ...]...], mexy napamn ]n-
rypntx cxoox nexnno nocrannxercx oneparop-crpenxa -> pastmenonannx cctnox.
Crp.254
Cospauue xsua xsue ua nefy
urot cosart x+m x+me +nemenr sa +nemenrom, ncnontsyercx ra xe cxema, uro n n cny-
uae maccnna maccnnon, no c onnm cymecrnenntm ornnunem x x+my ncera moxno o-
annrt nonte xnmu n snauenne, no oannenne x maccnny nontx +nemenron c nponycxamn
nnexcon nopoxaer neonpeenennte snauennx. Hpnmep:
xhash{fu1fs} = {favo1fe => "app1es",
second favo1fe => "oanges"}
xhash{vegefab1es} = {favo1fe => "con",
second favo1fe => "peas",
1easf favo1fe => "fun1p"}
xhash{meaf} = {favo1fe => "ch1cken", second favo1fe => "beef"}
p1nf $hash{fu1fs}{favo1fe}
app1es
B cneymme cxeme reneparop anonnmnoro x+ma xomnnnpyercx co cnncxom xnmu/ sna-
uenne, nosnpamaemtm nnemne nonporpammo:
fo $key {"hash1", "hash2", "hash3"}
{ $hash{$key} = {$ { $hash{$key} = {$ { $hash{$key} = {$ { $hash{$key} = {$e ee efufn11sf} } fufn11sf} } fufn11sf} } fufn11sf} }
sub efun11sf { efun {key1 => va1ue1, key2 => va1ue2} }
p1nf $hash{hash1}{key2}
va1ue2
ocfyn k snereufar xsua xsue
urot nonyunrt orentnoe snauenne, xpanxmeecx n x+me x+me, nao xnno yxasart naop
nocneonarentntx xnmue:
xhash = {
fu1fs => {favo1fe => "app1es", second favo1fe => "oanges"},
vegefab1es => {favo1fe => "con", second favo1fe => "peas", 1easf favo1fe => "fun1p"}
}
p1nf $hash{fu1fs}{second favo1fe}
oanges
Hcnontsyx cranaprnte npnemt paort c otuntmn x+mamn, moxno oprannsonart nnxn
no +nemenram x+ma x+me:
xhash = {
fu1fs => {favo1fe => "app1es", second => "oanges"},
vegefab1es => {favo1fe => "con", second => "peas"}
}
fo $food {keys xhash} { fo $food {keys xhash} { fo $food {keys xhash} { fo $food {keys xhash} {
p1nf p1nf p1nf p1nf "$foo "$foo "$foo "$food: d: d: d:\ \\ \n nn n\ \\ \f {" f {" f {" f {"
fo $key {keys x{$hash{$food}}} fo $key {keys x{$hash{$food}}} fo $key {keys x{$hash{$food}}} fo $key {keys x{$hash{$food}}} { {{ {
p1nf "key p1nf "key p1nf "key p1nf "key => => => => \ \\ \"$hash{$food}{$key} "$hash{$food}{$key} "$hash{$food}{$key} "$hash{$food}{$key}\ \\ \"," "," "," ","
} }} }
p1nf "} p1nf "} p1nf "} p1nf "}\ \\ \n" n" n" n"
} }} }
vegefab1es:
{favo1fe => "con", second => "peas", }
fu1fs:
{favo1fe => "app1es", second => "oanges", }
urot coprnponart sanncn x+m-rannnt no xnmuam, n sarononox nnxna moxno nxnmunrt
onepannm coprnponxn:
Crp.255
xhash = {
fu1fs => {favo1fe => "app1es", second => "oanges"},
vegefab1es => {favo1fe => "con", second => "peas"}
}
fo $food {sof keys xhash} { fo $food {sof keys xhash} { fo $food {sof keys xhash} { fo $food {sof keys xhash} {
p1nf "$food:\n\f {"
fo $key {sof k fo $key {sof k fo $key {sof k fo $key {sof keys x{$hash{$food}}} { eys x{$hash{$food}}} { eys x{$hash{$food}}} { eys x{$hash{$food}}} {
p1nf "$key => \"$hash{$food}{$key}\","
}
p1nf "}\n"
}
fu1fs:
{favo1fe => "app1es", second => "oanges", }
vegefab1es:
{favo1fe => "con", second => "peas", }
OLneneuue raccuea xsue
Maccnnt x+m-rannn nosnonxmr nnexcnponart uncnontm snauennem sanncn c nmeno-
nanntmn nonxmn. (Hpnmepom +roro cnyxnr xontneno y]ep, paccmarpnnaemt n no-
cnenem pasene +ro rnant.) B cneymmem npnmepe cosaercx maccnn x+me:
0aay = { {favo1fe => "app1es", second favo1fe => "oanges" },
{favo1fe => "con", second favo1fe => "peas", 1easf favo1fe => "fun1p" },
{favo1fe => "ch1cken", second favo1fe => "beef" }
}
p1nf $aay|0]{favo1fe}
app1es
Oparnre nnnmanne, uro nx xoncrpyxnn nna ...]...], xax n nx paccmorpenntx panee
xoncrpyxnn nna ...]...] n ...]...], mexy napamn cxoox nexnno nocrannxercx onepa-
rop-crpenxa -> pastmenonannx cctnox.
Cospauue raccuea xsue ua nefy
Moxno cosanart maccnnt x+me mar sa marom, npncnannax cctnxn na anonnmnte x+mn
+nemenram maccnna:
0aay|0] = {favo1fe => "app1es", second favo1fe => "oanges"}
0aay|1] = {favo1fe => "con", second favo1fe => "peas", 1easf favo1fe => "fun1p"}
0aay|2] = {favo1fe => "ch1cken", second favo1fe => "beef"}
p1nf $aay|0]{favo1fe}
app1es
Kax n n cnyuae maccnna maccnnon, nt moxere nocnontsonartcx ]ynxnne push:
push 0aay, {favo1fe => "app1es", second favo1fe => "oanges"}
push 0aay, {favo1fe => "con", second favo1fe => "peas", 1easf favo1fe => "fun1p"}
push 0aay, {favo1fe => "ch1cken", second favo1fe => "beef"}
p1nf $aay|0]{favo1fe}
app1es
B cneymmem npnmepe mt nocneonarentno unraem ns rexcrontx crpox napt
xnmu/snauenne n npenpamaem nx n maccnn x+me:
$dafa|0] = "favo1fe:app1es, second favo1fe:oanges"
$dafa|1] = "favo1fe:con, second favo1fe:peas, 1easf favo1fe:fun1p"
$dafa|2] = "favo1fe:ch1cken, second favo1fe:beef"
Crp.256
fo $1oop1ndex {0..$#dafa} {
fo $e1emenf {sp11f ., $dafa|$1oop1ndex]} {
{$key. $va1ue} = sp11f :, $e1emenf
$key = s/|\s\n]+// # ouuc1u1u o1 npobenoa
$key = s/|\s\n]+$://
$va1ue = s/|\s\n]+// # ouuc1u1u o1 npobenoa
$va1ue = s/f\s\n]+$//
$aay|$1oop1ndex]{$key} = $va1ue
}
}
p1nf $aay|0]{second favo1fe}
oanges
(Oparnre nnnmanne, uro mt sect nocnontsonannct xonrexcrno-uyncrnnrentno npone-
ypo aemoorue.euu cctnox (autoviviIication), onncanno n rnane .)
ocfyn k snereufar raccuea xsue
urot nonyunrt snauenne, xpannmoe n maccnne x+me, nao yxasart nnexc maccnna n xnmu
x+ma:
0aay|0] = {favo1fe => "app1es", second favo1fe => "oanges"}
0aay|1] = {favo1fe => "con", second favo1fe => "peas", 1easf favo1fe => "fun1p"}
0aay|2] = {favo1fe => "ch1cken", second favo1fe => "beef"}
p1nf p1nf p1nf p1nf $ $$ $aay aay aay aay|0]{ |0]{ |0]{ |0]{fav fav fav favo1fe o1fe o1fe o1fe} } } }
app1es
B cneymmem cnyuae mt nonnocrtm ntnonm maccnn x+me c nomomtm nnxna no ero +ne-
menram:
0aay|0] = {favo1fe => "app1es", second => "oanges"}
0aay|1] = {favo1fe => "con", second => "peas", 1easf => "fun1p"}
0aay|2] = {favo1fe => "ch1cken", second => "beef"}
fo $1oop1ndex {0..$#aay} {
p1nf "aay|$1oop1ndex]:\n\f{"
fo $key {keys x{$aay|$1oop1ndex]}} {
p1nf "$key => $aay|$1oop1ndex]{$key}, "
}
p1nf "}\n"
}
aay|0]:
{favo1fe => app1es, second => oanges, }
aay|1]:
{favo1fe => con, second => peas, 1easf => fun1p, }
aay|2]:
{favo1fe => ch1cken, second => beef, }
A nor xax cenart ro xe camoe, ncnontsyx nmecro nnexca nnxna cctnxy:
0aay|0] = {favo1fe => "app1es", second -> "oanges"}
0aay|1] = {favo1fe => "con", second => "peas", 1easf => "fun1p"}
0aay|2] = {favo1fe -> "ch1cken", second => "beef"}
fo $hashefeence {0aay} {
p1nf "{"
fo $key {sof keys x$hashefeence} {
p1nf "$key => $aay|$1oop1ndex]{$key}, "
}
p1nf "}\n"
}
{favo1fe => app1es, second => oanges, }
Crp.257
{favo1fe => con, second => peas, 1easf => fun1p, }
{favo1fe => ch1cken, second => beef, }
OLneneuue xsua raccueoe
X+mn, cocroxmne ns maccnnon, nosnonxmr pasnnart annte, nnexcnponannte uncno-
ntm snauennem, na sanncn.
Hocuasua. Hs :emipex eos+oruix ro+ouuau +accueoe u xoue xouu +accueoe ucno.isvmmc uauoo.ee
peoro.
B cneymmem npnmepe mt oxnnxem x+m maccnnon n onom npenoxennn:
xhash = {
fu1fs => |"app1es", "oanges"],
vegefab1es => |"con", "peas", "fun1ps"],
meaf => |"ch1cken", "ham"],
}
p1nf $hash{fu1fs}|0]
app1es
Oparnre nnnmanne, uro nx xoncrpyxnn nna ...]...], xax n nx paccmorpenntx panee
xoncrpyxnn nna ...]...] ...]...], n ...]...], mexy napamn cxoox nexnno nocrannxercx
oneparop-crpenxa -> pastmenonannx cctnox.
Cospauue xsua raccueoe ua nefy
urot copart x+m maccnnon ns orentntx +nemenron, moxno sanocnrt n x+m no nyx-
ntm xnmuom cctnxn na maccnnt, cosannte reneparopom anonnmntx maccnnon:
xhash{fu1fs} = |"app1es xhash{fu1fs} = |"app1es xhash{fu1fs} = |"app1es xhash{fu1fs} = |"app1es", "o ", "o ", "o ", "o anges"] anges"] anges"] anges"]
xhash{vegefab1es} = |"con", "peas", "fun1ps"] xhash{vegefab1es} = |"con", "peas", "fun1ps"] xhash{vegefab1es} = |"con", "peas", "fun1ps"] xhash{vegefab1es} = |"con", "peas", "fun1ps"]
xhash{meaf} = |"ch1cken", "ham"] xhash{meaf} = |"ch1cken", "ham"] xhash{meaf} = |"ch1cken", "ham"] xhash{meaf} = |"ch1cken", "ham"]
p1nf $hash{fu1fs}|0]
app1es
Ecnn nt npenounraere pyro napnanr, moxere nocnontsonartcx ]ynxnne push n xonrex-
crno-uyncrnnrentno nponeypo aemoorue.euu cctnox (autoviviIi-cation), onncanno n
rnane :
push 0{xhash{fu1fs}}, "app1es", "oanges" push 0{xhash{fu1fs}}, "app1es", "oanges" push 0{xhash{fu1fs}}, "app1es", "oanges" push 0{xhash{fu1fs}}, "app1es", "oanges"
push 0{xhash{vegefab1es}}, "con", "peas", "fun1ps" push 0{xhash{vegefab1es}}, "con", "peas", "fun1ps" push 0{xhash{vegefab1es}}, "con", "peas", "fun1ps" push 0{xhash{vegefab1es}}, "con", "peas", "fun1ps"
push 0{xhash{meaf}}, "ch1cken", "ham" push 0{xhash{meaf}}, "ch1cken", "ham" push 0{xhash{meaf}}, "ch1cken", "ham" push 0{xhash{meaf}}, "ch1cken", "ham"
p1nf $hash{fu1fs}|0]
app1es
ocfyn k snereufar xsua raccueoe
Bt ncera moxere nonyunrt ocryn x orentnomy +nemenry anntx, xpannmomy n x+me
maccnnon, yxasan xnmu n nnexc:
xhash = {
fu1fs => |"app1es", "oanges"],
vegefab1es => |"con", "peas", "fun1ps"],
meaf => |"ch1cken", "ham"],
}
p1nf p1nf p1nf p1nf $ $$ $hash hash hash hash{ {{ {fu1fs fu1fs fu1fs fu1fs}|0] }|0] }|0] }|0]
Crp.258
app1es
B cneymmem npnmepe mt nonnocrtm ntnonm orcoprnponannt no snauennxm xnmua x+m
maccnnon, ncnontsyx ]ynxnnm oin nx npeopasonannx maccnnon n rexcronte crpoxn:
xhash = {
fu1fs => |"app1es", "oanges"],
vegefab1es => |"con", "peas", "fun1ps"],
meaf => |"ch1cken", "ham"],
}
fo $key {sof keys xhash} { fo $key {sof keys xhash} { fo $key {sof keys xhash} { fo $key {sof keys xhash} {
p1nf "$key: p1nf "$key: p1nf "$key: p1nf "$key:\ \\ \f|", o1n{", ", 0{$hash{$key}}}, "] f|", o1n{", ", 0{$hash{$key}}}, "] f|", o1n{", ", 0{$hash{$key}}}, "] f|", o1n{", ", 0{$hash{$key}}}, "]\ \\ \n" n" n" n"
} }} }
fu1fs: |app1es, oanges]
meaf: |ch1cken, ham]
vegefab1es: |con, peas, fun1ps]
Censuue cnucku u konuueeue yqepu
C nomomtm onncanntx n +ro rnane crpyxryp mt moxem nerxo cosanart cranaprnte
crpyxrypt anntx rnna epenten (re annte xpanxrcx n oxontx ornernnennxx, coen-
nenntx n ysnax) nnn cnxsntx cnncxon. B cnyuae cnxsntx cnncxon annte xpanxrcx n +ne-
menrax, xoropte camn oprannsonant n nne cnncxa. Kaxt +nemenr yxastnaer na cne-
ymmn +nemenr cnncxa. (nx nycroponne cnxsanntx cnncxon xaxt +nemenr raxxe
cctnaercx n na npetymn +nemenr.) Tem camtm moxno nepememartcx no cnncxy or
+nemenra x +nemenry.
Ono ns nonynxpntx pasnonnnocre cnxsntx cnncxon xnnxercx xontneno y]ep, xoro-
pt nonyuaercx npn samtxannn cnxsnoro cnncxa n xontno. Kontneno y]ep xpannr annte
c nomomtm nyx nnexcntx +nemenron: rononnoro (head) n xonnenoro (tail). Hpn sanecennn
anntx n y]ep ynennunnaercx xonneno nnexc. Hpn nsnneuennn anntx ns y]epa ynenn-
unnaercx rononno nnexc. Kora rononno n xonneno nnexct nepexptnamrcx, y]ep cra-
nonnrcx nycrtm. Hepememax no samxnyromy xpyry rononno n xonneno nnexct, y]ep no-
snonxer +]]exrnnno ncnontsonart namxrt. (Hanpnmep, naxarnx na xnannmn xnannarypt
xpanxrcx na xomntmrepax IBM PC n nx xnonax n xontnentx y]epax, paccunranntx na nxr-
nanart +nemenron, n npn nx nepenonnennn pasaercx ryox.)
Cneymmn npnmep emoncrpnpyer cosanne xontnenoro y]epa ns uertpex +nemenron,
oprannsonanntx n nne maccnna. Iononno nnexc yer yxastnart na nepnt sanxrt
+nemenr, a xonneno na nepnt cnoont, cneymmn sa nocnennm sanxrtm. Hpn
ntxoe sa rpannny maccnna nnexct anromarnuecxn xoppexrnpymrcx, nepexox x nepno-
my +nemenry maccnna. Cnryannx, xora rononno +nemenr connaaer c xonnentm, yer
nnrepnpernponartcx namn xax nonnocrtm nycro y]ep. 3ro osnauaer, uro nt moxere
xpannrt n xontnenom y]epe ne onee rpex +nemenron, nocxontxy n name moenn nonno-
crtm sanonnennt y]ep neornnunm or nonnocrtm nycroro. (3ro ne camoe +]]exrnnnoe
ncnontsonanne namxrn, onaxo, ne satnare, uro nepe namn yuent npnmep.)
Mt yem moennponart xontneno y]ep c nomomtm maccnna, cocroxmero ns x+me. Kax-
t +nemenr maccnna +ro x+m c nymx xnmuamn. Knmu data npenasnauen nx xpanennx
anntx. Knmu next +ro nymn ncne sa rexymnm nnexc +nemenra maccnna. Tem camtm
nonyuaem cnxsnt cnncox nnexcon, xoropt n moennpyer nam xontneno y]ep.
Bor xax nnnnnannsnpyercx y]ep (rononno n xonneno nnexct ycranannnnamrcx n o-
no snauenne, uro yxastnaer na nycrory y]epa):
Crp.259
$buffe|0]{nexf} = 1
$buffe|0]{dafa} = 0
$buffe|1]{nexf} = 2
$buffe|1]{dafa} = 0
$buffe|2]{nexf} = 3
$buffe|2]{dafa} = 0
$buffe|3]{nexf} = 0
$buffe|3]{dafa} = 0
$head = 0
$fa11 = 0
urot sanecrn n y]ep nont +nemenr, nao nponepnrt, ne sanonnen nn y]ep. Ecnn y-
]ep nonon nosnpamaercx nont. Ecnn n y]epe eme ecrt mecro, n nero nomemaercx +ne-
menr, xonneno nnexc ynennunnaercx, a ]ynxnnx nosnpamaer ennnny:
sub sfoe
{ # npoaepxa aanonueuun byepa
1f {$buffe|$fa11]{nexf} != $head} { # a byepe ec1u uec1o
$buffe|$fa11]{dafa} = sh1ff
$fa11 = $buffe|$fa11]{nexf}:
efun 1
} e1se { # byep nepenonueu
efun 0
}
}
urot nsnneut ns y]epa +nemenr, nao nponepnrt, ne nycr nn on. Ecnn nycr -
nosnpamaem neonpeenennoe snauenne. Ecnn ner - nosnpamaem +nemenr, na xoropt yxa-
stnaer rononno nnexc, n ynennunnaem nocnenn:
sub ef1eve
{ # npoaepxa aanonueuun byepa
1f {$head != $fa11} { # a byepe ec1u auuue
$dafa = $buffe|$head]{dafa}
$head = $buffe|$head]{nexf}
efun $dafa
} e1se { # byep nyc1
efun undef
}
}
Hpnneem npnmep oannennx n nsnneuennx anntx ns y]epa:
sfoe 0
sfoe 1
sfoe 2
sfoe 3 # byep aanonueu, auuoe ue coxpaueuo
p1nf ef1eve, "\n"
p1nf ef1eve, "\n"
p1nf ef1eve, "\n"
0
1
2
Oparnre nnnmanne, uro xorx mt ntrannct nomecrnrt n y]ep uertpe +nemenra, rpn
nonnocrtm sanonnxmr ero, a uerneprt nrnopnpyercx.
Crp.260
Fnana 15

Cospauue nake1on u uopyne

Kopo1ko
3amnmennocrt n moyntnocrt na nennxnx npnnnnna nporpammnponannx. Perl oecneun-
naer nx ntnonnenne, npeocrannxx nosmoxnocrt pasnnart nporpammy na nonyanronomnte
]parmenrt rax, uro nporpammncry ne nao ecnoxonrtcx o xon]nnxrax mexy nnmn n oc-
rantno uacrtm nporpammt. nx enennx nporpammt na nesanncnmte ]parmenrt ncnont-
symrcx naremi Perl, xoropte cosamr nenepecexammnecx oo.acmu u+eu (namespaces). uro
raxoe onacrt nmen 3ro uacrt nporpammt co cnoe cocrnenno onacrtm nnnmocrn rno-
antntx nenrn]nxaropon -pyrnmn cnonamn, ona ]ynxnnonnpyer xax uacrnax reppnropnx
nporpammncra. Ha camom ene n Perl ner raxo nemn, xax onacrt nnnmocrn rnoantntx
nenrn]nxaropon, nmax raxax onacrt orpannunnaercx nexnm naxerom. Cosanax naxer,
nt nonyuaere onpeenennym rapanrnm roro, uro nam xo ne cmemaercx c nepemenntmn n
nonporpammamn pyroro ]parmenra. 3ro nosnonxer oprannsontnart xo, npenasnauennt
nx mnoroxparnoro ncnontsonannx, n nne naxeron. Kpome naxeron cymecrnymr raxxe +oov-
.u Perl. Moynn +ro naxert, oprannsonannte cnennantntm opasom. Hx moxno sarpyxart
n nnrerpnponart c xonxperno nporpammo. Haxonen, n Perl cymecrnymr raxxe r.acci
ocnona oexrno-opnenrnponannoro nporpammnponannx. O naxerax n moynxx peut ner n
+ro rnane, a o xnaccax n cneymme.
Rakefu
Ko, nomemaemt n naxer, moxer pasmemartcx n cocrnennom ]ane, sannmart necxont-
xo ]anon, xorx necxontxo naxeron raxxe moryr ennrt onn ]an. Hepexnmuenne mexy
naxeramn ocymecrnnxer xomana package. B cneymmem npnmepe cosaercx naxer, co-
xpanxemt xax ]an package1.pl:
package package1
8LGlN { }
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Komana package naunnaer nont naxer package1. Oparnre nnnmanne na no-
nporpammt BEGIN n END. Hepnax (ona otuno coepxnr nnnnnannsnpymmn xo) nt-
nonnxercx cpasy npn sarpysxe naxera (rounee, ona ntstnaercx, xax rontxo nnrepnperarop
oxonr o nee, ro ecrt eme oo oxonuannx sarpysxn naxera). Coornercrnenno, nonpo-
rpamma END ntstnaercx npn sanepmennn paort nnrepnperaropa n moxer coepxart
xo, ntnonnxmmn saxnmunrentnte onepannn (nanpnmep, saxptnammn nce orxptrte
]ant). Honporpammt BEGIN n END ntstnamrcx nexnntm opasom (onee roro, BE-
GIN ne yacrcx ntsnart xnno, axe ecnn nt +roro ouent saxornre: ona ynnuroxaercx Perl
cpasy nocne ncnontsonannx). B cnny ocoo ponn nmena +rnx nonporpamm cocroxr ns sa-
Crp.261
rnanntx yxn, n xnmuenoe cnono sub xnnxercx nx nnx neoxsarentntm.
Oparnre nnnmanne, uro mt onpeennnn n naxere nonporpammy subroutine1. K ne moxno
yer opamartcx n npeenax xoa, ncnontsymmero naxer. Kpome roro, cronr oparnrt nnn-
manne na xomany return, pacnonoxennym nne xaxo-nno nonporpammt, ona nosnpama-
er snauenne ncrnna nocne sarpysxn naxera, noxastnax, raxnm opasom, uro naxer roron x pa-
ore. (Ha camom ene nosnpamaercx nocnenee snauenne, ntuncnennoe n rene moynx. Ho-
+romy uacro nmecro return n nocnene crpoxe naxera crannrcx npocro nn]pa 1.)
urot ncnontsonart n nporpamme xo naxera, nao nomecrnrt n cnenapn xomany require:
equ1e package1.p1
Tenept moxno cctnartcx na nenrn]nxaropt naxera package1, orennn ero nmx or nen-
rn]nxaropa cnmnonamn ::. B npexnne npemena n xauecrne pasennrenx ntcrynan cnmnon
anocrpo]a (onnouno xantuxn), no renept Perl cneyer n +rom nonpoce rpannnn, ycra-
nonnenno pyrnmn xstxamn nporpammnponannx (a nmenno, C):
equ1e package1.p1
pac pac pac pack kk kage1::subouf1ne1{} age1::subouf1ne1{} age1::subouf1ne1{} age1::subouf1ne1{}
he11o!
Moxno nomecrnrt n naxer pyrne nenrn]nxaropt (nanpnmep, nepemennte):
package package1
8LGlN { }
$va $va $va $va1 11 1ab1e1 ab1e1 ab1e1 ab1e1 = = = = 1 1 1 1
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
urot cocnartcx na nepemennym nne naxera, npnercx cxoncrpynponart cocrannoe nmx
onee cnoxntm opasom. Hepntm ner pastmenontnammn npe]nxc $, sa nnm nmx
naxera, cneom pasennrent :: n rontxo sarem nmx nepemenno, no yxe es pastmeno-
ntnammero npe]nxca:
equ1e package1.p1
package1::subouf1ne1{}
p1nf p1nf p1nf p1nf $pac $pac $pac $pack kk kage1::va1ab1e1 age1::va1ab1e1 age1::va1ab1e1 age1::va1ab1e1
he11o!
1
Hocuasua. Cmapi cuumarcuc, ucno.isvmuu anocmpoq e+ecmo ::, no-nperue+v noooeprueaemc. Ho-
omo+v, uanpu+ep, ec.u npoucxooum uumepno.u o. cmporu 1his is my $owner's house", Perl :ecmuo
nonimaemc uamu nepe+euuvm $s us narema owner (no-euou+o+v, u+eemc e euov ue:mo opveoe). Umo-
oi omoeo ue npousou.o, eocno.isvmeci quevpui+u croora+u. 1his is my $]owner}'s house.
Oparnre nnnmanne: raxnm opasom nentsx opartcx o nepemenntx, onncanntx c xnm-
uentm cnonom my: onn onaamr nexcnuecxo onacrtm nnnmocrn n ocrynnt rontxo
nnyrpn moynx.
Ho ymonuannm ncnontsyercx naxer main. Ho+romy ecnn nt ne yxastnaere nmx naxera
xax, nanpnmep, nx xoncrpyxnnn $::variable1, ro Perl nocrannxer ero cam (ro ecrt ne-
pemennax $::variable1 +xnnnanenrna $main::variable1).
Ha camom ene moxno anromarnuecxn +xcnoprnponart nmena, yxasannte n naxere (xax,
nanpnmep, nmx nonporpammt subroutine1), n rexymym onacrt nmen. 3ro osnauaer, uro
ontme ne nyxno yer yxastnart nmx naxera nepe nmenem nonporpammt, xora nona-
onrcx ee ntsnart. urot onrtcx +roro, neoxonmo ncnontsonart +oov.u.
Crp.262
Mopynu
Moynn +ro naxert, o]opmnennte n nne orentntx ]anon, npnuem nmena nocnennx
connaamr c nmenamn moyne n nmemr pacmnpenne .pm. Cornamenne Perl onpeenxer, uro
nmx moynx onxno naunnartcx c sarnanno yxnt. Ko, coepxamncx n moyne, moxer +xc-
noprnponart rnoantnte nmena moynx n rexymym onacrt rnoantntx nmen. Ho+romy ne
nyxno yxastnart nmx naxera nepe nmenem nenrn]nxaropa npn xaxom opamennn x nemy.
Paccmorpnm npnmep. Mt cosaem moynt c nmenem Module1 n coxpanxem ero n ]ane
Module1.pm. Ko nonporpammt BEGIN, ntnonnxemt n momenr sarpysxn moynx, nc-
nontsyer cranaprnt moynt Exporter, urot +xcnoprnponart n rexymym onacrt rno-
antntx nmen nmx nonporpammt subroutine1:
package Modu1e1
8LGlN {
use Lxpofe {}
0l5A = Lxpofe
0LPOk1 = subouf1ne1 }
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Tenept +ror moynt moxno oannrt x ocnonnomy xoy. Otuno nx +roro ncnontsyercx
xomana use. B cnyuae ncnontsonannx xomant require noxnmuenne ntnonnxercx n mo-
menr ntnonnennx cnenapnx, onaxo c nomomtm use naxer sarpyxaercx na +rane xomnnnx-
nnn. Ho uncro cnyuanocrn ]ant, sarpyxaemte xomano require, n ]ant, sarpyxae-
mte xomano use, no ymonuannm nmemr ono n ro xe pacmnpenne (.pm). C nomomtm
cneymmero naopa xoman mt oannxem x nporpamme moynt Module1 n ntstnaem an-
romarnuecxn +xcnoprnponannym nonporpammy subroutine1:
use Modu1e1
subouf1ne1{}
he11o!
Ham eme mnoro nao ysnart npo naxert Perl. Haxert moxno nxnatnart pyr n pyra
paspemart +xcnoprnponart onpeenennte nmena n ne +xcnoprnponart nx no ymonuannm n
axe ntstnart necymecrnymmne nonporpammt (nocnenee moxno cenart c nomomtm
nonporpammt AUTAD). Oo ncem +rom n eme o ouent mnorom paccxastnaercx n
ocnonno uacrn rnant.

Henocpepc1neuume pemeuun
Kak cospafu nakef
nx cosannx naxera ncnontsyercx xomana package:
package uun
package
3ra xomana orxptnaer nonym onacrt rnoantntx nmen (ro ecrt onacrt nmen annoro
naxera). B onom ]ane moxno oxnnxrt necxontxo naxeron; moxno pasnnart naxer na
necxontxo ]anon, no, xax npannno, onn naxer coornercrnyer onomy ]any. Moxno
raxxe ncnontsonart nnoxennte naxert (cm. anee pasen Cosanne nnoxenntx moy-
Crp.263
ne), no onn ne nacneymr onacrt rnoantntx nmen or npaponrenx. Ho+romy npn nc-
nontsonannn nmen, onpeenenntx n ponrentcxom naxere, npnercx yxastnart nonnoe
nmx. Hpnmep naxera:
package package1
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
Hmx main coornercrnyer onacrn rnoantntx nmen rononnoro cnenapnx. Ero ne cneyer nc-
nontsonart xnno, n ono nocrannxercx anromarnuecxn nx cocranntx nmen c pasennrenem ::,
xora nmx naxera ne yxasano. Ecnn nx xomant package ne saano nmx, ro +ro osnauaer, uro
ne cosaercx uuraro onacrn nmen (n rom uncne n onacrn rnoantntx nmen rnannoro cne-
napnx) n, cneonarentno, npnercx yxastnart nonnoe nmx nx ncex nocneymmnx nepemen-
ntx n nonporpamm c nomomtm npe]nxca n nne nmenn naxera. (3ro axe onee crporoe
rpeonanne, uem ncnontsonanne nparmt use strict, rax xax npnxonrcx yxastnart nonnte
nmena axe nx nonporpamm.) C ocropoxnocrtm nao ncnontsonart nmena naxeron m, s, tr
n y xoncrpyxnnx nna m::... yer nnrepnpernponartcx Perl xax onepannx noncxa c nycrtm
manonom (xomana m/.../), a ne xax nmx nepemenno ns naxera m.
Paccmorpnm npnmep:
package package1
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
Hocne sarpysxn naxera nocnenxx xomana coomaer cncreme o ycnexe onepannn. 3a-
rpysxa naxera ntnonnxercx c nomomtm xomant require (ona neoxonma, ecnn naxer
naxonrcx n ]ane, nnemnem no ornomennm x annomy naxery). Ho ymonuannm xomana
require oannxer x nmenn naxera pacmnpenne .pm, rax uro naxer xpannrcx n ]ane c
pyrnm pacmnpennem, neoxonmo yxasart ero nonnoe nmx:
equ1e package1.p1
Komana require noxnmuaer xo naxera x nporpamme na +rane ntnonnennx, a ne xomnnnx-
nnn. Hocne sarpysxn moxno cctnartcx na nmena nepemenntx n nonporpamm naxera, yxast-
nax c nomomtm pasennrenx :: ero nmx nepe nenrn]nxaropom nepemenno:
equ1e package1.p1
package1::subouf1ne1{}
he11o!
Hocuasua. B oo.acmi u+eu narema sauocmc u+eua, ua:uuamuuec c ovre u cu+eo.oe noo:eprueauu
(ec.u omopocumi pasi+euoeieamuu npequrc). Hcr.m:euue+ e.emc nepe+euua $, eceeoa uaxoo-
uac e oo.acmu u+eu e.aeuoeo ceuapu. Jmo omuocumc marre ro ece+ cneua.iui+ nepe+euui+ Perl
u u+eua+ S1DIA, S1DOU1, S1DERR, ARCJ, ARCJOU1, EAJ, IAC u SIC oes npequrca-u+euu narema
(oare ec.u omu u+eua ucno.isvmmc o. e.e, om.u:uix om ecmpoeuuix ooermoe Perl).
Her neoxonmocrn xpannrt naxert n orentntx ]anax. Komant, npnneennte ntme,
moxno oennnrt n onn ]an, nocxontxy nepexnmuenne n nonym onacrt nnnmocrn
nponcxonr cpasy, xax rontxo nnrepnperarop ncrpeuaer xomany package:
package1::subouf1ne1{}
package package1
sub subouf1ne1 { p1nf "he11o!\n" }
he11o!
Oparnre nnnmanne, uro n annom cnyuae ner neoxonmocrn nn sarpyxart naxer xoman-
o require, nn nosnpamart snauenne ucmuua n xonne xoa, nocxontxy on naxonrcx n
rom xe camom ]ane.
Crp.264
Hocuasua. Oo.acmi e.ooa.iuix u+eu narema npeocmae.em cooo xou, u+ romopoeo coenaoaem c u+e+
narema, uo e roue oooae.emc pasoe.ume.i :: (e uaue+ c.v:ae "package1::). Coomeemcmeeuuo, u+eua,
ucno.isve+ie euvmpu narema, uo no+euae+ie e oo.acmi e.ooa.iuix u+eu e.aeuom ceuapu (c+. npeoi-
ovuvm noocrasrv), uaxoomc e xoue "main::.
Cospauue nakefa: koucfpykfop BEGN
nx nnnnnannsannn naxera otuno ncnontsyercx nonporpamma BEGIN n rene naxera.
Hcnontsyx repmnnonornm oexrno-opnenrnponannoro nporpammnponannx, nonporpam-
my BEGIN moxno nasnart roucmpvrmopo+ narema.
B cneymmem npnmepe nepemenno text npncnannaercx snauenne "Hello!\n", xoropoe
nnocnecrnnn ncnontsyercx n nonporpamme subroutine1:
package package1
sub sub sub sub 8LGlN 8LGlN 8LGlN 8LGlN
{ {{ {
$fexf = "he11o! $fexf = "he11o! $fexf = "he11o! $fexf = "he11o! \ \\ \n" n" n" n"
} } } }
sub subouf1ne1
{
p1nf $fexf
}
efun 1
(B cnny naxnocrn xoncrpyxropon xnmuenoe cnono sub nepe nmenem BEGIN ne-
oxsarentno.) Tenept, nocxontxy nepemennax text nnnnnannsnponana, mt moxem cno-
xono ee ncnontsonart npn ntsone nonporpammt annoro naxera:
equ1e package1.p1
package1::subouf1ne1{} package1::subouf1ne1{} package1::subouf1ne1{} package1::subouf1ne1{}
he11o!
Honporpamma BEGIN ntnonnxercx nenocpecrnenno n momenr sarpysxn (xomnnnxnnn)
naxera eme o sanepmennx cnnraxcnuecxoro pasopa ero coepxnmoro (ro ecrt xax rontxo
Perl naxonr ]nrypnym cxoxy, saxptnammym reno nonporpammt), no+romy, n uacrno-
crn, ona ne onxna cctnartcx na nonporpammt, onpeenennte nnxe. Hocne ntnonnennx
BEGIN nnrepnperarop nemenenno yanxer ee ns onacrn nnnmocrn n rena naxera, rax
uro nam nnxora ne yacrcx ntsnart nonporpammy BEGIN camocroxrentno (ona ncera
ntstnaercx nexnno camnm Perl). Moxno ncnontsonart necxontxo nonporpamm BEGIN n
rene naxera, onn yyr nocneonarentno ntnonnxrtcx n ntpactnartcx ns onacrn nn-
nmocrn n nopxxe cneonannx n no mepe sarpysxn naxera.
Hocxontxy BEGIN ntstnaercx cront pano, +ro yaunoe mecro nx onncannx nporornnon
nonporpamm (ecnn nt ncnontsyere nporornnt cm. pasen Hcnontsonanne npororn-
non n rnane 7). B ne raxxe yono onnctnart crarnuecxne nepemennte naxera, nennn-
mte nsnne, no coxpanxmmne snauenne mexy ntsonamn nonporpamm naxera (cm. pasen
Hocroxnnte (crarnuecxne) nepemennte n rnane 7). Haxonen, nparmt Perl uacro peann-
sonant nmenno n nne nonporpamm BEGIN, nocxontxy onn ntnonnxmrcx n nepnym oue-
pet n, cneonarentno, moryr nnnxrt na paory xomnnnxropa.
Cospauue nakefa: pecfpykfop END
Hoono romy, xax c nomomtm BEGIN nnnnnannsnpyercx naxer, n nonporpamme END
Crp.265
moxno ntnonnnrt nexoropte sanepmammne onepannn npn oxonuannn paort nnrepnpe-
raropa nanpnmep, urot saxptrt orxptrte ]ant n ocnoonrt ncnontsyemte cnc-
remnte pecypct. (Onaxo ne cneyer cnnmxom nonarartcx na paory END, nocxontxy
cnenapn moxer sanepmnrtcx anapnntm opasom eme o roro, xax eno oer o nx
ntsona.) Honporpamma END nastnaercx oecmpvrmopo+ narema
1
. B cneymmem npnmepe
mt neuaraem npomantnoe coomenne c nomomtm END:
package package1
sub 8LGlN { $fexf = "he11o!\n" }
sub subouf1ne1 { p1nf $fexf }
efun 1
sub LND sub LND sub LND sub LND
{ {{ {
p1nf "1hank you fo us1ng package! p1nf "1hank you fo us1ng package! p1nf "1hank you fo us1ng package! p1nf "1hank you fo us1ng package! \ \\ \n nn n" " " "
} }} }
(B cnny naxnocrn ecrpyxropon xnmuenoe cnono sub nepe nmenem END ne xnnxercx ox-
sarentntm.) A nor pesyntrar paort:
equ1e package1.p1
package1::subouf1ne1{}
he11o! 1hank you fo us1ng package!
Bt moxere onpeennrt n onom naxere necxontxo nonporpamm END, onn yyr nt-
stnartcx n nopxxe, oparnom nopxxy cneonannx n rene naxera. (Honporpammt END,
coornercrnymmne pasntm naxeram, yyr ntstnartcx n nopxxe, oparnom nopxxy sa-
rpysxn naxeron c nomomtm xoman require n use.) Kax n BEGIN, nonporpammt END ne
moryr ntstnartcx nontsonarenem, a rontxo camnm Perl (onn npocro ne nxnmuamrcx n o-
nacrt rnoantntx nmen naxera, rax uro npn nontrxe nx ntsona nt nonyunre coomenne
o omnxe: UndeIined subroutine). Bo npemx ntnonnennx END n cnennantnym nepe-
mennym $ sannctnaercx xo ntxoa, no+romy, no-nepntx, ero moxno nsmennrt, npncno-
nn +ro nepemenno nonoe snauenne, a no-nroptx, cneyer conmart ocropoxnocrt npn
ncnontsonannn nnyrpn END xoman, nsmenxmmnx snauenne nepemenno $ (nanpnmep,
ntsonon cncremntx xoman).
Kak onpepenufu fekyu nakef
Hmx rexymero naxera xpannrcx no ncrpoennom nenrn]nxarope PAAGE__. B cne-
ymmem npnmepe nmx rexymero naxera ntnonrcx ns nonporpammt subroutine1, nxo-
xme n naxer package1:
package package1
8LGlN { }
sub subouf1ne1 { p1nf PACAGL }
efun 1
LND { }
A nor pesyntrar ntsona nonporpammt subroutine1:
equ1e package1.p1
package1::subouf1ne1{}
package1

1
To, uro anrop nastnaer ecrpyxropom, n otuno npaxrnxe nporpammnponannx nastnaercx ]nnannsaropom na-
npnmep, nouyncrnyre pasnnny mexy ecrpyxropamn n C n meroamn Iinalie n Java. -Hpu+e:. peo.
Crp.266
Kak pasufu nakef ua ueckonuko qanoe
B onom ]ane moxer xpannrtcx necxontxo naxeron nx +roro ocrarouno nc-
nontsonart xomany package crontxo pas, cxontxo nam nyxno. Ho xax npoonxnrt naxer
sa rpannnt ]ana 3ro, xax oxastnaercx, roxe npocro ocrarouno onpeennrt n ne-
cxontxnx ]anax naxert c onnm n rem xe nmenem c nomomtm xomant package.
Hpenonoxnm, uro n qa.e file1.pl naxoxrcx xomant Perl, onpeenxmmne no-
nporpammy hello (oparnre nnnmanne, uro nmx naxera saano xax package1):
package package1 package package1 package package1 package package1
8LGlN { }
sub he11o { sub he11o { sub he11o { sub he11o { p1nf "h p1nf "h p1nf "h p1nf "he11o! e11o! e11o! e11o!\ \\ \n" n" n" n" } } } }
efun 1
LND { }
Tenept cosanm nropo ]an file2.pl, n xoropom raxxe onpeenxercx naxer package1 n
nonporpamma hello2:
package package1 package package1 package package1 package package1
8LGlN { }
sub he11o2 { sub he11o2 { sub he11o2 { sub he11o2 { p1nf "he11o aga1n! p1nf "he11o aga1n! p1nf "he11o aga1n! p1nf "he11o aga1n!\ \\ \n" n" n" n" } } } }
efun 1
LND { }
Tenept sarpysnm xomano require ]ant file1.pl n file2.pl n ncnontsyem nonporpammt
hello n hello2, ornocxmnecx x naxery package1, no pacnonoxennte n nyx pasntx ]anax:
equ1e f11e1.p1
equ1e f11e2.p1
package1::he11o{}
package1::he11o2{}
he11o!
he11o aga1n!
Hpaxrnunee yer pasnnart naxer na na ]ana nesamerno nx nontsonarenx. nx +roro n
nonporpammy BEGIN nao nomecrnrt xomany require. Tora ]an package1.pl yer co-
epxart xo
package package1
8LGlN { 8LGlN { 8LGlN { 8LGlN { equ1e package2.p1 equ1e package2.p1 equ1e package2.p1 equ1e package2.p1 } } } }
sub he11o { p1nf "he11o!\n" }
efun 1
LND { }
a ]an package2.pl xo
package package1 package package1 package package1 package package1
8LGlN { }
sub he11o2 { p1nf "he11o aga1n!\n" }
efun 1
LND { }
Tenept sarpysxa naxera n ntson nonporpamm hello n hello2 ntrnxnr rax:
equ1e package1.p1
package1::he11o{}
package1::he11o2{}
he11o!
he11o aga1n!
Oparnre nnnmanne na nopxox, n xoropom ntnonnxercx ntson nonporpamm
Crp.267
BEGIN n END n cnyuae pasnnxn naxera:
Hepnt ]an test.pl.
package fesf
8LGlN
{
p1nf "8eg1n/0\n"
equ1e fesf2.p1
p1nf "8eg1n/1\n"
}
LND
{ p1nf "Lnd/A\n" }

package fesf
8LGlN
{
p1nf "8eg1n/2\n"
}
LND
{ p1nf "Lnd/8\n" }
Bropo ]an test2.pl.
package fesf
8LGlN
{
p1nf "8eg1n/3\n"
}
LND
{ p1nf "Lnd/C\n" }

package fesf
8LGlN
{
p1nf "8eg1n/4\n"
}
LND
{ p1nf "Lnd/D\n" }
Inannt ]an:
equ1e fesf.p1
p1nf "1h1s 1s fhe fesf.\n"
Pesyntrar paort:
8eg1n/0
8eg1n/3
8eg1n/4
8eg1n/1
8eg1n/2
1h1s 1s fhe fesf.
LND/8
LND/A
LND/D
LND/C
Cospauue ropyne
Moynt Perl +ro naxer, xoropt xpannrcx n ]ane c rem xe nmenem, uro n y naxera, n c
Crp.268
pacmnpennem .pm. (Hcnontsonanne raxoro pacmnpennx ynpomaer sarpysxy moyne c no-
momtm xoman require n use, nocxontxy onn ncnontsymr pacmnpenne .pm no ymonua-
nnm.) Hanpnmep, nor xax onpeennrt moynt Module1, xpanxmncx n ]ane Module1.pm:
package Modu1e1
8LGlN { }
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Moynn moryr +xcnoprnponart nmena n onacrt rnoantntx nmen nporpammt (nnn py-
roro moynx, ecnn nocnenn sarpyxaer ero), no+romy npe]nxc n nne nmenn moynx n
pasennrenx :: npn ntsone ]ynxnn nnn nepemenntx moyne moxno onycrnrt. (Onaxo
ecnn nam ontme npanxrcx nmena c npe]nxcamn, moxere ocrannrt nx.) Fonee nopono o
rom, xax nayunrt Perl no ymonuannm +xcnoprnponart onpeenennte nmena moynx n re-
xymym onacrt rnoantntx nmen, paccxastnaercx n cneymmem pasene.
Hocuasua. Moruo er.m:umi np+o e me.o +oov. oorv+eumaum (POD Plain Old Documentation), onuci-
eamuvm npaeu.a paoomi c +oov.e+. Oua ueuopupvemc npu saepvsre +oov., ee eceeoa +oreme use.e:i e eu-
oe ASCII-qa.a u.u eo +uorecmee opveux qop+amoe (man, texinfo, HTML, rameMaker MI, ASDC-compliant
PostScript u m. o.) c no+ouim vmu.um, exoouux e cocmae Perl. Hpu+epi eueopeuu oorv+eumauu e roo
+oruo uamu e ca+ux +oov.x, .uoo eo ecmpoeuuo oorv+eumauu. Bo.ee noopoouo oo omo+ paccrasieaemc
e c.eovmue+ pasoe.e Perl.
okyreufupoeauue ropyne
Kax nsnecrno, xopomo nanncannax nporpamma onxna coepxart rexcr na ecrecrnennom
xstxe, noxcnxmmn ee paory, a ne rontxo oneparopt anropnrmnuecxoro xstxa nporpam-
mnponannx. Onnm ns xnaccnuecxnx cnocoon oxymenrnponannx nporpammt xnnxmrcx
xommenrapnn. Onaxo, ecnn nt saxornre nxnmunrt ontmo ]parmenr rexcra, xommenra-
pnn neyont npnercx nomemart cnmnon # n nauane xaxo crpoxn.
Hocuasua. om oo cux nop oo omo+ u ue vno+uua.oci, uo Perl +orem oopaoamieami cmporu, ua:uuamuue-
c co suara ro++eumapu e nepeo nosuuu, cneua.iui+ oopaso+. A u+euuo, roucmpvruu euoa
# 11ne uouep "uun-auna"
pacc+ampueammc rar oupermuei ro+nu.mopv, nepeonpeoe.muue u+ mervueeo qa.a
(FILE) u uo+ep cmporu (LIAE), romopi npuceoeu c.eovmue exoouo cmpo:re.
Hpu+ep.
x pe1
# 11ne 2 # 11ne 2 # 11ne 2 # 11ne 200 00 00 00 "Anonymous I11e" "Anonymous I11e" "Anonymous I11e" "Anonymous I11e"
p1nf LlNL, "\n"
d1e "fesf eo"
200
fesf eo af Anonymous I11e 11ne 201
Koneuno, ecnn nt nomemaere cno rexcr nocne sanepmamme Perl-nporpammy xoncrpyx-
nnn __END__, ro rora ner neoxonmocrn o]opmnxrt ero xax xommenrapn xomnnnx-
rop yer nrnopnponart nce, uro pacnonoxeno nocne __END__. Ho, no-nepntx, yonee
nomemart oxymenrannm nenocpecrnenno pxom c noxcnxemtm xoom, a no-nroptx, ra-
xo cnoco moxer xon]nnxronart c pyrnmn cpecrnamn Perl n uacrnocrn, c nosmox-
nocrxmn, onncanntmn anee n nopasene Hcnontsonanne anrosarpysxn n camosarpysxn
nonporpamm. K cuacrtm, Perl, xax ncera, npeocrannxer onee yont cnoco pemnrt
nocrannennym saauy. A nmenno, xora nt cosaere nporpammy na Perl, nt moxere nxnm-
unrt npxmo n reno nporpammt oxymenrannm (POD Plain Old Document), onnctnam-
mym, xax nao paorart c name nporpammo nnn moynem. Ona yer nrnopnponartcx
Crp.269
npn sarpysxe, no nt ncera moxere nsnneut ee n nne ASCII-]ana nnn axe n nne pas-
meuennoro HTML-]ana c nomomtm yrnnnr, nxoxmnx n cocran Perl.
Hauano oxymenrannn oosnauaercx cnmnonom panencrna () c nocneymmnm xnmuentm
cnonom, pacnonoxenntmn n nepno nosnnnn crpoxn es npemecrnymmnx npoenon, xo-
nen xoncrpyxnne cut, raxxe pacnonoxenno n camom nauane crpoxn. Bnyrpn oxy-
menrannn moryr pacnonarartcx ononnnrentnte xnmuente cnona, raxxe ntenxemte
cnmnonom panencrna n pacnonoxennte n nauane crpoxn, cnennantnte xomant pasmerxn,
onnctnaemte nnxe, n cocrnenno rexcr oxymenrannn. Komnnnxrop Perl nrnopnpyer nect
rexcr or nepnoro snaxa panencrna n o saxnmunrentno xoncrpyxnnn cut nx ncex ]par-
menron oxymenrannn, ncrpeuenntx n rene nporpammt. Haoopor, cnennantnte yrnnnrt
ntpesamr ]parmenrt oxymenrannn, ncrpoennte n Perl-nporpammy, oennxx nx n en-
nt oxymenr n ]opmarnpyx n coornercrnnn c xnmuentmn cnonamn n cnennantntmn xo-
manamn pasmerxn, nrnopnpyx oneparopt Perl. Hpnmep:
= == =pod pod pod pod {hauano ac1poeuuou ox {hauano ac1poeuuou ox {hauano ac1poeuuou ox {hauano ac1poeuuou oxy yy yueu1auu} ueu1auu} ueu1auu} ueu1auu} = == =1fem 1fem 1fem 1fem my my my my1esf 1esf 1esf 1esf
yuxun l<my1esf> npeuaauaueua nn npoaepxu cornacoaauuoc1u uoux auuux.
Oua auauaae1cn bea napaue1poa u ucnonuaye1 rnobanuuue nepeueuuue.
= == =cuf cuf cuf cuf {oue ac1poeuuou oxyue {oue ac1poeuuou oxyue {oue ac1poeuuou oxyue {oue ac1poeuuou oxyueu uu u1auu} 1auu} 1auu} 1auu}
sub my1esf {} {
......
}
Hpn paore co ncrpoenno n nporpammy oxymenranne neoxonmo yunrtnart cneymmn
]axr: xomnnnxrop yer nrnopnponart POD-rexcr, xax rontxo on ncrpernr POD-merxy (ro
ecrt nponsnontnt nenrn]nxarop c npemecrnymmnm snaxom panencrna, pacnonoxennt n
nauantno nosnnnn crpoxn). Onaxo POD-yrnnnrt, nsnnexammne ns nporpammt ncrpoen-
nym oxymenrannm, oprannsonant rax, urot nponepxrt nannune POD-merox rontxo n naua-
ne asanen (ro ecrt nocne nycro crpoxn), +ro oneruaer cnnraxcnuecxn pasop nxonoro
rexcra. Tem camtm, nanpnmep, nt moxere nxnmuart POD-xommenrapnn, xoropte ne nnnt
nn xomnnnxropy, nn yrnnnram, paorammnm c oxymenranne:
$a=3
= == =secef secef secef secef unv1s1b1e unv1s1b1e unv1s1b1e unv1s1b1e fexf fexf fexf fexf
1o1 1exc1 ue auuu uu xoununn1opy,
uu c1auap1uuu POD-y1unu1au Pe1!!!
=cuf 1ef us conf1nue fhe po =cuf 1ef us conf1nue fhe po =cuf 1ef us conf1nue fhe po =cuf 1ef us conf1nue fhe pogam gam gam gam
$b=4
oxymenrannx, nxnmuaemax n ncxont rexcr nporpammt Perl, crpyxrypnpyercx n nne
orentntx asanen (xax yxe yxastnanoct, +ro ynpomaer cnnraxcnuecxn pasop nxonoro
rexcra). Hmemrcx rpn nna asanen:
xomannt (command),
rexcront (ordinary text),
ocnonnt (verbatim),
re aosa +ro ]parmenr rexcra, orenennt or ocrantnoro rexcra nycro crpoxo n na-
uane n n xonne. (Oparnre nnnmanne, uro nexoropte POD-yrnnnrt ue pacc+ampueamm
crpoxy, coepxamym npoent, xax nycrym!)
Komannt asan naunnaercx co snaxa panencrna, sa xoroptm cneyer nenrn]nxarop
xomant. (Oparnre nnnmanne, uro c rouxn spennx xomnnnxropa Perl xomannt asan
nun, ncrpeuennt n rene nporpammt, naunnaer ]parmenr oxymenrannn nnnort o sa-
xnmunrentnoro cut nesanncnmo or roro, xnnxercx u+ saxonno xomano ]opmarnpo-
nannx nnn xe ner.) 3a nenrn]nxaropom xomant, orenennt or nero npoenamn, moxer
Crp.270
nrn rexcr, xoropt nno xnnxercx napamerpom xomant, nno npocro paccmarpnnaercx
xax xommenrapn. B nacroxmee npemx onycrnmtmn xnnxmrcx cneymmne xomant:
pod nycrax xomana, xoropo yono orxptnart ouepeno nox ncrpoenno o-
xymenrannn (ocoenno ecnn on coepxnr rexcr, npoonxammn npetymn ]par-
menr oxymenrannn, xoropt ne rpeyer nauantno xomant ]opmarnponannx).
cut xomana, sanepmammax nox ncrpoenno oxymenrannn.
head1 saeo.oeor saaer sarononox pasena. 3arononox moxer sannmart necxontxo
crpox n cocroxrt ns necxontxnx cnon.
head2 saeo.oeor saaer sarononox nopasena. 3arononox moxer sannmart ne-
cxontxo crpox n cocroxrt ns necxontxnx cnon.
over :uc.o orxptnaer cnncox +nemenron. Kaxt +nemenr cnncxa onnctnaercx
orentno xomano item, a xomana back sanepmaer cnncox (cm. nnxe). Heox-
sarentnt napamerp :uc.o yxastnaer nx POD-yrnnnrt orcryn, xoropt ncnontsyercx
nx ]opmnponannx cnncxa (ontmnncrno POD-yrnnnr ncnontsymr no ymonuannm sna-
uenne ).
item mercm saaer ouepeno +nemenr cnncxa. Ecnn xomana nmeer nn item
mercm, ro ]opmnpyercx mapxnponannt cnncox (bullet list). Ecnn xomana nmeer nn
item 1. mercm, item 2. mercm n r. ., ro ]opmnpyercx nymeponannt cnncox. Ecnn
xe xomana nmeer nn item mercm, re mercm ne connaaer nn c onnm ns +rnx
manonon, ro ]opmnpyercx otunt nemapxnponannt cnncox.
Hmeercx px cornamenn ornocnrentno cnncxon. Tax, xomant item moryr nc-
nontsonartcx rontxo nnyrpn noxa, orpannuennoro xomanamn over n back, a nnyrpn
xaxoro raxoro noxa onxna trt xorx t ona xomana item. Heoxonmo, urot
xomant item tnn cornaconant mexy coo rax, ecnn nepnax xomana saaer map-
xnponannt cnncox, ro n nce ocrantnte xomant onxnt saanart mapxnponannt cnn-
cox (ananornunoe ycnonne cnpanennno n nx nymeponannoro nnn nemapxnponannoro cnn-
cxa). Haxonen, nama POD-yrnnnra moxer nmert cnon cocrnennte npannna opaorxn
cnncxon rax, nexoropte yrnnnrt sanomnnamr rnn cnncxa no nepno xomane item n
nrnopnpymr coornercrnymmne yxasannx nocneymmnx xoman item.
back sanepmaer cnncox +nemenron (cm. ntme xomant over n item).
for +emra yxastnaer, uro rexcr, coepxamncx n +rom asane, npenasnauen nx
onpeenenno POD-yrnnnrt, nenrn]nnnpyemo nmenem +emra. onycrnmtmn mer-
xamn xnnxmrcx text, html, roff, man, latex, tex (nexoropte ns nnx xnnxmrcx cnnonn-
mamn). Hanpnmep, cneymmn asan nxnmuaercx n ntxono oxymenr rontxo n rom
cnyuae, ecnn ncrpoennax n nporpammy oxymenrannx ncnontsyercx nx cosannx
HTML-crpannnt:
=fo hfm1
<b>
<p> 1o npoc1o abaa nn h1ML</p>
begin +emra n end +emra +rn ne xomant ecrnymr ananornuno onncanno
ntme xomane for, no n ornnune or nee ntenxmr nent ]parmenr ncrpoenno o-
xymenrannn (nosmoxno, cocroxmn ns necxontxnx asanen) xax rexcr, npenasnauen-
nt nx xonxperno POD-yrnnnrt. Hpnmep:
=beg1n hfm1
Crp.271
<b> ucyuox 1.1. <lMG 5kC="f1gue.pg"> <b>
=end hfm1
Texcront asan naunnaercx c nepno nosnnnn crpoxn es nauantntx npoenon (n, ecre-
crnenno, ne moxer naunnartcx co snaxa panencrna). Taxo asan ]opmarnpyercx n coor-
nercrnnn co cnenn]nxo xonxperno POD-yrnnnrt (nanpnmep, ntpannnnaercx no nenomy
n npanomy xpam) n c yuerom xoman ]opmarnponannx, yxasanntx n rexcre. onycrnmt
cneymmne xomant ]opmarnponannx:
I<mercm> ntennrt rexcr xypcnnom (ncnontsyercx nx ntenennx rexcra n nx nmen
nepemenntx),
<mercm> ntennrt rexcr nonyxnpntm mpn]rom (ncnontsyercx nx xnmuentx cnon n
nx nasnann nporpamm),
S<mercm> rexcr coepxnr nepasnnaemte npoent,
C<uoeumuquramop> ncnontsyercx nx nnrepantntx xoncranr,
P<u+> ncnontsyercx nx nmen ]anon,
X<u+> nomeuaer +nemenr, nxnmuaemt n nnexc,
<> cnmnon nyneno nnnt, cnyxamn nx pasnnxn nexcnuecxnx +nemenron,
<u+> cnmnon, nmemmn nmx:

E<lt> cnmnon <,
E<gt> cnmnon <,
E<sol> cnmnon /,
Everbar cnmnon [.
<:uc.o> cnmnon c yxasanntm xoom,
<u+> - - cnmnon c yxasanntm cnenn]nuecxnm nmenem (nanpnmep, E<Agrave> nx
HTML-cnmnona, oosnauaemoro xax Agrave;),
<u+> nepexpecrnax cctnxa. onycrnmt cneymmne xoncrpyxnnn:
<u+> crpannna pyxonocrna, nmemmax yxasannoe nmx,
<u+/uoeumuquramop> merxa na crpannne pyxonocrna c yxasanntm nmenem,
<u+/"pasoe." > pasen na crpannne pyxonocrna c yxasanntm nmenem,
</"pasoe."> nnn <"pasoe."> pasen na rexyme crpannne pyxonocrna.
Kpome roro, nosmoxnt cneymmne ]opmt +ro xomant, xora n ntxonom oxymenre
nt ynnnre rontxo yxasannt rexcr, a cama cctnxa yer cxptra or namnx rnas:
<mercmu+> crpannna pyxonocrna c yxasanntm nmenem,
<mercmu+/uoeumuquramop> merxa na crpannne pyxonocrna c yxasanntm nmenem,
<mercmu+/pasoe. > pasen na crpannne pyxonocrna c yxasanntm nmenem,
<mercm/pasoe.> nnn <mercmpasoe.> pasen na rexyme crpannne pyxonocrna
(npn +rom rexcr ne moxer coepxart cnmnonon [ n /, a cnmnont < n > nnyrpn rexcra
onxnt trt napntmn).
Crp.272
Asan rnna verbatim naunnaercx c npoena nnn snaxa raynxnnn. (Ocrantnte crpoxn asana
moryr naunnartcx c nepno nosnnnn.) Taxo asan nocnponsnonrcx n oxymenre onn x o-
nomy c enncrnenntm ncxnmuennem snaxn raynxnnn ntpannnnamr rexcr no rpannnam
cronnon, cocroxmnx ns -mn cnmnonon. Asan ne moxer coepxart xomant ]opmarnpona-
nnx nnn escape-nocneonarentnocrn - nmo rexcr ntnonrcx onn x onomy.
Kak no yronauuk skcnopfupoeafu ureua ropynn
C nomomtm moynx Exporter moxno no ymonuannm +xcnoprnponart nmena moynx n o-
nacrt rnoantntx nmen pyroro moynx nnn nporpammt (ro ecrt ro onacrn, re npons-
nonrcx sarpysxa moynx). Hpn sarpysxe mero import moynx onpeenxer, xaxne nmena
nao nmnoprnponart n onacrt rnoantntx nmen. (Mero - +ro nonporpamma oexra; o
meroax mt yem onee nopono ronopnrt n rnane 1.) Cranaprnt moynt Exporter
nosnonxer nacrponrt +ror mero nx rexymero moynx.
Paccmorpnm npnmep. Cosanm moynt Module1 n +xcnoprnpyem c nomomtm moynx Ex-
porter n onacrt rnoantntx nmen rouxn sarpysxn nonporpammy subroutine1:
package Modu1e1
8LGlN
{
use use use use Lxpofe{} Lxpofe{} Lxpofe{} Lxpofe{}
0l5A 0l5A 0l5A 0l5A = = = = qW{Lxpofe} qW{Lxpofe} qW{Lxpofe} qW{Lxpofe}
0LPOk1 0LPOk1 0LPOk1 0LPOk1 = = = = qW{subouf1ne1} qW{subouf1ne1} qW{subouf1ne1} qW{subouf1ne1}
}
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Maccnn ISA yxastnaer Perl nponepnrt moynt Exporter na npemer meroon, xoropte
ne moryr trt naent n rexymem moyne, a nmenno, na npemer meroa import. Mac-
cnn EPT nepeuncnxer nmena (nannune pastmenontnammnx npe]nxcon oxsarent-
no), +xcnoprnpyemte no ymonuannm. Ecnn nt xornre +xcnoprnponart necxontxo nmen,
nyxno saart xomany rnna
0LPOk1 = qW{subouf1ne1 subouf1ne2 $va1ab1e1}
Tenept, ecnn xaxo-ro xo sarpysnr annt moynt, nonporpamma subroutine1 anroma-
rnuecxn oannxercx x onacrn rnoantntx nmen +roro xoa. Ho+romy +ry nonporpammy
moxno yer ntstnart es npe]nxca n nne nmenn moynx:
use use use use Modu1e Modu1e Modu1e Modu1e1 1 1 1
subouf1n subouf1n subouf1n subouf1ne ee e1{} 1{} 1{} 1{}
he11o!
Bt moxere raxxe yxasart Perl, xaxne nmena paspemeno +xcnoprnponart n rexymym o-
nacrt rnoantntx nmen, no ne cneyer +xcnoprnponart no ymonuannm. Cneymmn pas-
en paccmarpnnaer +ry remy onee nopono.
Kak paspeuufu skcnopf ureuu, uo no yronauuk ero ue skcnopfupoeafu
Xorx nmena moynx moxno +xcnoprnponart no ymonuannm npn sarpysxe, ocropoxnocrt
eme nnxomy ne memana (n xonne xonnon, naxert n moynn cosamrcx oruacrn sarem, uro-
t nsexart nepeceuennx n nanoxennx onacre nmen). Paspemaercx yxastnart Perl, xa-
Crp.273
xne nmena +oruo +xcnoprnponart, no no ymonuannm ne enart +roro. nx +roro neoxo-
nmo nomecrnrt raxne nmena n maccnn EPT_ n ncnontsonart moynt Exporter:
package Modu1e1
8LGlN
{
use Lxpofe{}
0l5A = qW{Lxpofe}
0LPOk1O 0LPOk1O 0LPOk1O 0LPOk1O = = = = qW{su qW{su qW{su qW{subouf1ne1} bouf1ne1} bouf1ne1} bouf1ne1}
}
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Tenept xo, ncnontsymmn annt moynt, cnocoen nmnoprnponart nonporpammy
subroutine1, no npn +rom no ymonuannm nmx +ro nonporpammt ne noxnnrcx n rnoant-
no onacrn nnnmocrn nporpammt, ecnn rontxo nontsonarent ne norpeyer +roro xnno.
Bor xax ntrnxnr nmnopr nmenn subroutine1 n pyrym rnoantnym onacrt nmen:
use Modu1e1 qW use Modu1e1 qW use Modu1e1 qW use Modu1e1 qW{ {{ {subouf1ne1} subouf1ne1} subouf1ne1} subouf1ne1}
subouf1ne1{} subouf1ne1{} subouf1ne1{} subouf1ne1{}
he11o!
Ormernm, uro npn raxo ]opme xomant use nmnoprnpymrcx rontxo nmena, nepe-
uncnennte nocne nmenn moynx, axe ecnn y moynx ecrt pyrne nenrn]nxaropt, +xc-
noprnpymmnecx no ymonuannm. urot nepeuncnennte nmena noxnnnnct n rnoantno
onacrn nmen, onn onxnt cymecrnonart n trt nomeuent xax +xcnoprnpyemte.
Kak ofknkufu urnopf ureu npu sarpyske ropynn
Ecnn nt ne xornre, urot moynt +xcnoprnponan n onacrt rnoantntx nmen cnon nmena,
onncannte xax +xcnoprnpyemte no ymonuannm, nao oannrt nycro cnncox (ro ecrt napy
xpyrntx cxoox) nocne nmenn moynx, yxasannoro n xomane use. Hanpnmep, ecnn moynt
Module1 no ymonuannm +xcnoprnpyer nonporpammy subroutine1, +xcnopr moxno orxnmunrt:
use use use use Modu1e1 {} Modu1e1 {} Modu1e1 {} Modu1e1 {}
subouf1ne1{}
undef1ned subouf1ne ma1n::subouf1ne1 ca11ed af sc1pf.p1 11ne 2
Kak sanpefufu skcnopf ureuu
Ecnn nt ne xornre, urot moynt +xcnoprnponan onpeenennte nmena, nx nao nepeuncnnrt
no npemx paort c moynem Exporter n maccnne EPT_FAI. B xauecrne npnmepa mt
npnneem moynt Uptime.pm, paorammn xax n onepannonntx cncremax cemecrna Unix,
rax n n Windows. 3ror moynt +xcnoprnpyer nonporpammy uptime, noxastnammym, cxont-
xo npemenn xomntmrep naxonrcx nxnmuenntm. urot nonyunrt +ro snauenne, mt ntstna-
em xomany Unix uptime (saxnmunn ee nmx n oparnte anocrpo]t). Hocxontxy n Windows
xomant uptime ne cymecrnyer, ro npn ncnontsonannn moynx nx onepannonno cncremt
Windows (mt nponepxem +ror ]axr, recrnpyx nepemennym $O, coepxamym nmx onepann-
onno cncremt) nonporpamma uptime ne onxna +xcnoprnponartcx:
package upf1me
8LGlN
{
use Lxpofe
Crp.274
0l5A = qW/Lxpofe/
1f 1f 1f 1f {$0 ne M5W1n {$0 ne M5W1n {$0 ne M5W1n {$0 ne M5W1n32} { 32} { 32} { 32} {
0LPOk1 0LPOk1 0LPOk1 0LPOk1 = = = = qW/upf1me/ qW/upf1me/ qW/upf1me/ qW/upf1me/
} e1se { } e1se { } e1se { } e1se {
p1nf "5oy, no upf1me ava11ab1e 1n W1n32. p1nf "5oy, no upf1me ava11ab1e 1n W1n32. p1nf "5oy, no upf1me ava11ab1e 1n W1n32. p1nf "5oy, no upf1me ava11ab1e 1n W1n32.\ \\ \n" n" n" n"
0LPOk1IAlL = qW/upf1me/ 0LPOk1IAlL = qW/upf1me/ 0LPOk1IAlL = qW/upf1me/ 0LPOk1IAlL = qW/upf1me/
} } } }
}
sub upf1me { p1nf upf1me }
efun 1
LND { }
Hycrt nmeercx cnenapn script1.pl, n xoropom mt ncnontsyem nonporpammy uptime ns
moynx Uptime:
use upf1me
upf1me{}
B onepannonno cncreme cemecrna Unix na +xpane noxnnrcx:
245pm 44 days, 20:32, 15 uses, 1oad aveage: 2.21, 1.4, 0.93
B cnyuae Windows pesyntrar yer nntm:
5oy, no upf1me ava11ab1e 1n W1n32.
undef1ned subouf1ne ma1n::upf1me ca11ed af sc1pf1.p1 11ne 2.
Hocuasua. Omuocume.iuo opveux eos+oruocme +oov. Exporter (uanpu+ep, oo ucno.isoeauuu xoua
"EXPOR11ACS) c+. oorv+eumaum +oov. u +uoeo:uc.euuie npu+epi eeo ucno.isoeauu e cmau-
oapmuix +oov.x Perl.
Skcnopfupoeauue es norou refopa iport
Kora xro-ro ncnontsyer nanncannt namn moynt, ntstnaercx mero moynx import. On
nmnoprnpyer nmena, +xcnoprnpyemte moynem. Hexoropte moynn nmemr cocrnennt
mero import, uro osnauaer, uro mero ns moynx Exporter ne ntstnaercx. Onaxo moxno
oprannsonart cocrnennym nponeypy nmnoprnponannx, ncnontsyx mero export_to_level
moynx Exporter.
Otuno on ntstnaercx n nontsonarentcxnx peannsannxx meroa import. Hanpnmep, nor
rax +xcnoprnpyercx nepemennax $variable1 moynx Module1 c nomomtm cocrnennoro
meroa import:
package Modu1e1
8LGlN { }
use Lxpofe{}
0l5A = qW{Lxpofe}
0LPOk1 = qW{$va1ab1e1}
$va1ab1e1 = 100
sub 1mpof
{
p1nf "lm 1mpof\n"
Mo Mo Mo Modu1e1 du1e1 du1e1 du1e1- -- ->expoffo1eve1{1, 0 >expoffo1eve1{1, 0 >expoffo1eve1{1, 0 >expoffo1eve1{1, 0LPOk1} LPOk1} LPOk1} LPOk1}
}
efun 1
LND { }
Ha camom ene n +rom cnyuae mero import enaer ne rax yx mnoro. On ntnonr coome-
nne Im import n +xcnoprnpyer nepemennym $variable1. Hocxontxy nmx nepemenno +xc-
noprnpyercx na onn yponent nnepx (ro ecrt x ntstnammemy moynm), meroy
Crp.275
export_to_level nepeaercx snauenne 1 n cnncox +xcnoprnpyemtx nmen. Tenept moxno
ncnontsonart moynt Module1 n pyrom naxere, a nepemennax $variable1 yer +xcnoprn-
ponartcx anromarnuecxn:
use Modu1e1
p1nf " p1nf " p1nf " p1nf "\ \\ \$va1ab1e1 $va1ab1e1 $va1ab1e1 $va1ab1e1 = ", = ", = ", = ", $va $va $va $va1 11 1ab1e1 ab1e1 ab1e1 ab1e1
ln 1mpof
$va1ab1e1 = 100
Cospauue enoeuuux ropyne
B rnane 13 npn paore c Term::Cap mt nnenn, uro moynn moryr trt e.oreuui+u. A
nmenno, moynt Cap xnnxercx noo+oov.e+ moynx Term. Ha camom ene c rouxn spennx
]nsnuecxoro pacnonoxennx moynn ne nxnatnamrcx (ro ecrt onncanne moynx Cap ne
xnnxercx nnoxenntm n onncanne moynx Term). Hpocro nomoynt pasmemaercx n xara-
nore c nmenem ponrentcxoro moynx, pacnonoxennom nnyrpn omero xaranora nnno-
rexn, Perl paccmarpnnaer pasennrent :: xax pasennrent nmen xaranoron npn noncxe
moyne (ro ecrt moynt Module1::Code1 npenpamaercx n Module1/Code1 nx Unix, n n
Module1Code1 nx Windows).
Paccmorpnm npnmep. Mt cosanm moynt Module1::Code1 n ncnontsyem ero no-
nporpammy subroutine1. urot nanncart ncxont rexcr Module1::Code1, cosanm no-
nt xaranor Module1 n nomecrnm n nero ]an Code1.pm c ncxontm rexcrom. Venrect,
uro xaranor nxonr n nyrt noncxa moyne Perl, - nanpnmep, cosare ero xax noxaranor
cnoero omamnero xaranora nnn xaranora nnnorexn Perl, re xpannrcx ontmnncrno
cranaprntx moyne
1
:
package Modu1e1::Code1
8LGlN
{
use Lxpofe{}
0l5A = qW{Lxpofe}
0LPOk1O = qW{subouf1ne1}
}
sub subouf1ne1 { p1nf "he11o!\n" }
efun 1
LND { }
Tenept moxno ncnontsonart nonporpammy subroutine1 ns moynx Module1::Code1, xo-
ropax ncero-nancero ntneer cnono Hello:
use Modu1e1::Code1
subouf1ne1{}
he11o!
Oparnre nnnmanne, uro nmx moynx, saannoe n xomane package, onpeeneno xax Mod-
ule1::Code1, a ne xax Code1. 3ro n ecrt ncrnnnoe nmx moynx, no xoroptm on yer ns-
necren Perl: nnrepnperarop ne sannmaercx mnoroyponnentm pasopom nmen rnna

1
K +romy conery anropa nao ornocnrcx, cxopee ncero, xax x neymecrno myrxe. Kax npannno, Perl npn noncxe moyne
ncnontsyer uecro.iro cranaprntx xaranoron - cranaprnax nnnorexa n ononnnrentnte (necranaprnte) moynn
naxoxrcx n pasntx xaranorax, panno xax n orentntx xarane npnnxro pacnonarart nnar]opmosanncnmte moynn (xorx
npn pyuno copxe Perl +ro n moxno nsmennrt). Bnepenne n +ry crpyxrypy moyne nontsonarenx na yponne noxa-
ranoron cpasy paspymnr nepenocnmocrt nce cncremt. Kpome roro, cosanne noxaranora n xaranore cranaprno n-
nnorex otuntm nontsonarenem nosmoxno rontxo n ononontsonarentcxo cncreme. - Hpu+e:. peo.

Crp.276
Module1::Code1, xora on cnepna naxonr nmx Module1, a nnmt sarem - nmx Code1. (To
ecrt nnoxennt xaranor tn nyxen, urot Perl cmor narn ]an c rexcrom moynx npn
ntnonnennn xomant use, a nmx naxera, urot cctnartcx na nero npn ocryne x nonpo-
rpammam n nepemenntm naxera nocne sarpysxn.
Rpoeepka eepcuu ropynn
Tenept, xora nt nayunnnct cosanart moynn, pyrne nporpammncrt cmoryr nc-
nontsonart nam xo. Ho uro, ecnn y nnx ner npannntno nepcnn namero xoa Moynt
Exporter nomoxer ycrponrt nponepxy nepcnn moynx. nx +roro npn paore nao onpee-
nnrt nepemennym $VESIN n npncnonrt e nomep nepcnn. Hanpnmep, n cneymmem
cnyuae x saam nomep nepcnn cnoero moynx 1.00:
package Modu1e1
8LGlN { }
use Lxpofe{}
0l5A = qW/Lxpofe/
0LPOk1 = qW/$va1ab1e1/
$vLk5lON $vLk5lON $vLk5lON $vLk5lON = == = 1.00 1.00 1.00 1.00
efun 1
LND { }
Ecnn renept xem-nno ncnontsyercx moynt Module1, nponepxy nepcnn moxno ntnonnnrt
c nomomtm meroa require_version moynx Exporter. Ecnn rexymax nepcnx ne coornercr-
nyer rpeyemo, renepnpyercx coomenne o omnxe. Komana nponepxn ntrnxnr cne-
ymmnm opasom:
use Modu1e1{}
Modu1e Modu1e Modu1e Modu1e1 11 1- -- -> >> >equ1e equ1e equ1e equ1e ves1on ves1on ves1on ves1on{2. {2. {2. {2.00} 00} 00} 00}
Modu1e1 2 equ1ed - fh1s 1s on1y ves1on 1
{Modu1e1. pm} af usem.p1 11ne 2
Fonee xoporxn cnoco yxasart nponepxy nomepa nepcnn nenocpecrnenno n xomane
use, xax +ro onncano n pasene Hcnontsonanne moyne Perl rnant 13. A nmenno, ecnn
cpasy nocne nmenn moynx yxasart uncno, ro Perl nponepxer, urot nomep nepcnn moynx,
yxasannt n nepemenno $VESIN, tn ne mentme nomepa, yxasannoro n xomane use
(n npornnnom cnyuae renepnpyercx coomenne o omnxe):
use Modu1e1 2.0 {}
Modu1e1 2 equ1ed - fh1s 1s on1y ves1on 1
af sc1pf1.p1 11ne 1.
Aefosarpyska ropyne
Btson necymecrnymme nonporpammt otuno npnnonr x coomennm o omnxe, ecnn
rontxo ne tna onpeenena nonporpamma c nmenem AUTAD. Hmenno ona ntstna-
ercx npn opamennn x necymecrnymme nonporpamme. Hmx ntsnanno nonporpammt
sanocnrcx n nepemennym $AUTAD, a nepeannte e aprymenrt n maccnn _.
3auacrym necymecrnymmax nonporpamma na camom ene cymecrnyer, no naxonrcx n
moyne, xoropt nt ne xornre sarpyxart, noxa on ne nonaonrcx (nmenno no+romy an-
nt nponecc nastnaercx anrosarpysxo). Onaxo nonporpammt moxer ecrnnrentno ne
trt, n n +rom cnyuae AUTAD onxna nmnrnponart ee paory. Hanpnmep, moxno
nayunrt AUTAD ntnonnxrt cncremnte xomant xax nonporpammt, npocro saxnm-
Crp.277
uax ntsnannym nonporpammy n ee aprymenrt n oparnte anocrpo]t.
Paccmorpnm npnmep. B moyne Autoload.pm mt ncnontsyem AUTAD nx ntnoa
nmenn n aprymenron necymecrnymme nonporpammt. Oparnre nnnmanne, uro nonpo-
rpamma AUTAD +xcnoprnpyercx npn sarpysxe moynx.
package Aufo1oad
8LGlN {
use Lxpofe {}
0l5A = qW{Lxpofe}
0LPOk1 = qW{Au1OLOAD}
}
sub Au1OLOAD {} {
my $subouf1ne = $Au1OLOAD
$subouf1ne = s/.":://
p1nf "ou ca11ed $subouf1ne W1fh fhese agumenfs: ", o1n{", ", 0}
}
efun 1
LND { }
3amertre, uro nmx nonporpammt, sanecennoe n nepemennym $AUTAD, coepxnr
nmx naxera, n xoropom ona (no mnennm Perl) onxna naxonrtcx. Hanpnmep, ecnn mt nt-
stnaem ns cnenapnx necymecrnymmym nonporpammy printem, nepemennax $AUT-
AD yer coepxart rexcr main::printem:
use Aufo1oad
p1nfem {1, 2, 3}
B moyne Autoload.pm nmx moynx yanxercx, a ocraercx rontxo nmx camo no-
nporpammt. Bor pesyntrar ntsona printem:
ou ca11ed p1nfem W1fh fhese agumenfs: 1, 2, 3
Tenept, xora nt snaere, xaxax nonporpamma tna ntsnana n c xaxnmn aprymenramn, c
nomomtm xomant require moxno sarpysnrt coepxamn ee moynt nnn ctmnrnponart
ee noneenne nnyrpn AUTAD.
Hocuasua. Koeoa Perl ue +orem uamu noonpoepa++v e +oov.e, ou e nepevm o:epeoi uuem noonpo-
epa++v AU1OLOAD us omoeo +oov. u mo.iro e c.v:ae uevoa:u eisieaem noonpoepa++v AU1OLOAD us
mervue oo.acmu euou+ocmu. Te+ ca+i+, ei +oreme o. rarooeo +oov. onpeoe.umi coocmeeuuvm
noonpoepa++v AU1OLOAD, oopaoamieamuvm omcvmcmevmuue e oauuo+ +oov.e noonpoepa++i. Ooua-
ro ovoime ocmoporui npu orcnopme u+eu. e maro+ c.v:ae noonpoepa++i AU1OLOAD more oo.rui
orcnopmupoeamic, a noonpoepa++i us pasuix, +oov.e +oevm neperpieamic.
Hcnonusoeauue aefosarpysku u carosarpysku nopnporparr
Ecnn nt ne xornre sarpyxart n xomnnnnponart nect cnenapn, ro moxere pasnrt ero na
moynn. nx +roro cnyxar moynn AutoLoader n AutoSplit. B +rom cnyuae rnannax uacrt
moynx oprannsyercx cnennantntm opasom, nosnonxmmnm no npemx paort cnenapnx sa-
rpyxart n xomnnnnponart neocrammne nonporpammt, a camn nonporpammt raxxe
pacnonaramrcx n ocoo ycrpoenntx orentntx ]anax. Kax +ro cenart na npaxrnxe, mt
ceuac paccxaxem.
Camt npocro (n cranaprnt) cnoco pasmemennx nonporpammt no orentntm ]anam
rax, urot nx moxno tno nnamnuecxn sarpyxart c nomomtm cpecrn moynx AutoLoader
ncnontsonanne moynx AutoSpIit. Hepe nauanom noxa nonporpamm n ncxonom moyne
nomemaercx xoncrpyxnnx __END__ (n pesyntrare xomnnnxrop yer nrnopnponart nox npn
sarpysxe moynx), n c nomomtm orentnoro cnenapnx c ntsonom meroa autosplit moynx
Crp.278
AutoSpIit moynt pasnnaercx na ]parmenrt. Hanpnmep, nx DOS nnn Windows nx +roro
moxno cosart .bat-]an pm2al.bat:
pe1 -W -e use Aufo5pl1f aufosp11f{"x1", "x2"}
sect nepntm napamerpom ntcrynaer nmx moynx, a nroptm xaranor, n xoropt yer
sanncan pesyntrar. B Unix nx sh-noontx oonouex +ro yer ntrnxert rax:
#!/b1n/sh
pe1 -We use Aufo5pl1f aufosp11f{$1, $2}
Mero autosplit nmeer cneymmn nn:
aufosp11f {aun, xa1anor, keep, check, modf1me}
Hepnt napamerp saaer nmx ]ana, xoropt onxen trt pasnr na orentnte nonpo-
rpammt (npn +rom cam ]an ne menxercx). B xauecrne nroporo napamerpa saaercx xara-
nor, n xoropom yyr pasmemartcx orentnte ]ant (no onomy ]any na xaxym no-
nporpammy). Ecnn xaranor ne yxasan nnn n xauecrne nero yxasana nycrax crpoxa, ncnont-
syercx xaranor lib/auto. Ocrantnte napamerpt xnnxmrcx neoxsarentntmn n npecrannx-
mr coo nornuecxne snauennx, ynpannxmmne paoro meroa autosplit.
Hocuasua. Umooi usoerami eos+oruix npoo.e+, e oouo+ qa.e oo.reu uaxooumic oouu +oov.i (narem).
Mero autosplit pasnnaer nonporpammt moynx no orentntm ]anam c pacmnpennem
.al. On raxxe cosaer nnexc autosplit.ix, pacnonoxennt n rom xe xaranore, uro n ]ant
.al. (3ror ]an yer ncnontsonartcx nx anrosarpysxn nonporpamm c nomomtm moynx
AutoLoader. Moynt AutoLoader onxen ntstnartcx n sarononxe moynx, pacnonoxenno-
ro o xoncrpyxnnn __END__; o nem noer peut nnxe.) B xauecrne nmenn nxonoro ]a-
na nt onxnt yxasart ]an, n xoropom pacnonoxen ncxont rexcr namero moynx n n
xoropom nox nonporpamm orenen or sarononxa xoncrpyxnne __END__. Ecnn ]an
nmeer cranaprnoe pacmnpenne .pm, ero moxno ne yxastnart autosplit oannr pac-
mnpenne cam. Ecnn ]an pacnonoxen ne n rexymem xaranore, nmx ]ana onxno coep-
xart nonnt nyrt. (Hanpnmep, ecnn moynt nnoxennt ro ecrt on nmeer nmx nna
u+1..u+2..u+3, nt onxnt camn samennrt pasennrent :: nmen naxeron na pase-
nnrent / nmen xaranoron.)
Hmx xaranora, saannoe n xauecrne napamerpa autosplit, cnyxnr rontxo nauantno rouxo
orcuera nx pasmemennx ]anon (xax npannno, +ro noxaranor auto xaranora, n xoropom
pacnonoxen ncxont moynt). B xauecrne ncrnnnoro mecra ncnontsyercx noxaranor
yxasannoro xaranora, nmx xoroporo connaaer c nmenem moynx. Hmx ]ana c pacmnpenn-
em .al connaaer c nmenem nonporpammt, xoropax n nero nomemaercx. Hanpnmep, ecnn
moynt nastnaercx Modul1, nonporpamma subr1, a n xauecrne asonoro xaranora yxa-
sano "./auto", nonporpamma yer sanncana n ]an ./auto/Modul1/subr1.al. Ecnn xe nmx
moynx cocrannoe (ro ecrt nmeer nn u+1..u+2..u+3), mt nonyunm cncremy nno-
xenntx xaranoron. Heoxonmo nouepxnyrt, uro nmx nxonoro ]ana ncnontsyercx
rontxo nx ocryna x ]any, nmx moynx mero autosplit nsnnexaer ns opaartnaemo-
ro nm ]ana camocroxrentno.
Hocuasua. Heromopie onepauouuie cucme+i ue noooeprueamm c.uuro+ o.uuuix u+eu noonpoepa+-
+i u/u.u narema. B marux c.v:ax AutoSplit vceraem u+ qa.a, o :e+ eioaem npeovnperoamuee cooo-
ueuue, +orem eosuuruvmi rouq.urm u+eu qa.oe, coomeemcmevmuux pasui+ +oov.+ u noonpo-
epa++a+. Bo usoerauue noooouix cumvau cmapameci npu ucno.isoeauuu AutoSplit u AutoLoader npu-
+eumi ropomrue u+eua.
Ocrantnte napamerpt xnnxmrcx neoxsarentntmn n npecrannxmr coo nornuecxne
]narn, npnnnmammne snauennx nont nnn ennnna, xoropte ynpannxmr paoro meroa
Crp.279
autosplit. Tax, ecnn rpern napamerp (keep) +ro .ori, ro nce cymecrnymmne ]ant c
pacmnpennem .al n xaranore, ncnontsyemom meroom autosplit nx sanncn pesyntrara pa-
ort, yanxmrcx, ecnn nx nnx n ncxonom moyne ner nanexame nonporpammt (ya-
nenne craptx nepcn). Ecnn uerneprt napamerp (check) +ro ucmuua, ro mero
autosplit nponepxer, uro n sarononxe moynx ecrnnrentno ncnontsyercx moynt Auto-
Loader, n npexpamaer paory, ecnn +ro ne rax. Haxonen, ecnn nxrt napamerp (modtime)
+ro ucmuua, ro mero autosplit nponepxer, uro ncxont rexcr moynx ne mon]nnn-
ponancx co npemenn nocnenero ntsona autosplit (cpannnnax npemx cosannx nnexcnoro
]ana autosplit.ix n npemx nocnenero peaxrnponannx nxonoro ]ana) n ne ntnonnxer
nnxaxnx ecrnn, ecnn nonropnoe pasnenne moynx na nonporpammt ne rpeyercx.
(3nauennx no ymonuannm: keep0, check1, modtime1.)
Hocuasua. Ec.u ei xomume, :mooi autosplit eieoou. na orpau coooueuu e npoecce ceoe paoomi, ne-
peo eeo eisoeo+ vcmauoeume nepe+euuvm $AutoSplit::JERBOSE e 1 u.u 2.
urot moynt mor ncnontsonart mexannsm anrosarpysxn ]anon, cosanntx c nomomtm
autosplit, ero rnannax uacrt onxna trt oprannsonana cnennantntm opasom. Hpnmepom
moxer cnyxnrt moynt POSIX, xoropt pasnnaer cnon mnorouncnennte nonporpammt na
orentnte ]ant n sarpyxaer nx rontxo rora, xora onn ecrnnrentno rpeymrcx nontso-
narenm:
package PO5l
use AufoLoade
......
sub Au1OLOAD
{
$AufoLoade::Au1OLOAD = $Au1OLOAD
gofo Aufo1oade::Au1OLOAD
}
......
3ect moynt POSIX onpeenxer cocrnennym nonporpammy AUTAD, xoropax nepeaer
ynpannenne nonporpamme AUTAD moynx AutoLoader, ecnn nonporpamma, xoropym
xouer ntsnart nontsonarent, orcyrcrnyer, AUTAD ntraercx narn n sarpysnrt ]an, n
xoropom onpeenen neocrammn xo. (Ha camom ene nonporpamma AUTAD moynx
POSIX ycrpoena cnoxnee, no mt nmeem eno c yuentmn npnmepamn, a ne peantntmn npo-
rpammamn.) Hpenonoxnm, uro moynt POSIX pacnonoxen n ]ane /usr/local/lib/perl5/
POSIX.pm. Tora AUTAD nontraercx sarpysnrt ]an
/usr/local/lib/perl5/auto/POSIX/noonpoepa++a.al. Ecnn +ror ]an cymecrnyer, AUTAD o-
paoraer ero coepxnmoe n nepeacr ynpannenne nonporpamme nmecre c nonyuenntmn napa-
merpamn. Hocne +roro nonporpamma c yxasanntm nmenem cranonnrcx onpeenenno, n nocne-
ymmne ntsont npoxoxr yxe es yuacrnx AUTAD. Ecnn ncnontsyercx mnoroyponne-
nt naxer - nanpnmep, Term::Cap, pacnonoxennt n ]ane /usr/local/lib/perl5/Term/Cap.pm,
AUTAD yer yunrtnart mnoroyponnenym crpyxrypy ornannenn (ro ecrt yer sarpy-
xart ]an /usr/local/lib/perl5/auto/Term/Cap/noonpoepa++a.al).
Hocuasua. Ec.u noonpoepa++a AU1OLOAD ue +orem uamu qa. nonpoepa++a.al, o. saepvsru qa.a
auto/nonpoepa++a.al ucno.isvemc ro+auoa require. Ta uuem vrasauui qa. no ece+ nvm+ noucra (ue
oosame.iuo cesauuix c mervuu+ naremo+) u saepvraem nepei uaoeuui qa.. B omo+ c.v:ae c.eovem
u+emi e euov, :mo mervuu noorama.oe marre exooum e cnucor nvme noucra, mar :mo qa.i, no.v:euuie
e pesv.imame oecmeu +emooa autosplit, +oruo pas+ecmumi rar ./auto/nonpoepa++a.al, a, uanpu+ep, uc-
xoouie +oov.u, ucno.isoeauuie o. ux eeuepauu, rar ./lib/+oyuo.pm. Oouaro, ec.u eo epe+ paoomi ce-
uapu c no+ouim qvuruu chdir us+euumi mervuu rama.oe, +oevm eosuuruvmi npoo.e+i.
Hocxontxy moynt POSIX ycrpoen ocrarouno cnoxno, emy norpeonanoct onpeennrt
cocrnennym nonporpammy AUTAD, xoropax nomnmo ntsona nonporpammt
Crp.280
AUTAD moynx AutoLoader ntnonnxer eme n nexoropte ononnnrentnte ecrnnx.
B ontmnncrne cnyuaen ocrarouno npocro nmnoprnponart n moynt nonporpammy
AUTAD moynx AutoLoader:
package MyModu1e
use AufoLoade Au1OLOAD
......
LND
sub subouf1ne1 { ...... }
......
Oparnre nnnmanne, uro nmx nmnoprnponanno nonporpammt onxno trt yxasano xnno,
nocxontxy no ymonuannm ona ne nmnoprnpyercx, xorx ee nmnopr n paspemen. (Honporpam-
mt n nepemennte rnna AUTAD, nrpammne cnennantnym pont n Perl, ne onxnt +xc-
noprnponartcx no ymonuannm, nocxontxy nennx pncx cmemnnannx nenrn]nxaropon.)
Hcnontsonanne moyne AutoSplit n AutoLoader nosnonxer ynpocrnrt nponemy cosannx
nporornnon nonporpamm nnexcnt ]an autosplit.ix, cosannt meroom autosplit, sa-
rpyxaercx n momenr ntnonnennx xomant use Autooader, (require Autooader noonoro
noneennx ne oecneunnaer). Hocxontxy manont ntsona nonporpamm naxnt nx nponepxn
cnnraxcnca, moynt AutoLoader ncera sarpyxaercx c nomomtm xomant use, a ne require.
Kpome roro, nyume nomecrnrt xomany use Autooader n reno nonporpammt BEGIN, rax
xax rora nporornnt nonporpamm cranyr nsnecrnt Perl n nepnym ouepet.
Bmecro moyne AutoSplit n AutoLoader moxno ncnontsonart raxxe moynt SelI-
Loader. B +rom cnyuae onpeenennx nonporpamm onxnt pasmemartcx nocne xon-
crpyxnnn __DATA__ (nmecro xoncrpyxnnn __END__ ), rax uro onn raxxe yyr nrnopn-
ponartcx xomnnnxropom. Hpn ntsone +rnx nonporpamm moynt SelILoader yer xomnn-
nnponart n sarpyxart nx. B cneymmem npnmepe SelILoader ncnontsyercx nx nnamnue-
cxo sarpysxn nonporpammt subroutine1 ns moynx Module1:
package Modu1e1
8LGlN
{
use Lxpofe
use 5e1fLoade
0l5A = qW{Lxpofe 5e1fLoade} 0l5A = qW{Lxpofe 5e1fLoade} 0l5A = qW{Lxpofe 5e1fLoade} 0l5A = qW{Lxpofe 5e1fLoade}
0LPOk1 = qW 0LPOk1 = qW 0LPOk1 = qW 0LPOk1 = qW/subouf1ne1/ /subouf1ne1/ /subouf1ne1/ /subouf1ne1/
}
efun 1
LND { }
DA1A DA1A DA1A DA1A
sub subouf1ne1 { sub subouf1ne1 { sub subouf1ne1 { sub subouf1ne1 { p1nf "he11o! p1nf "he11o! p1nf "he11o! p1nf "he11o!\ \\ \n" n" n" n" } }} }
Tenept ntsonem nonporpammy subroutine1 moynx Module1 ns pyroro moynx, -
oparnre nnnmanne, uro nonporpamma subroutine1 ne xomnnnnpyercx n ne sarpyxaercx
o nepnoro ntsona:
use Modu1e1
subouf1ne1{}
he11o!
Paccmorpnm, xax paoraer moynt SelILoader. Koncrpyxnnx __DATA__, xoropax noxnnnact
nnepnte n Perl nepcnn 5.001m, yxastnaer, uro xo, npenasnauennt nx xomnnnxnnn, sa-
xonunncx. Coepxnmoe ]ana or xoncrpyxnnn __DATA__ n o xonna ]ana nnn o xoncr-
pyxnnn __END__ opasyer nceno]an, ocrynnt nx urennx uepes ecxpnnrop
Harem::DATA (sect Harem +ro nmx naxera, xoropt opaartnancx n ror momenr, xora
Crp.281
tna ocrnrnyra merxa __DATA__).
Module1 nmnoprnpyer nonporpammy AUTAD ns moynx SelILoader. Hpn nepnom xe o-
pamennn x nonporpamme, xoropax ne onpeenena n annom moyne, AUTAD orxpoer
nceno]an, npounraer ero coepxnmoe, cxomnnnnpyer n sarpysnr n namxrt nce nonporpam-
mt, xoropte ncrperxrcx e npn urennn. Coornercrnenno, npn nocneymmnx opamennxx no-
nporpammt yyr yxe onpeenent n nonporpamma AUTAD ntstnartcx ne yer.
Hcnontsonanne SelILoader nosnonxer nsexart cnoxno nepapxnn ]anon na ncxe n neo-
xonmocrn ntstnart autosplit xaxt pas, xora n rexcr moynx nnocxrcx nsmenennx (a
raxxe ntnonnxrt noncx nyxnoro ]ana npn nepnom ntsone nonporpammt). 3a +ro npnxo-
nrcx nnarnrt onee onrnm npemenem sarpysxn, rax xax crpoxn rexcra, cneymmne sa xoncr-
pyxnne __DATA__, opaartnamrcx cnnraxcnuecxnm anannsaropom (xax annte, a ne xax
xomnnnnpyemt xo). Heocrarxom moynx SelILoader xnnxercx ro, uro on sarpyxaer cpasy
nce nonporpammt moynx n ne nosnonxer onpeenxrt nx moynx cocrnennym nonpo-
rpammy AUTAD (ona ncera nmnoprnpyercx ns moynx SelILoader). Hpenmymecrnom xe
moynx AutoLoader xnnxercx onee tcrpax sarpysxa moynx n orcyrcrnne nnmnnx anntx n
namxrn.
Crp.282
Fnana 16

Cospauue knaccon u o+ek1on

Kopo1ko
Oexrno-opnenrnponannoe nporpammnponanne +ro nonax rexnnxa, nonnomammax n
xnsnt crapt npnnnnn nporpammnponannx: pasenx n nnacrny. Hex oexrno-
opnenrnponannoro nporpammnponannx cocronr n uurancv.upoeauuu anntx n nonpo-
rpamm (nastnaemtx +emooa+u) n nne oexron. B pesyntrare xaxt oexr cranonnrcx
nonyanronomntm, npnnarnte (ro ecrt nnyrpennne) annte n merot oocannnamrcx
rax, urot onn ne cosanann ecnopxox n ome onacrn anntx. Oexr nsanmoecr-
nyer c ocrantno uacrtm nporpammt c nomomtm xopomo npoymannoro nnrep]eca a
nmenno, c nomomtm nynnuntx (ocrynntx nx ntsona nsnne) meroon.
B Perl peannsonan ne]opmannsonannt napnanr oexrno-opnenrnponannoro noxoa
no cyrn, nce enaere cnonmn pyxamn. Oexrno-opnenrnponannoe nporpammnponanne
sect npamaercx noxpyr necxontxnx xnmuentx nonxrn: xnacct, oexrt, merot, na-
cneonanne. Bor onee erantnoe pasxcnenne +rnx nonxrn c rouxn spennx Perl:
K.acc +ro naxer, xoropt moxer oecneunnart merot.
Memoo +ro nonporpamma, ncrpoennax n xnacc nnn oexr. Mero ncnontsyer cctn-
xy na oexr nnn nmx xnacca, nepeanaemoe emy n xauecrne nepnoro aprymenra.
Ooerm +ro cctnxa na +nemenr anntx. B ornnune or pyrnx cctnox, +ror +nemenr
snaer, x xaxomy xnaccy on ornocnrcx. Oexrt cosamrcx uepes xnacct.
Hac.eooeauue +ro nponecc nopoxennx onoro xnacca (ouepnero, nnn no-
poxennoro) ns pyroro xnacca (asonoro, nnn ponrentcxoro). B pesyntrare +roro npo-
necca ouepnn xnacc moxer ncnontsonart merot ponrentcxoro xnacca.
Bce +rn xoncrpyxnnn xnnxmrcx naxno uacrtm oexrno-opnenrnponannoro npo-
rpammnponannx. B +ro rnane yer erantno paccmorpena xaxax ns nnx.
Knaccu
B Perl xnacc - +ro npocro naxer, oecneunnammn merot nx pyrnx uacre nporpammt.
(Mero - +ro nonporpamma, cnxsannax c oexrom nnn xnaccom.)
B oexrno-opnenrnponannom nporpammnponannn (OOH) xnacct npecrannxmr coo
neuro rnna manonon nx oexron. To ecrt ecnn npecrannrt cee xnacc xax ]opmy nx