Perl Module Basics
use WeakRef qw(weaken);
# constructor returns new BOA::Logger objects
sub new {
my ($pkg, $filename) = @_;
# lookup $filename in %BOA::Logger::CACHE if an entry exists, return it
return $CACHE{$filename} if $CACHE{$filename};
# initialize $self as a reference to an empty hash
my $self = {};
# store in %CACHE
$CACHE{$filename} = $self;
weaken($CACHE{$filename});
# ... same as previous example ...
}
Inheritance
BOA::Logger is a simple module, but simple doesn't last. As more BOA developers
start using BOA::Logger, requests for new features will certainly start piling up. Sat
isfying these requests by adding new features to the module might be possible, but
the effect on performance might be severe. One solution would be to create a new
module called BOA::Logger::Enhanced that supported some enhanced features
and just copy the code in from BOA::Logger to get started. This has an unpleasant
consequence: The code would be harder to maintain since bugs would need to be
fixed in two places at once.
There is a better way. Object oriented classes can be enhanced using inheritance.
Inheritance enables one module to be based on one (or more) classes known as parent
or base classes. The new derived class that inherits from the parent class is known as
the child class. Here's an example module, called BOA::Logger::Enhanced, that
inherits from BOA::Logger:
package BOA::Logger::Enhanced;
use BOA::Logger;
@ISA = qw(BOA::Logger);
By assigning BOA::Logger to the package variable @ISA, the module tells Perl that
it is inheriting from the BOA::Logger class. This variable is pronounced is a and
refers to the fact that a BOA::Logger::Enhanced object is a BOA::Logger object.
Inheritance relationships are known as is a relationships.
45
45
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