Perl Module Basics
# write method   writes a line to the log file if log level is high enough
sub write {
   my ($self, $level, $message) = @_;
   $self >{fh} >print($message) if $level <= $self >{level};
}
1;
The module begins by using two modules you've met before: Carp and IO::File. Next, 
the first subroutine, new(), is defined. This is the constructor a class method that 
returns new objects. new() receives two arguments the name of the package and 
the filename to open.
The object itself is just a hash underneath. Most objects in Perl are really 
hashes, but it's possible to create objects based on anything you can make a ref 
erence to. Hashes are used so often for their inherent flexibility. In this case, the 
hash contains two keys  fh  and  level . The  fh  key contains an open IO::File 
object for the log file. The  level  key is set to the default log level of 1. Data elements 
kept in an object are known as the object's attributes.
So far so good, but what about that last section:
bless($self, $pkg);
return $self;
The call to bless()
13
 tells Perl that $self is an object in the package named $pkg.
This is how a reference becomes an object. After this point, methods can be called 
using the object, and they will result in subroutine calls in $pkg BOA::Logger in 
this case. A call to ref($self) will return $pkg ( BOA::Logger ) after blessing. 
Finally, since this is a constructor, the new object is returned to the caller.
Methods all share a common structure. They receive their $self object as an 
automatic first argument and any additional arguments after that. The two methods 
here, level() and write(), work with the data stored in the $self hash. The contents 
of the $self hash is known as instance data. Instance data is different for each 
instance (a fancy word for object) of this class.
13. There is also a single argument form of bless that blesses into the current package. This 
should be avoided because it doesn't allow for inheritance. Since there's no drawback to using 
the two argument form, it should be used in all cases.
41
41






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