`our' declaration causing problems with `strict' pragma across files

From: Dave Bakhash (cadet_at_alum.mit.edu)
Date: 07/21/04

Date: 21 Jul 2004 05:26:31 -0700

I have a set of executable scripts, all written in Perl. This time,
however, I would like all of these scripts to obtain their globals
from a single, separate file. I would also like all of my Perl files
to use the `strict' pragma:

 use strict;

which has been a requirement of mine since I started using Perl.

Here's how I thought to do it. Suppose I have a script, foo:

File: foo

package Foo;
use strict;
BEGIN { require "/path/to/Foo.pm"; }

print $BAR; # uses a global variable `$BAR' defined in Foo.pm

# code follows...

Now, the file Foo.pm will look like this:

File: Foo.pm

package Foo;
use strict;
our $BAR = 'BAR'; # global variable

The problem is that having the `use strict;' in the `foo' executable
file is causing problems, and Perl is complaining:

Variable "$BAR" is not imported at ./foo line 6.
Global symbol "$BAR" requires explicit package name at ./foo line 6.
Execution of ./foo aborted due to compilation errors.

What am I missing? Is there a better way to do this? Is `our' the
wrong declaration to be using here?


Relevant Pages