Sams Teach Yourself Perl In 21 Days 2nd Edition Jun 2002 ISBN 0672320355 pdf

Teach Yourself Perl 5 in 21 days

  David Till

Table of Contents:

Introduction ●

   Week 1

  

Day 1 Getting Started ●

  

  ●

  

Day 2 Basic Operators and Control Flow ●

  

  

  

  Day 3

  ●

  

Day 4 More Operators ●

  

  ❍

  

Day 5 Lists and Array Variables ●

  

  ❍

  

Day 6 Reading from and Writing to Files ●

  

  ●

  

  

Day 7 Pattern Matching ●

  

  

  ❍

  

Week 1

   Day 8

  

  

  

  

  

  

  ❍

  

Day 9 Using Subroutines ●

   Day 10

  

  ●

  

Day 11 Formatting Your Output ●

  

  ❍

  Day 12

  

  

  

  

  

Day 13 Process, String, and Mathematical Functions ●

  

  ❍

  

  

  

  

  

  

Day 14

  

  

  

  

  

  

  ❍

  

  

  

  

  

  

  

   Week 2

  

Day 15 System Functions ●

  

  ❍

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  ❍

  

  

  

  

  

Day 16 Command-Line Options ●

  

  

  

  

  

  

  

  

  

  

  

  

  

  ●

  

  

  

Day 17

  

  

  

  

  

  ❍

  

  

  

  

  

  

  

  

  

  

   Day 18

  

  ●

  

  

Day 19 Object-Oriented Programming in Perl ●

  

  ●

  

  

  

  

  

  ●

  

  

  

  

  

  

  

  

  

  

  

  ❍

  Week 3

  ❍

  

  Appendix B Co pyr ig ht © 1996 by Sa ms Pu bl ish in g SECOND EDITION syst em, or t r a n smit t ed by a n y mea n s, el ect r on ic, mech a n ica l , ph ot ocopyin g, r ecor din g, or ot h er wise, wit h ou t wr it t en per mission f r om t h e pu bl ish er . No pa t en t l ia bil it y is a ssu med wit h r espect t o t h e u se of t h e in f or ma t ion con t a in ed h er ein . Al t h ou gh ever y pr eca u t ion h a s been t a ken in t h e pr epa r a t ion of t h is book, t h e pu bl ish er a n d a u t h or a ssu me n o r espon sibil it y f or er r or s or omission s. Neit h er is a n y l ia bil it y a ssu med f or da ma ges r esu l t in g f r om t h e u se of t h e in f or ma t ion con t a in ed h er ein . For in f or ma t ion , a ddr ess Sa ms Pu bl ish in g, 201 W. 103r d St ., In dia n a pol is, IN 46290.

  Int e r nat io nal St andar d Bo o k Numbe r : 0-672-30894-0 HTML c o nve r sio n by : M/s. Lea f Wr it er s (In dia ) Pvt . Lt d.

  Website

  Publ ishe r and Pr e side nt Richard K. Swadley Ac quisit io ns

  Manag e r Greg Wiegand

  De ve l o pme nt Manag e r Dean Miller Manag ing Edit o r Cindy Morrow

  Mar ke t ing Manag e r John Pierce Assist ant Mar ke t ing Manag e r Kristina Perry

  Ac quisit io ns Edit o r Chris Denny De ve l o pme nt Edit o r s Angelique Brittingham, Keith Davenport

  So f t war e De ve l o pme nt Spe c ial ist Steve Straiger

  Pr o duc t io n Edit o r Tonya R. Simpson Co py Edit o r Kimberly K. Hannel Te c hnic al Re vie we r Elliotte Rusty Harold Edit o r ial Co o r dinat o r

  Bill Whitmer Te c hnic al Edit Co o r dinat o r

  Lynette Quinn Fo r mat t e r Frank Sinclair Edit o r ial Assist ant s

  Carol Ackerman, Andi Richter Rhonda, Tinch- Mize

  Co ve r De sig ne r Tim Amrhein Bo o k De sig ne r Gary Adair Co py Wr it e r Peter Fuller

  Pr o duc t io n Te am Supe r viso r Brad Chinn

  Pr o duc t io n Michael Brumitt, Charlotte Clapp, Jason Hand, Sonja Hart, Louisa Klucznik, Ayanna Lacey, Clint Lahnen, Paula Lowell, Laura Robbins, Bobbi Satterfield, Carol Sheehan, Chris Wilcox I wou l d l ike t o t h a n k t h e f ol l owin g peopl e f or t h eir h el p: Da vid Ma ckl em a t Siet ec Open Syst ems f or a l l owin g me t o t a ke t h e t ime of f t o wor k on t h e f ir st edit ion of t h is book Ever yon e a t Sa ms Pu bl ish in g, f or t h eir ef f or t s a n d en cou r a gemen t Jim Ga r dn er , f or t el l in g t h e peopl e a t Sa ms Pu bl ish in g a bou t me

  I'd a l so l ike t o t h a n k a l l t h ose f r ien ds of min e (you kn ow wh o you a r e) wh o t ol er a t ed my goin g st ir -cr a z y a s my dea dl in es a ppr oa ch ed.

