Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Sharing Global variables between scripts

by TASdvlper (Monk)
on Apr 16, 2004 at 20:49 UTC ( #345863=perlquestion: print w/ replies, xml ) Need Help??
TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I'm trying to declare global variables between scripts and am having some problems.

In a script called Dispatcher.cgi.pl I declare a package called Dispatcher (see below):

package DispatchParser; use strict; use warnings; use lib '../modules/perl'; use lib '../modules/db'; use CommonFunctions; use CGI; use DataBaseActions; use POSIX ":sys_wait_h"; use Data::Dumper; use CGI::Carp qw(fatalsToBrowser set_message); my $q = new CGI; my $username = &GetUser(); my @ValArray; our $project = $q->param('project'); our $release = $q->param('release'); our $globalpid; # defined later in script ... rest of code ...
Basically, I'm trying to expose $project, $release and $globalpid as global variable so other modules/script can use them.

But, in a script called TAZ.pl, I'm trying to use those variables and it's giving me a error message:

Undefined subroutine &DispatchParser::GetUser called at ../exe/TAZ.pl line 33.

Where the function GetUser is defined in the module CommonFunctions (see below).

I'm a little puzzled as to what is going on.

TAZ.pl snippet

use strict; use warnings; use lib '../modules/perl'; use lib '../modules/db'; use lib '../tsr'; use CommonFunctions; use POSIX ":sys_wait_h"; use Date::Manip; use Time_Diff; use LogIt; use DataBaseActions; use IOrate; use emailinfo; package DispatchParser; our ($project, $release, $globalpid); print "project = $project<br>\n"; my $username = &GetUser(); ### this is line 33
I'm guessing it has something to do with the order I include the package ?

Any help would be greatly appreciated.

I'm calling TAZ.pl from within Dispatcher.cgi.pl.

my $cmd = "/msg/spgear/tools/bin/perl ../exe/TAZ.pl \\\"$valhash{'agen +da'}\\\" now"; system("$cmd");
When TAZ.pl completes it goes back to Dispatcher.cgi.pl and then that code completes.

Comment on Sharing Global variables between scripts
Select or Download Code
Re: Sharing Global variables between scripts
by runrig (Abbot) on Apr 16, 2004 at 20:54 UTC
    Is your GetUser function exported from your CommonFunctions module to this module? Does it work if you say this?:
    my $username = CommonFunctions::GetUser();
    Update: what tilly said. The way the function would be exported, it is exported to the main package, not the DispatchUser package. The above code should work then.
      Yes, GetUser is exported from the CommonFunctions module. If I don't try to use those global variables, everything works fine.
Re: Sharing Global variables between scripts
by tilly (Archbishop) on Apr 16, 2004 at 20:58 UTC
    If CommonFunctions is using Exporter to do its export (or even if it has a custom import routine), then it will export into the package that you did the use from.

    In Dispatcher.cgi.pl your package statement preceeds the use statements so the functions wind up in package DispatchParser.

    In TAZ.pl you reversed that order so the functions wound up in package main, and then you're looking for them in package DispatchParser.

      I'm a little confused, so how do I resolve the problem (if I can) ? Thanks in advance. I don't normally use global variables so I'm a little new to this.
        You move the package declaration to the top of your script. That will remove the error message that you are getting.

        Note that this will not (as pointed out by gjb) magically cause data which exists in one process to be visible in another.

        Also note that global variables in general should be used with caution. You want to limit how many you have, and you want to get maximal value from each one.

Re: Sharing Global variables between scripts
by Fletch (Chancellor) on Apr 16, 2004 at 21:00 UTC

    Read perldoc Exporter.

    Update: Ahh, I stopped reading at the error about the unqualified sub name. If you're trying to share variables between processes you need something like IPC::Shareable.

Re: Sharing Global variables between scripts
by gjb (Vicar) on Apr 16, 2004 at 21:52 UTC

    Given the other responses I may be way off, but my impression is that you want to accomplish the following:

    1. the values of the variables $project et al. are set when Dispatcher.cgi.pl is executed;
    2. when TAZ.pl is executed, you want to use those values.
    Unfortunately this is not possible in a trivial way since once the execution of Dispatcher.cgi.pl is done, the values of all variables are gone without a trace.

    What you want to accomplish is the notion of a session, i.e. several interactions with a webserver that maintains the state between those interactions.

    The simplest way to do this is to embed the values of the variables you want to preserve in the HTML code the first script generates using hidden input fields. This is quick & dirty, hard to maintain and unsafe.

    Another way is to store those values in cookies on the client with all drawbacks associated with the use of cookies.

    Yet another is to use some session framework such as CGI::Application which is quite mature but might be intimidating at first.

    The "right thing to do" depends strongly on the type of job you've at your hands.

    Just my 2 cents, -gjb-

      thanks.

      Well, TAZ.pl is called in the middle of Dispatcher.cgi.pl. So, basically, when TAZ.pl is running so is Dispatcher. Once, TAZ.pl exits, it goes back and finishes the rest of the Dispatcher code. Now, saying that, wouldn't the variables still be around ?

        Thanks to a short CB exchange I now see what you have in mind. You're using a system call to execute TAZ.pl from within Dispatcher.cgi.pl.

        So yes, the variables still exist, but unfortunately you can't access them from within

        TAZ.pl</ocde> since both scripts are running in their own Perl interpr +eter and don't share anything.</p> <p>The best thing to do I guess is to transform <code>TAZ.pl
        into a function and call that from within Dispatcher.cgi.pl by directly including it in that file. The advantage is that you can make $project et al. parameters of that function which is much cleaner.

        You can also put that function into a module and load it with use.

        Hope this helps, -gjb-

Re: Sharing Global variables between scripts
by eXile (Priest) on Apr 17, 2004 at 00:52 UTC
    If I'm correct the main problem is sharing variables between invocations of a script or cross-script. You could use Cache::Cache to store and fetch your variables. Specifically Cache::FileCache is very useful for this.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://345863]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-10-22 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (113 votes), past polls