wertert has asked for the wisdom of the Perl Monks concerning the following question:

I've been working on some very strange behaviour with a very basic perl cgi script for a few days and something very strange is happening.
#!C:/perl/bin/perl -- use strict; use CGI qw/ :standard /; use CGI::Cookie; use CGI::Carp qw/ fatalsToBrowser /; use CGI::Session; my $q = new CGI; my $session = new CGI::Session("driver:File", $q->cookie('CGISESSID') || $q->param('CGISESSID') || und +ef, { Directory => 'C:/temp/test/area' } ) or die ($CGI::Session::errstr); my $session_id = $session->id(); print $session->header; print $q->start_html(-title=>"www\.test\.com");#, $q->Dump, $q->query_ +string; my $in=$session->param('loggedin'); print "in = ", ((defined $in) ? $in : "UNDEFINED"); my $count = $session->param("loggedin") || "1"; $session->param( "loggedin", ($count+1) ); print $q->end_html; sub not_called { my($count); print "hello - i am never called !"; #$count=$session->param( "loggedin"); <<<<< PROBLEM }
The basic script simply sets up a cookie and then simply increments a counter for the duration of the session.

The line marked as '<<<<< PROBLEM' is giving me the problem. It's in a function which is never called. When the line is commented out the script works ok. When the line is uncommented I get a problem. The problem seems to be that CGI::session is no longer interested in saving anything to disk ( ie in the /temp/test/area area ). There is no session management occuring so session->id is different every time. Comment out the line and everything works well. I can't say how weird this is !!!! why a line which is never called could affect the program is beyond me.

Perl is v5.8.6 on windows. Web Server is Apache.

I'm really stuck here and this is the weirdest problem I've seen in all my days of using perl !

PS - just so we're clear the '<<<<< PROBLEM' label was added after the code was pasted to perlmonks. I am not trying to run the script with that in place ;-)

Replies are listed 'Best First'.
Re: very strange problem with CGI::Session
by imp (Priest) on Aug 08, 2006 at 11:59 UTC
    One potential problem is that $session is used inside not_called(), but it is not passed into that function - meaning it is using the one from the outer scope.

    If you are running in a mod_perl environment and using Apache::Registry this would cause a problem with accidental closures.

    Basically the not_called becomes an inner sub which holds a reference to $session, and that reference persists for the lifetime of that apache instance. CGI::Session automatically flushes its content to disk (or db, depending on config) when DESTROY is called - which in this case would never happen.

Re: very strange problem with CGI::Session
by dtr (Scribe) on Aug 08, 2006 at 10:00 UTC

    Can you please try to run "ethereal" to look at the data that is being sent from your browser to the server, and then look at the data that the server sends back?

    There's no reason (as far as I can tell) that things should stop working if you uncomment that line - especially if you're sure that the line above is never printed. So, I guess that something else must be going on.

    Sorry this isn't much help!