About the Authors

  David Till

  Da vid Til l is a t ech n ica l wr it er wor kin g in Tor on t o, On t a r io, Ca n a da . He h ol ds a ma st er 's degr ee in compu t er scien ce f r om t h e Un iver sit y of Wa t er l oo; pr ogr a mmin g l a n gu a ges wa s h is ma jor f iel d of st u dy. He a l so h a s wor ked in compil er devel opmen t a n d on ver sion -con t r ol sof t wa r e. He l ist s h is h obbies a s "wr it in g, comedy, wa l kin g, du pl ica t e br idge, a n d f a n a t ica l su ppor t of t h e Tor on t o Bl u e Ja ys." He ca n be r ea ch ed via e-ma il a t am671@freenet.toronto.on.ca or davet@klg.com , or on t h e Wor l d Wide Web a t

  Kamran Husain Ka mr a n Hu sa in is a sof t wa r e con su l t a n t wit h exper ien ce in UNIX syst em pr ogr a mmin g.

  He h a s da bbl ed in a l l sor t s of sof t wa r e f or r ea l -t ime syst ems a ppl ica t ion s, t el ecommu n ica t ion s, seismic da t a a cqu isit ion a n d n a viga t ion , X Win dow/Mot if a n d Micr osof t Win dows a ppl ica t ion s. He r ef u ses t o divu l ge a n y mor e of h is qu a l if ica t ion s. Ka mr a n of f er s con su l t in g ser vices a n d t r a in in g cl a sses t h r ou gh h is compa n y, MPS In c., in Hou st on , Texa s. He is a n a l u mn u s of t h e Un iver sit y of Texa s a t Au st in . khusain@neosoft.com

  You ca n r ea ch Ka mr a n t h r ou gh Sa ms Pu bl ish in g or via e-ma il a t or mpsi@aol.com .

  Introduction

  Th is book is design ed t o t ea ch you t h e Per l pr ogr a mmin g l a n gu a ge in ju st 21 da ys. Wh en you f in ish r ea din g t h is book, you wil l h a ve l ea r n ed wh y Per l is gr owin g r a pidl y in

Who Should Read This Book?

  No pr eviou s pr ogr a mmin g exper ien ce is r equ ir ed f or you t o l ea r n ever yt h in g you n eed t o kn ow a bou t pr ogr a mmin g wit h Per l f r om t h is book. In pa r t icu l a r , n o kn owl edge of t h e C pr ogr a mmin g l a n gu a ge is r equ ir ed. If you a r e f a mil ia r wit h ot h er pr ogr a mmin g l a n gu a ges, l ea r n in g Per l wil l be a sn a p. Th e on l y a ssu mpt ion t h is book does ma ke is t h a t you a r e f a mil ia r wit h t h e ba sics of u sin g t h e UNIX oper a t in g syst em.

Special Features of This Book

  Th is book con t a in s some specia l el emen t s t h a t h el p you u n der st a n d Per l f ea t u r es a n d con cept s a s t h ey a r e in t r odu ced: Syn t a x boxes DO/DON'T boxes Not es Wa r n in gs Tips

  Syntax boxes

  expl a in some of t h e mor e compl ica t ed f ea t u r es of Per l , su ch a s t h e con t r ol st r u ct u r es. Ea ch syn t a x box con sist s of a f or ma l def in it ion of t h e f ea t u r e f ol l owed by a n expl a n a t ion of t h e el emen t s of t h e f ea t u r e. Her e is a n exa mpl e of a syn t a x box: for

  Th e syn t a x of t h e st a t emen t is for (expr1; expr2; expr3) { statement_block } expr1 is t h e l oop in it ia l iz er . It is eva l u a t ed on l y on ce, bef or e t h e st a r t of t h e l oop. expr2 is t h e con dit ion a l expr ession t h a t t er min a t es t h e l oop. Th e con dit ion a l expr ession expr2 while if in beh a ves ju st l ike t h e on es in a n d st a t emen t s: If it s va l u e is z er o, t h e l oop is t er min a t ed, a n d if it s va l u e is n on z er o, t h e l oop is execu t ed. statement_block is t h e col l ect ion of st a t emen t s t h a t is execu t ed if (a n d wh en ) expr2 h a s a n on z er o va l u e. expr3 is execu t ed on ce per it er a t ion of t h e l oop, a n d is execu t ed a f t er t h e l a st

  Don 't t r y t o u n der st a n d t h is def in it ion yet !

  DO/DON'T boxes

  pr esen t t h e do's a n d don 't s f or a pa r t icu l a r t a sk or f ea t u r e. Her e is a n exa mpl e of su ch a box: |

  ||

  DON'T con f u se t h e oper a t or (bit wise OR) wit h t h e oper a t or (l ogica l OR).

  

