I am seeking wisdom on how one would go about defining global variables for a cgi application that get their values from a database. These variables will change periodically (whenever the database values change) and they are used throughout an entire application.

The problem I am running into is that if I wait until a run mode to grab the data from the db then I must declare the global variables without a value at the top of the program. The data will usually be there for the first run mode but by the time we goto the second (or a different) run mode, then the data is blanked out again because of the global declarations with no value.

I have been able to call a seperate function below the setup function and manually define these global variables here, but I have been unsuccessful on calling it up with $self to attach to the db handle. If I could get this to work then it would probably be golden.

Thanks for any advice/help.

minimal code sample:
############################ our variable1 = 2003; our variable2 = 2004; sub setup{ my $self=shift; $self->start_mode('start'); $self->mode_param('rm'); my $dbh=getDbConnection("universe"); $self->param('dbh'=>$dbh); $self->run_modes( start=>\&showstart, search=>\&genSearchPage, show_type_1=>\&genShow1, show_type_2=>\&genShow2, calculate=>\&genCalculator, iamge=>\&generateImage ) } sub showstart { use variable1 here; use variable2 here; } sub genSearchPage { use variable1 here; use variable2 here; } sub genShow1 { use variable1 here; use variable2 here; } ... etc. etc.
#########################

As you can see, right now the variable1 and variable2 are hard coded, but I want to be able to get their values from a database and then have all the subs be able to access those variables with the values from the database. Problem is that I dont connect to the database until I am in setup, so unless I manually connect again to the database at the top, I cant get a connection to the db before I run setup.

Edited by footpad, 19 December 2003: Revised HTML formatting and added <code> tags.


In reply to 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.