Haloric has asked for the wisdom of the Perl Monks concerning the following question:

Hi o great ones.

I am using cgi.pm for the first time. I want to save() the request object to a file and use new CGI($saved) to create a copy when I need to (in a later http request).

The problem is that the save() only saves the state of the request params, not the env variables. I want things like remote_addr(), remote_host() and remote_user() to work on the restored object.

What is the most efficient way of doing this, is there something that already does it, or do I have to do it for myself.

Thanks

Replies are listed 'Best First'.
Re: Restoring CGI.pm object
by ccn (Vicar) on Oct 27, 2008 at 17:37 UTC

    You can store your %ENV in $cgi params before saving.
    Something like this:

    $cgi->param(-name=>"ENV-$_",-value=> $ENV{$_} for keys %ENV;

    And restore them back upon CGI restoring

    foreach my $p ($cgi->param()) { if ($p =~ /^ENV-(.*)/) { $ENV{$1} = $cgi->param($p); $cgi->delete($p); } }
      Thanks - although doesn't that affect my $ENV{} in the restoring process ?

      I would like it if I could refer to the original $cgi->remote_addr() method, and have it work, and not affect the current environment.

        Since remote_addr is exactly
        sub remote_addr { return $ENV{'REMOTE_ADDR'} || '127.0.0.1'; }
        the easiest way to isolate stored ENV is to use local %ENV
        If you check the source for CGI, you'll find that those functions read from the ENV vars populated by your webserver.

        -Paul

Re: Restoring CGI.pm object
by jeffa (Bishop) on Oct 27, 2008 at 18:03 UTC

    Will using sessions not do the trick? They allow you to preserve data in between HTTP requests.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)