DO ma ke su r e you a r e u sin g t h e pr oper bit wise oper a t or .

  It 's ea sy t o sl ip a n d a ssu me you wa n t bit wise OR wh en you r ea l l y wa n t bit wise AND. (Tr u st me.

  Notes

  a r e expl a n a t ion s of in t er est in g pr oper t ies of a pa r t icu l a r pr ogr a m f ea t u r e. Her e is a n exa mpl e of a n ot e:

  NOTE

  In l ef t -ju st if ied ou t pu t , t h e va l u e bein g displ a yed a ppea r s a t t h e l ef t en d of t h e va l u e f iel d. In r igh t - ju st if ied ou t pu t , t h e va l u e bein g displ a yed a ppea r s a t t h e r igh t en d of t h e va l u e f iel d.

  Warnings

  wa r n you of pr ogr a mmin g pit f a l l s t o a void. Her e is a t ypica l wa r n in g: You ca n n ot u se t h e last st a t emen t in side t h e do st a t emen t . Th e do st a t emen t , a l t h ou gh it beh a ves l ike t h e ot h er con t r ol st r u ct u r es, is a ct u a l l y impl emen t ed dif f er en t l y.

  Tips

  a r e h in t s on h ow t o wr it e you r Per l pr ogr a ms bet t er . Her e is a n exa mpl e of a t ip:

  TIP

  It is a good idea t o u se a l l u pper ca se l et t er s f or you r f il e va r ia bl e n a mes. Th is ma kes it ea sier t o dist in gu ish f il e va r ia bl e n a mes f r om ot h er va r ia bl e n a mes a n d f r om r eser ved wor ds.

Programming Examples

  Ea ch f ea t u r e of Per l is il l u st r a t ed by exa mpl es of it s u se. In a ddit ion , ea ch ch a pt er of t h is book con t a in s ma n y u sef u l pr ogr a mmin g exa mpl es compl et e wit h expl a n a t ion s; t h ese exa mpl es sh ow you h ow you ca n u se Per l f ea t u r es in you r own pr ogr a ms. Ea ch exa mpl e con t a in s a l ist in g of t h e pr ogr a m, t h e in pu t r equ ir ed by a n d t h e ou t pu t gen er a t ed by t h e pr ogr a m, a n d a n a n a l ysis of h ow t h e pr ogr a m wor ks. Specia l icon s a r e u sed t o poin t ou t ea ch pa r t of t h e exa mpl e: Type, In pu t -Ou t pu t , a n d An a l ysis. In t h e In pu t -Ou t pu t exa mpl e f ol l owin g List in g IN.1, t h er e a r e some specia l t ypogr a ph ic con ven t ion s. Th e in pu t you en t er is sh own in bold monospace t ype, a n d t h e ou t pu t gen er a t ed by t h e syst em or t h e pr ogr a m is sh own in plain monospace t ype. Th e syst em pr ompt ( $ in t h e exa mpl es in t h is book) is sh own so t h a t you kn ow wh en a comma n d is t o be en t er ed on t h e comma n d l in e.

  List ing IN.1. A simpl e Pe r l pr o g r am wit h c o mme nt s.

  1: #!/usr/local/bin/perl 2: # this program reads a line of input, and writes the line 3: # back out 4: $inputline = <STDIN>; # read a line of input 5: print( $inputline ); # write the line out $ programIN_1 This is a line of input.

  This is a line of input.

  Lin e 1 is t h e h ea der commen t . Lin es 2 a n d 3 a r e commen t s, n ot execu t a bl e l in es of code. Lin e 4 r ea ds a l in e of in pu t . Lin e 5 wr it es t h e l in e of in pu t on you r scr een .

End-of-Day Q& A and Workshop

  Ea ch da y en ds wit h a Q&A sect ion con t a in in g a n swer s t o common qu est ion s r el a t in g t o t h a t da y's ma t er ia l . Th er e a l so is a Wor ksh op a t t h e en d of ea ch da y t h a t con sist s of qu iz qu est ion s a n d pr ogr a mmin g exer cises. Th e exer cises of t en in cl u de BUG BUSTER exer cises t h a t h el p you spot some of t h e common bu gs t h a t cr op u p in Per l pr ogr a ms. Th e a n swer s t o t h ese qu iz qu est ion s a s wel l a s sa mpl e sol u t ion s f or t h e exer cises a r e pr esen t ed in Appen dix A, "An swer s."

Conventions Used in This Book

  Th is book u ses dif f er en t t ypef a ces t o h el p you dif f er en t ia t e bet ween Per l code a n d r egu l a r En gl ish , a n d a l so t o h el p you iden t if y impor t a n t con cept s. Act u a l Per l code is t ypeset in a specia l monospace f on t . You 'l l see t h is f on t u sed in l ist in gs a n d t h e In pu t -Ou t pu t exa mpl es, a s wel l a s in code sn ippet s. In t h e expl a n a t ion s of Per l f ea t u r es, comma n ds, f il en a mes, st a t emen t s, va r ia bl es, a n d a n y t ext you see on t h e scr een a l so a r e t ypeset in t h is f on t . Comma n d in pu t a n d a n yt h in g t h a t you a r e su pposed t o en t er a ppea r s in a bold monospace f on t . You 'l l see t h is ma in l y in t h e In pu t -Ou t pu t exa mpl es. Pl a ceh ol der s in syn t a x descr ipt ion s a ppea r in a n italic monospace f on t . Repl a ce t h e pl a ceh ol der wit h t h e a ct u a l f il en a me, pa r a met er , or wh a t ever el emen t it r epr esen t s.

  Italics

  h igh l igh t t ech n ica l t er ms wh en t h ey f ir st a ppea r in t h e t ext a n d a r e somet imes u sed t o emph a siz e impor t a n t poin t s.

