SpeedyCGI, clearing certain variables

Discussion in 'Perl Misc' started by Jason Carlton, Feb 5, 2010.

  1. 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}-
    >value; }


    TIA,

    Jason
    Jason Carlton, Feb 5, 2010
    #1
    1. Advertising

  2. On Feb 4, 11:55 pm, Jason Carlton <> wrote:
    > 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}-
    >
    > >value; }

    >
    > 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.
    Jason Carlton, Feb 5, 2010
    #2
    1. Advertising

  3. On Feb 5, 2:07 am, Jason Carlton <> wrote:
    > On Feb 4, 11:55 pm, Jason Carlton <> wrote:
    >
    >
    >
    >
    >
    > > 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}-

    >
    > > >value; }

    >
    > > 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.


    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}-
    >value; }


    # and so on
    Jason Carlton, Feb 5, 2010
    #3
  4. On 2010-02-05 04:55, Jason Carlton <> wrote:
    > 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
    Peter J. Holzer, Feb 5, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    13,715
  2. Replies:
    0
    Views:
    633
  3. Replies:
    2
    Views:
    500
    bruce barker
    Mar 25, 2008
  4. SAN CAZIANO
    Replies:
    8
    Views:
    160
    Dr John Stockton
    Oct 15, 2004
  5. speedyCGI + boa

    , Nov 9, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    98
    A. Sinan Unur
    Nov 9, 2005
Loading...

Share This Page