Chapter 2: Perl Module Basics

Chapter 2 - Perl Module Basics
by?Sam Tregar?
Apress ? 2002
has companion web siteCompanion Web Site

Overview

SPAGHETTI CODE—if you don't know what it means, you're probably writing it. Spaghetti code gets its name from the numerous and thoroughly knotted paths your program takes through its source code. In the classic case, every subroutine in the program will call every other subroutine at least once (if there are subroutines—goto is marinara for spaghetti code). Nothing is commented, or if it is, then the comments are misleading. Executable code is mixed in with subroutine declarations at random. Basically, it's your worst nightmare.

What makes spaghetti code so bad is that even a small change in one part of the program can have dire consequences in an unrelated area. Fixing bugs becomes a dangerous activity—find one, and two more spring from the mist. Code like this invariably gets rewritten rather than enhanced, at tremendous expense.

To combat spaghetti code, you need modular programming. Modular programming is the practice of breaking a large program into smaller pieces called modules. Each module offers its service through a well-documented interface. The internals of the module are considered private, or encapsulated.

The beauty of modular programming is that the internals of the module can change without affecting code that uses the module. Fixing bugs is usually just a matter of finding the offending code and making sure that the fix doesn't affect the interface. Furthermore, modular programming makes your job easier; you only need to worry about the implementation of a single module at a time, rather than an entire complex program.

click to expand

This chapter will explain Perl's support for modular programming and delve into modular programming's funny-looking cousin, object-oriented programming. You may want to skip this chapter if you have experience programming modules in Perl.



Writing Perl Modules for CPAN
Writing Perl Modules for CPAN
ISBN: 159059018X
EAN: 2147483647
Year: 2002
Pages: 110
Authors: Sam Tregar

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net