As the CGI::Application documentation says, using this module is the inverse of using "page-based" applications such as JSP, PHP, and ASP.
Instead of dividing the CGI application into "pages", the entire application is an object, written as a module. The module is used as any Perl module, with no need to be within the web cgi-bin path. The only file needed within the web cgi-bin path is a small CGI script, which only creates an instance of our application object.
To make an analogy to a "page-based" application, CGI::Applications uses "run-modes", which correspond to subroutines, where each subroutine can be seen as a "page". So, instead of creating three scripts ("pages"), such as:
NOTE: Don't try to 'print' anything directly to output in your Perl code. You'll probably get an error similar to this:
[Mon Aug 27 18:28:10 2001] [error] [client 127.0.0.1] malformed header from script.
Bad header=2: /home/kuuse/redantigua/www/scgi-bin/anymail.pl
If you want to get quick-and-dirty debug info about any variable, just insert the following temporary line where suitable in any run mode subroutine:
which will cause your application to output only the value of $my_bogus_variable to your browser.
A more flexible way is to print the variable to STDERR:
print STDERR $my_bogus_variable;
This will not cause any problems with malformed headers etc.
If you run the script from the command line, you will see the variable value among the other output.
If you run the script from the browser, you will find the variable value in the web server error log.
To go from one page/mode to another, the parameter "rm" has to be passed as
either as a HTML form field:
or a HTML query string:
<ahref="helloworld.cgi-app.pl?rm=mode3">Go to run mode 3</a>
We can fetch form/query parameters in the same manner as with the CGI.pm module. To fetch the HTML parameter "calling_mode",
To show a real example, we will use three pages/modes in our application, generating the same output, but each one using its own technique:
1. Using strings
2. Using the CGI query object
3. Using a HTML template
Now we have enough information to create the module HelloWordCgiApp2.pm.
Run the script from the command line:
# perl -w helloworld.cgi-app.pl
(offline mode: enter name=value pairs on standard input)
(press CTRL-D after entering the value pairs)
or in your browser (don't forget to configure DNS and Apache first):
This is a very simple application using one 'CGI wrapper', one module, and one template.
When the application grows, the module grows and there may be more templates. But it is still easy to organize.
You can pass your templates to your web designer, and encourage other programmers to use the same modular design, to (hopefully) build a happier future, understanding, and better modules.
If you want to see a full-scaled CGI::Application-based program, download AnyMail.