$session as a global variable, mod_perl

Posted on 2004-11-08
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 file and a bunch of pl files which have my subroutines in them.

The file has
require ‘’;
require ‘’;
require ‘’;

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 84

    Expert Comment

    Could you tie it to a file?
    LVL 7

    Expert Comment

    try this:

    in create a function, say init(), inside it assign $cgi=new CGI, and of course include a call oft init in 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

    I had good results using Class::WeakSingleton.

    See 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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    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…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now