Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

CGI script organisation

by LukeyBoy (Friar)
on Jan 27, 2002 at 10:40 UTC ( #141885=perlquestion: print w/replies, xml ) Need Help??
LukeyBoy has asked for the wisdom of the Perl Monks concerning the following question:

So I've coded a bunch of standalone scripts for websites before, but now I'm working on a project where I want to use modules to bundle a bunch of common functions (yay). So in my CGI directory I stick all the Perl .pl/.cgi files, but where should I put the .pm files? I obviously don't want them in the same directory since then the user could see them if they typed in the right URL (I think). What I want to do is lay it out like so:

   +--- cgi-bin - contains all the scripts
   +--- modules - contains my module files

But I can't figure out how to get the use operator to take a relative path (such as ../). I tried to append -I../modules in the magic Perl line at the top of the scripts, but that still bombs with Perl unable to find the mods. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: CGI script organisation
by grep (Monsignor) on Jan 27, 2002 at 11:02 UTC
    I set up my directory structure like this.
    /mysite /lib #contains modules /config #config information differing prod from dev /www #HTML,flash,... /cgi-bin #obvious /images #obvious
    This stucture makes it easy to move from dev to test to production enviroments by setting up enviroment specific files in config. The sensitive information in /lib and /config are also in the same level as the website root so it is not accessable from the webserver (i.e. the webserver does not even know about them),but you can access them by relative paths in your scripts. The more information you keep inaccessable the better. There is no need for a vistor to be able to know anything about database structure, what DBI driver you are using, or if you mistakingly rolled your own query string parser :).

    Then the fun part is to get your server locked down so that they can not get to see those files by other means.

    grep> cd pub
    grep> more beer
Re: CGI script organisation
by dws (Chancellor) on Jan 27, 2002 at 10:47 UTC
    But I can't figure out how to get the use operator to take a relative path (such as ../).

    Use an absolute path. Something like   use lib qw(/home/LukeBoy/modules);

      But if the module gets relocated (which it surely will when I release it) that'll mess everything up. Who knows where other users will install the stuff?
        Why not create a modules subdirectory below your app so that you can do this.
        use mod_dir::module1; use mod_dir::module2; module1->foo(); module2->foo2();


        "The doktor is in."

        But if the module gets relocated (which it surely will when I release it) that'll mess everything up.

        Then your install procedure will need to plug in a correct path.

Re: CGI script organisation
by tradez (Pilgrim) on Jan 27, 2002 at 12:54 UTC
    If you set your @INC correctly, you should be able to put your modules in any directory inside of it. Do this for me:
    someuser@somebox:~$ perl foreach $entry (@INC) { print "$entry \n"; } /usr/lib/perl5/5.6.1/i686-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i686-linux /usr/lib/perl5/site_perl/5.6.1 /usr/lib/perl5/site_perl .
    This is a list of easy to use places you should be able to stick a .pm file. For kicks lets use
    /usr/lib/perl5/site_perl/5.6.1/i686-linux Place your files in that directory. Then withing your perl file simply place
    #!/usr/bin/perl -T use whatever; #that pre .pm name of the file you used
    This will then scope your functions that you need to your current namespace. Have fun. Also read up on packaging to add another layer of power :). BTW mediators, if I am off a little on my logic, take it easy on me, is late :).
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
Re: CGI script organisation
by particle (Vicar) on Jan 27, 2002 at 11:04 UTC
    i don't understand. are you saying any http user can browse the directory where your scripts are?

    yuck. double yuck. this sounds like trouble. grep's post above has some good advice.


      Whoah, I overlooked the fact the anything under the CGI directory isn't browseable at all by default (at least with my Apache config). Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://141885]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-07-18 14:54 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (393 votes). Check out past polls.