Teach Yourself Perl 5 in 21 days
David Till
Table of Contents:
Introduction
● Wh o Sh ou l d Rea d Th is Book?
● Specia l Fea t u r es of Th is Book
● Pr ogr a mmin g Exa mpl es
● En d-of -Da y Q& A a n d Wor ksh op
● Con ven t ion s Used in Th is Book
● Wh a t You 'l l Lea r n in 21 Da ys
Week
1
Week at a Glance
● Wh er e You 'r e Goin g
Day
1
Getting Started
● Wh a t Is Per l ?
● How Do I Fin d Per l ?
❍ Wh er e Do I Get Per l ?
❍ Ot h er Pl a ces t o Get Per l
● A Sa mpl e Per l Pr ogr a m
● Ru n n in g a Per l Pr ogr a m
❍ If Somet h in g Goes Wr on g
● Th e Fir st Lin e of You r Per l Pr ogr a m: How Commen t s Wor k
❍ Commen t s
● Lin e 2: St a t emen t s, Token s, a n d <STDIN>
❍ St a t emen t s a n d Token s
❍ Token s a n d Wh it e Spa ce
❍ Wh a t t h e Token s Do: Rea din g f r om St a n da r d In pu t
● Lin e 3: Wr it in g t o St a n da r d Ou t pu t
❍ Fu n ct ion In voca t ion s a n d Ar gu men t s
● In t er pr et ive La n gu a ges Ver su s Compil ed La n gu a ges
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
2
Basic Operators and Control Flow
● St or in g in Sca l a r Va r ia bl es Assign men t
❍ Th e Def in it ion of a Sca l a r Va r ia bl e
❍ Sca l a r Va r ia bl e Syn t a x
❍ Assign in g a Va l u e t o a Sca l a r Va r ia bl e
● Per f or min g Ar it h met ic
❍ Exa mpl e of Mil es-t o-Kil omet er s Con ver sion
❍ Th e chop Libr a r y Fu n ct ion
● Expr ession s
❍ Assign men t s a n d Expr ession s
● Ot h er Per l Oper a t or s
● In t r odu ct ion t o Con dit ion a l St a t emen t s
● Th e if St a t emen t
❍ Th e Con dit ion a l Expr ession
❍ Th e St a t emen t Bl ock
❍ Test in g f or Equ a l it y Usin g ==
❍ Ot h er Compa r ison Oper a t or s
● Two-Wa y Br a n ch in g Usin g if a n d else
● Mu l t i-Wa y Br a n ch in g Usin g elsif
● Wr it in g Loops Usin g t h e while St a t emen t
● Nest in g Con dit ion a l St a t emen t s
● Loopin g Usin g t h e until St a t emen t
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
3
Understanding Scalar Values
● Wh a t Is a Sca l a r Va l u e?
● In t eger Sca l a r Va l u es
● Fl oa t in g-Poin t Sca l a r Va l u es
❍ Fl oa t in g-Poin t Ar it h met ic a n d Rou n d-Of f Er r or
● Usin g Oct a l a n d Hexa decima l Not a t ion
❍ Decima l Not a t ion
❍ Oct a l Not a t ion
❍ Hexa decima l Not a t ion
❍ Wh y Bot h er ?
● Ch a r a ct er St r in gs
❍ Usin g Dou bl e-Qu ot ed St r in gs
❍ Esca pe Sequ en ces
❍ Sin gl e-Qu ot ed St r in gs
● In t er ch a n gea bil it y of St r in gs a n d Nu mer ic Va l u es
❍ In it ia l Va l u es of Sca l a r Va r ia bl es
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
4
More Operators
● Usin g t h e Ar it h met ic Oper a t or s
❍ Expon en t ia t ion
❍ Th e Rema in der Oper a t or
❍ Un a r y Nega t ion
● Usin g Compa r ison Oper a t or s
❍ In t eger -Compa r ison Oper a t or s
❍ St r in g-Compa r ison Oper a t or s
❍ St r in g Compa r ison Ver su s In t eger Compa r ison
❍ Compa r ison a n d Fl oa t in g-Poin t Nu mber s
● Usin g Logica l Oper a t or s
❍ Eva l u a t ion Wit h in Logica l Oper a t or s
❍ Logica l Oper a t or s a s Su bexpr ession s
● Usin g Bit -Ma n ipu l a t ion Oper a t or s
❍ Wh a t Bit s Ar e a n d How Th ey Ar e Used
❍ Th e Bit -Ma n ipu l a t ion Oper a t or s
● Usin g t h e Assign men t Oper a t or s
❍ Assign men t Oper a t or s a s Su bexpr ession s
● Usin g Au t oin cr emen t a n d Au t odecr emen t
❍ Th e Au t oin cr emen t Oper a t or Pr e-In cr emen t
❍ Th e Au t odecr emen t Oper a t or
❍ Usin g Au t oin cr emen t Wit h St r in gs
● Th e St r in g Con ca t en a t ion a n d Repet it ion Oper a t or s
❍ Th e St r in g-Con ca t en a t ion Oper a t or
❍ Th e St r in g-Repet it ion Oper a t or
❍ Con ca t en a t ion a n d Assign men t
● Ot h er Per l Oper a t or s
❍ Th e Comma Oper a t or
❍ Th e Con dit ion a l Oper a t or
● Th e Or der of Oper a t ion s
❍ Pr eceden ce
❍ Associa t ivit y
❍ For cin g Pr eceden ce Usin g Pa r en t h eses
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
5
Lists and Array Variables
● In t r odu cin g List s
● Sca l a r Va r ia bl es a n d List s
❍ List s a n d St r in g Su bst it u t ion
● St or in g List s in Ar r a y Va r ia bl es
● Accessin g a n El emen t of a n Ar r a y Va r ia bl e
❍ Mor e Det a il s on Ar r a y El emen t Na mes
● Usin g List s a n d Ar r a ys in Per l Pr ogr a ms
❍ Usin g Br a cket s a n d Su bst it u t in g f or Va r ia bl es
● Usin g List Ra n ges
❍ Expr ession s a n d List Ra n ges
● Mor e on Assign men t a n d Ar r a y Va r ia bl es
❍ Copyin g f r om On e Ar r a y Va r ia bl e t o An ot h er
❍ Usin g Ar r a y Va r ia bl es in List s
❍ Su bst it u t in g f or Ar r a y Va r ia bl es in St r in gs
❍ Assign in g t o Sca l a r Va r ia bl es f r om Ar r a y Va r ia bl es
● Ret r ievin g t h e Len gt h of a List
● Usin g Ar r a y Sl ices
❍ Usin g List Ra n ges in Ar r a y-Sl ice Su bscr ipt s
❍ Usin g Va r ia bl es in Ar r a y-Sl ice Su bscr ipt s
❍ Over l a ppin g Ar r a y Sl ices
❍ Usin g t h e Ar r a y-Sl ice Not a t ion a s a Sh or t h a n d
● Rea din g a n Ar r a y f r om t h e St a n da r d In pu t Fil e
● Ar r a y Libr a r y Fu n ct ion s
❍ Sor t in g a List or Ar r a y Va r ia bl e
❍ Rever sin g a List or Ar r a y Va r ia bl e
❍ Usin g chop on Ar r a y Va r ia bl es
❍ Cr ea t in g a Sin gl e St r in g f r om a List
❍ Spl it t in g a St r in g in t o a List
❍ Ot h er List -Ma n ipu l a t ion Fu n ct ion s
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
6
Reading from and Writing to Files
● Open in g a Fil e
❍ Th e Fil e Va r ia bl e
❍ Th e Fil en a me
❍ Th e Fil e Mode
❍ Ch eckin g Wh et h er t h e Open Su cceeded
● Rea din g f r om a Fil e
❍ Fil e Va r ia bl es a n d t h e St a n da r d In pu t Fil e
❍ Ter min a t in g a Pr ogr a m Usin g die
❍ Rea din g in t o Ar r a y Va r ia bl es
● Wr it in g t o a Fil e
❍ Th e St a n da r d Ou t pu t Fil e Va r ia bl e
❍ Mer gin g Two Fil es in t o On e
● Redir ect in g St a n da r d In pu t a n d St a n da r d Ou t pu t
● Th e St a n da r d Er r or Fil e
● Cl osin g a Fil e
● Det er min in g t h e St a t u s of a Fil e
❍ Fil e-Test Oper a t or Syn t a x
❍ Ava il a bl e Fil e-Test Oper a t or s
❍ Mor e on t h e -e Oper a t or
❍ Test in g f or Rea d Per mission -t h e -r Oper a t or
❍ Ch eckin g f or Ot h er Per mission s
❍ Ch eckin g f or Empt y Fil es
● Rea din g f r om a Sequ en ce of Fil es
❍ Rea din g in t o a n Ar r a y Va r ia bl e
● Usin g Comma n d-Lin e Ar gu men t s a s Va l u es
❍ ARGV a n d t h e <> Oper a t or
● Open in g Pipes
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
7
Pattern Matching
● In t r odu ct ion
● Th e Ma t ch Oper a t or s
❍ Ma t ch -Oper a t or Pr eceden ce
● Specia l Ch a r a ct er s in Pa t t er n s
❍ Th e + Ch a r a ct er
❍ Th e [] Specia l Ch a r a ct er s
❍ Th e * a n d ? Specia l Ch a r a ct er s
❍ Esca pe Sequ en ces f or Specia l Ch a r a ct er s
❍ Ma t ch in g An y Let t er or Nu mber
❍ An ch or in g Pa t t er n s
❍ Va r ia bl e Su bst it u t ion in Pa t t er n s
❍ Excl u din g Al t er n a t ives
❍ Ch a r a ct er -Ra n ge Esca pe Sequ en ces
❍ Ma t ch in g An y Ch a r a ct er
❍ Ma t ch in g a Specif ied Nu mber of Occu r r en ces
❍ Specif yin g Ch oices
❍ Reu sin g Por t ion s of Pa t t er n s
❍ Pa t t er n -Sequ en ce Sca l a r Va r ia bl es
❍ Specia l -Ch a r a ct er Pr eceden ce
❍ Specif yin g a Dif f er en t Pa t t er n Del imit er
● Pa t t er n -Ma t ch in g Opt ion s
❍ Ma t ch in g Al l Possibl e Pa t t er n s
❍ Ign or in g Ca se
❍ Tr ea t in g t h e St r in g a s Mu l t ipl e Lin es
❍ Eva l u a t in g a Pa t t er n On l y On ce
❍ Tr ea t in g t h e St r in g a s a Sin gl e Lin e
❍ Usin g Wh it e Spa ce in Pa t t er n s
❍ Usin g Pa t t er n -Sequ en ce Va r ia bl es in Su bst it u t ion s
❍ Opt ion s f or t h e Su bst it u t ion Oper a t or
❍ Eva l u a t in g a Pa t t er n On l y On ce
❍ Tr ea t in g t h e St r in g a s Sin gl e or Mu l t ipl e Lin es
❍ Usin g Wh it e Spa ce in Pa t t er n s
❍ Specif yin g a Dif f er en t Del imit er
● Th e Tr a n sl a t ion Oper a t or
❍ Opt ion s f or t h e Tr a n sl a t ion Oper a t or
● Ext en ded Pa t t er n -Ma t ch in g
❍ Pa r en t h esiz in g Wit h ou t Sa vin g in Memor y
❍ Embeddin g Pa t t er n Opt ion s
❍ Posit ive a n d Nega t ive Look-Ah ea d
❍ Pa t t er n Commen t s
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Week
1
Week 1 in Review
Week
2
Week 2 at a Glance
● Wh er e You 'r e Goin g
Day
8
More Control Structures
● Usin g Sin gl e-Lin e Con dit ion a l St a t emen t s
❍ Pr obl ems wit h Sin gl e-Lin e Con dit ion a l St a t emen t s
● Loopin g Usin g t h e for St a t emen t
❍ Usin g t h e Comma Oper a t or in a for St a t emen t
● Loopin g Th r ou gh a List : Th e foreach St a t emen t
❍ Th e foreach Loca l Va r ia bl e
❍ Ch a n gin g t h e Va l u e of t h e Loca l Va r ia bl e
❍ Usin g Ret u r n ed List s in t h e foreach St a t emen t
● Th e do St a t emen t
● Exit in g a Loop Usin g t h e last St a t emen t
● Usin g next t o St a r t t h e Next It er a t ion of a Loop
● Th e redo St a t emen t
❍ Usin g next a n d redo wit h La bel s
● Th e continue Bl ock
● Th e goto St a t emen t
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
9
Using Subroutines
● Wh a t Is a Su br ou t in e?
● Def in in g a n d In vokin g a Su br ou t in e
❍ For wa r d Ref er en ces t o Su br ou t in es
● Ret u r n in g a Va l u e f r om a Su br ou t in e
❍ Ret u r n Va l u es a n d Con dit ion a l Expr ession s
● Th e return St a t emen t
● Usin g Loca l Va r ia bl es in Su br ou t in es
❍ In it ia l iz in g Loca l Va r ia bl es
● Pa ssin g Va l u es t o a Su br ou t in e
❍ Pa ssin g a List t o a Su br ou t in e
● Ca l l in g Su br ou t in es f r om Ot h er Su br ou t in es
● Recu r sive Su br ou t in es
● Pa ssin g Ar r a ys by Na me Usin g Al ia ses
● Usin g t h e do St a t emen t wit h Su br ou t in es
● Specif yin g t h e Sor t Or der
● Pr edef in ed Su br ou t in es
❍ Cr ea t in g St a r t u p Code Usin g BEGIN
❍ Cr ea t in g Ter min a t ion Code Usin g END
❍ Ha n dl in g Non -Exist en t Su br ou t in es Usin g AUTOLOAD
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
10
Associative Arrays
● Limit a t ion s of Ar r a y Va r ia bl es
● Def in it ion
● Addin g El emen t s t o a n Associa t ive Ar r a y
● Cr ea t in g Associa t ive Ar r a ys
● Copyin g Associa t ive Ar r a ys f r om Ar r a y Va r ia bl es
● Addin g a n d Del et in g Ar r a y El emen t s
● List in g Ar r a y In dexes a n d Va l u es
● Loopin g Usin g a n Associa t ive Ar r a y
● Cr ea t in g Da t a St r u ct u r es Usin g Associa t ive Ar r a ys
❍ Lin ked List s
❍ St r u ct u r es
❍ Tr ees
❍ Da t a ba ses
❍ Exa mpl e: A Ca l cu l a t or Pr ogr a m
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
11
Formatting Your Output
● Def in in g a Pr in t For ma t
● Displ a yin g a Pr in t For ma t
● Displ a yin g Va l u es in a Pr in t For ma t
❍ Cr ea t in g a Gen er a l -Pu r pose Pr in t For ma t
❍ Ch oosin g a Va l u e-Fiel d For ma t
❍ Pr in t in g Va l u e-Fiel d Ch a r a ct er s
❍ Usin g t h e Mu l t il in e Fiel d For ma t
● Wr it in g t o Ot h er Ou t pu t Fil es
❍ Sa vin g t h e Def a u l t Fil e Va r ia bl e
● Specif yin g a Pa ge Hea der
❍ Ch a n gin g t h e Hea der Pr in t For ma t
● Set t in g t h e Pa ge Len gt h
❍ Usin g print wit h Pa gin a t ion
● For ma t t in g Lon g Ch a r a ct er St r in gs
❍ El imin a t in g Bl a n k Lin es Wh en For ma t t in g
❍ Su ppl yin g a n In def in it e Nu mber of Lin es
● For ma t t in g Ou t pu t Usin g printf
● Su mma r y
● Q&A
● Wor ksh op
❍ Exer cises
Day
12
Working with the File System
● Fil e In pu t a n d Ou t pu t Fu n ct ion s
❍ Ba sic In pu t a n d Ou t pu t Fu n ct ion s
❍ Skippin g a n d Rer ea din g Da t a
❍ Syst em Rea d a n d Wr it e Fu n ct ion s
❍ Rea din g Ch a r a ct er s Usin g getc
❍ Rea din g a Bin a r y Fil e Usin g binmode
● Dir ect or y-Ma n ipu l a t ion Fu n ct ion s
❍ Th e mkdir Fu n ct ion
❍ Th e chdir Fu n ct ion
❍ Th e opendir Fu n ct ion
❍ Th e closedir Fu n ct ion
❍ Th e readdir Fu n ct ion
❍ Th e telldir a n d seekdir Fu n ct ion s
❍ Th e rewinddir Fu n ct ion
❍ Th e rmdir Fu n ct ion
● Fil e-At t r ibu t e Fu n ct ion s
❍ Fil e-Rel oca t ion Fu n ct ion s
❍ Lin k a n d Symbol ic Lin k Fu n ct ion s
❍ Fil e-Per mission Fu n ct ion s
❍ Miscel l a n eou s At t r ibu t e Fu n ct ion s
● Usin g DBM Fil es
❍ Th e dbmopen Fu n ct ion
❍ Th e dbmclose Fu n ct ion
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
13
Process, String, and Mathematical Functions
● Pr ocess- a n d Pr ogr a m-Ma n ipu l a t ion Fu n ct ion s
❍ St a r t in g a Pr ocess
❍ Ter min a t in g a Pr ogr a m or Pr ocess
❍ Execu t ion Con t r ol Fu n ct ion s
❍ Miscel l a n eou s Con t r ol Fu n ct ion s
❍ Th e sin a n d cos Fu n ct ion s
❍ Th e atan2 Fu n ct ion
❍ Th e sqrt Fu n ct ion
❍ Th e exp Fu n ct ion
❍ Th e log Fu n ct ion
❍ Th e abs Fu n ct ion
❍ Th e rand a n d srand Fu n ct ion s
● St r in g-Ma n ipu l a t ion Fu n ct ion s
❍ Th e index Fu n ct ion
❍ Th e rindex Fu n ct ion
❍ Th e length Fu n ct ion
❍ Ret r ievin g St r in g Len gt h Usin g tr
❍ Th e pos Fu n ct ion
❍ Th e substr Fu n ct ion
❍ Th e study Fu n ct ion
❍ Ca se Con ver sion Fu n ct ion s
❍ Th e quotemeta Fu n ct ion
❍ Th e join Fu n ct ion
❍ Th e sprintf Fu n ct ion
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
14
Scalar-Conversion and List-Manipulation Functions
● Th e chop Fu n ct ion
● Th e chomp Fu n ct ion
● Th e crypt Fu n ct ion
● Th e hex Fu n ct ion
● Th e int Fu n ct ion
● Th e oct Fu n ct ion
❍ Th e oct Fu n ct ion a n d Hexa decima l In t eger s
● Th e ord a n d chr Fu n ct ion s
● Th e scalar Fu n ct ion
● Th e pack Fu n ct ion
❍ Th e pack Fu n ct ion a n d C Da t a Types
● Th e unpack Fu n ct ion
❍ Un pa ckin g St r in gs
❍ Th e unpack Fu n ct ion a n d uuencode
● Th e vec Fu n ct ion
● Th e defined Fu n ct ion
● Th e undef Fu n ct ion
● Ar r a y a n d List Fu n ct ion s
❍ Th e grep Fu n ct ion
❍ Th e splice Fu n ct ion
❍ Th e shift Fu n ct ion
❍ Th e unshift Fu n ct ion
❍ Th e push Fu n ct ion
❍ Th e pop Fu n ct ion
❍ Cr ea t in g St a cks a n d Qu eu es
❍ Th e split Fu n ct ion
❍ Th e sort a n d reverse Fu n ct ion s
❍ Th e map Fu n ct ion
❍ Th e wantarray Fu n ct ion
● Associa t ive Ar r a y Fu n ct ion s
❍ Th e keys Fu n ct ion
❍ Th e values Fu n ct ion
❍ Th e each Fu n ct ion
❍ Th e delete Fu n ct ion
❍ Th e exists Fu n ct ion
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Week
2
Week 2 in Review
Week
3
Week 3 at a Glance
● Wh er e You 'r e Goin g
Day
15
System Functions
● Syst em Libr a r y Emu l a t ion Fu n ct ion s
❍ Th e getgrent Fu n ct ion
❍ Th e setgrent a n d endgrent Fu n ct ion s
❍ Th e getgrid Fu n ct ion
❍ Th e getnetent Fu n ct ion
❍ Th e getnetbyaddr Fu n ct ion
❍ Th e getnetbyname Fu n ct ion
❍ Th e setnetent a n d endnetent Fu n ct ion s
❍ Th e gethostbyaddr Fu n ct ion
❍ Th e gethostbyname Fu n ct ion
❍ Th e gethostent, sethostent, a n d endhostent Fu n ct ion s
❍ Th e getlogin Fu n ct ion
❍ Th e getpgrp a n d setpgrp Fu n ct ion s
❍ Th e getppid Fu n ct ion
❍ Th e getpwnam Fu n ct ion
❍ Th e getpwuid Fu n ct ion
❍ Th e getpwent Fu n ct ion
❍ Th e setpwent a n d endpwent Fu n ct ion s
❍ Th e getpriority a n d setpriority Fu n ct ion s
❍ Th e getprotoent Fu n ct ion
❍ Th e getprotobyname a n d getprotobynumber Fu n ct ion s
❍ Th e setprotoent a n d endprotoent Fu n ct ion s
❍ Th e getservent Fu n ct ion
❍ Th e getservbyname a n d getservbyport Fu n ct ion s
❍ Th e setservent a n d endservent Fu n ct ion s
❍ Th e chroot Fu n ct ion
❍ Th e ioctl Fu n ct ion
❍ Th e alarm Fu n ct ion
❍ Ca l l in g t h e Syst em select Fu n ct ion
❍ Th e dump Fu n ct ion
● Socket -Ma n ipu l a t ion Fu n ct ion s
❍ Th e socket Fu n ct ion
❍ Th e bind Fu n ct ion
❍ Th e listen Fu n ct ion
❍ Th e accept Fu n ct ion
❍ Th e connect Fu n ct ion
❍ Th e shutdown Fu n ct ion
❍ Th e socketpair Fu n ct ion
❍ Th e getsockopt a n d setsockopt Fu n ct ion s
❍ Th e getsockname a n d getpeername Fu n ct ion s
● Th e UNIX Syst em V IPC Fu n ct ion s
❍ IPC Fu n ct ion s a n d t h e require St a t emen t
❍ Th e msgget Fu n ct ion
❍ Th e msgrcv Fu n ct ion
❍ Th e msgctl Fu n ct ion
❍ Th e shmget Fu n ct ion
❍ Th e shmwrite Fu n ct ion
❍ Th e shmread Fu n ct ion
❍ Th e shmctl Fu n ct ion
❍ Th e semget Fu n ct ion
❍ Th e semop Fu n ct ion
❍ Th e semctl Fu n ct ion
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
16
Command-Line Options
● Specif yin g Opt ion s
❍ Specif yin g Opt ion s on t h e Comma n d Lin e
❍ Specif yin g a n Opt ion in t h e Pr ogr a m
● Th e -v Opt ion : Pr in t in g t h e Per l Ver sion Nu mber
● Th e -c Opt ion : Ch eckin g You r Syn t a x
● Th e -w Opt ion : Pr in t in g Wa r n in gs
❍ Ch eckin g f or Possibl e Typos
❍ Ch eckin g f or Redef in ed Su br ou t in es
❍ Ch eckin g f or In cor r ect Compa r ison Oper a t or s
● Th e -e Opt ion : Execu t in g a Sin gl e-Lin e Pr ogr a m
● Th e -s Opt ion : Su ppl yin g You r Own Comma n d-Lin e Opt ion s
❍ Th e -s Opt ion a n d Ot h er Comma n d-Lin e Ar gu men t s
● Th e -P Opt ion : Usin g t h e C Pr epr ocessor
❍ Th e C Pr epr ocessor : A Qu ick Over view
● Th e -I Opt ion : Sea r ch in g f or C In cl u de Fil es
● Th e -n Opt ion : Oper a t in g on Mu l t ipl e Fil es
● Th e -p Opt ion : Oper a t in g on Fil es a n d Pr in t in g
● Th e -i Opt ion : Edit in g Fil es
❍ Ba ckin g Up In pu t Fil es Usin g t h e -i Opt ion
● Th e -a Opt ion : Spl it t in g Lin es
● Th e -F Opt ion : Specif yin g t h e Spl it Pa t t er n
● Th e -0 Opt ion : Specif yin g In pu t En d-of -Lin e
● Th e -l Opt ion : Specif yin g Ou t pu t En d-of -Lin e
● Miscel l a n eou s Opt ion s
❍ Th e -u Opt ion
❍ Th e -U Opt ion
❍ Th e -S Opt ion
❍ Th e -D Opt ion
❍ Th e -T Opt ion : Wr it in g Secu r e Pr ogr a ms
● Th e -d Opt ion : Usin g t h e Per l Debu gger
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
17
System Variables
● Gl oba l Sca l a r Va r ia bl es
❍ Th e Def a u l t Sca l a r Va r ia bl e: $_
❍ Th e Pr ogr a m Na me: $0
❍ Th e User ID: $< a n d $>
❍ Th e Gr ou p ID: $( a n d $)
❍ Th e Ver sion Nu mber : $]
❍ Th e In pu t Lin e Sepa r a t or : $/
❍ Th e Ou t pu t Lin e Sepa r a t or : $
❍ Th e Ou t pu t Fiel d Sepa r a t or : $,
❍ Th e Ar r a y El emen t Sepa r a t or : $"
❍ Th e Nu mber Ou t pu t For ma t : $#
❍ Th e eval Er r or Messa ge: $@
❍ Th e Syst em Er r or Code: $?
❍ Th e Syst em Er r or Messa ge: $!
❍ Th e Cu r r en t Lin e Nu mber : $.
❍ Mu l t il in e Ma t ch in g: $*
❍ Th e Fir st Ar r a y Su bscr ipt : $[
❍ Mu l t idimen sion a l Associa t ive Ar r a ys a n d t h e $; Va r ia bl e
❍ Th e Wor d-Br ea k Specif ier : $:
❍ Th e Per l Pr ocess ID: $$
❍ Th e Cu r r en t Fil en a me: $ARGV
❍ Th e Wr it e Accu mu l a t or : $^A
❍ Th e In t er n a l Debu ggin g Va l u e: $^D
❍ Th e Syst em Fil e Fl a g: $^F
❍ Con t r ol l in g Fil e Edit in g Usin g $^I
❍ Con t r ol l in g Debu ggin g: $^P
❍ Th e Pr ogr a m St a r t Time: $^T
❍ Su ppr essin g Wa r n in g Messa ges: $^W
❍ Th e $^X Va r ia bl e
● Pa t t er n Syst em Va r ia bl es
❍ Ret r ievin g Ma t ch ed Su bpa t t er n s
❍ Ret r ievin g t h e En t ir e Pa t t er n : $&
❍ Ret r ievin g t h e Un ma t ch ed Text : t h e $` a n d $' Va r ia bl es
❍ Th e $+ Va r ia bl e
● Fil e Syst em Va r ia bl es
❍ Th e Def a u l t Pr in t For ma t : $~
❍ Specif yin g Pa ge Len gt h : $=
❍ Lin es Rema in in g on t h e Pa ge:
$-❍ Th e Pa ge Hea der Pr in t For ma t : $^
❍ Bu f f er in g Ou t pu t : $|
❍ Th e Cu r r en t Pa ge Nu mber : $%
● Ar r a y Syst em Va r ia bl es
❍ Th e @_ Va r ia bl e
❍ Th e @ARGV Va r ia bl e
❍ Th e @F Va r ia bl e
❍ Th e @INC Va r ia bl e
❍ Th e %INC Va r ia bl e
❍ Th e %ENV Va r ia bl e
❍ Th e %SIG Va r ia bl e
● Bu il t -In Fil e Va r ia bl es
❍ STDIN, STDOUT, a n d STDERR
❍ ARGV
❍ DATA
❍ Th e Un der scor e Fil e Va r ia bl e
● Specif yin g Syst em Va r ia bl e Na mes a s Wor ds
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
Day
18
References in Perl 5
● In t r odu ct ion t o Ref er en ces
● Usin g Ref er en ces
● Ref er en ces a n d Ar r a ys
● Mu l t idimen sion a l Ar r a ys
● Ref er en ces t o Su br ou t in es
❍ Usin g Su br ou t in e Templ a t es
● Usin g Su br ou t in es t o Wor k wit h Mu l t ipl e Ar r a ys
❍ Pa ss By Va l u e or By Ref er en ce?
● Ref er en ces t o Fil e Ha n dl es
❍ Wh a t Does t h e *variable Oper a t or Do?
● Usin g Symbol ic Ref er en ces… Aga in
❍ Decl a r in g Va r ia bl es wit h Cu r l y Br a ces
● Mor e on Ha r d Ver su s Symbol ic Ref er en ces
● For Mor e In f or ma t ion
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
● Exer cises
Day
19
Object-Oriented Programming in Perl
● An In t r odu ct ion t o Modu l es
❍ Th e Th r ee Impor t a n t Ru l es
● Cl a sses in Per l
● Cr ea t in g a Cl a ss
● Bl essin g a Con st r u ct or
❍ In st a n ce Va r ia bl es
● Met h ods
● Expor t in g Met h ods
● In vokin g Met h ods
● Over r ides
● Dest r u ct or s
● In h er it a n ce
● Over r idin g Met h ods
● A Few Commen t s Abou t Cl a sses a n d Object s in Per l
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
● Th e require Fu n ct ion
❍ Th e require Fu n ct ion a n d Su br ou t in e Libr a r ies
❍ Usin g require t o Specif y a Per l Ver sion
● Th e $#array Va r ia bl es
❍ Con t r ol l in g Ar r a y Len gt h Usin g $#array
● Al t er n a t ive St r in g Del imit er s
❍ Def in in g St r in gs Usin g <<
● Specia l In t er n a l Va l u es
● Usin g Ba ck Qu ot es t o In voke Syst em Comma n ds
● Pa t t er n Ma t ch in g Usin g ?? a n d t h e reset Fu n ct ion
❍ Usin g reset wit h Va r ia bl es
● Ot h er Fea t u r es of t h e <> Oper a t or
❍ Sca l a r Va r ia bl e Su bst it u t ion a n d <>
❍ Cr ea t in g a List of Fil en a mes
● Gl oba l In dir ect Ref er en ces a n d Al ia ses
● Pa cka ges
❍ Def in in g a Pa cka ge
❍ Swit ch in g Bet ween Pa cka ges
❍ Th e main Pa cka ge
❍ Ref er r in g t o On e Pa cka ge f r om An ot h er
❍ Specif yin g No Cu r r en t Pa cka ge
❍ Pa cka ges a n d Su br ou t in es
❍ Def in in g Pr iva t e Da t a Usin g Pa cka ges
❍ Pa cka ges a n d Syst em Va r ia bl es
❍ Accessin g Symbol Ta bl es
● Modu l es
❍ Cr ea t in g a Modu l e
❍ Impor t in g Modu l es In t o You r Pr ogr a m
❍ Usin g Pr edef in ed Modu l es
● Usin g Per l in C Pr ogr a ms
● Per l a n d CGI Scr ipt s
● Tr a n sl a t or s a n d Ot h er Su ppl ied Code
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
● En t er in g a n d Exit in g t h e Per l Debu gger
❍ En t er in g t h e Debu gger
❍ Exit in g t h e Debu gger
● List in g You r Pr ogr a m
❍ Th e l comma n d
❍ Th e - Comma n d
❍ Th e w Comma n d
❍ Th e // a n d ?? Comma n ds
❍ Th e S Comma n d
● St eppin g Th r ou gh Pr ogr a ms
❍ Th e s Comma n d
❍ Th e n Comma n d
❍ Th e f comma n d
❍ Th e Ca r r ia ge-Ret u r n Comma n d
❍ Th e r Comma n d
● Displ a yin g Va r ia bl e Va l u es
❍ Th e X Comma n d
❍ Th e V Comma n d
● Br ea kpoin t s
❍ Th e b Comma n d
❍ Th e c Comma n d
❍ Th e L Comma n d a n d Br ea kpoin t s
❍ Th e d a n d D Comma n ds
● Tr a cin g Pr ogr a m Execu t ion
● Lin e Act ion s
❍ Th e a Comma n d
❍ Th e A Comma n d
❍ Th e < a n d > Comma n ds
❍ Displ a yin g Lin e Act ion s Usin g t h e L Comma n d
● Ot h er Debu ggin g Comma n ds
❍ Execu t in g Ot h er Per l St a t emen t s
❍ Th e H Comma n d: List in g Pr ecedin g Comma n ds
❍ Th e ! Comma n d: Execu t in g Pr eviou s Comma n ds
❍ Th e T Comma n d: St a ck Tr a cin g
❍ Th e p Comma n d: Pr in t in g a n Expr ession
❍ Th e = Comma n d: Def in in g Al ia ses
❍ Pr edef in in g Al ia ses
❍ Th e h Comma n d: Debu gger Hel p
● Su mma r y
● Q&A
❍ Qu iz
Week
3
Week 3 in Review
Appendix
A
Answers
● An swer s f or Da y 1, "Get t in g St a r t ed"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 2, "Ba sic Oper a t or s a n d Con t r ol Fl ow"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 3, "Un der st a n din g Sca l a r Va l u es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 4, "Mor e Oper a t or s"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 5, "List s a n d Ar r a y Va r ia bl es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 6, "Rea din g f r om a n d Wr it in g t o Fil es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 7, "Pa t t er n Ma t ch in g"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 8, "Mor e Con t r ol St r u ct u r es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 9, "Usin g Su br ou t in es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 10, "Associa t ive Ar r a ys"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 11, "For ma t t in g You r Ou t pu t "
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 12, "Wor kin g wit h t h e Fil e Syst em"
❍ Exer cises
● An swer s f or Da y 13, "Pr ocess, St r in g, a n d Ma t h ema t ica l Fu n ct ion s"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 14, "Sca l a r -Con ver sion a n d List -Ma n ipu l a t ion Fu n ct ion s"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 15, "Syst em Fu n ct ion s"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 16, "Comma n d-Lin e Opt ion s"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 17, "Syst em Va r ia bl es"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 18, "Ref er en ces in
Per l 5"
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 19, "Object -Or ien t ed Pr ogr a mmin g in Per l "
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 20, "Miscel l a n eou s Fea t u r es of Per l "
❍ Qu iz
❍ Exer cises
● An swer s f or Da y 21, "Th e Per l Debu gger "
❍ Qu iz
Appendix
B
ASCII Character Set
Credits
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 : h t t p://l ea f .st pn .sof t .n et
e-mail : l ea f wr it er s@l ea f .st pn .sof t .n et
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 [email protected] or [email protected], or on t h e Wor l d Wide Web a t http://www.interlog.com/~davet/.
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 .
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 [email protected] or
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:
Th e syn t a x of t h e for 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 in expr2 beh a ves ju st l ike t h e on es in while a n d if 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.
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:
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 italicmonospace 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:
Day 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 .
Day 3, "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 .
Day 4, "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.
Day 5, "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.
Day 6, "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.
Day 7, "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 :
Day 8, "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.
Day 9, "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.
Day 10, "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.
Day 11, "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.
Day 12, "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.
Day 13, "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
Day 14, "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 :
Day 15, "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.
Day 16, "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.
Day 17, "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.
Day 18, "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.
Day 19, "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.
Day 20, "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.
We e k
1
Week at a Glance
CONTENTS
● Wh er e You 'r e Goin g
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 , Da y 1, "Get 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 .
Da y 1, "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 .
Da y 2, "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 .
Da y 4, "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.
Da y 5, "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.
Da y 6, "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, Da y 7, "Pa 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.
Chapter 1
Getting Started
CONTENTS
● Wh a t Is Per l ?
● How Do I Fin d Per l ?
❍ Wh er e Do I Get Per l ?
❍ Ot h er Pl a ces t o Get Per l
● A Sa mpl e Per l Pr ogr a m
● Ru n n in g a Per l Pr ogr a m
❍ If Somet h in g Goes Wr on g
● Th e Fir st Lin e of You r Per l Pr ogr a m: How Commen t s Wor k
❍ Commen t s
● Lin e 2: St a t emen t s, Token s, a n d <STDIN>
❍ St a t emen t s a n d Token s
❍ Token s a n d Wh it e Spa ce
❍ Wh a t t h e Token s Do: Rea din g f r om St a n da r d In pu t
● Lin e 3: Wr it in g t o St a n da r d Ou t pu t
❍ Fu n ct ion In voca t ion s a n d Ar gu men t s
● Er r or Messa ges
● In t er pr et ive La n gu a ges Ver su s Compil ed La n gu a ges
● Su mma r y
● Q&A
● Wor ksh op
❍ Qu iz
❍ Exer cises
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
● How t o wr it e a ver y simpl e Per l pr ogr a m
● Th e dif f er en ce bet ween in t er pr et ive a n d compil ed pr ogr a mmin g l a n gu a ges
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 Da y 2, "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 oe-t ocol (FTP). Th e f ol l owin g is a sa mpl e FTP session e-t h a e-t e-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 <[email protected]>. If you have any
230-other unusual problems, please report them to <[email protected]>.
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.
6. Th e get comma n d copies t h e f il e perl-5.001.tar.gz 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
gunzipis 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.eduu 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.gzwil 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, a n d Per l u su a l l y is n a med /usr/local/bin/perl.
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 In t er n et a ddr ess 192.48.96.9
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 In t er n et a ddr ess 192.245.137.1
Directory /pub/perl/sources genetics.upenn.edu In t er n et a ddr ess 128.91.200.37
Directory /perl5
Eu r ope
Site Location
ftp.funet.fi In t er n et a ddr ess 128.214.248.6 Directory
/pub/languages/perl/ports/perl5
ftp.zrz.tu-berlin.de In t er n et a ddr ess Directory /pub/unix/perl130.149.4.40
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>;
$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
Wh en you r u n program1_1, 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 l in e of in pu t , program1_1 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
If List in g 1.1 is st or ed in t h e f il e program1_1 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 :
● En t er t h e comma n d ./program1_1, 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.
● Add t h e cu r r en t dir ect or y . t o you r PATH 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.
If you r eceive t h e messa ge
/usr/local/bin/perl not found
or somet h in g simil a r , t h is mea n s t h a t Per l is n ot in st a l l ed pr oper l y on you r ma ch in e. See t h e sect ion "How Do I Fin d Per l ?" ea r l ier t oda y, f or mor e det a il s.
If you don 't u n der st a n d t h ese in st r u ct ion s or a r e st il l h a vin g t r ou bl e r u n n in g List in g 1.1, t a l k t o you r syst em a dmin ist r a t or .
The First Line of Your Perl Program: How Comments
Work
Now t h a t you 've r u n you r f ir st Per l pr ogr a m, l et 's l ook a t ea ch l in e of List in g 1.1 a n d f igu r e ou t wh a t it does.
Lin e 1 of t h is pr ogr a m is a specia l l in e t h a t t el l s t h e syst em t h a t t h is is a Per l pr ogr a m:
Let 's br ea k t h is l in e down , on e pa r t a t a t ime:
● Th e f ir st ch a r a ct er in t h e l in e, t h e # ch a r a ct er , is t h e Per l comment character. It
t el l s t h e syst em t h a t t h is l in e is n ot a n execu t a bl e in st r u ct ion .
● Th e ! ch a r a ct er is a specia l ch a r a ct er ; it in dica t es wh a t t ype of pr ogr a m t h is is.
(You don 't n eed t o wor r y a bou t t h e det a il s of wh a t t h e ! ch a r a ct er does. Al l you h a ve t o do is r emember t o in cl u de it .)
● Th e pa t