Chapter 2
To provide the advertised enhanced functionality, the class will override the
write() method. Overriding is when a child class replaces a parent class's method.
Here's a new write() method that puts a timestamp on every log line. This code
would be placed in BOA/Logger/Enhanced.pm:
sub write {
my ($self, $level, $message) = @_;
$message = localtime(time) . " : " . $message;
$self >{fh} >print($message) if $level <= $self >{level};
}
The method modifies the $message parameter to contain a timestamp and then
prints out the line in the same way as the original BOA::Logger::write(). Here's an
example using the new module:
use BOA::Logger::Enhanced;
my $logger = BOA::Logger::Enhanced >new("logs/boa.log");
$logger >level(10);
$logger >write(10, "The log level is at least 10!");
When BOA::Logger::Enhanced >new() is called, Perl first looks in the
BOA::Logger::Enhanced package to see if a subroutine called new() is defined.
When it finds that there is no BOA::Logger::Enhanced::new(), Perl checks to see if
@ISA is defined and proceeds to check each package name listed in @ISA for the
required method. When it finds BOA::Logger::new(), it calls the subroutine with
two arguments, BOA::Logger::Enhanced and logs/boa.log. BOA::Logger::Enhanced
gets assigned to $pkg in BOA::Logger::new() and used in the call to bless():
bless($self, $pkg);
The result is that BOA::Logger::new() returns an object in the BOA::Logger::Enhanced
class without needing to know anything about BOA::Logger::Enhanced! Isn't Perl
great?
CAUTION Don't be fooled by the similar class names no
automatic inheritance is happening between BOA::Logger and
BOA::Logger::Enhanced. Inheritance must be explicitly declared
through @ISA to be used.
46
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