Great Question.

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


In reply to Re: CGI Application and global variables (from a database) by freddo411
in thread CGI Application and global variables (from a database) by tmaciak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.