Link to home
Start Free TrialLog in
Avatar of tongueroo
tongueroo

asked on

$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’;
etc

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,

Tung
Avatar of ozo
ozo
Flag of United States of America image

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:

#!/usr/bin/perl
#require section

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

init();

# rest of your code

1;
ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial