You can create a package variable and export it, just like a subroutine:
package Local::Foo;
use strict;
use warnings;
use Exporter qw(import); # Allows you to export things
our @EXPORT = qw(@foo_array);
our @foo_array = ( ... );
1;
Now, you can use it in your program:
my @local_array = @foo_array;
This is called a really. really bad idea. Variables are polluting your main program's namespace without your knowledge. Plus, you could change @foo_array
and destroy its special meaning.
If you go this route, do not do the export. Simply refer to the variable directly via the package name. After all, you've declared it with our
:
package Local::Foo;
...
our @foo_array = ( ... );
And, in your program:
my @program_array = @Local::Foo::foo_array;
This is a tiny bit better. You won't have namespace collisions, and you know where that value is coming from. However, I would take the lead of use constant
which uses subroutines to define constants. When you say this:
use PI => 3.141529;
What you're really doing is this:
sub PI {
return 3.141529;
}
That allows you to do this:
my $circumference = PI * ( 2 * $radius );
You could take a similar task with your array:
package Local::Foo;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(foo_array);
sub foo_array {
return qw( ... );
}
Now, to use this in your program:
use Local::Foo qw(foo_array);
...
my @program_array - foo_array;
You can access the value of foo_array
, but you can't change it at all. It's totally protected -- a constant value.