Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Good global variable scheme? 1

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
0
0
US
I have a dozen Perl scripts which use the same .pm file for global variables. It works fine -- but then to detect a mistyped variable name easily, I added use strict;. Now it appears that to avoid the warnings, I have to explicitly export every variable in the file.

Is that true? Is there a short-hand way of saying export all variables?

Is there a cool way to continue to use the .pm file for global variables, use strict, and not go through the hassle of explicitly exporting every variable.

Any other good ideas?

Thanks.
 
Maybe.
Just inside the module, under where it says:

package [module name here];

Add this:

no strict 'vars';

It countermands the strict pragma within the scope of the block. That should do it.

--jim
 
Hi [smile]
Tks for quick reply. It didn't work. I still get warnings.
Global file:
Code:
package globaldata;
no strict 'vars';
...

Script:
Code:
use strict 'vars';
...

But I stil get the warnings like:
Variable "$var" is not imported at ... or
Global symbol "$var" requires explicit package name at ...

Any other ideas?

gnubie
 
use strict 'vars';

for $vars

use strcit qw/var vars variables/;

for $var $vars and $variables

am i right Coderifous ? ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Neversleep,

Would you elaborate please? I don't understand what you are suggesting.

g
 
use strict 'name';
$name = 'stuff';
print $name;

this will work cause we say
use strict 'name'; # so it 'my' your var called name ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Hi Neversleep,

Sorry. I must be incredibly gnu at this. How does this allow me to continue to use the .pm file for global variables, use strict, and not go through the hassle of explicitly exporting every variable?

I appreciate your help. I just don't see any relationship between my problem and your suggestion.

gnubie
 
your .pm files modify or used a $g_ from the script ?

so we could say your script look like this

#!perl -W
use strict;
use mymodules;
my $g_name = "stuff";
mymodules::myfunc;

and this 'myfunc' is the one modifying or reading the $g_name

so in mymodules.pm u can do
package mymodules;
use strict 'g_name';
sub myfunc { print $g_name; }
1;

both will work that way !

but why not use OO style ?
like this
#!perl -W
use strict;
use mymodules;
my $g_name = "stuff";
mymodules::myfunc($g_name);

package mymodules;
sub myfunc ($) { print $_[0]; }
1;

and "... and your suggestion" that was not my sugjestion

if this dosent help be more specific (with a code ex)
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Sorry, I took a long lunch.

NEVERSLEEP:

use strict 'vars';

is saying: any variable mentioned from here to the end of the enclosing scope must refer either to a lexical variable or to an explicitly allowed global. If it's not one of those, a compilation error results.

You can also turn on strict checking of symbolic dereferences and accidental use of barewords with this pragma. Normally people just say:

use strict;

to enable all three strictures.
(Courtesy of the Camel)

So it's not specifying that a variable called $vars is to be 'strictified'.

gnubie:

Try this:

our @EXPORT_OK;
push @EXPORT_OK, $package_name::{$_} for keys %package_name::;

I don't know if this will work, but one way or the other, all the package globals are able to be identifed via a hash by the same name as the package. So maybe you can automate the process through that some how. The two lines above may do it. May need some syntactic tweaking though.

Hope this helps.

--jim

 
Hi Jim [smile]

Close! The idea of populating the @EXPORT array with the variable list is a good one. Your loop populating the array worked just fine. However, it is not being imported into the script.

The exact global variable file contains:
Code:
package globaldata;
use Exporter;
@ISA=('Exporter');
our @EXPORT;
push @EXPORT,$globaldata::{$_} for keys %globaldata::;

$var1="hello\n";
$var2="goodbye\n";
1

The exact script contains:
Code:
use globaldata;
use strict;
print "$var1\n";

Running the script always results in Global symbol "$var1" requires explicit package name at .... Other ideas?

BTW, I'm running Perl v5.6 for Win32 running XP.

And Thanks!

g
 
Question, is the @EXPORT array actually containing anything since you are defining the package globals after we are 'pushing' onto the array? Put the
Code:
push @EXPORT,$globaldata::{$_} for keys %globaldata::;
at the end of the module. That way all the variables have been created. Make sense? Get back to me!

PS - I'm going home now. Be back in two hours (maybe).

--jim
 
Use the Exporter module. Here's a sample:
Code:
package Constants;
use Exporter;
$Pi = 3.14;
$Avagadros = 6.02e23;
push @EXPORT, $Pi, $Avagadros; # These variables I want to export automatically
$R = 8.31;
$STP = 22.4;
push @EXPORT_OK, $R, $STP; # This variable is exported only on request, when you load your module.

Then, in your script:

Code:
use YourModule ('$R');
print $Pi; #prints 3.14
print $Avagadros; # prints 6.02e23
print $R; # prints 8.31
print $STP; # Doesn't print anything - didn't ask for it!

This is untested, but it should work. The part I'm uncertain about is how you would call for $R. That use YourModule ('$R'); line doesn't look right to me somehow. Give it a try, though - it should get you going.

 
Whoops, in my above post, I dind't mean to include $STP in @EXPORT_OK. Not sure what I was thinking.
 
You can simply export the vars you want by using
Code:
@EXPORT = ('$var1','$var2');
If you want to export them all using a for loop like you are trying to do, you'll have to do something like
Code:
push @EXPORT, "\$$_" for keys %globaldata::;
or
Code:
push @EXPORT, '$'.$_ for keys %globaldata::;

jaa
 
OK [thumbsup] we have a winner! Thank you jaa[medal]. The code that did the trick was:

Code:
push @EXPORT, "\$$_" for keys %globaldata::;
.

Jim (Coderiferous) -- you were very close.

Thank you all who made suggestions. I really appreciate the help.

gnubie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top