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

hi, i was wondering if there is any method to clean up a cgi object's parameters that was saved in the session earlier on? calling $cgi->delete_all(); will only delete that instance of cgi, but not the one saved inside the session.


file 1:
$session->save_param($cgi);

file 2:

$session->load_param($cgi);

#clean up the $cgi parameters that were saved into session in file 1 here so it won't be so bloated.

...redirecting to file 3.

file 3:

when file 3 loads, $cgi is clean.

Replies are listed 'Best First'.
Re: how to clear cgi object from session?
by almut (Canon) on Sep 13, 2007 at 09:34 UTC

    If I'm understanding you correctly, I think you want $session->clear( ... ). You can use it to remove a single parameter ( $session->clear('paramname') ), multiple parameters ( $session->clear([qw(paramname1 paramname2]) ), or the entire content of the session ( $session->clear() ).

    To delete the session itself, use $session->delete(). Depending on context (in particular in persistent environments like FCGI), you might also need to call $session->flush() after manipulating session contents, in order to explicitly synchronize in-memory with on-disk representation of the session data.

    (BTW, I'm assuming we're talking about CGI::Session.)

      anyone?
Re: how to clear cgi object from session?
by adrive (Scribe) on Sep 14, 2007 at 11:21 UTC
    hmm seems to have lost my own reply. Yes, well i'm using CGI::Session, and I wanted to know whether there was a way to clean up my saved cgi object (CGI.pm). Could it be $session->clear($cgi)? let's take another sample:
    main.pl : my $cgi = New CGI; my $session = new CGI::Session(); $cgi->param("name", "Barney"); $session->save_param($cgi); #saves cgi object into session $cgi->redirect(URI => "body.pl?sessionid=".$session->id);
    then on body.pl :
    my $cgi = New CGI; my $sessionid = $cgi->param("sessionid"); my $session = new CGI::Session($sessionid); $session->load_param($cgi); $cgi->param("name"); #we still have barney around. $session->clear($cgi); #ASSUMPTION - CLEAN UP CGI $cgi->redirect(URI => "car.pl?sessionid=".$session->id);
    Then on car.pl :
    my $cgi = New CGI; my $sessionid = $cgi->param("sessionid"); my $session = new CGI::Session($sessionid); $session->load_param($cgi); print $cgi->param("name"); #should not have anything anymore

      It should just work if you call $session->clear() (without passing the $cgi object). As I already said, this would clear all parameters, though. In case you have other stuff stored in the session which you don't want to have cleared, you could do $session->clear([$cgi->param()]) instead, which would clear nothing but the parameters of the current CGI object.

      BTW, save_param($cgi) does not store the CGI object itself. It calls the param() method on the object passed in. This method, when called without an argument, returns a list of all parameters, which are then stored in the session. (In theory, the API of CGI::Session could likewise accept clear($cgi), to then call param() on the CGI object, etc... — but that's not how it's implemented.)  HTH.