Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cgi and perl database interaction

3 views
Skip to first unread message

Pawel Predki

unread,
Dec 1, 2009, 6:52:38 PM12/1/09
to beginn...@perl.org
Hello,
I have a website that uses a php engine for news generation and,
basically, most of the other pages. It uses a MySQL database to store
the majority of the page contents (i.e. news).

However, I've written before that I've started using simple CGI scripts
in Perl to make some activities automatic (i.e. statistics updates,
standings updates - it's a sports-related website :) ).

At first, I used the Storable module and kept all the data in flat files
but it generally is not the best solution so I moved to DBI.

Now, the thing is that the PHP scripts also connect to the database and,
presumably, uphold the connection over the duration of the session so as
not to disconnect and reconnect continually when the user browses the
website.

My question is - is it possible to do the same thing with those CGI
scripts? At the moment, each script 'requires' a module where a function
is defined which returns a database handle upon connecting to the
database. This is not an efficient solution and I would like to change
that. There is no mod_perl running on the server but maybe there is a
way to keep the connection via some Apache mechanisms. I'm not
experienced with the server operation that much so forgive me if what I
wrote is hogwash ;)
Cheers,
Pawl

Rene Schickbauer

unread,
Dec 1, 2009, 7:12:02 PM12/1/09
to beginn...@perl.org
Hi!

> Now, the thing is that the PHP scripts also connect to the database and,
> presumably, uphold the connection over the duration of the session so as
> not to disconnect and reconnect continually when the user browses the
> website.
>
> My question is - is it possible to do the same thing with those CGI
> scripts?

CGI scripts are basically scripts executed at the commandline with a few
parameters and environment variables set (each call normally starts a new
instance).

You could look into the various web frameworks if and how they solved the
problem. If you can't install anything on the server besides CGI scripts,
you're probably out of luck, though.

If you CAN install additional software on the server, mod_perl could be the way
to go. Or run your own small perl-based server for those pages on another port,
HTTP::Server::Simple::CGI (or my Maplat-Framework) might fit your profile - you
may even run them as backend only on localhost and use a PHP script as simple
proxy 8-)

It really depends on what you're planing in the long run.

LG
Rene

--
#!/usr/bin/perl #99BoB (C)2004 cavac:prg count drink vessel place act1 act2
@a=@ARGV;$c=$a[0]||99;$b=" ".($a[2]||"bottles")." of ".($a[1]||"beer");$w=" ".
($a[3]||"on the wall").",";do{print"$c$b$w\n$c$b,\n".($a[4]||"take one down").
", ".($a[5]||"pass it around").",\n".--$c."$b$w\n\n";}while($c);print"END!\n";

Pawel Predki

unread,
Dec 1, 2009, 8:12:06 PM12/1/09
to beginn...@perl.org
Hi!
Thanks for the quick reply. I know it would be much simpler if I just
installed mod_perl and some kind of a framework (I've started learning
Mason and I think I would be able to do what I want using this solution
) and rewrote the whole webpage but that's too much work and it's really
not necessary :) The site is not big, we don't have much traffic so such
inefficiencies don't cost us much. I just wanted to know if there was a
straightforward solution to this problem and I figured there could be
problems with such injection of Perl into a php-dominated application :)

I will look into the HTTP::Server::Simple::CGI and your framework but
I'm not sure I'm knowledgeable enough to deal with that :) I mean I
don't really get the idea of a phps being only proxies. At the moment a
sample php file where I include my own cgi script looks like this:

<?php

include 'maincore.php';
include 'header.php';

echo file_get_contents('some/location/mycgiscript.cgi');

include 'footer.php';
?>

However, there are also a lot of files where there is much more php code
and no perl code as well as small panels written in pure perl :) How
would the distinction of what serves which pages work?

I know that, in the long run, such distinction and such way of doing
things makes little sense but, like I said, I don't want to restructure
the whole site (I want to keep the php engine which is rather useful and
user-friendly) but I want to use my CGI scripts in an efficient way. To
be honest, the most efficient way would be to rewrite those scripts in
php which shouldn't be too big of a problem but I'm really interested in
Perl right now and that's where I stand :)


Rene Schickbauer pisze:

Greg Jetter

unread,
Dec 1, 2009, 9:43:12 PM12/1/09
to beginn...@perl.org

There is absolutely no reason to keep a connection to the database active
once the query has finished and the results are fetched and processed , doing
so only ties up system resources and memory. The default for Mysql in a
un- altered server install is 50 concurrent connections. A web site can very
easily surpass this if the connections are kept alive. The behavior of the
DBI module returning a handle that you interact with is the most efficient use
of resources . And I'm pretty sure if you research it you will find that PHP
does the same thing as PHP is modled after Perl on many levels. Once the
object is created it persist for the duration of the script or until it is
destroyed with a call to disconnect. Or you risk getting "unable to connect ,
too many connections" from your MySql server. and if you have not
programmed to handle the error , you will get silent failure with the
page just hanging up never completing a read or write.

good luck

Greg


Pawel Predki

unread,
Dec 2, 2009, 4:53:19 AM12/2/09
to Rene Schickbauer, beginn...@perl.org
Thanks, I'll see if I'm able to use that information.

Rene Schickbauer pisze:


> Paweł Prędki wrote:
>
>> I will look into the HTTP::Server::Simple::CGI and your framework but
>> I'm not sure I'm knowledgeable enough to deal with that :) I mean I
>> don't really get the idea of a phps being only proxies. At the moment
>> a sample php file where I include my own cgi script looks like this:
>

> HTTP::Server::Simple::CGI is rather simple, really. You could basically
> put in your CGI scripts with a few simple modifications.
>
> Maplat is based on that module, too. Although it has more a bunch more
> features, it still shares the same simple-to-use principles, except you
> most likely use the included Template Toolkit module to render the pages.


>
>> However, there are also a lot of files where there is much more php
>> code and no perl code as well as small panels written in pure perl :)
>> How would the distinction of what serves which pages work?
>

> The perl scripts are in a separate directory, so you could use
> mod_rewrite or similar.
>
> You can also use a single PHP script to proxy multiple perl pages, if
> you use links like that:
>
> http://yourserver/proxy.php?get=nameofscript
>
> LG
> rene
>

Pawel Predki

unread,
Dec 2, 2009, 5:01:29 AM12/2/09
to Greg Jetter, beginn...@perl.org

Greg Jetter pisze:

If that's the case then there is no problem. I thought that the database
connection persists until the user navigates away from the site (there
is some kind of an inactivity timeout). Taking into consideration that
there are many sql queries from one single page consisting of many
separate php 'modules' I would assume that each of the separate files
doesn't connect to the db on its own but rather uses one connection.

I guess that it is possible for the first include in each php file to
create a database connection which is then visible to all the submodules
included in the same file. I need to look into the php mechanism more
closely.

Thanks for your input.
Pawel

0 new messages