Perl Module Basics
Now code that uses BOA::Logger can dispense with the package name:
use BOA::Logger;
open_log("logs/boa.log");
log_level(10);
write_log(5, "Hello log reader.");
write_log(15, "Debugging data here..");
Of course, the full package specification would still work you can always refer to
BOA::Logger::write_log().
Now, BOA is a big application. In fact, BOA stands for big ol' application, so
many other modules will be using BOA::Logger. Most of these modules will only be
calling write_log(). The only code that will call open_log() and log_level() is the
startup code. Fortunately users of the module can choose what symbols they want
exported by providing a list of symbols to use:
use BOA::Logger qw(write_log);
Without this addition, a use BOA::Logger will import all exported symbols. To
import nothing from a module that exports symbols by default, use an empty list:
use BOA::Logger ();
Subroutines aren't the only thing you can export. Variables can also be exported.
For example, BOA::Logger could omit the log_level() subroutine and just export
$LOG_LEVEL directly:
@EXPORT = qw(open_log $LOG_LEVEL write_log);
Now code that wants to set the logging level can import the $LOG_LEVEL variable
and manipulate it directly:
use BOA::Logger qw($LOG_LEVEL write_log);
$LOG_LEVEL = 10;
write_log(10, "Log level set to 10.");
I'll return to the Exporter to provide more details in the next chapter.
BEGIN
Another problem with the BOA::Logger module is that other modules have to wait
for open_log() to get called before they can use write_log(). This makes it difficult
for modules to log their compilation and initialization. To solve this problem, the
33
33
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