Perl Module Basics
Functional Modules
The most obvious way to build a module is to place subroutines in the module and 
document them as the module's interface. For example, here's a module that 
provides a logging facility for a fictional application called BOA:
package BOA::Logger;
$LOG_LEVEL = 1; # default log level is 1
# open log file
sub open_log {
   my $filename = shift;
   open(LOG_FILE, ">>$filename") or die "Unable to open $filename : $!";
   print LOG_FILE "BOA log started: " . localtime(time) . "\n";
}
# set logging level
sub log_level { $LOG_LEVEL = shift; }
# write a log message if level is set high enough
sub write_log {
   my ($level, $message) = @_;
   print LOG_FILE "$message\n" if $level <= $LOG_LEVEL;
}
1;
CAUTION A real logging module would use flock() to prevent file 
corruption, but that would make these examples twice as long! The 
code in this chapter is kept as simple as possible real production code 
would need significant enhancement.
The concept for the module is simple BOA::Logger will provide logging at 
varying levels of detail known as log levels. The module's interface consists of three 
subroutines open_log(), log_level(), and write_log(). The application must call 
open_log() before the first call to write_log(). When a piece of code calls write_log(),
it provides two arguments, $level and $message itself. If $level is less than or equal 
to the currently set log level, the message is printed to the log. The log level defaults to 
1 and the application can change the value using the log_level() subroutine.
31
31






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

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