Praise For Higher-Order Perl

3y ago
4 Views
2 Downloads
1.94 MB
592 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Sabrina Baez
Transcription

Praise for Higher-Order Perl . . .As a programmer, your bookshelf is probably overflowing with books that did nothing to change the way youprogram . . . or think about programming.You’re going to need a completely different shelf for this book.While discussing caching techniques in Chapter 3, Mark Jason Dominus points out how a large enoughincrease in power can change the fundamental way you think about a technology. And that’s precisely whatthis entire book does for Perl.It raids the deepest vaults and highest towers of Computer Science, and transforms the many arcane treasuresit finds—recursion, iterators, filters, memoization, partitioning, numerical methods, higher-order functions,currying, cutsorting, grammar-based parsing, lazy evaluation, and constraint programming—into powerfuland practical tools for real-world programming tasks: file system interactions, HTML processing, databaseaccess, web spidering, typesetting, mail processing, home finance, text outlining, and diagram generation.Along the way it also scatters smaller (but equally invaluable) gems, like the elegant explanation of thedifference between “scope” and “duration” in Chapter 3, or the careful exploration of how best to returnerror flags in Chapter 4. It even has practical tips for Perl evangelists.Dominus presents even the most complex ideas in simple, comprehensible ways, but never compromises onthe precision and attention to detail for which he is so widely and justly admired.His writing is—as always—lucid, eloquent, witty, and compelling.Aptly named, this truly /is/ a Perl book of a higher order, and essential reading for every serious Perlprogrammer.—Damian Conway, Co-designer of Perl 6

-

- Mark Jason DominusAMSTERDAM BOSTON HEIDELBERG LONDONNEW YORK OXFORD PARIS SAN DIEGOSAN FRANCISCO SINGAPORE SYDNEY TOKYOMorgan Kaufmann Publishers is an imprint of Elsevier

