in reply to CGI Application and global variables (from a database)
First off, you'll probably be lynched for using globals, but they are quite handy in some circumstances. I use them. I read mine out of a configuration file, but getting them out of a DB is conceptually the same.
In my case I'm using CGI:App with CGI's so keep in mind this is executed for each 'hit' from the browser. If I was in a high volume environment, I'd be looking at mod_perl. Anyway, I took the following approach to solve the same problem:
I declared a base class that extended CGI::App
package MyCGIApp; use CGI::Application::ValidateRM; use strict; use vars qw( $VERSION); $VERSION = 1.00; use base qw(CGI::Application); sub cgiapp_init { my $self = shift; ####################### package G; use Config::General; use vars qw( %CF ); %G::CF = ParseConfig("../mymodules/config.dat"); ... more stuff would be here (perhaps connecting to the DB) ... } # end MyCGIApp.pm
Meanwhile in another file / perl module I extend from MyCGIApp, inheriting stuff including the variables. I use the package qualified name of the "global" variable (conviently called "G") so that strict won't complain.
#!/usr/local/bin/perl package CDNR; use base MyCGIApp; # Extending the class ... runmodes, etc, etc, ... in one of them: $t->param( 'Header' => "My Header", 'HeaderText' => "$G::CF{headertext}", );
In my example above, I've put all my globals into a hash, but you could use a scalar or two if you liked. That's it. Works like a charm.
BTW, speaking to the estimeed Monks around here; what is your opinion of my particular use of inheritence in this case?
Cheers,
-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday
|
|---|