SpeedyCGI, clearing certain variables

J

Jason Carlton

I have a rather lengthy CGI script that I wrote about 5 years ago, but
it's still functional. At the very beginning, though, it connects to a
database, then loads several variables. It would be nice if these
could stay persistent.

Rather than rewriting the whole thing, I'm thinking that it might be
simpler to use SpeedyCGI to just keep a connection open to these
static variables.

Is there a way for me to empty certain variables on each run?
Specifically, I'd like to undefine params and cookies. I thought that
this would work, but it didn't:

# Parse params into @contents
undef @params;
undef @contents;
@params = param;
@contents{@params} = map param($_) => @params;

# Fetch cookies, $id = $cookies{'ID'}->value;
undef %cookies;
%cookies = fetch CGI::Cookie;
foreach $key (keys %cookies) { $cookies{$key} = $cookies{$key}-

TIA,

Jason
 
J

Jason Carlton

I have a rather lengthy CGI script that I wrote about 5 years ago, but
it's still functional. At the very beginning, though, it connects to a
database, then loads several variables. It would be nice if these
could stay persistent.

Rather than rewriting the whole thing, I'm thinking that it might be
simpler to use SpeedyCGI to just keep a connection open to these
static variables.

Is there a way for me to empty certain variables on each run?
Specifically, I'd like to undefine params and cookies. I thought that
this would work, but it didn't:

# Parse params into @contents
undef @params;
undef @contents;
@params = param;
@contents{@params} = map param($_) => @params;

# Fetch cookies, $id = $cookies{'ID'}->value;
undef %cookies;
%cookies = fetch CGI::Cookie;
foreach $key (keys %cookies) { $cookies{$key} = $cookies{$key}-


TIA,

Jason

I believe that I stumbled across my own answer.

CPAN states that you can change the shebang line to:

#!/usr/bin/speedy

This seems to work fine, but when I changed it to:

#!/usr/bin/perl

use CGI::SpeedyCGI;
my $sp = CGI::SpeedyCGI->new;

The variables seemed to work the way I expected. I only had to declare
the ones I wanted to make persistent, and the others died on their
own. Ie:

my ($dbh); # clear it on first run
our ($dbh); # keep it persistent for second+ runs


If you have any other insight, I'm all ears. But so far, this seems to
be working.
 
J

Jason Carlton

I believe that I stumbled across my own answer.

CPAN states that you can change the shebang line to:

#!/usr/bin/speedy

This seems to work fine, but when I changed it to:

#!/usr/bin/perl

use CGI::SpeedyCGI;
my $sp = CGI::SpeedyCGI->new;

The variables seemed to work the way I expected. I only had to declare
the ones I wanted to make persistent, and the others died on their
own. Ie:

my ($dbh); # clear it on first run
our ($dbh); # keep it persistent for second+ runs

If you have any other insight, I'm all ears. But so far, this seems to
be working.

Well, I wish I could delete that! Turns out, I had set maxruns to 1
during the testing, so even though it looked like it was working
correctly, it wasn't. My bad.

I'm still looking for advice on how to keep some variables persistent,
but clear others. Here's what I have, and so far it's keeping them all
persistent:

#!/usr/bin/speedy

use CGI::Carp qw(fatalsToBrowser);
use CGI qw:)standard);
use CGI::Cookie;
use strict;
use DBI;

# Hoping to keep persistent
my ($dbh, $home, $basepath, @permissions, $mailprog);
our ($dbh, $home, $basepath, @permissions, $mailprog);

unless (defined($dbh)) { $dbh = getSQL(); }
unless (defined($home)) { $home = ...; }
unless (defined($basepath)) { $basepath = ...; }
unless (defined(@permissions)) { @permissions = (...); }
unless (defined($mailprog)) { $mailprog = ...; }

# Hoping to change with each run
my (@params, @contents, %cookies);

@params = param;
@contents{@params} = map param($_) => @params;

%cookies = fetch CGI::Cookie;
foreach $key (keys %cookies) { $cookies{$key} = $cookies{$key}-

# and so on
 
P

Peter J. Holzer

I have a rather lengthy CGI script that I wrote about 5 years ago, but
it's still functional. At the very beginning, though, it connects to a
database, then loads several variables. It would be nice if these
could stay persistent.

Rather than rewriting the whole thing, I'm thinking that it might be
simpler to use SpeedyCGI to just keep a connection open to these
static variables.

Is there a way for me to empty certain variables on each run?
Specifically, I'd like to undefine params and cookies. I thought that
this would work, but it didn't:

Consider using FastCGI as an alternative. A FastCGI script looks roughly
like this:

use CGI::Fast;

# initialisation code, e.g. connect to database, etc.

while(my $q = CGI::Fast->new()) {

# handle one request.

}

Since you get a new CGI::Fast object for each request, you alse get new
params and cookies. Also, all lexical variables declared in the loop are
automatically cleared, only the ones declared before the loop survive
across queries.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top