What You'll Learn in 21 Days

  In you r f ir st week of l ea r n in g Per l , you 'l l l ea r n en ou gh of t h e ba sics of Per l t o wr it e ma n y u sef u l Per l pr ogr a ms. Her e's a su mma r y of wh a t you 'l l l ea r n in Week 1:

  

, "Ge t t ing St ar t e d," t el l s you h ow t o get Per l , h ow t o r u n Per l

pr ogr a ms, a n d h ow t o r ea d f r om you r keyboa r d a n d wr it e t o you r scr een .

  

, "Basic Ope r at o r s and Co nt r o l Fl o w," t ea ch es you a bou t simpl e

  a r it h met ic, h ow t o a ssign a va l u e t o a sca l a r va r ia bl e, a n d h ow t o con t r ol

  

, "Unde r st anding Sc al ar Val ue s," t ea ch es you a bou t in t eger s,

  f l oa t in g-poin t n u mber s, a n d ch a r a ct er st r in gs. It a l so sh ows you t h a t a l l t h r ee a r e in t er ch a n gea bl e in Per l .

   , "Mo r e Ope r at o r s," t el l s you a l l a bou t oper a t or s a n d expr ession s in Per l a n d t a l ks a bou t oper a t or a ssocia t ivit y a n d pr eceden ce.

   , "List s and Ar r ay Var iabl e s," in t r odu ces you t o l ist s, wh ich a r e col l ect ion s of va l u es, a n d t o a r r a y va r ia bl es, wh ich st or e l ist s.

   , "Re ading f r o m and Wr it ing t o Fil e s," t el l s you h ow t o in t er a ct

  wit h you r f il e syst em by r ea din g f r om in pu t f il es, wr it in g t o ou t pu t f il es, a n d t est in g f or pa r t icu l a r f il e a t t r ibu t es.

   , "Pat t e r n Mat c hing ," descr ibes pa t t er n -ma t ch in g in Per l a n d sh ows

  h ow you ca n su bst it u t e va l u es a n d t r a n sl a t e set s of ch a r a ct er s in t ext st r in gs.

  By t h e en d of Week 2, you 'l l h a ve ma st er ed a l most a l l t h e f ea t u r es of Per l ; you 'l l a l so h a ve l ea r n ed a bou t ma n y of t h e l ibr a r y f u n ct ion s su ppl ied wit h t h e l a n gu a ge. Her e's a su mma r y of wh a t you 'l l l ea r n :

   , "Mo r e Co nt r o l St r uc t ur e s," discu sses t h e con t r ol f l ow st a t emen t s n ot pr eviou sl y cover ed.

   , "Using Subr o ut ine s," sh ows h ow you ca n br ea k you r pr ogr a m in t o sma l l er , mor e ma n a gea bl e, ch u n ks.

   , "Asso c iat ive Ar r ays," in t r odu ces on e of t h e most power f u l a n d

  u sef u l con st r u ct s in Per l -a r r a ys-a n d it sh ows h ow you ca n u se t h ese a r r a ys t o simu l a t e ot h er da t a st r u ct u r es.

  

, "Fo r mat t ing Yo ur Out put ," sh ows h ow you ca n u se Per l t o

pr odu ce t idy r epor t s.

   , "Wo r king wit h t he Fil e Syst e m," sh ows h ow you ca n in t er a ct wit h you r syst em's dir ect or y st r u ct u r e.

   , "Pr o c e ss, St r ing , and Mat he mat ic al Func t io ns," descr ibes t h e

  l ibr a r y f u n ct ion s t h a t in t er a ct wit h pr ocesses r u n n in g on t h e syst em. It a l so descr ibes t h e f u n ct ion s t h a t per f or m t r igon omet r ic a n d ot h er

   , "Sc al ar -Co nve r sio n and List -Manipul at io n Func t io ns," descr ibes

  t h e l ibr a r y f u n ct ion s t h a t con ver t va l u es f r om on e f or m t o a n ot h er a n d t h e f u n ct ion s t h a t wor k wit h l ist s a n d a r r a y va r ia bl es.

  By t h e en d of Week 3, you 'l l kn ow a l l t h e f ea t u r es a n d ca pa bil it ies of Per l . It cover s t h e r est of t h e Per l l ibr a r y f u n ct ion s a n d descr ibes some of t h e mor e esot er ic con cept s of t h e l a n gu a ge. Her e's a su mma r y of wh a t you 'l l l ea r n :

   , "Syst e m Func t io ns," descr ibes t h e f u n ct ion s t h a t ma n ipu l a t e t h e Ber kel ey UNIX a n d UNIX Syst em V en vir on men t s.

   , "Co mmand-Line Opt io ns," descr ibes t h e opt ion s you ca n su ppl y wit h Per l t o con t r ol h ow you r pr ogr a m r u n s.

   , "Syst e m Var iabl e s," descr ibes t h e bu il t -in va r ia bl es t h a t a r e in cl u ded a u t oma t ica l l y a s pa r t of ever y Per l pr ogr a m.

  

