Click here to get back home

CGI scripts and modular design?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
CGI scripts and modular design? psyshrike 09-28-2004
Posted by psyshrike on September 28, 2004, 9:41 am
Please log in for more thread options
Howdy,

I've been poking through some different shopping cart software, trying
to decide which one to use. All of them use piped filehandles or
backticks to handle modularization.

I will be modifying my store with some very custom requirements. It
will be about a dozen modules before it is complete.

My question is, Is there are reason why CGI scripts don't:

use lib "./lib" ;

and use modular code instead of backticking everything? My virtual
hosting ISP permits calling locally installed libraries. Is it common
practice for ISP's to not allow this? If I use modular design am I
hurting my portability if I decide to go with another webhosting
service?

I have the hardest darned time sorting through the bazillion scripts
in my cgi-bin. If I can't come up with a reason not to, I am turning
half of them into object libraries, and sticking them in /cgi-bin/lib

The only reason I can think of for not doing this is executable size.
If memory is quota'd this could be a problem. The quota would have to
be pretty anemic to be a factor though.

Comments? Recomendations?

Can other folks confirm that they do this too?

-Thanks in advance
-Matt


Posted by Scott W Gifford on September 28, 2004, 4:06 pm
Please log in for more thread options
shrike@cyberspace.org (psyshrike) writes:

[...]

> I have the hardest darned time sorting through the bazillion scripts
> in my cgi-bin. If I can't come up with a reason not to, I am turning
> half of them into object libraries, and sticking them in /cgi-bin/lib

Sticking them in cgi-bin/lib might make them directly executable,
depending on Web server configuration, which may not be what you want
(especially if including the file has side-effects).

I usually create a cgi-lib directory outside of the Web server's root,
and put all CGI libraries in there. That way they can't be read or
executed directly by the Web server.

I sometimes also set the PERL5LIB environment variable in Apache to
specify where my libraries are, then use this code to untaint it and
pull it in:

BEGIN {
if ($ENV and $ENV =~ /^(.*)$/)
{
# Blindly untaint. Taintchecking is to protect from Web data;
# the environment is under our control.
eval "use lib '$_';"
foreach (reverse split(/:/,$1));
}
}

That makes it easier to use the same script in different locations, by
just changing the Apache config (with .htaccess if you don't control
the entire server).

Overall, this is a very good idea. It should make your code easier to
maintain, encourage you to re-use code, and ensure that when you fix a
bug in one place it is fixed everywhere.

-----ScottG.


Posted by Matt S Trout on September 28, 2004, 6:11 pm
Please log in for more thread options
>Howdy,
>
>I've been poking through some different shopping cart software, trying
>to decide which one to use. All of them use piped filehandles or
>backticks to handle modularization.
>
>I will be modifying my store with some very custom requirements. It
>will be about a dozen modules before it is complete.
>
>My question is, Is there are reason why CGI scripts don't:
>
>use lib "./lib" ;
>
>and use modular code instead of backticking everything? My virtual
>hosting ISP permits calling locally installed libraries. Is it common
>practice for ISP's to not allow this? If I use modular design am I
>hurting my portability if I decide to go with another webhosting
>service?

Possibly, if their webserver is configured to execute scripts with an absolute
path.

>I have the hardest darned time sorting through the bazillion scripts
>in my cgi-bin. If I can't come up with a reason not to, I am turning
>half of them into object libraries, and sticking them in /cgi-bin/lib

No, definitely do this.

>Comments? Recomendations?

Try at the top of your scripts -

BEGIN {
$location = $ENV;
$location =~ s!/[^/]+$!! || $location = '.';
unshift(@INC,"$/lib");
};

That seems to cater nicely for such things - if you're called with an absolute
path it'll push <path>/lib into the include list, if not it'll add './lib'.

Also, because it does an unshift on @INC you'll get modules in your lib before
ones in the main perl tree, which means you can "upgrade" stuff by copying your
own version in, at least for pure perl modules.
--
Today's proposed addition to the PDP-11 instruction set:
EPI Execute Programmer Immediately
Today's fatal mistake:
#include <pi-syspkt.h> [ My homepage is http://www.trout.me.uk ]


Posted by John Bokma on September 28, 2004, 7:25 pm
Please log in for more thread options
shrike@cyberspace.org (psyshrike) wrote in

> Howdy,
>
> I've been poking through some different shopping cart software, trying
> to decide which one to use. All of them use piped filehandles or
> backticks to handle modularization.
>
> I will be modifying my store with some very custom requirements. It
> will be about a dozen modules before it is complete.
>
> My question is, Is there are reason why CGI scripts don't:
>
> use lib "./lib" ;

What is . ? I remember that there was (is?) no guarantee that it's the
cwd of your script. You can use FindBin to solve this problem though.

> and use modular code instead of backticking everything? My virtual
> hosting ISP permits calling locally installed libraries. Is it common
> practice for ISP's to not allow this?

Some don't give shell access.

> If I use modular design am I
> hurting my portability if I decide to go with another webhosting
> service?

There is so much choice in webhosting those days that it's not that
difficult to find one that meets your requirements.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html


Posted by Gunnar Hjalmarsson on September 28, 2004, 10:47 pm
Please log in for more thread options
John Bokma wrote:
>> My virtual hosting ISP permits calling locally installed
>> libraries. Is it common practice for ISP's to not allow this?
>
> Some don't give shell access.

Assuming we are talking about pure Perl modules, why would you need
shell access to upload them to a local library?

Possible providers who permit that you run your own CGI scripts
written in Perl, but don't 'permit' the use of own modules, are
ignorant and not worth to take into consideration.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Similar ThreadsPosted
Accomodate for poor db design using Sort::Maker? December 9, 2006, 12:48 am
The Month in Perl Software Design: Review of January 2005 February 10, 2005, 10:39 pm
something like CPAN::AutoInc for scripts June 7, 2006, 4:18 pm
New Script Directory needs your scripts listed September 3, 2005, 6:32 pm
Free CGI scripts to search sites? February 12, 2006, 10:15 am
Running compiled Inline C perl scripts on more than one machine February 9, 2006, 12:12 pm
What module is needed to allow scripts to post to HTTPS sites? August 2, 2006, 8:06 am
trying to use HTML::Mason on apache2 but scripts come up as plain text in the browser October 23, 2006, 1:50 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap