You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ajit Deshpande <aj...@skycorp.net> on 2000/12/09 00:28:47 UTC

Re: Persistent objects between init and content handlers

On Fri, Dec 08, 2000 at 03:33:32PM -0800, Perrin Harkins wrote:
> On Fri, 8 Dec 2000, Doran L. Barton wrote:
> > I've got information stored in a DBI-accessible database. I would like to
> > read this information from the database and populate an object with the
> > data (really just a tree of hashes). This database "dump" into the object
> > should occur at child-init (so, I'm guessing with a PerlChildInitHandler 
> > directive). Then, I need to use the object functions/methods to navigate 
> > the data in a content handler.
> > 
> > I tried passing the object using $r->notes(), but it didn't work. Then I
> > read somewhere the data in $r->notes() is destroyed after each request- so
> > of course that isn't going to work. 
> 
> Just put it in a global.
> 
> $Cached::Data::Thingy = $my_object;

If $my_object does not need to be different for different child httpds,
then simply put it in the parent httpd. Or better still use
Apache::Session (since you already have a neat DBI-accessible database
setup). From the perldoc of Apache::Session :

        use strict;

        my %global_data;

        eval {
            tie %global_data, 'Apache::Session::File', 1,
               {Directory => '/tmp/sessiondata'};
        };
        if ($@) {
           die "Global data is not accessible: $@";
        }

        my $dbh = DBI->connect($global_data{datasource},
           $global_data{username}, $global_data{password}) || die $DBI::errstr;

        undef %global_data;

Of course, you can use Apache::Session::[MySQL|Oracle||DBI-d/b] instead
of Apache::Session::File.

Ajit