Perl CGI::Session save_param saves all parameters as array under one key

1k Views Asked by At

Im using CGI::Session to store session data from CGI::Application (specifically i'm using CGI::Session through the CGI::Application::Plugin::Session module). In one of my application modes I do this:

    my $self = shift;
    # Get CGI query object
    my $q = $self->query();
    $self->session->save_param($q);

To save my parameters to the session data however on retrieving them using $self->session->param('user') I find that only the user parameter contains any data even though other parameters are being sent server side and are accessible through $q->param() the user parameter retrieved from the session is an array of the parameters, however i would expect that $self->session->param('user') would return a single string with the contents of the parameter 'user'.
Is this behavior expected?
If so why?

1

There are 1 best solutions below

1
On

I'm not sure I understand exactly what you mean, but this looks weird. You're not doing what the CGI::Session doc says you should. You can't just save the CGI object. You need to store each param individually.

# Storing data in the session:
$session->param('f_name', 'Sherzod');

If you want to just store all the CGI params in your Session, do it like this:

# $q := CGI object
# $session := CGI::Session object

$session->param('foo', $q->param('foo'));
$session->param('bar', $q->param('bar'));

Or you might even do it like this for all of them:

foreach my $key ($q->param) {
  $session->param($key, $q->param($key));
}