, "Re f e r e nc e s in Pe r l 5," descr ibes t h e poin t er a n d r ef er en ce

f ea t u r es of Per l 5, in cl u din g mu l t i-dimen sion a l a r r a ys.

   , "Obj e c t -Or ie nt e d Pr o g r amming in Pe r l ," descr ibes t h e object -

  or ien t ed ca pa bil it ies a dded t o Per l 5. Th ese en a bl e you t o h ide in f or ma t ion a n d divide you r pr ogr a m in t o in dividu a l f il e modu l es.

   , "Misc e l l ane o us Fe at ur e s o f Pe r l ," cover s some of t h e mor e exot ic or obscu r e f ea t u r es of t h e l a n gu a ge.

   , "The Pe r l De bug g e r ," sh ows you h ow t o u se t h e Per l debu gger t o discover er r or s qu ickl y.

  We e k

  1 Week at a Glance

CONTENTS

  In you r f ir st week of t ea ch in g you r sel f Per l , you 'l l l ea r n en ou gh of t h e ba sics t o wr it e ma n y u sef u l Per l pr ogr a ms. Al t h ou gh some exper ien ce in u sin g a pr ogr a mmin g l a n gu a ge wil l be a n a dva n t a ge a s you r ea d t h is book, it is n ot r equ ir ed. In pa r t icu l a r , you don 't n eed t o kn ow t h e C pr ogr a mmin g l a n gu a ge bef or e you r ea d t h is book.

  To u se t h is book ef f ect ivel y, you sh ou l d be a bl e t o t r y ou t some of t h e f ea t u r es of Per l a s you l ea r n t h em. To do t h is, you sh ou l d h a ve Per l r u n n in g on you r syst em. If you don 't h a ve Per l t t in g St a r t ed," t el l s h ow you ca n get it f or f r ee.

  Ea ch ch a pt er of t h is book con t a in s qu iz a n d exer cise qu est ion s t h a t t est you on t h e ma t er ia l cover ed in t h e da y's l esson . Th ese qu est ion s a r e a n swer ed in Appen dix A, "An swer s."

  Where You're Going Th e f ir st week cover s t h e essen t ia l s of Per l . Her e's a su mma r y of wh a t you 'l l l ea r n .

  

, "Get t in g St a r t ed," t el l s you h ow t o get Per l , h ow t o r u n Per l pr ogr a ms, a n d h ow

t o r ea d in pu t f r om you r keyboa r d a n d wr it e ou t pu t t o you r scr een .

  

, "Ba sic Oper a t or s a n d Con t r ol Fl ow," t ea ch es you a bou t simpl e a r it h met ic, h ow t o

  a ssign a va l u e t o a sca l a r va r ia bl e, a n d h ow t o con t r ol execu t ion u sin g con dit ion a l st a t emen t s. n u mber s, a n d ch a r a ct er st r in gs. It a l so sh ows you t h a t a l l t h r ee a r e in t er ch a n gea bl e in Per l .

  

, "Mor e Oper a t or s," t el l s you a l l a bou t oper a t or s a n d expr ession s in Per l a n d t a l ks

a bou t oper a t or a ssocia t ivit y a n d pr eceden ce.

   , "List s a n d Ar r a y Va r ia bl es," in t r odu ces you t o l ist s, wh ich a r e col l ect ion s of va l u es, a n d t o a r r a y va r ia bl es, wh ich st or e l ist s.

   , "Rea din g f r om a n d Wr it in g t o Fil es," t el l s you h ow t o in t er a ct wit h you r f il e

  syst em by r ea din g f r om in pu t f il es, wr it in g t o ou t pu t f il es, a n d t est in g f or pa r t icu l a r f il e a t t r ibu t es.

  Fin a l l y, t t er n Ma t ch in g," descr ibes pa t t er n ma t ch in g in Per l a n d sh ows h ow you ca n su bst it u t e va l u es a n d t r a n sl a t e set s of ch a r a ct er s in t ext st r in gs.

  Th is is qu it e a bit of ma t er ia l t o l ea r n in on e week; h owever , by t h e en d of t h e week you 'l l kn ow most of t h e essen t ia l s of Per l a n d wil l be a bl e t o wr it e ma n y u sef u l pr ogr a ms.

Chapter 1 Getting Started CONTENTS

  

  

  Wel come t o Teach Yourself Perl 5 in 21 Days. Toda y you 'l l l ea r n a bou t t h e f ol l owin g: Wh a t Per l is a n d wh y Per l is u sef u l How t o get Per l if you do n ot a l r ea dy h a ve it How t o r u n Per l pr ogr a ms

