Perl Module Basics
When my fellow BOA developers see this message, they'll likely jump to the
conclusion that there's something wrong with BOA::Logger. They'll send me angry
e mails and I'll be forced to sign them up for spam.
11
Nobody wants that, and
thankfully the situation can be avoided. The Carp module, which comes with Perl,
can be used to place the blame where it belongs. Here's a new version of the module
header and open_log() using Carp:
package BOA::Logger;
use Carp qw(croak);
sub open_log {
my $filename = shift;
open(LOG_FILE, ">>$filename") or croak("Unable to open $filename : $!");
print LOG_FILE "BOA log started: " . localtime(time) . "\n";
}
Now the blame is properly placed and the resulting error is
Unable to open /path/to/log : No such file or directory at caller.pl line 5
The croak() routine provides a die() replacement that assigns blame to the
caller of the subroutine. The Carp module also provides a warn() replacement
called carp(), as well as routines to generate full back traces. See the Carp docu
mentation for more details; you can access it with the command perldoc Carp.
Object Oriented Modules
As previously mentioned, BOA is a big ol' application. In fact, it's so big that just
one log file will not be enough. There are several subsystems (GUI, Network, Database,
and so on) that each need their own log files with independent log levels. One way to
address this would be to create a new package for each log file and copy and
paste the code from BOA::Logger into each one creating BOA::Logger::GUI,
BOA::Logger::Network, and so on. This approach has some obvious drawbacks
the code becomes harder to maintain since a change in once place has to be care
fully replicated in each copy. Also, it would be difficult to use multiple BOA::Logger
clones at the same time they all want to export write_log(), so you'd have to
forgo exporting and type the whole package name for every call.
There is an easier way. Instead of creating a new package just to hold some
state information, you'll create an object oriented module that provides an object
for each log file. These objects will contain the state necessary to support a single
11. I recommend Oprah's book club mailing list.
35
35
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved