- Shopping Bag ( 0 items )
Techniques and tricks to master basic and advanced OO Perl for programmers who already have basic to intermediate skills in procedural Perl.
Programmers who already have basic to intermediate skills in procedural Perl and understand fundamental concepts of object orientation will get a solid understanding of basic and advanced object-oriented Perl. This book clarifies when, where and why to use Perl. Featuring many techniques and tricks, it presents solutions to ...
Techniques and tricks to master basic and advanced OO Perl for programmers who already have basic to intermediate skills in procedural Perl.
Programmers who already have basic to intermediate skills in procedural Perl and understand fundamental concepts of object orientation will get a solid understanding of basic and advanced object-oriented Perl. This book clarifies when, where and why to use Perl. Featuring many techniques and tricks, it presents solutions to common programming problem and explains how to combine Perl and C++.
It's about how to create larger, more robust applications that require less effort to built, less time to debug, fewer resources to maintain, and less trouble to extend.
Specifically, it's about how to do all that with the object-oriented features of Perl--how those features work, and how to make use of the many labor-saving techniques and "tricks" that they make possible. Presenting these new language features requires only a few chapters in total, but the range of programming styles and idioms those extra features make available would fill several books this size. This one tries to concentrate on the most useful and powerful ones.
This book is also about the tremendous flexibility of Perl's approach to object orientation, and how, in Perl, there's almost always more than one object-oriented way to solve a given problem. You'll find that the text revisits a few key examples time and again, extending and reimplementing them in various ways. Sometimes those changes will add extra functionality to a previous example, sometimes they'll merely illustrate an alternative solution (with different strengths and limitations).
This book is about developing programming skills that scale. Perl is a great language for "one-line-stands"--ad hoc solutions that are quick, cryptic, and unstructured. But Perl can also be a great language for developing large and complex applications. The only problem is that while "quick, cryptic, and unstructured" is cute in a throw-away script, it's not so amusing in a 5,000 or 50,000 lines of application code.Object-oriented programming techniques can help to solve the problem of building large, maintainable, reusable, and comprehensible systems in Perl.
Above all, this book is about how Perl makes object-oriented programming enjoyable, and how object-oriented programming makes Perl even more so. Life is too short to endure the cultured bondage-and-discipline of Eiffel programming, or to wrestle the alligators that lurk in the muddy semantics of C++. Object-oriented Perl gives you all the power of those languages (and more!), but very few of their tribulations. And best of all, like regular Perl, it's fun!
Who's this book for?
This book was written for the whole Perl community. In other words, for an eclectic range of people of wildly differing backgrounds, interests, and levels of experience.
To that end, it starts slowly, assuming only a very basic familiarity with the core features of Perl itself: scalars, arrays and hashes, pattern matching, basic I/O, and simple subroutines. If these things sound familiar, this book is definitely for you. If they don't, Chapter 2 provides a quicker refresher course in everything you'll need to know.
The only other assumption that's made is that you're interested in object orientation. Maybe you've only heard about its many advantages. Maybe you're familiar with the basic concepts (if not, see Chapter 1). Maybe you've already had some experience--even a bad one!--in another object-oriented language. If so, you might find Appendix B a useful place to start.
If you've already dabbled in object-oriented Perl--perhaps blessed a hash or two--this book is also for you. If you start at Chapter 4, you'll find a range of increasingly specialized techniques that will expand your repertoire and maybe even challenge some of your notions about object-oriented programming.
If you're an experienced object-oriented Perl programmer, this book's for you too. The earlier chapters might contain a few tricks you haven't seen before (blessed a regular expression or a typeglob lately?), and the later chapters may suggest some novel approaches you haven't considered before (for example, the section on generic trees in Chapter 12 is well worth a look).
Even if you're a Perl guru, this book's still for you. Check out Chapter 7, where object methods that don't have a class are called on class objects that don't have a method. Or try Chapter 11, where objects are stored as one of their own attributes. Or explore Chapter 13, where the entire dispatch mechanism is replaced with one that provides multiply dispatched methods and subroutine overloading.
What is object-oriented Perl?
Object-oriented Perl is a small amount of additional syntax and semantics, added to the existing imperative features of the language. Those extras allow regular Perl packages, variables, and subroutines to behave like classes, objects, and methods.
It's also a small number of special variables, packages and modules, and a large number of new techniques, that together provide inheritance, data encapsulation, operator overloading, automated definition of commonly used methods, generic programming, multiply-dispatched polymorphism, and persistence.
It's an idiosyncratic, no-nonsense, demystified approach to object-oriented programming, with a typical disregard for the accepted rules and conventions. It draws inspiration (and sometimes syntax) from many different object-oriented predecessors, adapting their ideas to its own needs. It reuses and extends the functionality of existing Perl features, and in the process throws an entirely new slant on what they mean.
In other words, it's everything that regular Perl is, only object-oriented.
What object-oriented Perl isn't.
Perl was not originally designed--or implemented--as an object-oriented language. Its version of object orientation is simple and well integrated, but it's not fundamental to the language.
That evolutionary development still shows in the few places where object-oriented features of the language are still experimental or incomplete. For example, recent versions of the Perl compiler support weakly-typed variables, which provide some level of run-time checking and optimization for accesses to the data stored Perl objects. But the current version of the mechanism, and its two support modules, don't actually enforce type safety or proper data encapsulation, and they don't fully support inheritance.
Because Perl wasn't originally designed as an object-oriented language, the other thing that object-oriented Perl isn't, is fast. Calling a method through an object is significantly slower than calling a regular Perl subroutine. Just how much slower is a matter of some debate, and depends on whether you measure entire systems or just raw single-call invocation speed.
A single method call is about 30% slower than a regular call to the same subroutine (depending on your hardware, operating system, the phase of the moon, etc.) That's because, before it can call the appropriate subroutine, a method call has to work out the class of the object on which the method was invoked, so it knows which method to use (see Chapters 3 and 6). In contrast, a regular call has no choice to make--it just jumps straight into the subroutine you specified. That's always going to be faster.
Though they're individually much slower, method calls are also more powerful than regular subroutine calls, due to a feature known as polymorphism (see Chapters 1 and 7). In a larger system that redresses the speed imbalance in a little, but in general it's fair to say that an object-oriented implementation of a system in Perl will almost never be faster than the equivalent non-object-oriented implementation, and will usually be somewhere between 20 to 50 percent slower.
Those figures are enough to turn many people away from object-oriented Perl, and that's a pity because then they miss out on the many compensating benefits of object-oriented design and implementation: simpler analysis methods, a domain-oriented approach to design, cleaner and more compact code, more robust implementations, greater code modularity, easier debugging, easier maintenance, more comprehensible interfaces to modules (including operator-based interfaces), better abstraction of software components, less namespace pollution, greater code reusability, greater scalability of software, and better marketability of the final product.
The sad thing is that people get spooked by the numbers (20 to 50 percent slower!!!) and forget what that really means (.just as fast in six months time, when processor speeds have doubled).
Where to find what you're looking for
This book is arranged as a tutorial on the various object-oriented features of Perl, and the many techniques that have been developed for using those features.
Chapter 1 provides a quick revision of the fundamentals of object orientation: objects, classes, attributes, methods, inheritance, polymorphism, aggregation, interfaces, genericity, and persistence. Chapter 2 offers a refresher on various aspects of the Perl language that are most relevant to object-oriented Perl: basic data types, subroutines and closures, references, packages and modules, typeglobs, and the CPAN.
Chapter 3 introduces the basic features of Perl's object-oriented mechanisms, concentrating on the most commonly used way of building classes and objects. Chapters 4 and 5 explore the numerous other ways of implementing objects in Perl. Chapter 6 introduces Perl's inheritance mechanism and Chapter 7 discusses Perl's approach to polymorphism.
Chapter 8 describes two freely available packages that can be used to automate the creation of class methods and attributes. Chapter 9 describes the Perl tie mechanism, which allows objects to be accessed like regular Perl variables or filehandles. This theme is continued in Chapter 10, where Perl operator overloading facilities are introduced.
Chapter 11 looks at the problem of Perl's poor support for encapsulation, and suggests three class implementation techniques that can be used to protect attributes within objects. Chapter 12 explains why generic programming techniques are rarely needed in Perl, and suggests several useful approaches for the few situations where they are required.
Chapter 13 looks at a generalized form of polymorphism known as multiple dispatch, which allows every argument of a method call to contribute to the invocation process. Chapter 14 examines how persistence techniques can be integrated with an object-oriented system.
Appendix A provides a condensed summary of Perl's object-oriented features, with examples of the syntax related for each. Appendix B provides an overview of those same features designed to assist programmers who are already familiar with one of four major object-oriented languages: Smalktalk, C++, Java, and Eiffel. A glossary of important object-oriented terms is also provided.
Throughout the text, each feature and technique is introduced with an example of a common type of programming problem that the feature or technique helps to solve. If you're looking for a specific technique to solve a particular problem, here's a list of the example problems, and the chapter and section in which they're discussed. The words "(and following)" indicate a major example that continues in subsequent sections of the same chapter.
Chapter 4: A bit-string class
Chapter 6: The "empty subclass" test
Bugs: storing reports
Chapter 3: Three little rules
Chapter 8: Creating classes (and following)
Case insensitive hashes
Chapter 9: Case-insensitive hashes
CDs: storing catalog information
Chapter 3: A simple class (and following)
Chapter 4: Reimplementing CD::Music (and following)
Chapter 6: Inheriting the CD::Music class
Chapter 8: Automating the CD::Music class (and following)
Chemical vs medical names
Chapter 11: Ambiguous keys in a securehash
Colours: RGB represention
Chapter 11: Converting to "fast" mode (and following)
Chapter 14: Class specific persistence (and following)
Days of the week
Chapter 10: Fallback operations (and following)
Debugging: tracking the source of data
Chapter 9: Implementing a scalar using a non-scalar (and following)
EBCDIC: automated interconversion with ASCII
Chapter 9: Filtered filehandles
Employees: tracking and identifying
Chapter 8: Flag-like attributes (and following)
GDP: per capita, China vs USA
Chapter 10: The problem
Genetics: accessing and manipulating larger DNA databases
Chapter 9: A base/codon array (and following)
Graphical user interfaces: event handling
Chapter 13: What is multiple dispatch (and following)
Hashes: nested iterations of the same hash
Chapter 4: An array-specific example
Chapter 5: So how can a subroutine be an object?
Hashes: sorted keys
Chapter 9: Sorted hashes
Chapter 5: A lexer object
Chapter 7: Polymorphic methods for the Lexer class
Chapter 8: Subroutines as attributes
Chapter 12: Closures as generic subroutines
Chapter 3: Destructors and circular data structures
Chapter 5: Paging STDOUT (and following)
Chapter 4: An object-oriented password
Chapter 8: Key-like attributes
Personality traits: statistical analysis
Chapter 8: Hash attributes
Chapter 5: A simple pretty-printer
Chapter 7: The simple pretty-printer objectified (and following)
Queues: implementing a customer queue
Chapter 8: List attributes
Regular expressions: making them more readable
Chapter 5: Blessing a regular expression (and following)
Chapter 10: A Roman numerals class (and following)
Round pegs in square holes
Chapter 13: Handling dispatch resolution failure
Semaphores: control of a transceiver
Chapter 4: Advantages of a pseudo-hash (and following)
Chapter 6: Inheritance and pseudo-hashes
Soldiers: dog-tag information
Chapter 8: Key-like attributes
Chapter 11: Encapsulation via closures (and following)
Stringification: of simple and hierarchical data types
Chapter 13 Non-class types as parameters (and following)
Chapter 14: Encoding/serialization
Chapter 6: Constructors and inheritance (and following)
Trees: generic binary search trees and heaps
Chapter 12: The generic Tree class (and following)
Unique IDs: autoincremented scalar variables
Chapter 9: A simple example
The following typographical conventions are used throughout this book:
Technical terms (all of which are explained in the Glossary) are introduced in italics.
Code examples and fragments are set in a fixed-width font.
Comments in code are set in a fixed-width italic font.
Sections of code that are of special significance are set in a bold fixed-width font. Often these sections represent changes from a previous version of the code.
All forms of system-level text and anything you might actually type yourself--file names, command-line instructions, program input, URLs, and newgroup names--are set in a sans-serif font.
Any kind of program output, including exception and error messages, is set in a bold sans-serif font.
The small number of names on the cover of most textbooks--in this case, just one--is an outright deception. Every textbook is a collaboration of a large number of dedicated and talented people. In writing this book I have been guided, helped, and supported by a multitude of generous and wise people. I am especially grateful to.
My beloved Linda, who has borne most of the burden of our lives these past six months, and who still found time to contribute her wisdom and insight to this book. Every day she sustains me with her extraordinary beauty, grace, and love; she is my inspiration, my joy, and my light.
My parents, James and Sandra, for their faith, love, and very practical support over so many years. They have been the giants on whose shoulders I have stood.
My parents-in-law, Corallie and Fred, for their encouragement, their generosity, and their love over the past decade.
Larry Wall, for giving us all this wondrous adventure playground called Perl.
Nat Torkington, who has been a constant source of wise counsel, practical assistance, and a host of favors far beyond the call of friendship, duty, or even Antipodean solidarity.
Randal Schwartz who graciously found time in his insanely busy life to review large chunks of this book, and whose suggestions have improved it immeasurably.
Bennett Todd, whose excellent advice, unflagging enthusiasm, and endless encouragement, has buoyed me through some very dark hours.
Mark-Jason Dominus, whose insight and experience were instrumental in setting the overall structure and direction of my early drafts, and whose guidance I have sorely missed ever since.
The many other reviewers of my manuscripts, who have devoted extraordinary amounts of time, effort, and ability to dissecting my English and my Perl: Ilya Zakharevich for his excellent advice on the care and feeding of operators; Tim Bunce, François Désarménien, Brian Shensky, and Adrian Blakey, for knocking my SQL into shape; Gary Ashton-Jones, for highlighting the important of sequence; Leon Brocard, for keeping the focus where it ought to be; David Cantrell, for his tremendous attention to detail; and to John Dlugosz, for making me question so many assumptions.
The many other members of the Perl community who have shared their understanding with me, and helped to resolve all manner of finer points of Perl lore.
My colleagues at Monash University, for their support and encouragement. In particular: David Abramson, John Crossley, Trevor Dix, Linda McIver, Ann Nicholson, Steve Welsh, and Ingrid Zukerman.
Marjan Bace and the staff of Manning Publications, who have guided and encouraged me at every stage of development with patience, good humour, and very practical support: Brian Riley and Peter Schoenberg, who watched over me; Ted Kennedy, who masterminded the review process, adding his own invaluable suggestions; Ben Kovitz, for his guidance and advice on the material itself; Mary Piergies who turned the raw manuscript into a polished book; Leslie Haimes, who provided its oustanding "look-and-feel"; Syd Brown, who worked typesetting miracles.
Posted February 5, 2000
when i saw the glowing reviews from schwartz and christiansen on the back of the book, i figured it had to be something special. let me just say that mr. conway blew threw the perl primer (about the first 70 pages) so quickly and carelessly that i had to read it again. i found several coding errors in those first 70 pages alone. after that, i wasn't about to trust anything he said in the rest of the book, even if i COULD understand what he was talking about. if there's another object-oriented perl book out there, i need to find it NOW!!!Was this review helpful? Yes NoThank you for your feedback. Report this reviewThank you, this review has been flagged.