Introduction To Perl
Introduction To Perl
October 2013
Document reference: 3169-2013
#!/usr/local/bin/perl
#
#Findalltheimagesthathavebeendownloaded,andhowoften.
#Storeinanestedhash:filename>size>count
usestrict;
useData::Dumper;
my%downloads;#Predeclarethehash
while(<>){
nextunless/Download/;#ignoreeverythingthatsnotadownload
nextif/imageBar/;#ignoredownloadsfortheimageBar
m!/hulton/(\w+?)/(.+\.jpg)!;#getthedirectory(size)andfilename
#Ifthefilenamedoesn'texist,weneedtocreateit.
$downloads{$2}={}unlessdefined$downloads{$2}
$downloads{$2}>{$1}++;#incrementfilename>sizebyone
}
$Data::Dumper::Indent=3;#Definethepaddingfortheoutput
$Data::Dumper::Sortkeys="true";#Sortkeys(defaultASCIIsort)
printDumper(\%downloads);#displaythegathereddata
2013
Slide 1 of 69
PracticalExtractionandReportLanguage
2013
Languageforeasymanipulationof
text
files
processes
Hasmanysimilarietiesto:
C
Java
unixshell
Convertersavailablefor:
awk
sed
Version1.000wasreleasedin1987,version5cameoutin1994.
Unicodecamein2000,withPerl5.6
Perlnowreleaseamajorstableversionupgradeannually(5.18.0in2013)
Perl6(Perl5withlotsofbellsandwhistles)starteddevelopmentinSummer2005.
Don'tholdyourbreath.
Slide 2 of 69
Overview
Avoidsinbuiltlimitsusesfullvirtualmemory
Manydefaultsdoeswhatyouwant
Easytodofromthecommandprompt
perle'print"helloworld\n"'
Easytodosimplescripts
#!/usr/local/bin/perl
#ahelloworldscript
printhelloworld\n;
useof#!forspecifyingprocessor
fileneedstobeexecutable
chmod+xhello
2013
Onlyhasthreebasictypesofdata
Slide 3 of 69
Versionsavailablefor
2013
acornRISCOS
Aix/HPUX/Irix/DigitalUnix/UnixWare/etc
AS/400
Solaris/SunOS
BeOS
Linux(allplatforms)
MacOS(system7.5onwards)
MacOSX
MSDos
Xenix
NovellNetware
VMS
IBMOS/2
BSD(Free,Net&Open)
QNX
Windows(i386,Alpha&Win64)
PDAs
MobilePhones(Symbian,[Jailbroken]iPhone,
Android,WindowsMobile)
Slide 4 of 69
ScalarVariables
SimplestkindofPerlvariable
Basisforeverythingelse
scalarvariablenamesareidentifiedbyaleading$character
$name
variablenamestartswithazAZthen[azAZ09_]
$A_very_long_variable_name_ending_in_1
holdnumericorstringdata,noseparatetypes.
$thing=123;
$thing2=fred;
doesnotneedtobepredeclared.
Lexicalscope:
while(expression){
my$variable;#$variableisonlyaccessiblewithinthisloop
local$var2;#$var2alreadyexists,andcangetanewvaluewithin
#thisloop,butrevertswhenbackoutsidethisloop
#morestuff;
}
2013
Slide 5 of 69
ScalarVariablesnumeric
Allnumericvariablesstoredasrealinternally
floatingpointliteralsasinC
1.25
7.25e45
$num=12e24;
integerliterals
12
2004
octal(base8)constantsstartwithleading0
0377
$y=0177;#0177octalis127decimal
hex(base16)constantsstartwithleading0x(or0X)
0x7f
$y=0x7f;#0x7fhexis127decimal
2013
Slide 6 of 69
ScalarVariablesString
Literalstringssinglequotedordoublequoted
$fred=hello;
$fred=hello;
2013
Nolimitonstringlengthotherthanmemory
Allowfull8bit0255characterset
noterminatingNULL
canmanipulaterawbinarydata(egimagefiles)
Slide 7 of 69
Doublequotedstrings
Delimitedbydoublequotes
Scannedforbackslashescapes(fulllistinrefguide)
\n
\cC
\x8
:newline
:controlC
:hex08=backspace
$string=helloworld\n;
Scannedforvariableinterpolation
$a=fred;
$b=hello$a;#Giveshellofred
2013
Unassignedvariableinterpolatesasnullstring
Slide 8 of 69
Singlequotedstrings
Delimitedbysinglequotes
Anycharacterexcept\andislegal,includingnewline
togetor\useabackslashtoescape:\and\\
don\t;#5characters
hello
there;#11charsinclnewline
Notscannedforvariableinterpolation
Usedlessoftenthandoublequotedstrings
Goodforfixedtext:
$footer='<divid="credit_graphics"><ahref="http://lucas.ucs.ed.ac.uk/"title="Edited
byKiz"><!
><imgsrc="/images/kiz_logo.jpg"alt="EditedbyKiz"
class="noborder"/></a><!
><ahref="http://web.archive.org/web/*/http://lucas.ucs.ed.ac.uk"
title="ArchivedbytheWayBackMachinesinceJuly2000"target="_blank"><img
src="/images/wayback_logo_sm.gif"alt="ArchivedbytheWayBackMachinesinceJuly2000"
class="noborder"/></a><!
><ahref="http://www.apache.org/"><imgsrc="/icons/apache_pb2.png"alt="Poweredby
Apache"title="PoweredbyApache2.2.xwebserver.(c)ApacheFoundation"class="left"
border="0"/></a>></div>';
2013
Slide 9 of 69
Operators
NumericoperatorsgenerallythesameasC/Java
+/*%**&|>><<(andmore)
2013
precedenceasinC
fulllistinreferenceguide
Slide 10 of 69
Operators(contd)
Numericorstringoperands
contextistakenfromoperator
string<>numericconversionisautomatic
trailingnonnumberisstrippedfromstringsinconversion
123+1gives124
123zzz+1gives124
zzz123+1gives1
autoincrementissmart,andcanworkinstrings
$a=zzz;
$a++;#$aisaaaa
autoincrementcanbetooclever:
$foo="1zzz";$foo++;print$foo;#gives"2"
$foo="zz1";$foo++;print$foo;#gives"zz2"
2013
useWARNINGS(mentionedattheendoftheworkbook)
Slide 11 of 69
Comparison Operators
Differentsetsofoperatorsfornumericandstringcomparison
Comparison
Equal
Notequal
Lessthan
Greaterthan
Lessthanorequalto
Greaterthanorequalto
Numeric
==
!=
<
>
<=
>=
String
eq
ne
lt
gt
le
ge
Thus...
(12lt5)#truesincestringcomparison
(5<12)#trueinnumericcontext
Smartmatchoperator(since5.10butseeperldocperlsyn)
"Foo"~~"Foo"
42~~42
42~~42.0
42~~"42.0"
2013
#true
#true
#true
#true
"Foo"~~"Bar"
#false
42~~"42x"
#false
"Moose"~~[Foo,Bar,Baz] #false
"Moose"~~[qw(FooBarMooseBaz)] #true
Slide 12 of 69
StringOperators
Concatenationoperator
$string=hello.world;#giveshelloworld
#Note'.'not'+'!!
Stringrepetitionoperator
$string=fredx3;
#givesfredfredfred
(3+2)x4;
#gives5555.Typeconversionagain.
#Bracketsneededasxhasprecedenceover+
3+2x4;
#gives2225.Typeconversionagain.
Reverse
$string=fred;
$newstring=reverse($string);#Givesderf
2013
Slide 13 of 69
MoreStringOperators
chop()
removesthelastcharacterfromastringvariable
$x=helloworld;
chop($x);#xisnowhelloworl
chomp()
Perl5replacementforthecommonuseofchopinPerl4
removesanewlinefromtheendofastringvariable
$x=helloworld;
chomp($x);#$xisstillhelloworld
$x=helloworld\n;
chomp($x);#$xishelloworld
defaultbehaviour.Specialvariable$/forgeneralcase
$x=helloworld;
$old_newline=$/;
$/=d;#Weknowitsabadideabut...
chomp($x);#$xishelloworl
2013
Slide 14 of 69
SimpleInput/Output
<STDIN>representsstandardinput
$line=<STDIN>;
#getsonelinefrominputstreaminto$line
newlinesexistontheendoftheline,canberemovedwithchomp()
$line=<STDIN>;
chomp($line);
print()
takesalistofarguments
defaultstostandardoutput
printHellothere\n;
2013
Slide 15 of 69
Arrays
Secondvariabletype
Simplyanorderedlistofscalarvalues
Listisagenerictermforlistliteralandarray.
Listliteraliscommaseparatedsequenceofscalars,inparenthesesnota
variable
(1,2,3)
(1,one)
()#Emptylist
Arrayislistofscalarsavariablenumberofscalars
Arrayvariablenamesareidentifiedbyaleading@character
@poets=(jonson,donne,herbert,marvell);
$#namecontainstheindexofthelastelementofarray@name
$lastIndex=$#poets;#$lastIndexcontains3
2013
@fredand$fredcanbothexistindependently,separatenamespace(memory
allocation)
Slide 16 of 69
Moreaboutarrays
Individualscalarvaluesaddressedas$name[n],etc,indexedfrom0
@fred=(7,8,9);
print$fred[0];#prints7
$fred[0]=5;#Now@fredcontains(5,8,9)
Arrayscanbeincludedinarrays
Insertedarrayelementsaresubsumedintotheliteral.Thelistisnotan
element.
@barney=@fred;
@barney=(2,@fred,zzz);#@barneycontains(2,5,8,9,zzz)
Literallistscanbeassignedto
excessvaluesontherighthandsidearediscarded
excessvaluesonthelefthandsidearesettoundef.
($a,$b,$c)=(1,2,3);#$a=1,$b=2,$c=3
($a,$b,$c)=(1,2);#$a=1,$b=2,$c=undef
($a,$b)=(1,2,3);#$a=1,$b=2,3isignored
2013
Slide 17 of 69
Arrayslices
Alistofelementsfromthesamearrayiscalledaslice
indexesarealistenclosedinsquarebrackets
@prefixsinceasliceisalist
@fred[0,1];#Sameas($fred[0],$fred[1])
@fred[1,2]=(9,10);#Sameas$fred[1]=9;$fred[2]=10;
slicesalsoworkonliterallists
@who=(fred,barney,betty,wilma)[1,3];
#@whocontains(barney,wilma)
@who=(fred,barney,betty,wilma)[1..3];
#@whocontains(barney,betty,wilma)
2013
Arrayslicescountasliterallists,notarrays
@who[0]isaoneelementliterallist,notanarray!
Itwillprobablydowhatyouwant,butnotforthereasonyouthink.
see"ScalarandArrayContext"(slide23)
Slide 18 of 69
Arrayoperators
push()andpop()
addandremoveitemsfromtherighthandsideofanarray/list(theend)
push(@mylist,$newvalue);#like@mylist=(@mylist,$newvalue)
$oldvalue=pop(@mylist);#Putslastvalueof@mylistin$oldvalue
pop()returnsundefifgivenanemptylist
push()alsoacceptsalistofvaluestobepushed
@mylist=(1,2,3);
push(@mylist,4,5,6);#@mylist=(1,2,3,4,5,6)
2013
Slide 19 of 69
shift()andunshift()
addandremoveitemsfromthelefthandsideofanarray/list(thestart)
unshift(@fred,$newValue);#like@fred=($newValue,@fred)
$oldValue=shift(@fred);#like($oldValue,@fred)=@fred
shift()returnsundefifgivenanemptyarray
unshift()alsoacceptsalistofvaluestobeadded
@mylist=(1,2,3);
unshift(@mylist,4,5,6);#@mylist=(4,5,6,1,2,3)
2013
Slide 20 of 69
push/pop/shift/unshiftDiagram
$x=shift(@myarray);$x=pop(@myarray);
unshift(@myarray,$a,$b,$c)push(@myarray,$a,$b,$c)
2013
Slide 21 of 69
Morearrayoperators
reverse()
Reversestheorderofthearguments
@a=(7,8,9);
@b=reverse(@a);#@b(9,8,7)
@b=reverse(1,@a);#@b(9,8,7,1)
sort()
sortsargumentsasstringsinascendingASCIIorder
defaultcaseofgeneralsortfacility
@x=sort(red,green,blue);#gives(blue,green,red)
@y=sort(1,5,10,15);#gives(1,10,15,5)
Sortcanbetoldhowtodothesorting:
@z=sort{$a<=>$b}@y;#Numericalsort(ascending)(1,5,10,15)
@z=sort{$b<=>$a}@y;#Descendingnumericalsort,gives(15,10,5,1)
@x=sort{$bcmp$a}(b,r,g);#gives(r,g,b)
@z=sortmy_sort_function@y#Nottellingyouhowtowritefunctions...
chomp()
Applieschomptoeachelementinanarray
chomp(@words_in_LOTR);
2013
Slide 22 of 69
ScalarandArrayContext
LotsofPerliscontextdriven
operandsareevaluatedinascalarorarraycontext
@fred=@barney;#ordinaryarrayassignment
$fred=@barney;
#Returnsnumberofelementsinarray@barney
#$fredcontains5(using@barneyfromslide17)
ArraysandLiteralListsdodifferentthings
$number=@array;
#$numbercontainsthenumberofelementsinthearray
$number=('aa','bb','cc','dd','ee','ff');
$number=qw(aabbccddeeff);
#$numberdoesnotcontainthenumberofelementsintheliterallist,but
#thevalueofthelastelementofthelist:$numbercontains'ff'
$number=@array[$n..$m];
#anarraysliceisconsideredaliterallist,therefore$numbercontains
#thevalueofthelastelementoftheslice
@all=<STDIN>isusedtoreadallofstandardinputintoanarray
#Eachlinewillbeterminatedwithanewline
@all=<STDIN>;#ReaduntilEndOfFile
#fromkeyboard:usectrl+d(unix)orctrl+z(windows)
chomp(@all);
2013
Slide 23 of 69
Controlstructures
2013
StatementblockenclosedincurlybracketslikeC/Java
selection(flowcontrol)
if/unlessstatement
optionalelsifandelseblocks
noelsunless
loops(iterativecontrol)
while/until
for
foreach
switch(addressedinpart2)
Slide 24 of 69
Controlstructures(contd)
curlybracesarealwaysrequired(unlikeC/Java).
if(expression){
statement1;
statement2;
....
}elsif(expression2){
statement1;
statement2;
....
}elseif(expression3){
statement1;
statement2;
....
}else{
statement1;
statement2;
....
}
}
print"Testingforquantuminstabilitiesinsubethericsingularities\n"if
$trekkie;
2013
Slide 25 of 69
Whatistrue?
"Truthisselfevident"
Controlexpressionsareevaluatedasastring(scalarcontext)
if($thing){
statement;
}
""(nullstring)and0(numericzero)arefalse
undefinanumericcontextconvertsto0
undefinastringcontextconvertsto""
0#convertsto0sofalse
11#sumsto0sofalse
"1"#not""or0sotrue
"0e0"#not""or0sotrue!(0e0isastring)
"0e0"+0#sumsto0,sofalse
undef#convertsto""or0(dependingoncontext)sofalse
scalarkeys%my_new_hash#therearenoelementsinthehash,so0...false
Fromslide12operatorsreturntrue/false:
($count<$max)#truefor$count=5&$max=10
($sring1ne$string2)#truefor$string1=Ian&$string2=Alex
($thing1~~@thing2)#truefor$thing1=a&@thing2=(a,b,c)
2013
Slide 26 of 69
Morecontrolstructures
Iterativecontrolstructures
whileiterateswhile(expression)istrue
while($temperature>32){
statement1;
statement2;
}
untiliteratesuntil(expression)istrue
until($temperaturelt'3'){
statement1;
statement2;
}
nextstopsthecurrentflowandexecutesthenextiteration
laststopsthecurrentflowandexitstheloop
my$i=1;
while($i>0){
nextif($i%2);#%is"modulus":returnstheexcessafterthedivision
#2%2=0;3%2=1;4%2=0;8%3=2;27%14=13;etc
lastif($i>101);
print$i;
$i++;
}#Thiscodeisflawed....canyouspotwhy?
2013
Slide 27 of 69
forstatement
forstatementmuchlikeC(andjava)
for(initial_exp;test_exp;increment_exp)
for($i=1;$i<=10;$i++){
print$i;
}
aninfiniteloop
for(;;){
statement1;
statement2;
...
}
#suggestusinglasttoexit
2013
Slide 28 of 69
foreachstatement
Iteratesthroughlistsinorderassigningtoscalarvariable
scalarvariableislocaltotheloop
foreachmy$i(@list){
statement1;
$i+=$random_number;#Note:thismodifiestheactualelementin@list
....
}
toprintanarrayinreverse
foreachmy$b(reverse@a){
print$b;
}
ifscalarvariableomitteddefaultsto$_
foreach(reverse@a){#assignsto$_
print;#prints$_
}
2013
Slide 29 of 69
Summary
2013
ScalarVariables
numeric
stringssinglevsdoublequoted
Operators
numericvsstring
comparisonandimplicationscontext
SimpleI/O
inputfromSTDIN
print()
Arrays
namingandcomparing
slicesandsizes
push(),pop(),shift(),unshift(),reverse()&sort()
ControlStructures
whatistrue?
if(),unless(),while(),until(),for()&foreach()
next&last
Slide 30 of 69
PracticalExercises
AllquestionsarefromtheLearningPerlbook
Chapters2,3&4
ThereisnoCorrectAnswerifitworks,itsright1
Perlmotto:"TIMTOWTDI"(timtowtiddy:"ThereIsMoreThanOneWayToDoIt")
Suggestedwaytowritecode:
writecodeandsavetoafile
typeperlfilenametoruntheprogram
ForWindowsusers:
clickontheappsmenu,andruncmd
Inthewindowthatopens,enter
cdm:/users/trxxyyy/LocalDocuments/Desktop
reminder:ctrlzratherthanctrldforendoffile
1Someanswersaremoreaestheticallypleasingthanothers
2013
Slide 31 of 69
Part2
2013
Slide 32 of 69
AssociativearraysHashes
Thirdandfinalvariabletype.
listofindexesandassociatedvalues
likeatwocolumntable
hashvariablenameisidentifiedbyaleading%character
%hash
indexesarescalarscalledkeys
valuesarescalarscalledvalues
individualscalarvaluesaddressedas$name{key}
$superheroes{superman}=DCComics;
#createsakeysupermanin%superheroes,withvalueDCComics
$superheroes{FantasticFour}=MarvelComics;
#createsakeyFantasticFourin%superheroes,withvalueMarvelComics
2013
%superheroes,@superheroesand$superheroescanallexistindependently
Slide 33 of 69
Initialisingandcopyinghashes
Initialisinghashes
usingalist
%identities=(Superman,ClarkKent,Spiderman,PeterParker);
usingkeyandvaluepairs(sinceperl5.001)
%identities=(Superman=>ClarkKent,Spiderman=>PeterParker);
Copyinghashes
ashashes
%identities=%superheroes;#normalwaytocopywholehash
aslists
@ident_list=%identities;#resultsin@ident_listas
#(Superman,ClarkKent,Spiderman,PeterParker)
#or(Spiderman,PeterParker,Superman,ClarkKent,)
%ident_2=@ident_list;#creates%ident_2asacopyof%identities
2013
orderofinternalstorageundefined
Slide 34 of 69
HashOperators
keys()operator
producesalistofkeys
orderisarbitrary
@list=keys(%identities);#@listgets(superman,Spiderman)
#or(Spiderman,superman)
parenthesesoptional
#onceforeachkeyof%identities
foreach$hero(keys%identities){
printThesecretidentityof$herois$identities{$hero}\n;
}
inscalarcontext,keys()returnsnumberofelements
$num_elems=keys(%identities)#$num_elemscontains2
if(keys(%superheroes)){
#ifkeys()isnotzero,thehashisnotempty
statement1;
....
}
2013
Slide 35 of 69
HashOperators(contd)
values()operator
producesalistofvalues
sameorderaskeys()
@list=values(%identities);#@listgets(PeterParker,Clarkkent)
or(ClarkKent,PeterParker)
parenthesesoptional
@list=values%identities;
2013
Slide 36 of 69
Hashoperators(contd)
each()operator
returnseachkeyvaluepairastwoelementlist
afterlastelement,returnsemptylist
while(($first,$last)=each(%lastnames)){
printThelastnameof$firstis$last\n;
}
delete()operator
removesahashkeyvaluepair
returnsvalueofdeletedelement
%names=(Clark=>Kent,Peter=>Parker);#%nameshastwoelements
$lastname=delete$names{Clark);#%nameshasoneelement
2013
Slide 37 of 69
Ahashexample
Countingoccurrenceofwordsinagivenarray
#Assume@words_in_LOTRhasbeendefinedandcontainsallthewords,
#onepercell,fromthetrilogy"LordofTheRings"(thereareover12,000
#pages,sothat'salotofwords!)
#predeclarethehash
my%count;
#calculatethewordcount
foreach$word(@words_in_LOTR){
$count{lc($word)}++;#lc()returnsthewordinlowercase
}
#nowprintouttheresults
foreach$word(keys%count){
print$wordwasseen$count{$word}times\n;
}
print"Thereare".scalar(keys%count)."uniquewords,fromatotalof"
.scalar@words_in_LOTR."wordsinthetrilogy\n";
2013
7linesofcodetocountthewords
Slide 38 of 69
BasicI/O:STDIN
InputfromSTDIN
#readnextinputline
$line=<STDIN>;
#readrestofinputuntilEOF(CTRL+D)
@lines=<STDIN>;#returnsundefonEOF
Useof$_
$_=<STDIN>;#readnextinputline
$_isthedefaultvariableforinput
<STDIN>;#equivalentto$_=<STDIN>;
usedinloops
while($_=<STDIN>){...};
while(<STDIN>){...};#uses$_
2013
Slide 39 of 69
OutputtoSTDOUT
print()isalistoperator
printhelloworld\n;
print(helloworld\n);
returns0or1
$a=print(hello,world,\n);
mayneedtoaddbrackets(parentheses,orroundbraces)
print(2+3),hello\n;#wrong!prints5,ignoreshello\n
print((2+3),hello\n);#right,prints5hello
print2+3,hello\n;#right,prints5hello
printf()forformattedoutput
sameasCfunction
firstargumentformatcontrol
subsequentargumentsdatatobeprinted
printf%15s%5d%10.2f\n,$s,$n,$r;
2013
parenthesesoptional
Slide 40 of 69
GeneralInput/Output
Useoffilehandles
#samplescripttocopyafile
open(SOURCE,data.in)||dieCannotopendata.inforreading\n;
open(OUTPUT,>/tmp/data.out)||dieCannotopenoutputfile
/tmp/data.out\n;
while($line=<SOURCE>){
#processinsomeway
printOUTPUT$line;
}
close(SOURCE);
close(OUTPUT);
Filehandlesareuppercasebyconvention,notrequired
Youmayalsosee:
open(my$foo,data.in);
2013
Slide 41 of 69
MoreI/O
Optionsforopen()
#equivalenttofileforreading
open(FH1,<file);
#canusevariableinterpolationoverwritestheexistingfile
open(FH2,>$filename);
#appendmodeoutput
open(FH3,>>file);
#Openforreadingandwritingpointeratthestartofthefile
open(LOCAL_LOG,+<$filename);
#openforreadingandwritingclearthefilepriortouse
open(LOCAL_LOG2,+>file);
#inputfromapipe
open(PIPE,ps|);
#outputtoapipethesortdoesnotoccuruntilOUTisclosed
open(SORTED,|sort>>/tmp/file);
#ensurethefileisencodedcorrectly
open(UTF8_DATA,"<:encoding(utf8)",$filename);
2013
Slide 42 of 69
Diamondoperator
readslinesfromfilesspecifiedoncommandline
$line=<>;
uses@ARGV(Note:usesshift,so@ARGVemptiesinuse)
while($line=<>){...};#anunderstandableform
while(<>){...};#amorecommonform(uses$_)
readsfromSTDINif@ARGVempty
while($line=<>){print$line;};#catcommand
while(<>){print;};#ditto
canassignto@ARGV
@ARGV=("The_Hobbits","The_Fellowship_of_the_Ring",
"The_Two_Towers","The\Return\of\the\King");
while(<>){#processthefiles
push@lines_in_LOTR,$_;
}
2013
(willsay"Can'topenThe_Hobbits:Nosuchfileordirectoryat...")
4linestoreadthreefilesonthedisk
Slide 43 of 69
Regularexpressions
Patterntomatchagainstastring
Usedingrep,sed,awk,ed,vi,emacs
Perlusesasuperset
Seethereferenceguideforamorecompletelistofoptions
2013
Slide 44 of 69
Matchoperator
$string=~m/pattern/
matchesthefirstoccurrenceofpattern
returnstrueorfalse
tofind"LandRover"invariousfiles:
#dogrep'LandRover'file1file2file3
while($line=<>){
if($line=~m/LandRover/){
print$line;
}
}
2013
Slide 45 of 69
Matchoperator(contd)
canusedefaults
use$_
m/LandRover/;
omitm
/LandRover/;
thus:
while(<>){#usingdefaults
printif/LandRover/;
}
2013
Slide 46 of 69
Substituteoperator
$string=~s/pattern/string2/
toreplace"ipod"with"mp3player"
while($line=<>){
$line=~s/ipod/mp3player/;
print$line;
}
substitutesonlythefirstoccurrenceofpattern
returnstrueorfalse
replacessubstringsinagivenstring
Canusedefaults
use$_
while(<>){s/ipod/mp3player/;print;}
2013
Becareful:"tripod"becomes"trmp3player"
Slide 47 of 69
Patterns
Singlecharacterpatterns
thecharacteritself
/a/
adefinedstringofcharacters
/landrover/
/./
anycharacter(exceptnewline)
Alternations(asopposedtoalterations)
/Fred|Barney|Nate|Weirdly/#FredorBarneyorNateorWeirdly
#NateSlateisFred'sbossatthequarry
#WeirdlyGruesomeisFred'sneighbour
2013
Slide 48 of 69
Patterns(contd)
characterclasses
/[abcde]/;#matchasingleletter
/[aeiouAEIOU]/;#matchasinglevowel
/[09]/;#useofrange
/[09\]/;#matchdigitorminus
/[azAZ09_]/;#matchletter,digitor
#underscore
inversecharacterclasses
/[^aeiouAEIOU]/;#matchasinglenonvowel
/[^09]/;#matchasinglenondigit
/[^^]/;#matchanysinglecharacter
#exceptcaret
Predefinedcharacterclasses(abbreviationsfor)
\s#whitespace[\t\r\n\f]
\d#digits[09]
\w#words[azAZ09_]
\W#nonwords[^azAZ09_]
\D#nondigits[^09]
\S#nonspace[^\r\t\n\f]
2013
Slide 49 of 69
Anchoringpatterns
^forbeginningofstring
/^fred/;#matchesfredatstartofstring
/^fr^ed/;#matchesfr^edatstartofstring
$forendofstring
/fred$/;#matchesfredatendofstring
/$fred$/;#matchesthecontentsofthe
#variable$fredatendofstring
/fr\$ed$/;#matchesfr$edatendofstring
2013
Slide 50 of 69
Anchoringpatterns(contd)
\band\Bforwordboundaries
/ipod\b/;#matchestripodandipodbut
#notipodate
/\bipod/;#matchesipodandipodatebut
#nottripod
/\bipod\B/;#matchesipodatebutnotipod
#ortripod
Note:Aboundaryisdefinedasthebitbetweena\wcharacterandanon\w
character.
As\wexcludesalmostallpunctuation,thereareboundariesinthemiddleof
stringslike"isn't","[email protected]","M.I.T",and"key/value"
NoteII:ipodateacompoundC12H13I3N2O2thatisadministeredintheformofits
sodiumorcalciumsaltforuseasaradiopaquemediumincholecystography
andcholangiography
2013
Slide 51 of 69
Groupingpatterns
Multipliers
*zeroormoreofprecedingcharacter(class)
/^\s*\w/;#matchesanynumberofspacesatthestartofastring
#(copeswithtabindents,blanklines,etc)
+oneormoreofprecedingcharacter(class)
/\b\w+\b/;#aword,ofanylength:from'a'to'zenzizenzizenzic'
?zerooroneofprecedingcharacter(class)
/\boff?\b/;#matchesthewords'of'or'off'
Examplethepirate'scry
/aa?r*g+h*!?/
#aarrgghhhhh!,arrggggg!,arrrgggg
#a,ah,arh
#Notethat"blag"isvalid,asthereisnoboundarydefinition
#atthestartofregexp
2013
Slide 52 of 69
Matchingvariables
Readonlyvariables
$&stringthatmatchestheregularexpression
$partofstringbeforematch
$partofstringaftermatch
$_=thisisasamplestring;
/sa.*le/;#matchsample
#$isthisisa
#$&issample
#$isstring
Avoidusingthem!
Whilstthesearefineforsimplecode&debuggingoncetheyappearincodeonce,they
areappliedtoeverymatch,whichslowstheperformanceoftheprogram.
Ifyouwanttoget$&,youwouldbebettercapturingitismoreefficientwithin
Perl.
2013
Slide 53 of 69
Capturing&Clustering&Cloistering
Whatdowedotocapturethematchedinformationinaregexp?
$foo=1:IanStuart8097;
$foo=~/\b((\w+)\s+(\w+))\b/;
#$1=IanStuart;$2=Ian;$3=Stuart
Sometimesoneneedstoclusteragrouptogether:
/^cat|cow|dog$/
needstobe
/^(?:cat|cow|dog)$/
sothatthecatdoesn'trunawaywiththe^
Sometimesoneneedstoapplymodifierstojustasmallbitoftheregexp(makinga
cloister):
/Harry(?ix:\s*[AZ]\.?\s+)?Truman/
#matchesHarryTruman,HarryS.Truman,HarrySTruman,
#Harrys.Truman,HarrysTrumanandevenHarrys
Truman
(Seeslide57&58for'i'&'x')
2013
Slide 54 of 69
Matchingoperatorrevisited
$string=~m/pattern/
Operatesonanystring
$a=helloworld;
$a=~/^he/;#true
worksonanyscalar
if(<STDIN>=~/^[yY]/){...};#inputstartingwithY
Usingdifferentdelimiterm/.../
useanynonalphanumericcharacter
m/^\/usr\/etc/;#usingslash
m#^/usr/etc#;#using#foradelimeter
2013
Slide 55 of 69
SubstitutionOperatorrevisited
$string=~s/pattern/string2/
worksonanyscalarvalue
#Change$whichtothisisaquiz
$which=thisisaquiz;
$which=~s/test/quiz/;
#Changeanarrayelement
$someplace[$here]='turnleftattheroundabout';
$someplace[$here]=~s/left/right/;
#prependxtohashelement
$d{'t'}=marksthespot;
$d{t}=~s/^/x/;
Usingdifferentdelimiter
$_=~s#fred#barney#;
$filename=~s#\#/#g;
Globalreplacements/pattern/string/g
$_=footfoolbuffoon;
s/foo/bar/g;#$_becomesbartbarlbufbarn
2013
Slide 56 of 69
Generalmodifiers
Ignoringcase
$which=~s/TEST/quiz/i;
Howtoreacttonewlines(\n)
#smodifiermeans"."canmatchthenewline
/Perl.Programming/s#'IusePerl
#Programmingisfun'
#mmodifiermeans"^"&"$"matchesthestart
#andendoflines,notwholestrings
s/^$editor/$author/m#'Theeditorsnameis
#Ian,and...'
Fastermatching
#Compilethepatternmatchonce.
#Couldbeaproblemas$speakerwillnotbe
#changed
$person=~/$speaker/o
2013
Slide 57 of 69
Generalmodifiers(continued)
Allowwhitespaceandcommentswith/x
AverycomplexexampleFindduplicatewords:
my$input_record_separator=$/;
$/=""#definesa"newline"asbeingablankcharacter,not"\n"
while(<>){
while(
m!#using'!'asthedelimiter
\b#startatawordboundary
(\w\S+)#captureawordlikechunk(2ormorecharacters)
(
\s+#separatedbysomespace
\1#fromthethinginthebracesabove
)+#repeatedly
\b#untilanotherwordboundary
!xig#'x'allowsspacesandcommentsintheregexp(asdonehere)
#'i'tomatch"Is"with"is"
#'g'tolookformultiplematchesinoneparagraph
){
print"Duplicateword'$1'foundinparagraph$.\n"#the$1matchesthethingin
#thefirstsetofbracesabove
}
}
$/=$input_record_separator;#resetrecordseparator
2013
Slide 58 of 69
split()operator
split(/pattern/,string)
splitsstringoneachoccurrenceofaregularexpression
returnslistofvalues
$line=merlyn::118:10:Randal:/home/merlyn:/usr/bin/perl;
@fields=split(/:/,$line);
#split$lineonfielddelimeters
#now@fieldsbecomes(merlyn,,118,10,
#Randal/home/merlyn,/usr/bin/perl)
$_isthedefaultstring
@words=split(//);#sameassplit(//,$_)
whitespaceisthedefaultpattern
@words=split;#sameassplit(/\s+/,$_)
2013
Slide 59 of 69
join()operator
join(string,list)
gluestogetheralistofvalueswithagivenstring
@fields=(merlyn,,118,10,Randal,/home/merlyn,
/usr/bin/perl)
$newline=join(:,@fields);
#$newlinenowequalsmerlyn::118:10:Randal:/home/merlyn:/usr/bin/perl
note:joinusesstring,notregularexpression
$newline=join(:,@fields);
2013
Slide 60 of 69
Switchstatement
FromPerl5.10,the'switch'featurewasintroduced
Oldschool:
for($var){#using$_
/^abc/&&do{&some_stuff;last};
/^def/&&do{&other_stuff;last};
/^xyz/&&do{&alternative;last};
{¬hing;};
}
Preferredform
givenisexperimental
usev5.10.1;
for($var){
when(/^abc/){&some_stuff;}
when(/^def/){&other_stuff;}
when(/^xyz/){&alternative;}
default{¬hing;}
}
usev5.10.1;
given($var){
when(/^abc/){&some_stuff;}
when(/^def/){&other_stuff;}
when(/^xyz/){&alternative;}
default{¬hing;}
}
Perl5.10.0hasaslightlybrokenswitchimplementation
Perl5.10to5.14haveahinkyimplementationofgiven
2013
Slide 61 of 69
Afinalexample
AnalyzethewordsinthetrilogyLordoftheRings
@ARGV=("TheFellowshipoftheRing","TheTwoTowers","TheReturnoftheKing");
my%count;
my$sum;
while(<>){#eachlineisin$_
chomp;
@words_in_line=split;#defaultstosplittingonwhitespace
#Dotheanalysis
foreach$word(@words_in_line){
$count{lc($word)}++;#lc()returnsthewordinlowercase
$sum++;
}
}
#displaytheresults
print"Thereare".
scalar(keys%count).
"uniquewords,fromatotalof$sumwordsinthetrilogy\n";
foreach$word(keys%count){
print$wordwasseen$count{$word}times\n;
}
#14linestobrieflyanalyzethewordsintheLordoftheRingstrilogy!
2013
Slide 62 of 69
Summary
2013
Associativearrayshashes
naming
initialising
copying
keys(),values(),each(),delete()operators
BasicI/O
inputfromSTDIN
diamondoperatorand@ARGV
outputtoSTDOUT
useoffilehandles
optionstoopen()
Slide 63 of 69
Summary(contd)
2013
Matchandsubsituteoperations
thereadonlyvariables
Regularexpressions
characterclasses
anchoringpatterns
mutipliers
split()andjoin()operators
theswitchstatement
Slide 64 of 69
Diagnostics
Thewswitchgivesusefuldiagnostics
perlwfilename
#!/usr/local/bin/perlw
2013
warnsofsuchthingsas:
useofvariableswithoutpredeclaration
redeclaringlocalisedvariables
silentchangeofcontext
variablesdeclaredorsetbutneverused
Slide 65 of 69
thedebugger
perldfilename
#!/usr/local/bin/perld
Debuggercommands
llist
ssinglestep(goesintosubroutines)
nnextstatement(silentlydoesthesubroutine)
bnnnsetbreakpointatlinennn
cnnncontinuetolinennn
p..printscalarvalue
x...printsthecontentsofthething(array,hash,etc...)
anyperlstatement
qquit
2013
Slide 66 of 69
InformationResources
Electronic
manpages
manperlstyle
perldoccommand
perldocNet::Ping
perldocperlfaq
Websites
http://www.perl.org#ThemainPerlwebsite
http://www.perl.com#AusesofPerlblog
http://www.perlfoundation.org/#forthepoliticallymotivated
http://perlmonks.org/#helpwithperl
http://www.pm.org/#Alooseassociationofinternational
#PerlUserGroups
http://edinburgh.pm.org/#PerlMongersatEdinburgh
http://search.cpan.org/#CPAN(ComprehensivePerlArchiveNetwork),
#formodules
2013
Slide 67 of 69
InformationResources(contd)
2013
Newsgroups
comp.lang.perl.*(announce,misc,modules,moderated&tk)
Literature
OReillybooks(http://www.oreilly.com/pub/topic/perl)
ManningPublications(http://www.manning.com/catalog/perl/)
Slide 68 of 69
PracticalExercises
AllquestionsarefromtheLearningPerlbook
Chapters5,6&7
ThereisnoCorrectAnswerifitworks,itsright2
Perlmotto:"TIMTOWTDI"(timtowtiddy:"ThereIsMoreThanOneWayToDoIt")
Somesolutionsarefasterorcleanerthanothers
ForWindowsusers:
clickontheappsmenu,andruncmd
Inthewindowthatopens,enter
cdm:/users/trxxyyy/LocalDocuments/Desktop
Usectrlzratherthanctrldforendoffile
ForUnixusers:
Toemailallfilesthatstartmytoyourself,run:
forfinmy*;domailad@dress<$f;done;
2Someanswersaremoreaestheticallypleasingthanothers
2013
Slide 69 of 69