Problem with global variables

F

Florence HENRY

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.
 
G

Gunnar Hjalmarsson

Florence said:
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 didn't quite understand why you can't import those names to the
modules where they need to be used, but an alternative way to access
them is to use the fully qualified names, i.e.
@pdsdb_var::columns_char and @pdsdb_var::columns_num.
 
F

Florence HENRY

Le vendredi 30 Avril 2004 21:37, Gunnar Hjalmarsson a déclaré:
I didn't quite understand why you can't import those names to the
modules where they need to be used, but an alternative way to access
them is to use the fully qualified names, i.e.
@pdsdb_var::columns_char and @pdsdb_var::columns_num.

Thanks ! that's exactly what I was looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top