Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How to organize a CGI project?

by weini (Friar)
on Jul 15, 2002 at 12:56 UTC ( #181758=perlmeditation: print w/replies, xml ) Need Help??

Dear fellow monks,

this one is language-independent and therefore quite offtopic, but I hope to get some input though ...

I am starting my first CGI project and want to build an application using LAMP. I've read a lot in books and The Monastery and learned how to get things working. Of course I will use strict, CGI and DBI and I plan to write the whole thing the literate programming style.

What I am wondering about is how to get the different functions, web-forms etc. organized. It's no big application at all, but I'd like it to be well done ... after thinking and sleeping over it, I would start building these files:

    "Home" of the application with links to the different other scripts
    Functions used by all other scripts: print page_header (whith navigation bar, window-title and app.-logo), print page_foot, connect to database, define colors and what not)
    Implementing the functions for adding, modifying and deleting patients)
    Functions for running tests on the patients (run test, print result, search for old test ...)
  • and so on ...

Would this be a good start or are these too much files for a relatively small thingy?
Is building a module with standard functions the right way (

I appreciate any wisdom, comments, hints - thank you!


Replies are listed 'Best First'.
Re: How to organize a CGI project?
by boo_radley (Parson) on Jul 15, 2002 at 13:51 UTC

    Get a pad of big sized post-it notes and start doing some preliminary design before you even think about "we'll need common routines for these things", "" or "that.php" or any other technological concern.
    Sit down with another developer and start hashing things out : if you'll need a login screen write "login" on a postit, and tack it to the wall. If you need a way for users to query test data, put "search for old tests" on an postit, and tack it to the wall. As you and your colleague free associate, you'll change the association of the notes ("The query page should come after the login, and maybe we should have a description of how the query page works, and some links describing some basic queries on that page...") -- this is why I like using postit notes, you can shuffle them around as needed and keep things neater than a paper sketch or a whiteboard.

    After some time you'll have a good collection of postits that should give you a rough idea of how your project design should look. Take each postit and start fleshing out the design for that particular item, what ui elements need to be present, business logic, and so on, and you'll have a good start on a solid design.

Re: How to organize a CGI project?
by Aristotle (Chancellor) on Jul 15, 2002 at 13:38 UTC
    Functions used by all other scripts: print page_header (whith navigation bar, window-title and app.-logo), print page_foot, connect to database, define colors and what not)

    With regards to the printing functions, that sounds like what you actually want to do is have a look at the various templating modules, such as the shooting star Template Toolkit II.

    Update: perrin wrote a very good article for on Choosing a templating system.

    Makeshifts last the longest.

Re: How to organize a CGI project?
by rattusillegitimus (Friar) on Jul 15, 2002 at 13:44 UTC

    Check out CGI::Application. For the web application I've been designing, its use of a single instance CGI script passing "run-modes" to the module has really helped me organize the application into logical sets of functions for each page. The documentation is pretty straightforward, IMNSHO, as well.


    He seemed like such a nice guy to his neighbors / Kept to himself and never bothered them with favors
    - Jefferson Airplane, "Assassin"

    update: whoops, left out a word

Re: How to organize a CGI project?
by Fastolfe (Vicar) on Jul 15, 2002 at 18:19 UTC
    For larger web projects, sometimes it helps to create a set of Unix-ish directories and build from there:
    $app/bin your main script and/or associated utilities $app/etc configuration files, persistent state $app/lib all of your modules or 3rd-party modules you need to bundle $app/var any misc. state or files you create/use at run-time (if any) $app/doc documentation (though a README or INSTALL would be nice in $app/) $app/htdocs the web 'root' of your application, attached to your web site using server configuration
    Keep in mind that though it's a common practice to stick all of your CGI scripts in a /cgi-bin/ directory, it's not necessarily the best practice. Most web servers will let you drop a script.cgi directly into your document root, (and if you're extension-conscious, Apache's MultiViews or another content negotiation mechanism could allow you to link to it just as "script"), which could help with URI UI issues.

    Sometimes this is overkill (and it sounds like it might be for this particular project), but this is useful for keeping stuff organized and allows a project to be completely self-contained and portable.

Re: How to organize a CGI project?
by thraxil (Prior) on Jul 15, 2002 at 13:47 UTC

    it sounds like you're off to a pretty good start. for small to medium sized projects, that's about how i usually set it up, with a core module and a bunch of smaller scripts. at the very least, try to keep any sql out of the smaller scripts. as the project gets bigger, you can refactor by identifying important classes and putting them in their own seperate modules.

    be careful about putting too much (any) display logic in that core module though. if you make good use of a templating engine like HTML::Template instead of having functions like 'navigation_bar','print_page_header', etc. it should be pretty easy. HTML::Template, and DBI also go quite well together.

    and if you're doing testing, stick with the convention and have a subdirectory 't' with your test scripts in it and use Test::Harness to bring them together.

    good luck with the literate programming. i haven't really found a satisfactory way to do it yet with perl. pod just doesn't cut it for literate programming.

    anders pearson

Re: How to organize a CGI project?
by Abstraction (Friar) on Jul 15, 2002 at 13:55 UTC
    Some wisdom(hopefully) from my experience.

    Keeping functions that are common to more than one file in a seprate module is a good idea.

    What I like to do is to keep all business logic in one file and all display code in another. This gives me a 3 tier model: data, logic, and presentation. While I find this helpful you may not. Use whatever works best in your situation.

    You didn't mention it in your post but I would recommend using a templating system, such as HTML::Template. There are several other templating systems available so take a look at a few of them before deciding which one will work best for this project.

    Hopefully this will help out a little.

Re: How to organize a CGI project?
by weini (Friar) on Jul 18, 2002 at 05:34 UTC
    Hi all,
    a big THANK YOU for all those good hints. I spent the last days downloading the modules and reading the docu. You really help me to have a good start.
    Thanx again!


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2017-04-29 14:37 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (532 votes). Check out past polls.