Chapter 2
The Class
A class in Perl is nothing more than a package that happens to have subroutines
meant to be used as methods. Here's an example of BOA::Logger transformed into
a class.
package BOA::Logger;
use Carp qw(croak);
use IO::File;
# constructor returns new BOA::Logger objects
sub new {
my ($pkg, $filename) = @_;
# initialize $self as a reference to an empty hash
my $self = {};
# open the log file and store IO::File object in $self >{fh}
my $filehandle = IO::File >new(">>$filename");
croak("Unable to open $filename : $!") unless $filehandle;
# print startup line
$filehandle >print("BOA log started: " . localtime(time) . "\n");
# store the filehandle in $self
$self >{fh} = $filehandle;
# set default log_level of one
$self >{level} = 1;
# bless $self as an object in $pkg and return it
bless($self, $pkg);
return $self;
}
# level method changes log level for this log object
sub level {
my ($self, $level) = @_;
$self >{level} = $level;
}
40
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