I'm trying to learn CGI::Session for another project, so I re-wrote the "simple example" script from the CGI.pm documentation to use sessions and HTML::Template.

Working on the "simple example" has me wondering if this is not so simple? It gets a little confusing working with the CGI object, the Session object, and the Templates, and then trying to keep straight in your head how the client is going to interact with the script.

One suprise was that I had to write what seemed to be a lot of extra code just to unset values in the session when they were no longer needed.

Another is that my session doesn't exactly seem to clear correctly.

There is an html template that goes along with this where I just have a bunch of <TMPL_IF> statements looking for which checkbox to select, etc.

I am hoping someone with more experience can point me in the right direction here? I'm not sure if I'm going about this the right way- and I need the concepts clear in my head before I go on to the actual work.

#!/usr/bin/perl -T ## # Supposed to be simple cgi example # with sessions and templates. ## use strict; use warnings; use diagnostics; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; use HTML::Template; use CGI::Session; use DBI; use Data::Dumper; my $DEBUG = 1; my $TMPL_ROOT = '/htdocs/tmpl'; my $cgi = CGI->new(); my $dbh = DBI->connect( 'dbi:mysql:sessions', 'dbuser', 'dbpass', { 'RaiseError' => '1', 'PrintError' => '1', 'AutoCommit' => '1' }) or die $DBI::errstr; my $sess = CGI::Session->new("driver:mysql;serializer:storable", $cgi, {'Handle'=> $dbh }) or die( CGI::Session->errstr ); print $sess->header, $cgi->start_html(-title=>"$0"); if ($cgi->param('.reset')) { $sess->clear(); print $cgi->h3('A session has been cleared'); } if ($sess->param()) { print $cgi->h3('A session has been loaded'); } else { print $cgi->h3('This is a new empty session'); } # checkbox group if (my @checks = $cgi->param('words')) { my $cxhist = ( $sess->param("words.cxhist") or {} ); my %cx = map { $_ => 1 } @checks; foreach (keys %cx) { # maintain a list of boxes I check $cxhist->{"$_"}++; # store a bool for html template $sess->param(-name=>"words.$_", -value=>1); } foreach (keys %{$cxhist} ) { # look for unchecked boxes to clear if (! $cx{$_}) { $sess->param(-name=>"words.$_", -value=>0); } } # maintain history of checkboxes $sess->param(-name=>"words.cxhist", -value=>$cxhist ); } # select if (my $selected = $cgi->param('color')) { # save selected color in the session my $selhist = ( $sess->param("color.selhist") or {} ); $selhist->{$selected}++; $sess->param(-name=>"color.$selected", -value=>1); # clear old unselected colors out of the session foreach (keys %{$selhist}) { if ( $_ ne $selected ) { $sess->param(-name=>"color.$_", -value=>0); } } # maintain history of selections $sess->param(-name=>"color.selhist", -value=>$selhist ); } # Save everything for next request $sess->save_param(); # print out page loading stuff from session or cgi my $tmpl = HTML::Template->new(filename=>"$TMPL_ROOT/test.tmpl", associate=>$sess); # debug option to show session $DEBUG && $tmpl->param('session.dump', Dumper($sess)); print $tmpl->output(); print $cgi->end_html;

In reply to CGI::Session Simple Example by mull

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.