$session as a global variable, mod_perl

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,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Could you tie it to a file?
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

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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.