Imagine a very adaptable organism of indeterminate species responding to changes in its environment. Temperature rising? Evolve a thicker skin. Gravity increasing? Add more legs. Water table rising? Grow fins.
Similarly, our code changes as various demands upon it increase: size of input data, number of concurrent users, and so on. You can think of those demands as happening along different dimensions; in other words, they can vary independently of each other and any given program may be subject to one or more type of demand.
How far along each of those dimensions you are able to evolve your code before it succumbs to the pressures of change depends on your skills; the further along each dimension you go, the more skillful you need to be.
When you're rewriting legacy code, it may help to bump it along these dimensions one step at a time until you get it to where you want. Space doesn't permit me to go into much detail, so I'll just cover some steps of some dimensions broadly.
Flat scripting: One monolithic block of code.
Subroutines: Abstracting common functionality; procedural programming.
Using objects, making libraries of reusable subroutines.
Subclassing existing modules.
Creating application-specific object-oriented modules.
Using tieing and overloading (see Section 10.2).
4.10.2 Input Interfaces—Complexity
Hand parsing of @ARGV.
Configuration files: see Simon Cozens' module Config::Auto (http://search.cpan.org/dist/Config-Auto/).
4.10.3 Error Handling—Application Size
No error handling at all—"program and pray."
Checking return codes from built-in functions.
Writing subroutines to return distinct error conditions.
Exception handling: using die() and eval() for throwing and catching exceptions.
Throwing objects as exceptions, either with die(), or using a module such as Dave Rolsky's Exception::Class (http://search.cpan.org/dist/Exception-Class/). Graham Barr's Error.pm, maintained by Arun Kumar U (http://search.cpan.org/dist/Error/) provides nice try and catch keywords, but has a memory leak triggered by nested closures. Such usage is rare, but if you don't want to worry about whether you've unwittingly created a nested closure, don't use it.
4.10.4 Logging—Volume and Granularity
What's "logging"? Wait for something to go wrong or evidence needed of past behavior, then panic.
Ad-hoc print() and warn() statements.
Ad-hoc print() statements directed to specific files, possibly with hand-rolled timestamping, process/host/user identification, and log file rotation.
Mike Schilli's Log::Log4perl (CPAN and http://log4perl.sourceforge.net) is based on Java's Log4j utility, and provides an object-oriented logging capability with severity levels and completely configurable multiplexing of messages to all kinds of destinations. See Section 10.1.4.
Mark Pfeiffer's Log::Dispatch::FileRotate (http://search.cpan.org/dist/Log-Dispatch-FileRotate/) automates log file rotation and provides file locking into the bargain. Can be plugged into Log::Log4perl.
4.10.5 External Information Representation—Extensibility
External data represented in plain text, likely implicitly assumed to be in the ISO Latin-1 encoding, or (especially in some locales) Unicode UTF-8, but with ad-hoc syntax.
Various configuration file formats such as the core modules Data::Dumper and Storable, and CPAN modules for more specific applications such as Config::IniFiles, Config::Auto, DBD::CSV, and so on.
When you want a structured markup format that's easy to read, get Brian Ingerson's YAML (originally Yet Another Markup Language; now, hopping on the already overcrowded recursive retronym bandwagon, YAML Ain't Markup Language) from http://search.cpan.org/dist/YAML/. A number of Perl modules use it as their data interchange format, such as Ingerson's CGI::Kwiki, which can be used for building Wiki sites [LEUF01].
For maximum interoperability, XML (see Section 8.3.7). YAML is extraordinarily capable, but when you need to speak to a third party, they're more likely to understand XML than YAML.
4.10.6 External Data Storage—Complexity, Concurrency, and Volume
Plain files—no locking, no structured data.
DBM files written by one of the AnyDBM_File modules (perldoc AnyDBM_File).
DBM files with home-grown locking to avoid concurrent access race conditions.
Accessing database servers with Tim Bunce's DBI.pm (see Section 8.3.6).
Integrating object classes with databases: Tangram (http://search.cpan.org/dist/Tangram/), Alzabo (http://search.cpan.org/dist/Alzabo/), and so on.
4.10.7 Web Server Processing—Scalability
Processing form inputs and generating content for a browser by hand.
Using Lincoln Stein's CGI.pm to do either or both.
Using Sam Tregar's HTML::Template (http://search.cpan.org/dist/HTML-Template/) or Andy Wardley's Template Toolkit (http://search.cpan.org/dist/Template-Toolkit/) to separate the output appearance from the code.
Using mod_perl on an Apache server for improved performance (see [BEKMAN03]).
Using Dave Rolsky's HTML::Mason (http://search.cpan.org/dist/HTML-Mason/) for advanced templating under mod_perl.
Integrating XML with Matt Sergeant's AxKit (http://search.cpan.org/dist/AxKit/) or extending into vertical applications too numerous to mention.
4.10.8 Event Handling—Application Complexity and Interoperability
Punt. Event handling is not for the fainthearted.
Using Joshua N. Pritikin's Event.pm (http://search.cpan.org/dist/Event/).
Using Rocco Caputo's Perl Object Environment (POE). See CPAN and http://poe.perl.org/. POE can wrap an event loop around just about any kind and number of asynchronous activity, usually without even needing to start a new thread or process. See also Uri Guttman's Stem (http://www.stemsystems.com and CPAN).
4.10.9 Client/Server or Interprocess Communication—Interoperability
Again, punt—beginning programmers don't try this.
Except at primitive levels like leaving messages in files for other processes to find. Usually has race conditions and other bugs.
Using fork(). Handcrafted use of signals, pipes, and semaphores.
Using higher-level protocols for interprocess communication; see, for example, Graham Barr's Net::Cmd (in the Perl core as of version 5.8.0).
Interoperating with other applications using standard remote procedure call (RPC) mechanisms such as XML-RPC [STLAURENT01] and SOAP (both covered in Paul Kulchenko's distribution at http://search.cpan.org/dist/SOAP-Lite/).
The dimension of robustness deserves space all to itself, and I'll get to it in Section 10.1.