ebook img

Object Oriented Perl PDF

122 Pages·2007·0.839 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Object Oriented Perl

Object Oriented Perl #!/usr/bin/perl # Copyright (c) Marcus Post, <[email protected]> # # # # $_=q,my(@f|@c|x$_=q.my(@f|@c|x$_=q.my(@f|@c|x$_=q.m(@f||@c|x$_=q.m(@f||@c|xx @w);@a=@f=<DAT%@w);@a=@f=<DAT%@w);@a=@f=<DAT%@w;@a=@f=<DAAT%@w;@a=@f=<DAAT%% A>;seek(DATA|0!A>;seek(DAA|0!!A>;seek(DAA|0!A>;seek(DAA|0!!A>;seek(DAA|0!!AA |0);@c=<DATA>;Y|0);@c<DATA>;Y||0);@c<DATA>Y||0);@c<DATA>Y|||0);@c<DATA>Y|||| until(($_=pop(zutil(($_==pp(zuttil(($_==p(zuttil(($_==p(zutttil(($_==p(zuttt @c))=~/^_/){};Qc))=~/^_/){};Qc)))=~/^_/{};Qc)))=~/^_/{};Qc))))=~/^_/{};Qc))) unshift(@a|$_)xnshift(@a|$_)xnshhift(a|$_)xnshhift(a|$_)xnshhiift(a|$_)xnshh ;for(1..3){pri%;for(1.3){pri%;ffor1.3){pri%;ffor1.3){pri%;ffor11.3){pri%;fff nt(shift(@c));!nt(shft(@c));!ntt(hft(@c));!ntt(hft(@c));!ntt(hftt(@c));!nttt }for(@f){my($sY}for@f){my($sY}for@f){my($sY}for@f){my($sY}for@f){mmy($sY}foo );split//;$_=sz);splt//;$_=sz);splt//;$_=sz);splt//;$_=sz);splt//;$_==sz);ss hift(@c);$_=~sQhift(c);$_=~sQhift(c);$_=~sQhift(c);$_=~sQhift(c);$_=~sQQhiff /(.{15}).*/\1/x/(.{15})*/\1/x/(.{15})*/\1/x/(.{15})*/\1/x/(.{15}})*\1/xx/(.. ;@w=split//;fo%;@w=split/;fo%;@w=split/;fo%;@w=split/;fo%;@w=spllit;fo%%;@ww r(@_){$w[$s+15!r(@_){$w[$s15!r(@_){$w[$s15!r(@_){$w[$s15!!(@_){$$w[s15!!!(@@ −$_]=(($w[$s]eY−$_]=(($w[$s]YY−_]=(($w[$s]YY−_]=(($w[$s]YY−_]=((($[$s]]YY−__ q"|")?".":$w[$zq"|")?".":$w[$zq|")?"."::$[$zq|")??."::$[[$z|")???.::$$[[$z|| s]);$s++;}for(Qs]);$s++;}for(Qs];$s++;}}or(Qs];$$s+;}}orr(Qs]$$s++;}}orr(Qss 1..75){unless(x1..75){unless(x1.75){unnlss(x1.775){uulsss(x1.75){uuulsss(x11 $w[$_]ne’’){$w%$w[$_]ne’’){$w%$w$_]nee’’{$w%$$w$_]nn’’{{$w%$$w_]nnn’’{{$w%$$ [$_]=$w[($_−1)![$_]=$w[($_−1)![$_=$w[[($_−)![$_==w[[($$_−)![$__w[[[($$_−)![[ ];}}print(joinY];}}print(joinY];}prinnt(joinY;}prinntt(joinY;}pinnntt(joinYY ""|@w);print"\z""|@w);print"\z""|w);;print"\z"|w);;pprint"\z"|w;;ppprint"\zz n";}print@a;,;#n";}print@a;.;#n";priint@a;.;#n;priintt@a;.;#n;piinntt@a;.;## y!|zY\!%x!,Q!;#y!|zY\!%x!.Q!;#y!zY\!!%x!.Q!;#!zY\!!%x!!.Q!;#!z\!!!%x!!.Q!;## s{Q.*\n}[]g;#<>s{Q.*\n}[]g;#<>sQ.*\nn}[]g;#>sQ.*\nn}[]]g;#>sQ.\nnn}[]]g;#>ss eval;#EndFini!$eval;#EndFini!$eal;#EEndFin!$eal;;##nddFin!$ea;;###nddFin!$ee Paul Fenwick Jacinta Richardson ObjectOrientedPerl byPaulFenwickandJacintaRichardson Copyright©2001-2007PaulFenwick([email protected]) Copyright©2001-2007JacintaRichardson([email protected]) Copyright©2001-2007PerlTrainingAustralia(http://perltraining.com.au) Copyright©2001ObsidianConsultingGroup CoverartworkCopyright(c)2001MarcusPost.Usedwithpermission. TheuseofacamelimagewiththetopicofPerlisatrademarkofO’Reilly&Associates,Inc.Usedwithpermission. ConventionsusedthroughoutthistextarebasedupontheconventionsusedintheNetizentrainingmanualsbyKirrilyRobert,andfoundat http://sourceforge.net/projects/spork Distributionofthisworkisprohibitedunlesspriorpermissionisobtainedfromthecopyrightholder. ThistrainingmanualismaintainedbyPerlTrainingAustralia,andcanbefoundathttp://www.perltraining.com.au/notes.html. Thisisrevision1.11ofPerlTrainingAustralia’s"ObjectOrientedPerl"trainingmanual. Table of Contents 1.Introduction.....................................................................................................................................1 Courseoutline.............................................................................................................................1 Assumedknowledge...................................................................................................................1 Moduleobjectives.......................................................................................................................1 Platformandversiondetails........................................................................................................2 Thecoursenotes..........................................................................................................................2 Othermaterials............................................................................................................................3 2.Anobjectorientedrefresher..........................................................................................................5 Inthischapter..............................................................................................................................5 Objectorientationinbrief...........................................................................................................5 Objectsandmethods...................................................................................................................5 Classes.........................................................................................................................................6 Inheritance...................................................................................................................................7 Multipleinheritance...........................................................................................................7 Polymorphism.............................................................................................................................8 Exercise.......................................................................................................................................8 TenrulesforwhentouseOO.....................................................................................................9 Chaptersummary......................................................................................................................10 3.ExternalFilesandPackages........................................................................................................13 Inthischapter............................................................................................................................13 Splittingcodebetweenfiles......................................................................................................13 Require............................................................................................................................13 Usestrictandwarnings...................................................................................................14 Example...........................................................................................................................14 Exercises...................................................................................................................................15 Introductiontopackages...........................................................................................................15 Thescopingoperator.................................................................................................................16 Packagevariablesandour.........................................................................................................17 Exercises...................................................................................................................................17 Chaptersummary......................................................................................................................18 4.Modules..........................................................................................................................................19 Inthischapter............................................................................................................................19 Moduleuses..............................................................................................................................19 Whatisamodule?.....................................................................................................................19 Thedouble-colon......................................................................................................................20 Exercise.....................................................................................................................................20 WheredoesPerllookformodules?..........................................................................................20 Findinginstalledmodules.........................................................................................................21 Exercise...........................................................................................................................21 UsingCPANmodules......................................................................................................21 Writingmodules........................................................................................................................22 Useversusrequire...........................................................................................................23 Warningsandstrict..........................................................................................................23 Exercise...........................................................................................................................23 Thingstoremember...............................................................................................24 Exporter.....................................................................................................................................24 Chaptersummary......................................................................................................................24 PerlTrainingAustralia(http://perltraining.com.au/) iii 5.OurfirstPerlObject.....................................................................................................................27 Inthischapter............................................................................................................................27 Classesarejustpackages..........................................................................................................27 Methodsarejustsubroutines.....................................................................................................27 Blessingareferentcreatesanobject.........................................................................................28 Constructorfunctions................................................................................................................29 PlayingCardinfull....................................................................................................................30 Exercises...................................................................................................................................30 Chaptersummary......................................................................................................................31 6.ArgumentPassing.........................................................................................................................33 Inthischapter............................................................................................................................33 Namedparameterpassing.........................................................................................................33 Defaultarguments.....................................................................................................................34 Namedparametersandobjectconstructors..............................................................................34 Exercises...................................................................................................................................35 Chaptersummary......................................................................................................................35 7.PracticalExercise-PlayingCards..............................................................................................37 GroupExercises-PlanningtheClass.......................................................................................37 IndividualExercise-WritingtheClass....................................................................................37 PracticalUsage-TheCardGame"War"..................................................................................37 8.Classmethodsandvariables........................................................................................................39 Inthischapter............................................................................................................................39 Whatisaclassmethod?............................................................................................................39 Anexampleclassmethod..........................................................................................................39 Classvariables...........................................................................................................................40 Packagevariablesandclassvariables..............................................................................41 Exercises...................................................................................................................................41 Chaptersummary......................................................................................................................41 9.Destructors....................................................................................................................................43 Inthischapter............................................................................................................................43 Perl’sgarbagecollectionsystem...............................................................................................43 Destructorfunctions..................................................................................................................43 Exercises..........................................................................................................................45 Otherusesfordestructors.........................................................................................................46 Groupexercises...............................................................................................................46 Weakreferences........................................................................................................................46 Chaptersummary......................................................................................................................47 10.Inheritance...................................................................................................................................49 Inthischapter............................................................................................................................49 SowhatisinheritanceinPerl?..................................................................................................49 Methoddispatch..............................................................................................................49 Directeddispatch...................................................................................................50 Dispatchviasubroutinereference..........................................................................51 Exercises..........................................................................................................................51 Constructorsandinheritance...........................................................................................52 Universalmethods.....................................................................................................................53 Theisa()method..............................................................................................................53 Thecan()method.............................................................................................................54 Problemswithinitialisers..........................................................................................................54 Initialisersanddiamondinheritance................................................................................55 iv PerlTrainingAustralia(http://perltraining.com.au/) Changingparents.............................................................................................................56 ThePerlTrainerclassinfull......................................................................................................58 Exercises...................................................................................................................................58 Chaptersummary......................................................................................................................59 11.Redispatchingmethodcalls.......................................................................................................61 Inthischapter............................................................................................................................61 Passitonplease........................................................................................................................61 Exercises...................................................................................................................................62 Optionalredispatch...................................................................................................................62 Mandatoryredispatch................................................................................................................63 Exercises...................................................................................................................................64 ProblemswithNEXT................................................................................................................64 UsingEVERYtocallallmethods.............................................................................................65 UsingEVERYandEVERY::LASTinpractice.........................................................................66 Constructors.....................................................................................................................66 Destructors.......................................................................................................................67 Exercises..........................................................................................................................67 Chaptersummary......................................................................................................................67 12.Inside-outobjects........................................................................................................................69 Inthischapter............................................................................................................................69 Problemswithblessedhashes...................................................................................................69 Whatisaninside-outobject?....................................................................................................69 Errorchecking.................................................................................................................70 Strongencapsulation.......................................................................................................70 Attributeaccess................................................................................................................70 Inside-outplayingcards..................................................................................................71 \do{my$anon_scalar}...........................................................................................71 Exercise.....................................................................................................................................72 Aproblemwithinside-outobjects............................................................................................72 Inheritanceandattributes..........................................................................................................73 Helpermodules.........................................................................................................................73 ChapterSummary.....................................................................................................................73 13.BuildingclasseswithClass::Std................................................................................................75 Inthischapter............................................................................................................................75 UsingClass::Std........................................................................................................................75 Objectcreation..........................................................................................................................75 DefiningAttributes....................................................................................................................75 Objectconstruction...................................................................................................................76 Automaticaccessors..................................................................................................................76 Readaccessors.................................................................................................................77 Writeaccessors................................................................................................................77 Automaticinitialisation.............................................................................................................77 :name...............................................................................................................................78 Defaultvalues..................................................................................................................78 Summaryof:ATTRoptions......................................................................................................78 Exercise.....................................................................................................................................79 Objectdestruction.....................................................................................................................79 Debuggingfeatures...................................................................................................................80 Methodtraits(overloads)..........................................................................................................80 IssueswithClass::Std................................................................................................................81 PerlTrainingAustralia(http://perltraining.com.au/) v ChapterSummary.....................................................................................................................82 14.Abstractclasses...........................................................................................................................83 Inthischapter............................................................................................................................83 Abstracting................................................................................................................................83 GroupExercise..........................................................................................................................86 Chaptersummary......................................................................................................................86 15.Polymorphism.............................................................................................................................87 Inthischapter............................................................................................................................87 Usingpolymorphism.................................................................................................................87 Inheritancevsinterfacepolymorphism.....................................................................................87 AddingdefaultmethodsandtheUNIVERSALclass...............................................................88 Moreoninheritancepolymorphism..........................................................................................88 Exercises...................................................................................................................................89 Chaptersummary......................................................................................................................89 16.PracticalExercise-theGameofChess....................................................................................91 Requiredreading.......................................................................................................................91 GroupQuestions.......................................................................................................................91 IndividualExercises..................................................................................................................91 GroupDiscussion......................................................................................................................92 17.Operatoroverloading.................................................................................................................93 Inthischapter............................................................................................................................93 Whatisoperatoroverloading?..................................................................................................93 Overloadingstringification........................................................................................................93 Inheritanceandoverloading............................................................................................94 Exercises...................................................................................................................................96 Overloadingcomparisonoperators...........................................................................................96 Magicauto-generation..............................................................................................................97 Exercise...........................................................................................................................97 Overloadingusingattributes.....................................................................................................97 Exercises...................................................................................................................................98 Chaptersummary......................................................................................................................98 18.Exceptions....................................................................................................................................99 Inthischapter............................................................................................................................99 Whatisanexception?...............................................................................................................99 ThrowingexceptionsinPerl.....................................................................................................99 CatchingexceptionsinPerl.......................................................................................................99 HavingPerlthrowmoreexceptions........................................................................................100 Real-worldexamplesofexceptions........................................................................................101 TheErrormodule....................................................................................................................103 LoadingtheErrormodule.............................................................................................104 SyntaxprovidedbytheErrormodule............................................................................104 tryBLOCKCLAUSES........................................................................................105 catchCLASSwithBLOCK.................................................................................105 exceptBLOCK.....................................................................................................105 otherwiseBLOCK...............................................................................................105 finallyBLOCK.....................................................................................................105 Errorobjects..................................................................................................................105 ConstructinganErrorobject................................................................................105 Errorsyntax..........................................................................................................106 Chaptersummary....................................................................................................................106 vi PerlTrainingAustralia(http://perltraining.com.au/) 19.Conclusion.................................................................................................................................109 Whatyou’velearnt..................................................................................................................109 Wheretonow?........................................................................................................................109 Furtherreading........................................................................................................................109 Books.............................................................................................................................110 Online............................................................................................................................110 Colophon..........................................................................................................................................111 PerlTrainingAustralia(http://perltraining.com.au/) vii viii PerlTrainingAustralia(http://perltraining.com.au/) List of Tables 13-1.Summaryof:ATTRoptions......................................................................................................78 13-2.Typeoverloadingflags...............................................................................................................80 PerlTrainingAustralia(http://perltraining.com.au/) ix x PerlTrainingAustralia(http://perltraining.com.au/)

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.