What Is Perl?

  Per l is a n a cr on ym, sh or t f or Pr a ct ica l Ext r a ct ion a n d Repor t La n gu a ge. It wa s design ed by La r r y Wa l l a s a t ool f or wr it in g pr ogr a ms in t h e UNIX en vir on men t a n d is con t in u a l l y bein g u pda t ed a n d ma in t a in ed by h im.

  For it s ma n y f a n s, Per l pr ovides t h e best of sever a l wor l ds. For in st a n ce: Per l h a s t h e power a n d f l exibil it y of a h igh -l evel pr ogr a mmin g l a n gu a ge su ch a s

  C. In f a ct , a s you wil l see, ma n y of t h e f ea t u r es of t h e l a n gu a ge a r e bor r owed f r om C. Like sh el l scr ipt l a n gu a ges, Per l does n ot r equ ir e a specia l compil er a n d l in ker t o t u r n t h e pr ogr a ms you wr it e in t o wor kin g code. In st ea d, a l l you h a ve t o do is wr it e t h e pr ogr a m a n d t el l Per l t o r u n it . Th is mea n s t h a t Per l is idea l f or pr odu cin g qu ick sol u t ion s t o sma l l pr ogr a mmin g pr obl ems, or f or cr ea t in g pr ot ot ypes t o t est pot en t ia l sol u t ion s t o l a r ger pr obl ems. Per l pr ovides a l l t h e f ea t u r es of t h e scr ipt l a n gu a ges sed a n d a wk, pl u s f ea t u r es n ot f ou n d in eit h er of t h ese t wo l a n gu a ges. Per l a l so su ppor t s a sed-t o-Per l t r a n sl a t or a n d a n a wk-t o-Per l t r a n sl a t or .

  In sh or t , Per l is a s power f u l a s C bu t a s con ven ien t a s a wk, sed, a n d sh el l scr ipt s.

  NOTE

  Th is book a ssu mes t h a t you a r e f a mil ia r wit h t h e ba sics of u sin g t h e UNIX oper a t in g syst em As you 'l l see, Per l is ver y ea sy t o l ea r n . In deed, if you a r e f a mil ia r wit h ot h er pr ogr a mmin g l a n gu a ges, l ea r n in g Per l is a sn a p. Even if you h a ve ver y l it t l e pr ogr a mmin g exper ien ce, Per l ca n h a ve you wr it in g u sef u l pr ogr a ms in a ver y sh or t t ime. By t h e en d of , "Ba sic Oper a t or s a n d Con t r ol Fl ow," you 'l l kn ow en ou gh a bou t Per l t o be a bl e t o sol ve ma n y pr obl ems.

How Do I Find Perl?

  To f in d ou t wh et h er Per l a l r ea dy is a va il a bl e on you r syst em, do t h e f ol l owin g: If you a r e cu r r en t l y wor kin g in a UNIX pr ogr a mmin g en vir on men t , ch eck t o see wh et h er t h e f il e /usr/local/bin/perl exist s. If you a r e wor kin g in a n y ot h er en vir on men t , ch eck t h e pl a ce wh er e you

  If you do n ot f in d Per l in t h is wa y, t a l k t o you r syst em a dmin ist r a t or a n d a sk wh et h er sh e or h e h a s Per l r u n n in g somewh er e el se. If you don 't h a ve Per l r u n n in g in you r en vir on men t , don 't despa ir -r ea d on !

