LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl module, global variables (https://www.linuxquestions.org/questions/programming-9/perl-module-global-variables-564655/)

true_atlantis 06-26-2007 03:12 PM

Perl module, global variables
 
what is the standard convention for specifying global, static variables in a perl module? for example, i want an '$admin' variable to be available at any point of execution which will contain an email address. i was thinking of just creating a 'GLOBALS' module that you can access variables by doing

use x::GLOBALS;
my $x=new x::GLOBALS;

$x->{'admin'};


and the 'new' will set all $self->{'admin'} like vars. how is this currently accomplished in the perl world?

thanks

chrism01 06-26-2007 06:44 PM

Well, TMTOWTDI in Perl, but I do this just after my use ... declarations:

Code:

# Declare cfg pkg so we can refer to it anywhere
{
    package cfg;

    # Config file params
    %cfg::params = ();

    # Database interaction
    $cfg::dbh = '';
}

and then later access as
Code:

    if( $cfg::params{'DEBUG'} >= 1 )
    {
        print "load_ctrl()\n";
    }

or
Code:


    $sql = "blah";
    $sth = $cfg::dbh->prepare($sql);

Basically means that var is global across Perl file and always prefixed with pkg name.


All times are GMT -5. The time now is 09:37 PM.