16.1.7.1. Using one CGI program rather than many for each major application
Having one file makes things simple; there is only one file one must
edit to make changes. One doesn't need to look through multiple
files in order to find a particular section of code. Imagine you saw
a directory with multiple applications:
web_store.cgi
order.cgi
display_cart.cgi
maintain_cart.cgi
Without delving into the source code, you might pick out that
web_store.cgi is the primary application.
Furthermore, you might conclude that the program probably prints out
a front page inviting the user to shop and provides links to the
other CGI programs. You would also be able to tell which scripts have
to do with ordering, displaying, and maintaining cart information.
However, without actually going into the source code of all these CGI
scripts, it is difficult to tell how they relate to one another. For
example, can you add or delete shopping cart items from the order
page?
Instead, you can make just one CGI program:
web_store.cgi. This combined script can then
import the functionality of order forms, cart data display and
maintenance using libraries or modules.
Second, different components often need to share code. It is much
simpler for one component to access code in another component if they
are in the same file. Moving shared code into modules is certainly an
alternative that works well for applications distributed into
multiple CGI scripts. However, using modules to share common code
requires a greater degree of planning to know what code can be shared
and what code will not. A single file is more amenable to making
simple changes.
It is possible to use modules with this single CGI program approach.
In fact, you can keep file sizes small if you want by making the
primary CGI script a basic interface, or a wrapper, that routes
requests to other modules. In this scenario, you create multiple
modules that handle the different tasks. In many ways it is like
having multiple files except that all HTTP requests are directed
through a common front-end.
If you write CGI scripts that you distribute so that others may
download and install them on their own systems, then you may
certainly want to reduce the number of files in your application. In
this scenario, the focus is on making the application easy to install
and configure. People installing software care more about what the
package does than one individual tasks are handled by which
component, and it is easier for them to avoid accidentally deleting a
file they didn't realize was important if the number of files
is minimized.
The final reason you may wish to combine CGI scripts is if you are
running FastCGI. FastCGI runs a separate process for each of your CGI
scripts, so the fewer scripts you have, the fewer separate processes
are running.