Perl Module Basics
UNIVERSAL
All classes in Perl implicitly inherit from a common base class UNIVERSAL. The
UNIVERSAL class provides three methods that can be used on all objects isa(),
can(), and VERSION().
The isa() method can be used to determine if an object belongs to a particular
class or any child of that class. This is preferable to using ref() to check the class of
an object since it works with inheritance. For example, the following code prints Ok :
my $logger = BOA::Logger::Enhanced >new("logs/boa.log");
print "Ok" if $logger >isa( BOA::Logger );
but this similar code does not:
my $logger = BOA::Logger::Enhanced >new("logs/boa.log");
print "Ok" if ref($logger) eq BOA::Logger ;
This is because ref() returns the name of the class that the object belongs to which
is BOA::Logger::Enhanced. Even though BOA::Logger::Enhanced inherits from
BOA::Logger, that won't make eq return true when comparing them as strings. The
moral here is simple: Don't use ref() to check the class of objects, use isa() instead.
To check if an object supports a method call, use can(). You can use can() to
provide support for older versions of modules while still taking advantage
of the newer features. For example, imagine that at some point in the future
BOA::Logger::Enhanced adds a method set_color() that sets the color for the next
line in the log file. This code checks for the availability of the set_color() method
and calls it if it is available:
if ($logger >can( set_color )) {
$logger >set_color( blue );
}
$logger >write("This might be blue, or it might not!");
Another way to query the features provided by a module is to use the VERSION()
method. With no arguments, this method looks at the $VERSION variable defined in
the class package and returns its value. If you pass an argument to VERSION(), then the
method will check if the class's $VERSION is greater than or equal to the argument
and die() if it isn't. This form is used by use when use is passed a version number.
For example, this statement calls BOA::Logger >VERSION(1.1) after BOA::Logger is
compiled and exits with an error message if the call returns false:
use BOA::Logger 1.1;
47
47
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