Chapter 2
this means that each client module will create its own BOA::Logger object. This 
will have a number of unpleasant side effects. First, when each BOA::Logger object 
is destroyed, it will write its own  BOA log exited  message. Second, each 
BOA::Logger object will consume a file handle. Many systems limit the number of 
open file handles a process can have, so it's best not to use more than necessary.
We can solve this problem using, you guessed it, class data. Class data is data 
that is stored at the class level and is not associated with any specific object. In 
Perl, class data is supported through package scoped variables. It can be used to 
maintain state separate from each objects' own state. Common uses of class data 
include keeping track of the number of objects created or the number of objects 
still alive. In this case, you'll use a hash called %CACHE to maintain a cache of 
BOA::Logger objects:
# constructor   returns new BOA::Logger objects
sub new {
   my ($pkg, $filename) = @_;
   # lookup $filename in %BOA::Logger::CACHE   if an entry exists, return it
   return $CACHE{$filename} if $CACHE{$filename};
    
   # initialize $self as a reference to an empty hash
   my $self = {};
   # store in %CACHE
   $CACHE{$filename} = $self;
   # ... same as previous example ...
}
When new() is called, it will first check the cache to see if you've already got a 
BOA::Logger object for the filename. If it does, the existing object is immediately 
returned. If not, the new object is stored in the cache for future lookups.
This works, but it causes a subtle change in BOA::Logger's behavior. After 
adding the cache, DESTROY is only called at program exit, rather than when the last 
reference to a BOA::Logger object goes out of scope. This is because objects aren't 
destroyed until the last reference to them goes out of scope; %CACHE maintains a ref 
erence to every object created by new() and as a package variable it never goes out 
of scope. This might be acceptable behavior, but if it's not, you could fix it by using 
the WeakRef module.
15
 WeakRef provides weaken(), which enables you to create 
references that don't prevent objects from being destroyed. This version will allow 
the BOA::Logger objects to be destroyed as soon as possible:
15. Written by Tuomas J. Lukka and available on CPAN
44






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