perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier>
Subject Re: lexical scoping under mod perl solution
Date Thu, 12 Feb 2009 20:34:07 GMT
As someone pointed out, these things can be very practical too.

# my nifty cgi-bin

use strict;
use warnings;

our $big_table;

package XX;
no strict;
$var1 = 0;

sub sub1 {
my $key = shift;

   unless (defined($big_table)) {
   if ($XX::var1) {
     return $big_table->{$key};
   return undef;

sub sub2 {
my $key = shift;



The $big_table var (and the underlying hash) will be initialised the 
first time the above mod_perl handler (or cgi-bin script under mod_perl) 
gets called /in this apache child/.  Then it will retain its value 
across multiple invocations (http requests) /within this apache child/, 
as long as the child is alive (thus potentially saving hundreds of 
reloads of the big table).
$XX::var1 on the other hand, is really local to this running script 
instance, but global to the subs within it.
$key is really local to each sub in all respects.

Beauty is in the eyes of the beholder.
Perl and mod_perl may not be appreciated by the purists, but it's neat 
what you can do with them.

I'd love to have something like the above that works /multi-platform/ 
*across* different apache children/threads.
Perhaps based on DBI...

View raw message