• • Table of Contents Index Perl Medic: Transforming Legacy Code By Peter J Scott Publisher : Addison Wesley Pub Date : March 10, 2004 ISBN : 0-201-79526-4 Pages : 336 Slots : 1.0 Cure whatever ails your Perl code! Maintain, optimize, and scale any Perl software whether you wrote it or not Perl software engineering best practices for enterprise environments Includes case studies and code in a fun-toread format If you code in Perl, you need to read this book Adam Turoff, Technical Editor, The Perl Review Perl Medic is more than a book It is a wellcrafted strategy for approaching, updating, and furthering the cause of inherited Perl programs Allen Wyke, co-author of several computer books including JavaScript Unleashed and Pure JavaScript Scott's explanations of complex material are smooth and deceptively simple He knows his subject matter and his craft-he makes it look easy Scott remains relentless practical-even the 'Analysis' chapter is filled with code and tests to run Dan Livingston, author of several computer books including Advanced Flash 5: Actionscript in Action Bring new power, performance, and scalability to your existing Perl code! Today's Perl developers spend 60-80% of their time working with existing Perl code Now, there's a start-to-finish guide to understanding that code, maintaining it, updating it, and refactoring it for maximum performance and reliability Peter J Scott, lead author of Perl Debugged, has written the first systematic guide to Perl software engineering Through extensive examples, he shows how to bring powerful discipline, consistency, and structure to any Perl program-new or old You'll discover how to: Scale existing Perl code to serve larger network, Web, enterprise, or e-commerce applications Rewrite, restructure, and upgrade any Perl program for improved performance Bring standards and best practices to your entire library of Perl software Organize Perl code into modules and components that are easier to reuse Upgrade code written for earlier versions of Perl Write and execute better tests for your software or anyone else's Use Perl in team-based, methodologydriven environments Document your Perl code more effectively and efficiently If you've ever inherited Perl code that's hard to maintain, if you write Perl code others will read, if you want to write code that'll be easier for you to maintain, the book that comes to your rescue is Perl Medic • • Table of Contents Index Perl Medic: Transforming Legacy Code By Peter J Scott Publisher : Addison Wesley Pub Date : March 10, 2004 ISBN : 0-201-79526-4 Pages : 336 Slots : 1.0 Copyright Preface Perl or perl? Obtaining Perl Historical Perl Typographical Conventions Perl Versions Acknowledgments Who This Book Is For For Further Reference Perl 6 Chapter 1 Introduction (First Response) Section 1.1 First Things First Section 1.2 Reasons for Inheritance Section 1.3 What Next? Section 1.5 Get Personal Section 1.7 Warnings Section 1.4 Observe the Program in Its Natural Habitat Section 1.6 Strictness Chapter 2 Surveying the Scene Section 2.1 Versions Section 2.2 Part or Whole? Section 2.3 Find the Dependencies Chapter 3 Test Now, Test Forever (Diagnosis) Section 3.1 Testing Your Patience Section 3.2 Extreme Testing Section 3.3 An Example Using Test:: Modules Section 3.5 A Final Encouragement Section 3.4 Testing Legacy Code Chapter 4 Rewriting (Transplants) Section 4.1 Strategizing Section 4.2 Why Are You Doing This? Section 4.3 Style Section 4.5 Restyling Section 4.7 Editing Section 4.9 Antipatterns Section 4.4 Comments Section 4.6 Variable Renaming Section 4.8 Line Editing Section 4.10 Evolution Chapter 5 The Disciplined Perl Program Section 5.1 Package Variables vs Lexical Variables Section 5.2 Warnings and Strictness Section 5.3 use strict in Detail Section 5.5 Selective Disabling Section 5.7 Perl Poetry Section 5.4 use warnings in Detail Section 5.6 Caveat Programmer Chapter 6 Restructuring (The Operating Table) Section 6.1 Keep It Brief Section 6.2 Cargo Cult Perl Section 6.3 Escaping the Global Variable Trap Section 6.4 Debugging Strategies Chapter 7 Upgrading (Plastic Surgery) Section 7.1 Strategies Section 7.2 Perl 4 Section 7.3 Perl 5.000 Section 7.4 Perl 5.001 Section 7.5 Perl 5.002 Section 7.7 Perl 5.004 Section 7.9 Perl 5.6.0 Section 7.11 Perl 5.8.0 Section 7.13 Perl 5.8.2 Section 7.6 Perl 5.003 Section 7.8 Perl 5.005 Section 7.10 Perl 5.6.1 Section 7.12 Perl 5.8.1 Section 7.14 Perl 5.8.3 Chapter 8 Using Modules (Genetic Enhancement) Section 8.1 The Case for CPAN Section 8.2 Using CPAN Section 8.3 Improving Code with Modules Section 8.4 Custom Perls Chapter 9 Analysis (Forensic Pathology) Section 9.1 Static Analysis Section 9.2 Eliminating Superfluous Code Section 9.3 Finding Inefficient Code Section 9.4 Debugging Chapter 10 Increasing Maintainability (Prophylaxis) Section 10.1 Making It Robust Section 10.2 Advanced Brevity Section 10.3 Documentation Section 10.5 Version Control System Integration Section 10.4 Custom Warnings Chapter 11 A Case Study Section 11.1 The Setup Section 11.2 Triage Section 11.3 Desperately Seeking Sanity Section 11.5 Incorporating Modules Effectively, Part 1 Section 11.7 Making It Mature, Part 1 Section 11.9 Making It Mature, Part 3 Section 11.4 Coming into the 21st Century Section 11.6 Incorporating Modules Effectively, Part 2 Section 11.8 Making It Mature, Part 2 Section 11.10 Advanced Modification Chapter 12 Conclusion (Prognosis) Section 12.1 In Conclusion Section 12.3 A Final Thought Section 12.2 Perl People Source Code Tie::Array::Bounded Benchmark::TimeTick smallprofpp Bibliography About the Author Index Copyright Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers discounts on this book when ordered in quantity for special sales For more information, please contact: U.S Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales (317) 581-3793 international@pearsontechgroup.com Visit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Scott, Peter (Peter J.), 1961 Perl medic : transforming legacy code / Peter Scott p cm Includes bibliographical references and index ISBN 0-201-79526-4 Perl (Computer program language) I Title QA76.73.P22S395 2004 005.13'3 dc22 2004041060 Copyright © 2004 by Pearson Education, Inc All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher Printed in the United States of America Published simultaneously in Canada For information on obtaining permission for use of material from this work, please submit a written request to: Pearson Education, Inc Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 Text printed on recycled paper First printing, February 2004 Dedication To my mother, for teaching me to read at a tender age after much begging One thing led to another… [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] MacPerl, obtaining magic numbers Mail::Audit Mail::Internet Mail::Send 2nd Mail::SpamAssassin makefile Makefile.PL map Memoize module MIME::Base64 MIME::Lite Model-View-Controller pattern modularity, evolving module installation, manual module, definition Module::Build Module::CoreList 2nd Module::Info msg_ok() my() 2nd 3rd 4th 5th [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] Net::Cmd Net::LDAP Net::LDAP::Entry 2nd Net::LDAP::Filter Net::LDAP::Search newsgroups no no strict 'refs' no strict 'subs' no strict 'vars' no warnings no warnings 'redefine' [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] ok() open() operator overloading operator, diamond optimizing for speed optimizing, different reasons oraperl our() 2nd 3rd 4th overloading [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] parentheses, useless periodicals, Perl PERL Perl 4 2nd Perl 4.018 Perl 5.000 Perl 5.001 Perl 5.002 Perl 5.003 Perl 5.004 Perl 5.005 Perl 5.6.0 Perl 5.6.1 Perl 5.8.0 2nd Perl 6 Perl golf Perl Mongers Perl Object Environment Perl poetry Perl source, obtaining Perl success stories Perl, accentless Perl, maintainability Perl, obtaining Perl, old versions of Perl, version detection perlhist perlrun perlstyle pgperl Phoenix, Tom Plain Old Documentation 2nd 3rd 4th pmtools POD formatters POD_ [See Plain Old Documentation] pod_ok() pod2xml POE_ [See Perl Object Environment] poetry mode poetry, bad portals, Perl PPM 2nd pr cis pragma 2nd 3rd pseudo-hashes [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] rationale, design RCS 2nd 3rd Real World, the refactoring references 2nd references, symbolic 2nd 3rd regression testing regular expressions, debugging regular expressions, ineffective Retroperl return codes, checking return statement revision control RFC 822 rocisperl Rolsky, Dave runtests() [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] Safe.pm scalar() Scalar::Util SCCS 2nd Schwartz, Randal 2nd Schwern, Michael scope scoping, dynamic 2nd scoping, lexical search.cpan.org 2nd 3rd sentinel comments shebang line 2nd shebang line, modifying 2nd slices, hash slices, useless smallprofpp SOAP Socket.pm sort pragma Source Code Control System source file typing sqlperl Storable module STORE() 2nd STORESIZE() stream processing, line by line strict stringification, useless style subroutine prototypes subroutine, redefinition Subversion suidperl 2nd Switch module sybperl [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] Tangram tarballs Template Toolkit temporary variables test.pl Test.pm Test:: modules Test::Builder 2nd Test::Exception Test::Harness 2nd Test::Inline Test::MailMessage Test::More 2nd Test::NoWarnings Test::Pod Test::Simple 2nd Test::Tutorial testing, regression Text::Balanced throws_ok() 2nd Thunderbirds Tie::Array::Bounded 2nd 3rd 4th Tie::File TIEARRAY 2nd tieing 2nd 3rd Time::HiRes tkperl TMTOWTDI typeglobs [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] undef Unicode uniperl universe, fundamental structure of URI Parsing URI.pm use blib use lib use strict 2nd 3rd 4th 5th 6th use strict 'refs' use strict 'subs' use strict 'vars' 2nd use vars 2nd 3rd use warnings 2nd 3rd 4th 5th use_ok() [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] variable renaming variable, lexical 2nd variable, package 2nd 3rd virtues of Perl programmers, principal Visual Perl [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] warnings in modules warnings, custom warnings, disabling warnings, lexical warnings, whether to leave in production code Web server processing, evolving WWW::Mechanize 2nd 3rd [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] XML-RPC XP_ [See Extreme Programming]2nd [See Extreme Programming]3rd [See Extreme Programming] [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [L] [M] [N] [O] [P] [R] [S] [T] [U] [V] [W] [X] [Y] YAML YAPC YAPE::Regex::Explain Yet Another Society ... read, if you want to write code that'll be easier for you to maintain, the book that comes to your rescue is Perl Medic • • Table of Contents Index Perl Medic: Transforming Legacy Code By Peter J Scott Publisher : Addison Wesley. .. : Addison Wesley Pub Date : March 10, 2004 ISBN : 0-201-79526-4 Pages : 336 Slots : 1.0 Copyright Preface Perl or perl? Obtaining Perl Historical Perl Typographical Conventions Perl Versions... Section 7.4 Perl 5.001 Section 7.5 Perl 5.002 Section 7.7 Perl 5.004 Section 7.9 Perl 5.6.0 Section 7.11 Perl 5.8.0 Section 7.13 Perl 5.8.2 Section 7.6 Perl 5.003 Section 7.8 Perl 5.005