##
#!perl
use strict;
use CGI;
use CGI::Session;
use Exception::Class::TryCatch;
$| = 1;
use lib './REJBlib';
use pkgerr;
# NB: Iff all works right, he debug screen ought never be seen!
use CGI::Carp::DebugScreen (
engine => 'TT',
debug => 1,
lines => 5,
modules => 1,
environment => 1,
raw_error => 1,
overload => 1,
);
my $query = new CGI;
my $url = $query->url(-path_info=>1);
my $cnt = $query->param('cnt');
my $session;
if (defined $cnt) {
$cnt += 1;
$session = CGI::Session->load('driver:mysql',$query,{ DataSource => 'dbi:mysql:profitorius',
User => 'rejbyers',
Password => 'Didr39Qcab' });
my $extest = $session->is_expired;
my $emptest = $session->is_empty;
if ($extest) {
print $query->header;
print $query->start_html(-title=>'Secrets of SESSIONS',);
print $query->end_html;
} else {
print $session->header;
my $pref = $session->dataref;
my %ph = %$pref;
print $query->start_html(-title=>'Secrets of SESSIONS',);
print "The session has not expired.
";
if ($emptest) {
print "The session is empty.
";
} else {
print "The session is NOT empty.
";
print "The following are the known session parameters.
Parameter Value ";
foreach my $k (keys %ph) {
print "$k $ph{$k} ";
}
print "
";
print "Click me";
}
print "";
print "
";
print $query->end_html;
}
} else {
$session = CGI::Session->new('driver:mysql',$query,{ DataSource => 'dbi:mysql:profitorius',
User => 'rejbyers',
Password => 'Didr39Qcab' });
$session->expire("5m");
$session->param('qwerty',12345);
$session->param('zxcvb',98765);
print $session->header;
print $query->start_html(-title=>'Secrets of SESSIONS',);
print "Click me";
print "";
my $tobj = pkgerr->new(3,5);
print "Checking if package was properly created:\n\tuid = ",$tobj->uid,"\n\tut = ",$tobj->ut,"\n\n";
print "\nCalling check function.\n";
$tobj->check;
print "Calling a good function\n\t";
$tobj->good_func;
print "\nCalling a bad function within a try/catch block\n";
try eval {
$tobj->bad_func;
};
if ( catch my $err) {
print "We caught a fatal error from the calling code\n\t",$err->error,"\n\n";
}
print "";
print $query->end_html;
}
####
The session has not expired.
The session is empty.