Chapter 11
Instance Parameters
Every CGI::Application needs at least two pieces a module containing a subclass
of CGI::Application and an instance script that uses that module. The example
instance script in Listing 11 2 does nothing more than call new() on the BBS class
and then run() on the returned object:
my $bbs = BBS >new();
$bbs >run();
For simple applications this is all you'll need. However, it is possible to use the
instance script to modify the behavior of the application.
One way CGI::Application provides configurability is through the TMPL_PATH
option. When your application calls the load_tmpl() method to instantiate an
HTML::Template object, the filename must be either an absolute path or relative
to the current directory. But if you specify the TMPL_PATH option to new(), you can
tell CGI::Application to look elsewhere for your templates. For example, if I wanted
to keep the templates for the BBS application in /usr/local/templates, I could
adjust the call to new() to look as follows:
my $bbs = BBS >new(TMPL_PATH => "/usr/local/templates/");
This option could be used to deploy multiple instances of the BBS module with
different designs coming from different template sets. Each instance script uses
the same module, but the constructed objects will use different templates, and as
a result the user will see a different design in his or her browser.
Aside from configuring CGI::Application, new() also includes support for
application specific parameters. Using the PARAMS option, you can specify a set of
key value pairs that can be accessed later by the application. For example, imagine
that the BBS module was written to store its messages in a Berkley DB file accessed
using the DB_File module. Each instance of the BBS will need its own file to store
messages. This could be provided using the PARAMS option to new():
my $bbs = BBS >new(PARAMS => { message_db => "/tmp/bbs1.db" });
Now in the BBS code this value can be accessed using the param() method on
the CGI::Application object:
use DB_File;
sub list {
my $self = shift;
my $filename = $self >param( message_db ); # get the configured db filename
tie %db, "DB_File", $filename; # access it with DB_File
# ...
}
268
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved