Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here is some test code that I constructed to both test session management functions and error handling in a package used by a CGI script. First the package:

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;

And now the trivial CGI script:

#!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 "<p>The session has not expired.</p>"; if ($emptest) { print "<p>The session is empty.</p>"; } else { print "<p>The session is NOT empty.</p>"; print "<p>The following are the known session parameters.</p><ta +ble><thead><tr><th>Parameter</th><th>Value</th></tr></thead><tbaody>" +; foreach my $k (keys %ph) { print "<tr><td>$k</td><td>$ph{$k}</td></tr>"; } print "</table>"; print "<a href=\"$url?cnt=$cnt\">Click me</a>"; } print "<pre>"; print "</pre>"; 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 "<a href=\"$url?cnt=1\">Click me</a>"; print "<pre>"; my $tobj = pkgerr->new(3,5); print "Checking if package was properly created:\n\tuid = ",$tobj->u +id,"\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->er +ror,"\n\n"; } print "</pre>"; print $query->end_html; }

Now, just about everything in these scripts works as anticipated, except for the conditional block that tests whether or not an existing session is empty. NB: Given how the session is first created, it should never happen that a session that has not expired is empty (I was considering adding a random variate, as a CGI parameter, that empties the session of parameters say, 20% of the time, and of course, any empty session that has not expired would, on the next access, be populated, but that part of the experiment got pre-empted by the current behaviour).

What I see happening is the session data being displayed as expected when the session has not expired, but when the session has expired, I see the following output (copied from MS IE and pasted below):

The session has not expired. The session is empty.

Why is this? Did I make a mistake on how I used is_expired (it IS, in fact, used only after a call to load)?

Obviously load did not return NULL after the session expired, as otherwise the call to is_expired should be expected to fail. But the value returned by is_expired is clearly wrong. This is with both Activestate Perl 5.12 and Activestate Perl 5.16, both running on The latest Windows build of Apache's httpd server (2.4.3?). As a work-around, I can check for an expired session by checking whether or not my session is empty (as I ALWAYS create several session parameters), but this seems like a kludge, and the problem that session's is_expired function lies remains unfixed. Any ideas?



In reply to Problems getting session management to work: is_expired seems to lie to me by ted.byers

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2016-12-04 19:52 GMT
    Find Nodes?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:

      Results (69 votes). Check out past polls.