Where Do I Get Perl?

  On e of t h e r ea son s Per l is becomin g so popu l a r is t h a t it is a va il a bl e f r ee of ch a r ge t o a n yon e wh o wa n t s it . If you a r e on t h e In t er n et , you ca n obt a in a copy of Per l wit h f il e- t r a n sf er pr ot ocol (FTP). Th e f ol l owin g is a sa mpl e FTP session t h a t t r a n sf er s a copy of t h e Per l dist r ibu t ion . Th e it ems sh own in bol df a ce t ype a r e wh a t you wou l d en t er du r in g t h e session .

  $ ftp prep.ai.mit.edu Connected to prep.ai.mit.edu.

  220 aeneas FTP server (Version wu-2.4(1) Thu Apr 14 20:21:35 EDT 1994) ready. Name (prep.ai.mit.edu:dave): anonymous 331 Guest login ok, send your complete e-mail address as password.

  Password: 230-Welcome, archive user! 230- 230-If you have problems downloading and are seeing "Access denied" or 230-"Permission denied", please make sure that you started your FTP 230-client in a directory to which you have write permission.

  230- 230-If you have any problems with the GNU software or its downloading, 230-please refer your questions to <gnu@PREP.AI.MIT.EDU>. If you have any 230-other unusual problems, please report them to <root@aeneas.MIT.EDU>.

  230- 230-If you do have problems, please try using a dash (-) as the first 230-character of your password - this will turn off the continuation

  230- 230 Guest login ok, access restrictions apply. ftp> cd pub/gnu 250-If you have problems downloading and are seeing "Access denied" or 250-"Permission denied", please make sure that you started your FTP 250-client in a directory to which you have write permission.

  250- 250-Please note that all files ending in '.gz' are compressed with 250-'gzip', not with the unix 'compress' program. Get the file README 250- and read it for more information.

  250- 250-Please read the file README 250- it was last modified on Thu Feb 1 15:00:50 1996 - 32 days ago 250-Please read the file README-about-.diff-files 250- it was last modified on Fri Feb 2 12:57:14 1996 - 31 days ago 250-Please read the file README-about-.gz-files 250- it was last modified on Wed Jun 14 16:59:43 1995 - 264 days ago 250 CWD command successful. ftp> binary 200 Type set to I. ftp> get perl-5.001.tar.gz 200 PORT command successful.

  150 Opening ASCII mode data connection for perl-5.001.tar.gz (1130765 bytes). 226 Transfer complete. 1130765 bytes received in 9454 seconds (1.20 Kbytes/s) ftp> quit 221 Goodbye. Th e comma n ds en t er ed in t h is session a r e expl a in ed in t h e f ol l owin g st eps. If some of t h ese st eps a r e n ot f a mil ia r t o you , a sk you r syst em a dmin ist r a t or f or h el p.

  1. Th e comma n d $ ftp prep.ai.mit.edu con n ect s you t o t h e ma in Fr ee Sof t wa r e Fou n da t ion sou r ce deposit or y a t MIT.

  2. Th e u ser ID anonymous t el l s FTP t h a t you wa n t t o per f or m a n a n on ymou s FTP oper a t ion .

  3. Wh en FTP a sks f or a pa sswor d, en t er you r u ser ID a n d n et wor k a ddr ess. Th is l et s t h e MIT syst em a dmin ist r a t or kn ow wh o is u sin g t h e MIT a r ch ives. (For secu r it y r ea son s, t h e pa sswor d is n ot a ct u a l l y displ a yed wh en you t ype it .)

  4. Th e comma n d cd pub/gnu set s you r cu r r en t wor kin g dir ect or y t o be t h e dir ect or y con t a in in g t h e Per l sou r ce.

  5. Th e binary comma n d t el l s FTP t h a t t h e f il e you 'l l be r eceivin g is a f il e t h a t con t a in s u n r ea da bl e (n on -t ext ) ch a r a ct er s. get perl-5.001.tar.gz

  6. Th e comma n d copies t h e f il e f r om t h e MIT sou r ce deposit or y t o you r own sit e. (It 's u su a l l y best t o do t h is in of f -pea k h ou r s t o ma ke t h in gs ea sier f or ot h er In t er n et u ser s-it t a kes a wh il e.) Th is f il e is qu it e l a r ge beca u se it con t a in s a l l t h e sou r ce f il es f or Per l bu n dl ed t oget h er in t o a sin gl e f il e.

  7. Th e quit comma n d discon n ect s f r om t h e MIT sou r ce r eposit or y a n d r et u r n s you t o you r own syst em.

  On ce you 've r et r ieved t h e Per l dist r ibu t ion , do t h e f ol l owin g:

  1. Cr ea t e a dir ect or y a n d move t h e f il e you ju st r eceived, perl-5.001.tar.gz , t o t h is dir ect or y. (Or , a l t er n a t ivel y, move it t o a dir ect or y a l r ea dy r eser ved f or t h is pu r pose.)

  2. Th e perl-5.001.tar.gz f il e is compr essed t o sa ve spa ce. To u n compr ess it , en t er t h e comma n d $ gunzip perl-5.001.tar.gz gunzip is t h e GNU u n compr ess pr ogr a m. If it 's n ot a va il a bl e on you r syst em, see you r syst em a dmin ist r a t or . (You ca n , in f a ct , r et r ieve it f r om prep.ai.mit.edu u sin g a n on ymou s FTP wit h t h e sa me comma n ds you u sed t o r et r ieve t h e Per l dist r ibu t ion .)

  Wh en you r u n gunzip , t h e f il e perl-5.001.tar.gz wil l be r epl a ced by perl- 5.001.tar , wh ich is t h e u n compr essed ver sion of t h e Per l dist r ibu t ion f il e.

  3. Th e n ext st ep is t o u n pa ck t h e Per l dist r ibu t ion . In ot h er wor ds, u se t h e in f or ma t ion in t h e Per l dist r ibu t ion t o cr ea t e t h e Per l sou r ce f il es. To do t h is, en t er t h e f ol l owin g comma n d: $ tar xvf - <perl-5.001.tar su bdir ect or ies wh er e a ppr opr ia t e; t h is en su r es t h a t t h e Per l sou r ce f il es a r e or ga n iz ed in a l ogica l wa y.

  4. Usin g you r f a vor it e C compil er , compil e t h e Per l sou r ce code u sin g t h e ma kef il e pr ovided. (Th is ma kef il e sh ou l d h a ve been cr ea t ed wh en t h e sou r ce f il es wer e u n pa cked in t h e l a st st ep.)

  5. Pl a ce t h e compil ed Per l execu t a bl e in t o t h e dir ect or y wh er e you n or ma l l y keep you r execu t a bl es. On UNIX syst ems, t h is dir ect or y u su a l l y is ca l l ed /usr/local/bin /usr/local/bin/perl , a n d Per l u su a l l y is n a med .

  You migh t n eed you r syst em a dmin ist r a t or 's h el p t o do t h is beca u se you migh t n ot h a ve t h e n ecessa r y per mission s.

Other Places to Get Perl

  If you ca n n ot a ccess t h e MIT sit e f r om wh er e you a r e, you ca n get Per l f r om t h e f ol l owin g sit es u sin g a n on ymou s FTP: Nor t h Amer ica

  Site Location

  ftp.netlabs.com In t er n et a ddr ess 192.94.48.152 Directory /pub/outgoing/perl5.0 ftp.cis.ufl.edu In t er n et a ddr ess 128.227.100.198 Directory /pub/perl/src/5.0 ftp.uu.net

  192.48.96.9 In t er n et a ddr ess Directory /languages/perl ftp.khoros.unm.edu In t er n et a ddr ess 198.59.155.28 Directory /pub/perl ftp.cbi.tamucc.edu In t er n et a ddr ess 165.95.1.3 Directory /pub/duff/Perl ftp.metronet.com

  192.245.137.1 In t er n et a ddr ess Directory /pub/perl/sources genetics.upenn.edu

  128.91.200.37 In t er n et a ddr ess Directory /perl5

  Eu r ope

  Site Location

  ftp.cs.ruu.nl 131.211.80.17

  In t er n et a ddr ess Directory /pub/PERL/perl5.0/src ftp.funet.fi In t er n et a ddr ess 128.214.248.6 Directory /pub/languages/perl/ports/perl5 ftp.zrz.tu-

  130.149.4.40 In t er n et a ddr ess berlin.de

  Directory /pub/unix/perl src.doc.ic.ac.uk In t er n et a ddr ess 146.169.17.5 Directory /packages/perl5

  Au st r a l ia

  Site Location

  sungear.mame.mu.oz.au In t er n et a ddr ess 128.250.209.2 Directory /pub/perl/src/5.0

  Sou t h Amer ica

  Site Location

  ftp.inf.utfsm.cl In t er n et a ddr ess 146.83.198.3 Directory /pub/gnu

  You a l so ca n obt a in Per l f r om most sit es t h a t st or e GNU sou r ce code, or f r om a n y sit e t h a t a r ch ives t h e Usen et n ewsgr ou p comp.sources.unix .

A Sample Perl Program

  Now t h a t Per l is a va il a bl e on you r syst em, it 's t ime t o sh ow you a simpl e pr ogr a m t h a t il l u st r a t es h ow ea sy it is t o u se Per l . List in g 1.1 is a simpl e pr ogr a m t h a t a sks f or a l in e of in pu t a n d wr it es it ou t .

  

List ing 1.1. A simpl e Pe r l pr o g r am t hat r e ads and wr it e s a l ine o f input .

  1: #!/usr/local/bin/perl 2: $inputline = <STDIN>; 3: print( $inputline );

  $program1_1 This is my line of input.

  This is my line of input. $

  Lin e 1 is t h e h ea der commen t . Lin e 2 r ea ds a l in e of in pu t . Lin e 3 wr it es t h e l in e of in pu t ba ck t o you r scr een .

  Th e f ol l owin g sect ion s descr ibe h ow t o cr ea t e a n d r u n t h is pr ogr a m, a n d t h ey descr ibe it in mor e det a il .

Running a Perl Program

  To r u n t h e pr ogr a m sh own in List in g 1.1, do t h e f ol l owin g:

  1. Usin g you r f a vor it e edit or , t ype t h e pr eviou s pr ogr a m a n d sa ve it in a f il e ca l l ed program1_1 .

  2. Tel l t h e syst em t h a t t h is f il e con t a in s execu t a bl e st a t emen t s. To do t h is in t h e UNIX en vir on men t , en t er t h e comma n d $ chmod +x program1_1

  3. Ru n t h e pr ogr a m by en t er in g t h e comma n d $ program1_1 program1_1

  Wh en you r u n , it wa it s f or you t o en t er a l in e of in pu t . Af t er you en t er t h e program1_1 l in e of in pu t , pr in t s wh a t you en t er ed, a s f ol l ows:

  $ program1_1 This is my line of input.

  This is my line of input.

If Something Goes Wrong

  program1_1 If List in g 1.1 is st or ed in t h e f il e a n d r u n a ccor din g t o t h e pr ecedin g st eps, t h e pr ogr a m sh ou l d r u n su ccessf u l l y. If t h e pr ogr a m doesn 't r u n , on e of t wo t h in gs h a s l ikel y h a ppen ed: Th e syst em ca n 't f in d t h e f il e program1_1 .

  Th e syst em ca n 't f in d Per l . If you r eceive t h e er r or messa ge program1_1 not found or somet h in g simil a r , you r syst em cou l dn 't f in d t h e f il e program1_1 . To t el l t h e syst em wh er e program1_1 is l oca t ed, you ca n do on e of t wo t h in gs in a UNIX en vir on men t :

  ./program1_1 En t er t h e comma n d , wh ich gives t h e syst em t h e pa t h n a me of program1_1 r el a t ive t o t h e cu r r en t dir ect or y.

  . PATH Add t h e cu r r en t dir ect or y t o you r en vir on men t va r ia bl e. Th is t el l s t h e syst em t o sea r ch in t h e cu r r en t dir ect or y wh en l ookin g f or execu t a bl e pr ogr a ms su ch a s program1_1 .