Learn how to a build a cloud-first strategyRegister Now


$session as a global variable, mod_perl

Posted on 2004-11-08
Medium Priority
Last Modified: 2006-11-17
Im in the process of converting my cgi scripts to mod_perl compatiable so I can run it with Apache::Registry

My scripts have global variables, and I know that this is the cause of my problem.  Im using CGI::Session, and then declaring $session = new CGI::Session( undef, $cgi->cookie(‘CGISESSID’) || undef, {Directory=>$session_dir});

I have a main.pl file and a bunch of pl files which have my subroutines in them.

The main.pl file has
require ‘file1.pl’;
require ‘file2.pl’;
require ‘file3.pl’;

All the subfunctions in the pl files require the $session variable, so I have declared $session as a global variable, in every single file.
use vars qw( $cgi $session )

So the global variables are messing up as expected, because mod_perl doesn’t reload the scripts every time, so I understand that.  However, I don’t now the best way to resolve this.

I know I can drop using $cgi and $session as global variables and passing them in as parameters to all the subfunctions, but I have so many subfunctions and my code will become a mess.

Is there a better way to implement this?

Thanks in advance,

Question by:tongueroo
LVL 85

Expert Comment

ID: 12532387
Could you tie it to a file?

Expert Comment

ID: 12535311
try this:

in main.pl create a function, say init(), inside it assign $cgi=new CGI, and of course include a call oft init in main.pl. Example:

#require section

sub init{
  $cgi=new CGI;
  $session = new CGI::Session( undef, $cgi->cookie(‘CGISESSID’) || undef, {Directory=>$session_dir});'


# rest of your code

LVL 18

Accepted Solution

kandura earned 2000 total points
ID: 12553684
I had good results using Class::WeakSingleton.

See http://search.cpan.org/author/JJORE/Class-WeakSingleton-1.03/WeakSingleton.pm for details.

The crux is to make your scripts call a class method to get the session object, instead of directly relying on a global variable.
In this case, they would do something like

  my $session = My::WeakSession->instance;

This way Class::WeakSingleton will make sure it will only instantiate one object, and because it is a weak reference, it will go away after one run. Just make sure you do not use a global variable to hold the object!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question