Chapter 2
Notice how the package variable $LOG_LEVEL is used to maintain state between
calls to log_level() and write_log(). By state I mean that the module contains
variables that store the value of past operations between calls to the interface.
Thus the state of the module changes over time as the interface is used.
Here's a possible usage of the module, which would go in a separate script file:
# use the module
use BOA::Logger;
# open the log file
BOA::Logger::open_log("logs/boa.log");
# set the log level higher
BOA::Logger::log_level(10);
# write a log entry at level 5
BOA::Logger::write_log(5, "Hello log reader.");
# write a log entry at level 15 this won t be printed to the log
BOA::Logger::write_log(15, "Debugging data here.");
Exporting
BOA::Logger is useful enough, but it could be improved. For one thing, the module
takes too much typing. To solve this problem, you can use the Exporter. The
Exporter enables you to export symbols from your module into the package of the
calling module. Exporting makes an entry in the calling package's symbol table
that points to the called package. To export the three subroutines in BOA::Logger,
you would change the top of the module source file, BOA/Logger.pm, to read as
follows:
package BOA::Logger;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(open_log log_level write_log);
The second line loads the Exporter module require is commonly used here, but
use also works. The third line accesses Perl's inheritance mechanism I'll describe
inheritance in more detail in the Object Oriented Modules section, but for now
you can just treat it as magic code that makes the Exporter work. Finally, the
@EXPORT array is initialized with the list of symbols to export.
32
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