Senior EditorPublishing Services ManagerAssistant EditorCover DesignCover IllustrationCompositionTechnical IllustrationCopyeditorProofreaderInterior PrinterCover PrinterTim CoxSimon CrumpRichard CampYvo Riezebos DesignYvo Riezebos DesignCepha Imaging Pvt. Ltd.Dartmouth Publishing, Inc.Eileen KramerDeborah PratoThe Maple-Vail Book Manufacturing GroupPhoenix ColorMorgan Kaufmann Publishers is an imprint of Elsevier.500 Sansome Street, Suite 400, San Francisco, CA 94111This book is printed on acid-free paper. 2005 by Elsevier Inc. All rights reserved.Designations used by companies to distinguish their products are often claimed as trademarks orregistered trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim,the product names appear in initial capital or all capital letters. Readers, however, should contactthe appropriate companies for more complete information regarding trademarks and registration.No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any formor by any means—electronic, mechanical, photocopying, scanning, or otherwise—without prior writtenpermission of the publisher.Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford,UK: phone: ( 44) 1865 843830, fax: ( 44) 1865 853333, e-mail: permissions@elsevier.com.uk. You mayalso complete your request on-line via the Elsevier homepage (http://elsevier.com) by selecting“Customer Support” and then “Obtaining Permissions.”Library of Congress Cataloging-in-Publication DataApplication submittedISBN: 1-55860-701-3For information on all Morgan Kaufmann publications,visit our Web site at www.mkp.com or www.books.elsevier.comPrinted in the United States of America05 06 07 08 095 4 3 21

For Lorrie

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvRecursion and Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . .11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.1 Why Private Variables Are Important . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.5 . . . . . . . . . . . . . . . . . . . . . .1.6 - . . . . . . . . . . . . . . . . . . . . . . .1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.7.1 More Flexible Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.8.1 Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.8.2 Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13561216252632333335Dispatch Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.1 Table-Driven Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.2 Advantages of Dispatch Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.3 Dispatch Table Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.1.4 Default Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.2.1 HTML Processing Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41434549525459Caching and Memoization . . . . . . . . . . . . . . . . . . . . . . . . .633.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.1 Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6566676869 ix

x 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1 Scope and Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.2 Lexical Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.3 Memoization Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6.1 Functions Whose Return Values Do Not Depend on TheirArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6.2 Functions with Side Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6.3 Functions That Return References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6.4 A Memoized Clock? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.6.5 Very Fast Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.7.1 More Applications of User-Supplied KeyGenerators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.7.2 Inlined Cache Manager with Argument Normalizer . . . . . . . . . . . . . . . . . . . .3.7.3 Functions with Reference Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.7.4 Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.7.5 Custom Key Generation for Impure Functions . . . . . . . . . . . . . . . . . . . . . . .3.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.8.1 Memoization of Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.12.1 Profiling and Performance Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.12.2 Automatic Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.12.3 Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89909393949699100101108109110111113Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1154.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.1.1 Filehandles Are Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.1.2 Iterators Are Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.1.3 Other Common Examples of Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.2.1 A Trivial Iterator: upto() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Syntactic Sugar for Manufacturing Iterators . . . . . . . . . . . . . . . . . . . . . . . . .4.2.2 dir walk() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.2.3 On Clever Inspirations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115115117118119121122123124 70717273767980808081828384

4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.1 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.2 Genomic Sequence Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.3 Filehandle Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.4 A Flat-File Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Improved Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.5 Searching Databases Backwards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Query Package That Transforms Iterators . . . . . . . . . . . . . . . . . . . . . . . . . .An Iterator That Reads Files Backwards . . . . . . . . . . . . . . . . . . . . . . . . . . . .Putting It Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.3.6 Random Number Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.4.1 imap() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.4.2 igrep() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.4.3 list iterator() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.4.4 append() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.1 Avoiding the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.2 Alternative undefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.3 Rewriting Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.4 Iterators That Return Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.5 Explicit Exhaustion Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.6 Four-Operation Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.5.7 Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.6.1 Using foreach to Loop Over More Than One Array . . . . . . . . . . . . . . . . . .4.6.2 An Iterator with an each-Like Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.6.3 Tied Variable Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary of tie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tied Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tied Filehandles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.7 : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.7.1 Pursuing Only Interesting Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.7.2 Referring URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.7.3 robots.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.7.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192197200From Recursion to Iterators . . . . . . . . . . . . . . . . . . . . . . . .203 5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2045.1.1 Finding All Possible Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206xi

xii 5.1.2 Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.1.3 Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.2 . . . . . . . . . . . . . . . . . . .5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.4 . . . . . . . . . . . . . . . . . . .5.4.1 Tail-Call Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Someone Else’s Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.4.2 Creating Tail Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5.4.3 Explicit Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Eliminating Recursion From fib() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209212215225229229234239242243Infinite Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2556.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.2.1 A Trivial Stream: upto() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.2.2 Utilities for Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.3.1 Memoizing Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.5.1 Generating Strings in Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.5.2 Regex Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.5.3 Cutsorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.6 - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.6.1 Approximation Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.6.2 Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.6.3 The Tortoise and the Hare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.6.4 Finance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.7.1 Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.7.2 Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6.7.3 Symbolic Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10313319320320Higher-Order Functions and Currying . . . . . . . . . . . . .3257.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.2 - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.2.1 Automatic Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.2.2 Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Prototype Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325333335337338

7.2.3 More Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.2.4 Yet More Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.3 reduce() combine() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.3.1 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Praise for Higher-Order Perl. As a programmer, your bookshelf is probably overflowing with books that did nothing to change the way you program.orthink about programming. You’re going to need a completely different shelf for this book. While discussing caching techniques in Chapter 3, Mark Jason Dominus points out how a large enough

Related Documents:

Why Perl? Perl is built around regular expressions -REs are good for string processing -Therefore Perl is a good scripting language -Perl is especially popular for CGI scripts Perl makes full use of the power of UNIX Short Perl programs can be very short -"Perl is designed to make the easy jobs easy,

Perl can be embedded into web servers to speed up processing by as much as 2000%. Perl's mod_perl allows the Apache web server to embed a Perl interpreter. Perl's DBI package makes web-database integration easy. Perl is Interpreted Perl is an interpreted language, which means that your code can be run as is, without a

Other Perl resources from O’Reilly Related titles Learning Perl Programming Perl Advanced Perl Programming Perl Best Practices Perl Testing: A Developer’s . Intermedi

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Perl's creator, Larry Wall, announced it the next day in his State of the Onion address. Most notably, he said "Perl 6 is going to be designed by the community." Everyone thought that Perl 6 would be the version after the just-released Perl v5.6. That didn't happen, but that's why "Perl" was in the name "Perl 6."

tutorial Sorry about that but I have to keep my tutorial's example scripts short and to the point Finally, this is a tutorial for Perl/Tk only I will not be teaching perl here So if you know perl, continue But if you are a beginner to perl, I would recommend that you read my perl tutorial

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18, 1987 by Larry Wall. Perl culture Perl Poems BEFOREHAND: close door, each window & exit; wait until time. open spellbook, study, read (scan, select, tell us);