in reply to How to keep an object between HTTP requests?

In general that's not possible. If you tell us more about your environment, we might be able to tell you how to cache the object. For example persistent environments like mod_perl, FCGI or HTTP::Server::Simple allow you to keep objects around.

The most portable solution is to store (and restore) the object using Storable, but not all data types can be serialized that way.

Another way would be to only load/initialize the parts of your object that you really need, instead of initializing everything just to display the log in screen. But you haven't told us about the nature of your object, so it's hard to advise on that.

  • Comment on Re: How to keep an object between HTTP requests?

Replies are listed 'Best First'.
Re^2: How to keep an object between HTTP requests?
by kornerr (Novice) on Feb 15, 2010 at 09:01 UTC
    I use mod_perl under Apache2. Currently I'm examining Apache::Session module.

      It's easily possible to keep objects persistent per Apache process using mod_perl. If you need finer grained resolution, like for example per-user or per-session, then looking into one of the Session modules is worth it. Not every session module can handle large objects, as not all of them employ Storable to persist your large objects.

        I tried to use CGI::Session together with CGI for keeping CustomObject between web page updates:

        /cgi-bin/session:

        #!/usr/bin/perl -wT use strict; use warnings; use CGI; use CGI::Session; use CustomObject; my $cgi = new CGI; my $session = new CGI::Session("driver:File", $cgi, {Directory => "/tm +p"}); print $cgi->header(-type => "text/html", -charset => "utf-8"), $cgi->start_html("Session management"); # Initialize session object. unless ($session->param("custom_object")) { my $customObject = new CustomObject("test_login", "test_password") +; $session->param("custom_object", $customObject); print $cgi->p("Custom object initialized"); } my $customObject = $session->param("custom_object"); print $cgi->p("Login: " . $customObject->login()), $cgi->p("Password: " . $customObject->password()), $cgi->end_html();

        /usr/lib/perl5/site_perl/CustomObject.pm:

        package CustomObject; sub login { my $self = shift; return $self->{LOGIN}; } sub new { my ($className, $login, $password) = @_; my $self = bless { LOGIN => $login, PASSWORD => $password }, $className; return $self; } sub password { my $self = shift; return $self->{PASSWORD}; } 1;

        All I get is that CustomObject is instantiated each web page update and the /tmp directory is populated with cgisess_* files.

        It's easily possible to keep objects persistent per Apache process using mod_perl.
        Bad idea. It's very unlikely you'll get the same Apache process on the next request.