You are on page 1of 75
The Basics of Perl 2.1 Origins and Uses of Per} Scilars and Their Opertions Assignment Statements and Sipe apt uad Out 24 Contol Statements Fandimentals of Arrays 8.6 Hashes 103 References 8.8 uncione 89 Pater Matching 8.10 File Inpucand Ourpat 21 An Example Summary » Review Quen» Execs elem Intrface (CCl) prop=nning. However, itis beneficial o mort pro= grammer to ocome fila with Pests capailises, Peli Meible, power, ‘widely used programming langvage—and it would be so even if CGI program ‘ming didnot eust. This chaper takes you oa quick tour of Pel, introducing ‘most of the important concepss and constructs but lesving out many of the ‘etl ofthe language. In spite ofits beri, however, ifyou are an experienced programmer, you cin lesen to write wef Per programs by studying this chap- ‘er. The sinilanty of Perl to other common programming languages, especialy (Cand JavaScript makes his relatively easy, at least fr those who know one of She agus ned more denis there are mmeows books deed wr am 312 Chapter 8-Me Basis ofPot 8.4 8.2 “This chapter begins with a description of Peels sealar valves snd variables and their use in expressions and asignment statements, Net, it coves contol ‘xpresons andthe cllecron of contol tems available in Per Then it Jnroduces Perl's two builein dea srsrares, rays ad hashes, and references ‘This is followed by description offnctions and how they are defined and called including the poeulie way or high-level lngvge) hat parumeters ce ‘sed. Finally the chapter covers Perl ptetn matching and the basi oper ‘Sons of fie input and eurput. Although we atte to describe ed ina sng ‘apr, do no be mised ino thinking thar this sl or simple langage — inisnether Origins and Uses of Perl Pec began asa relatively small language withthe modest purpose of including and expending onthe operations ofthe teat-procesing lnguage awe ad the system adminisvaon capabilites of the UNIX shell ngage, inilly sh Perl was ist released in 1987 ater being developed and implemented by Larry ‘Wall Since then it as grown considera, borrowing fenires fom othe la guages a wel a inverting afew of is ow Along the wast picked up support for communications using sockets, 1 module construc, and stpport for abject ‘rented programming. Is text pater-natching epabiiis are elaborte snd powerful whichis one ofthe reson became the most popula language for {Crt programming. Peis paver thacching has buen copied into several eet languages, incloding JaaSexpt, Ruby, and PHE, Per is now used for many of the small to medium-size programming projet formerly done in C. Pert is language whose smplementation is between compiled (o machine code) and interpreted languages, Pel progr ate compiled tn intermediate form, in part to check for errors, bot monly to make possible inpessiverun- time performance even though iti interpeeied. When me refer to perl, We mean the Pel language processing system, which compiles and interprets pro- tgams. The per system includes a debugger, among other things Perl has been ported to every common computing platorm, rom the vati- ‘ows versions of UNIX to Windows and everthing in beeen, Mos versions of [UNDG come with Ped, and Mac OS X has Pel installed Scalars and Thelr Operations Perl has three eategores of varsbles—scalas ray, an hashes each iden Sid by the ist character of their names (¢ fr scalar arable, ¥ for array vari ale and for ash varable). ‘This seson dicuses the important characteristics of the most commonly used kind of vatables, namely scalar Scalar variables can store three diferent kinds of values: numbers character strings and references, which are addresses. We postpone dxeusing references ‘il Seton 87. 1 Sap Pinter gee coe/ formar i lan oa 82 Selo and Tho Opoatons 313, "The numeri values stored in salar variables are represented internally in ouble-precsion foning-point frm Although there ia way o suggest che compile) that integer operations be wee on scalar numeric wales in most ‘ase the operations are dane in double- precision Hating pois. 8.2.1 Numeric and String Liter ‘Numeric literals can have the forms of ether integers or oating point valves Integer iter are strings of digits, Flooting-pont Inerals can have either dei ‘nal points or exponent or both. Exponent ae specified with an uppercase or lowerease and.a possibly signed ineger ten. The following ave legal snumeri eral 727.2 72 12. TR Med Ted 02 7.2802 Integer ters canbe wren in hexadecral (hse 16) by preceding their fst ig with ther Ox oF ‘Sting ters an spear in wo forms, depending cn whether their delimit cers are single quotes) or double quotes) Single-quoted sing literals can- ot include characters specified with escape sequences, such ar newline cariters specified with \n# If an actalsingle-quote character i needed in a String lneal thats delisted by single quotes, the embedded single quote is receded bya backlash s shown inthe fllowing example "You\'re the most freckly person T\"ve ever net" If an escape sequence is embedded in a single-quoted sting lies, cach character inthe sequence i aken Leal a isl. For example, the sequence {pin the following string itera willbe treated as owo characters, balsash andane "You have freckles, \n but T don\'t Ifa suring literal with the same characteristics as single-quned strings i needed bat you want to ure different delimiter, precede the delimiter with g, ‘shown inthe folowing example: BT don't want to go, T can't go, T won't gol the new delimiters a parenthesis brace, a Bracke, or an angle bracket, the eft element ofthe pair must he used on the lef and the right element must be used on the right, For example geE don't want to go, T can't go, I won't gol> ouble-quoted trig trl ifr from single-quoted string literals wo sways: Fit they ca include speci characters specie wich expe sequences, Sccond, embedded arable natns at interpolated into the sting, which means ‘that their ales ae subttted for their names. We discs the Bre of thee di ferences here; che oer willbe discussed in Sesion 822. Avago an ovo cl ac loess che Thgeel ar ethers hg eatin a bmg are are meen er ‘Sian some ent, a ears nec ts bon one ae

You might also like