It is currently Sat, 26 Nov 2022 06:34:02 GMT

Author Message
 initializing a global variable at apache startup
I'm using apache with mod_perl and would like to initialize a global
variable on startup for later use by a PerlHandler module.  Later I'll
probably need to protect it with some kind of mutex, but I'll cross that
bridge when I come to it.

Anyway, in the startup, I'm successfully populating an array called
@titles (which out of desperation to make it global I'm calling
@main::titles in the code).  The populating works, but in a later
experimental PerlHandler module called "Apache::DumpPatterns" the array
seems to be empty.

In httpd.conf, I've given the following directive...
PerlRequire conf/

Now the
package Apache::Startup;

        use Apache ();
        use lib Apache->server_root_relative('lib/perl');

use Apache::Registry ();
use Apache::Constants ();
use CGI qw(-compile :all);
use CGI::Carp();
use CGI qw/:standard/;
use CGI::Cookie;
use DBI;

my $dbh = DBI->connect('dbi:Pg:dbname=fore', 'fore', 'forgeahead');
my $cursor;
my $title;
@main::titles = ();

if ($dbh) {
        $cursor = $dbh->prepare('select pattern from foredomain');

        if (!$DBI::err) {
                while (($title) = $cursor->fetchrow) {                                                       push (@main::titles, $title);



Here's the PerlHandler directive(s)
SetHandler perl-script
PerlHandler Apache::DumpPatterns

Now for the code.  Here nothing gets sent back to the
browser client - the array, which in my ignorance I think should be
global and populated by the startup code, is empty.
package Apache::DumpPatterns;

use strict;
use Apache::Constants qw(:common);

sub handler {
        my $r = shift;
        my $pattern;


        if (undef(@main::patterns)) {
                $r->print("<b>\@patterns not defined</b>");
        else {
                $r->print("<b>\@patterns defined</b>");

                for $pattern (@main::patterns) {


        return DONE;

Any help on this would be very greatly appreciated.  It's probably a
dumb question - I haven't coded in Perl for years and never in this kind
of environment.


 Tue, 01 Jun 2004 11:39:17 GMT   
 initializing a global variable at apache startup
It's me again - the original poster.  My apologies for this post.  I
used different variable names in startup and dump.  Sigh.

 Tue, 01 Jun 2004 22:12:28 GMT   
 initializing a global variable at apache startup

OK, initially I thought that the problem was that you are not in main::
as you have declared package Apache::Startup. I don't think that you
need/want that declaration, but if you are explicitly specifying main::,
I'll have to look closer.

You say that the populating works. How do you know this?

I would recommend using Data::Dumper at various points to make sure that
your data looks like you expect it to.

--- code left here for the sake of followups ---

Note that __END__ is a no-no in mod_perl. You probably want to get rid
of that. No, it is not causing this problem, but it's a no-no

Rich Bowen

 Tue, 01 Jun 2004 20:08:52 GMT   
   [ 3 post ] 

Similar Threads

1. initialize the global variables in .so files

2. Apache worker MPM for global variables

3. Global Variables in Apache 0.8.8

4. Global Variables Within Apache Handlers

5. file contents into variable, or global variable from shell script

6. Dynamically setting environment variables on apache startup?

7. how to initialize a global instance

8. Kernel freeze when initializing graphic card (during X startup)

9. how do you initialize a service on startup?

10. Initializing ksh variables

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software