Problem with global variables

From: Florence HENRY (raslebol_at_duspam.fr)
Date: 04/30/04


Date: Fri, 30 Apr 2004 20:22:46 +0200

Hello,

I do not manage to propagate my global variables through all the subroutines
of my program.

I define some global variables using a module :

### pdsdb_var.pm
package pdsdb_var;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(@columns_char @columns_num);
@columns_char = (1, 2, 3, 4, 5);
@columns_num = ("blah", "blah", "blah");
1;
__END__
### end of pdsdb_var.pm

And I use them in the main program :

#!/usr/bin/perl
use pdsdb_var qw(@columns_char @columns_num);
print @columns_char . "\n";
print @columns_num . "\n";
### end of main

Until here, it works nice. My problem is that I cannot use those global
variables from a subroutine that is defined in another package :

### pdsdb_sub.pm
package pdsdb_sub;
sub main::PrintVar {
        print @columns_char . "\n";
        print @columns_num . "\n";
        return;
}
1;
__END__
### end of pdsdb_sub.pm

If I use this function from my main program :

#!/usr/bin/perl
use pdsdb_var qw(@columns_char @columns_num);
use pdsdb_sub;
print @columns_char . "\n";
print @columns_num . "\n";
main::PrintVar();
### end of main

the program prints :
5
3
0
0

which means that my global variables are not defined within pdsdb_sub.pm

How could I manage to propagate my global variables within all my
subroutines ?

I precise that I cannot write :

        use pdsdb_var qw(@columns_char @columns_num);

within pdsdb_sub.pm because my real problem is a little more complicated
than that :

* I have several programs, and some of them share the same data (same name,
same content), and some just share the data type (same name but content
differs), so I defined several modules pdsdb_var1.pm, pdsdb_var2.pm, ...
which I include where needed.

* All the programs use the same functions defined in
pdsdb_sub.pm (so I have to use pdsdb_sub in each one). These functions are
supposed to use the variables defined in pdsdb_varX.pm, which content
differs following the main calling program.

Thanks for any help.

-- 
Florence Henry
florence point henry arobasse obspm point fr


Relevant Pages

  • Re: Problem with global variables
    ... Florence HENRY wrote: ... > How could I manage to propagate my global variables within all my ...
    (comp.lang.perl.misc)
  • Re: passing global variable by reference?
    ... > I'm trying to stick my frequently used subroutines into a Sysadmin.pm ... Of course the first one I tried uses global variables and I ... sub do_something { ... Set variables that you'll need to keep between calls .. ...
    (comp.lang.perl.modules)
  • Re: Global variable is not so global
    ... Lisp isn't something that landed on earth from outer space. ... int main ... This is good because the use of global variables is bad design anyway. ... funtions, only subroutines. ...
    (comp.lang.lisp)
  • Re: Global variable is not so global
    ... Lisp isn't something that landed on earth from outer space. ... > int main ... > This is good because the use of global variables is bad design anyway. ... > funtions, only subroutines. ...
    (comp.lang.lisp)
  • Re: [SPARK] Code safety and information hiding
    ... the documentation:). ... This can be achieved by the environment task executing the initialization code for the package during elaboration, or by an initialization task in the package body. ... design was better than the global variable it replaces. ... The customers of the SW designed around large numbers of undocumented global variables seemed satisfied with it. ...
    (comp.lang.ada)