gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have to assign the value for the variable and i have to get the value to all my cgi file. Is it possible to declare global variable in cgi file.

Thanks in advance

  • Comment on How to declare global variable in CGI file?

Replies are listed 'Best First'.
Re: How to declare global variable in CGI file?
by rlucas (Scribe) on Jun 28, 2005 at 06:38 UTC
    Wow, where to start?

    Do you want to have a central place (SPOT - Single Point of Truth) for certain variables seen by all your CGI scripts? Try putting things in a MySiteConf.pm module and having all scripts use MySiteConf.

    In some cases, having environment variables (in the implicit %ENV hash) set appropriately can suffice. This is the case if you have the same script in various locations where it should behave differently based upon some changing circumstance. Look into checking %ENV in your perl and using SetEnv and SetEnvIf in your Apache setup.

    It generally gets more complicated from there, with AppConfig and all the rest. Choose the simplest thing that fits your reasonably-anticipated needs (and is within your current level of comprehension).

Re: How to declare global variable in CGI file?
by holli (Abbot) on Jun 28, 2005 at 06:37 UTC
    You mean a single variable across multiple scripts? That is certainly not possible. If you need such thing you will have to store the value in a file or a database of some kind.


    holli, /regexed monk/
      If it is per session you may use a cookies and CGI::Session to tuck data away that is usable via all the cgi scripts that know about the session store. Else if you just want to load up default config vars in your cgi scripts you may use a require on each script that loads a config.pl file that has all of your global config vars present. If you just want to share a writable var between processes look at IPC::Shareable. Not very pretty -- but certainly doable.


      -Waswas
Re: How to declare global variable in CGI file?
by CountZero (Bishop) on Jun 28, 2005 at 10:08 UTC
    Does that variable have to be "global" on a script-basis, CGI-request basis, site-basis, server-basis, user-basis, ... ?

    All of these things are possible, but not in the same way.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law