package pkgerr; use strict; use Exporter; use Exception::Class::TryCatch; use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw (Exporter); @EXPORT = (); @EXPORT_OK = qw(good_func bad_func uid ut); %EXPORT_TAGS = (All => [qw(&good_func &bad_func &uid &ut)]); my $class_data = { idusers => "", user_type_id => "", }; sub new { my $class = shift; my $self = {}; my $uid = shift; my $ut = shift; $self->{'idusers'} = $uid; $self->{'user_type_id'} = $ut; bless($self, $class); return $self; } sub good_func { my $self = shift; print "I'm a good function\n"; } sub bad_func { my $self = shift; die "I'm a bad function\n"; } sub uid { my $self = shift; return $self->{'idusers'}; } sub ut { my $self = shift; return $self->{'user_type_id'}; } sub check { my $self = shift; my $dummy; try eval { $dummy = uid($self); bad_func($self); }; if (catch my $err) { print "We set \$dummy to $dummy, and then invoked a bad function\n"; print $err->error,"\n"; } } 1; #### #!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.

"; foreach my $k (keys %ph) { print ""; } print "
ParameterValue
$k$ph{$k}
"; 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.