TEAM LinG Praise forHigher-Order Perl . . . Asaprogrammer,yourbookshelfisprobablyoverflowingwithbooksthatdidnothingtochangethewayyou program...orthinkaboutprogramming. You’regoingtoneedacompletelydifferentshelfforthisbook. WhilediscussingcachingtechniquesinChapter3,MarkJasonDominuspointsouthowalargeenough increaseinpowercanchangethefundamentalwayyouthinkaboutatechnology.Andthat’spreciselywhat thisentirebookdoesforPerl. ItraidsthedeepestvaultsandhighesttowersofComputerScience,andtransformsthemanyarcanetreasures itfinds—recursion,iterators,filters,memoization,partitioning,numericalmethods,higher-orderfunctions, currying,cutsorting,grammar-basedparsing,lazyevaluation,andconstraintprogramming—intopowerful andpracticaltoolsforreal-worldprogrammingtasks:filesysteminteractions,HTMLprocessing,database access,webspidering,typesetting,mailprocessing,homefinance,textoutlining,anddiagramgeneration. Alongthewayitalsoscatterssmaller(butequallyinvaluable)gems,liketheelegantexplanationofthe differencebetween“scope”and“duration”inChapter3,orthecarefulexplorationofhowbesttoreturn errorflagsinChapter4.ItevenhaspracticaltipsforPerlevangelists. Dominuspresentseventhemostcomplexideasinsimple,comprehensibleways,butnevercompromiseson theprecisionandattentiontodetailforwhichheissowidelyandjustlyadmired. Hiswritingis—asalways—lucid,eloquent,witty,andcompelling. Aptlynamed,thistruly/is/aPerlbookofahigherorder,andessentialreadingforeveryseriousPerl programmer. —DamianConway,Co-designerofPerl6 TEAM LinG TEAM LinG - TEAM LinG TEAM LinG - Mark Jason Dominus AMSTERDAM(cid:127)BOSTON(cid:127)HEIDELBERG(cid:127)LONDON NEW YORK(cid:127)OXFORD(cid:127)PARIS(cid:127)SANDIEGO SANFRANCISCO(cid:127)SINGAPORE(cid:127)SYDNEY(cid:127)TOKYO MorganKaufmannPublishersisanimprintofElsevier TEAM LinG SeniorEditor TimCox PublishingServicesManager SimonCrump AssistantEditor RichardCamp CoverDesign YvoRiezebosDesign CoverIllustration YvoRiezebosDesign Composition CephaImagingPvt.Ltd. TechnicalIllustration DartmouthPublishing,Inc. Copyeditor EileenKramer Proofreader DeborahPrato InteriorPrinter TheMaple-VailBookManufacturingGroup CoverPrinter PhoenixColor MorganKaufmannPublishersisanimprintofElsevier. 500SansomeStreet,Suite400,SanFrancisco,CA94111 Thisbookisprintedonacid-freepaper. ©2005byElsevierInc.Allrightsreserved. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarksor registeredtrademarks.InallinstancesinwhichMorganKaufmannPublishersisawareofaclaim, theproductnamesappearininitialcapitalorallcapitalletters.Readers,however,shouldcontact theappropriatecompaniesformorecompleteinformationregardingtrademarksandregistration. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinanyform orbyanymeans—electronic,mechanical,photocopying,scanning,orotherwise—withoutpriorwritten permissionofthepublisher. PermissionsmaybesoughtdirectlyfromElsevier’sScience&TechnologyRightsDepartmentinOxford, UK:phone:(+44)1865843830,fax:(+44)1865853333,e-mail:[email protected] alsocompleteyourrequeston-lineviatheElsevierhomepage(http://elsevier.com)byselecting “CustomerSupport”andthen“ObtainingPermissions.” LibraryofCongressCataloging-in-PublicationData Applicationsubmitted ISBN:1-55860-701-3 ForinformationonallMorganKaufmannpublications, visitourWebsiteatwww.mkp.comorwww.books.elsevier.com PrintedintheUnitedStatesofAmerica 05 06 07 08 09 5 4 3 2 1 TEAM LinG For Lorrie TEAM LinG TEAM LinG Preface........................................................... xv RecursionandCallbacks........................... 1 1.1 ...................................... 1 1.2 ...................................................... 3 1.2.1 WhyPrivateVariablesAreImportant.............................. 5 1.3 .............................................. 6 1.4 ............................................... 12 1.5 ...................... 16 1.6 -....................... 25 1.7 .......................................................... 26 1.7.1 MoreFlexibleSelection........................................ 32 1.8 ......................................... 33 1.8.1 FibonacciNumbers........................................... 33 1.8.2 Partitioning................................................ 35 DispatchTables.................................... 41 2.1 ....................................... 41 2.1.1 Table-DrivenConfiguration .................................... 43 2.1.2 AdvantagesofDispatchTables................................... 45 2.1.3 DispatchTableStrategies....................................... 49 2.1.4 DefaultActions............................................. 52 2.2 ..................................................... 54 2.2.1 HTMLProcessingRevisited.................................... 59 CachingandMemoization......................... 63 3.1 ........................................... 65 3.2 .................................................. 66 3.2.1 StaticVariables.............................................. 67 3.3 ..................................................... 68 3.4 ................................................... 69 ix TEAM LinG