CGI::Sessions are independent of script by default. So you should be able to do just that.
Just don't forget to persist the session ID on the client in some way. It can be done with a cookie, see session header()
for example. The ID and the session object will be retrieved automatically (if saved properly).
See CGI::Session new()
If called without any arguments, $dsn defaults to driver:file;serializer:default;id:md5
and CGI::Session::Driver::file
.
You can configure them to use the store and settings that you prefer.
Basic example CGI script using sessions:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # show errors in browser
use CGI::Session;
# new query object
my $q = CGI->new();
# new session object, will get session ID from the query object
# will restore any existing session with the session ID in the query object
my $s = CGI::Session->new($q);
# print the HTTP header and set the session ID cookie
print $s->header();
# print some info
print "<pre>\n";
print "Hello!\n\n";
printf "Your session ID is: %s\n", $s->id;
printf "This sessin is: %s\n", $s->is_new ? 'NEW': 'old';
printf "Stored session 'test' value: '%s'\n", $q->escapeHTML($s->param('test'));
printf "CGI Params: %s\n", join ', ', $q->param;
# handle the form submit
if(defined $q->param('save')){
# save param value in the session
$s->param('test', $q->param('test'));
printf "Set session value: '%s'\n", $q->escapeHTML($s->param('test'));
}
elsif(defined $q->param('delete')){
# delete session
$s->delete;
print "Session will be deleted.\n";
}
print "\n</pre>\n";
# simple HTML form
printf <<'_HTML_', $q->escapeHTML($s->param('test'));
<hr/>
<form>
Session value "test": <input type="text" value="%s" name="test"/>
<button type="submit" name="save">Save Value</button>
<button type="submit" name="delete">Delete session</button>
</form>
_HTML_
# eof (linebreak needed after _HTML_)