Re: Global variables from a module, while using strict
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Jun 2005 11:32:02 -0700
Shea Martin wrote:
> I have a module which is basically a about 50 global variables and about
> 2 methods. Because I am 'using' strict, all of the variables are
> declared with 'my'.
These two statements form a contradition. Variables declared with 'my'
are, by definition, not global. It is a common misperception that
"strict forces you to declare all variables with 'my'". It does not.
What it does do is force you to fully qualify all package (ie, global)
variables. That means if you have a package variable $bar in package
Foo, you must refer to it as $Foo::bar.
There is a way around this restriction. Any package variable
"declared" (which is probably not the right word to use here) with the
keyword 'our' can be used for the duration of the lexical scope of the
declaration:
package Foo;
use strict;
$Foo::bar = 'Hello';
{
our ($bar, $baz);
$baz = 'World';
print "$bar $baz"; # Prints Hello World
}
print $bar; # error! Variable must be fully qualified.
> I in my scripts which use the module, I can't access any of the
> variables
That's because your 'my' variables went out of scope. This happened
either 1) At the beginning of the next package statement; 2) At the end
of the innermost { } block; 3) At the end of the file in which they
were declared. They don't exist anywhere else.
>, though I can access the sub routines.
subroutines are always "global". That is, they belong to a package.
And they are not held to the same "strict" restrictions as variables.
> I looked at Exporter, but there are two problems with the examples I
> see. One, they export the globals into the 'main' namespace, which I
> don't want.
Exporter simply exports the variables. It does not export them "into"
anything. For that, you must have the corresponding "import" (which is
usually called automatically for you by the "use" statement). If you
'use' a module Foo from within a package Bar, the variables you
exported will be imported into the package Bar, not package main.
> I want them to be accessable like this: Module::myGlobal.
Assuming you meant $Module::myGlobal, that is a package variable. If
you want package variables, use package variables, not lexical
variables. Either change all your 'my's to 'our's, or get rid of all
the 'my's and fully qualify the variables.
> The other examples show the importing script explicity importing every
> variable. This would be very tedious.
Have you considered creating one global configuration hash, and simply
exporting that?
package Foo;
use base 'Exporter';
our @EXPORT_OK = qw/%cfg/;
our %cfg;
$cfg{tmpdir} = "/tmp/";
$cfg{name} = "Paul";
# etc
1;
---------------------
#!/usr/bin/perl
use strict;
use warnings;
use Foo qw/%cfg/;
print "Name: $cfg{name}\n";
__END__
See also the descriptions of scoping available in
perldoc -f my
perldoc -f our
perldoc perlsub
Hope this helps,
Paul Lalli
.
- References:
- Global variables from a module, while using strict
- From: Shea Martin
- Global variables from a module, while using strict
- Prev by Date: make: Fatal error: Command failed for target `test_dynamic'
- Next by Date: Re: Copy local Groups -- Get SID
- Previous by thread: Re: Global variables from a module, while using strict
- Next by thread: make: Fatal error: Command failed for target `test_dynamic'
- Index(es):
Relevant Pages
|
|