The full warning message on our system (Solaris, Apache 1.3.3, mod_perl
1.21?, perl 5.005_02) is:

[Tue Jun 13 17:37:25 2000] null: Use of uninitialized value at
/usr/local/lib/perl5/5.00502/CGI/Cookie.pm line 70.

We are porting perl/cgi scripts to run under Apache;:Registry and in the
process of porting I am getting the warning above from one of the scripts.
In investigating the problem I went to
http://perl.apache.org/guide/porting.html to look for examples of cookie use
with mod_perl.  I found that with slight modification to one of the samples
I could re-produce the problem.  The modification involved moving the
initialization of a CGI object reference out of a subroutine.  Initializing
our $q = new CGI; in a subroutine would mean re-arranging a fair amount of
code in our scripts.  Has anyone hit this before? Does anyone have a
suggestion for getting around the problem?  The code that reproduces the
problem follows.

Ronald Schmidt

PS I am a mod_perl newbie.  Go easy.  Newbies are people too.

  use strict;
  use CGI;
  use CGI::Cookie;
  use vars qw($q $switch $status $sessionID);
  use diagnostics;
  
    $q = new CGI; # this seems to trigger the problem

  init();
  print_header();
  print_status();
  
  ### <-- subroutines --> ###
  
  # the init code
  ###########
  sub init{
#    $q = new CGI; # move this up to exhibit bug
    
    $switch = $q->param("switch") ? 1 : 0;
    
      # try to retrieve the session ID
              # fetch existing cookies
    my %cookies = CGI::Cookie->fetch;
    $sessionID = exists $cookies{'sessionID'} 
       ? $cookies{'sessionID'}->value : '';
    
      # 0 = not running, 1 = running
    $status = $sessionID ? 1 : 0;
    
      # switch status if asked to
    $status = ($status+1) % 2 if $switch;
    
    if ($status){
        # preserve sessionID if exists or create a new one
      $sessionID ||= generate_sessionID() if $status;
    } else {
        # delete the sessionID
      $sessionID = '';
    }
    
  } # end of sub init
  
  #################
  sub print_header{
      # prepare a cooke
    my $c = CGI::Cookie->new
      (-name    => 'sessionID',
       -value   => $sessionID,
       -expires => '+1h');
    
    print $q->header
      (-type   => 'text/html',
       -cookie => $c);
  
  } # end of sub print_header
  
  

Reply via email to