Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global scope

1 view
Skip to first unread message

Vance E. Neff

unread,
Apr 22, 2009, 4:04:57 PM4/22/09
to beginn...@perl.org
Hi,

I'm trying to get some variables to be global in scope.

I have declared a bunch of variables that I want to be accessible
throughout my program.
I have a module options.pm defined as:
<pre>
package options;
use strict;
use base qw(Exporter);
our @EXPORT = qw(@BenefitsOptions %BenefitsOptions);
our @EXPORT_OK = qw();

our %BenefitsOptions = (
"Dental" => 1,
"Full" => 2,
"Base" => 3,
"Comm." => 4,
"END"
);
@BenefitsOptions = ();

1;
</pre>

and at the beginning of each of my CGI programs I have the line:
use options;

each program may access the variables defined in options.pm and may call
other .pm modules that also need to have access to those same options.pm
variables and those .pm modules might call another.pm modules that need
to access those same variables defined in options.pm.

I read the perlmod, perlobj, Exporter, perltoot and frankly, I'm now
more confused then ever.
I tried putting the variable declarations in a BEGIN block with no success.

I'm getting the error:
Global symbol "xx" requires explicit package name at module.pm line nn.

What is the best way to do this?

Vance

Gunnar Hjalmarsson

unread,
Apr 22, 2009, 5:35:38 PM4/22/09
to beginn...@perl.org

I for one get: "Global symbol "@BenefitsOptions" requires explicit
package name at options.pm line 14."

Besides that, you need to realize that when you say

use options;

in your script, you import the symbols into the package where that
statement is placed, probably package main. The variables are indeed
available also in other packages, but you need to either say

use options;

in each of those modules as well, or use the fully qualified name, e.g.

%options::BenefitsOptions

> What is the best way to do this?

One way is to make use of the %ENV hash.

$ENV{BenefitsOptions} = {


Dental => 1,
Full => 2,
Base => 3,
'Comm.' => 4,

};

Then you can say anywhere in your program:

foreach ( keys %{ $ENV{BenefitsOptions} } ) {
print "$_ = $ENV{BenefitsOptions}->{$_}\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Dermot Paikkos

unread,
Apr 22, 2009, 5:32:31 PM4/22/09
to Vance E. Neff, beginn...@perl.org
Hi,

Hello,


package options;
use strict;

use warnings; ### This might have helped

use base qw(Exporter);
our @EXPORT = qw(@BenefitsOptions %BenefitsOptions);

#our @EXPORT_OK = qw(); # If your not going to export anything,
# you might as well leave this out.

our %BenefitsOptions = (
"Dental" => 1,
"Full" => 2,
"Base" => 3,

"Comm." => 4, # Do you really want a period (.) here?
"END" # What is this for?
);
# @BenefitsOptions = ();
# This causing errors.
my @BenefitsOptions = (); # Works with strict pragma.

1;
...
Vance


I can't see what the problem is apart from the points above. This works for me

#!/bin/perl
use strict;
use warnings;
use options;

print "Comm. is $BenefitsOptions{'Comm.'}\n";

>Comm. is 4


Perhaps you edited this for the email. Good luck,
Dp.



Vance E. Neff

unread,
Apr 23, 2009, 2:18:31 PM4/23/09
to beginn...@perl.org
Gunnar,

Thanks for your response!

That is what I started with but then I was getting the error:
xxx.pm: "our" variable $DataBaseType redeclared at xxx.pm line nn.

Well it turns out that error was caused by some other problem. I did
not realize that "our" was not a function. So you can't do something
like this:
our $variable = "value1" if (something is true);
our $variable = "value2" if (something else is true);

I fixed that and things seem to working as expected.

Thanks a lot!

Vance

Gunnar Hjalmarsson

unread,
Apr 23, 2009, 3:57:00 PM4/23/09
to beginn...@perl.org
Vance E. Neff wrote:
> Gunnar,
>
> Thanks for your response!
>
> That is what I started with

Which "that" are you referring to? Please do not top post.

<snip>

> So you can't do something like this:
> our $variable = "value1" if (something is true);
> our $variable = "value2" if (something else is true);

At http://perldoc.perl.org/perlsyn.html#Statement-Modifiers it's stated
that the behavior of my() is undefined when modified with a statement
modifier. Maybe that's true for our() as well.

Dermot Paikkos

unread,
Apr 23, 2009, 9:33:26 AM4/23/09
to beginn...@perl.org, ven...@intouchmi.com
> Dermot,
>
> Thanks for responding!
>
> Your points are correct; unfortunately, those errors were created by
my
> simplification attempts.
> But the problem is when I wish to refer to those variables in other
.pm
> modules that may be included with with use statements in the main .cgi
> program and those same variables may need to be also referenced in
> other
> included .pm inside referenced in those previously mentioned .pm
> modules.
>


That shouldn't be a problem. Just `use options` in those modules as
well.


There is a convention with modules, and someone might be better able to
explain it than me, where the name-space is broken up a bit. So rather
than

use options.pm; (it's your current directory).

use YourApp::Options; (It's in a folder called YourApp which is listed
in your lib path).


You dont' have to do this of course. This is Perl, there is more than
one way to do it, however these sorts of convention usually come about
for a good reason.

Good luck,
Dp.


0 new messages