Domanda

I am writing two perl script which requires an array consisting long list of strings Here I wanted to declare my array in a separate perl package and use this package to access that array in other two perl scripts

Can you please tell me how could i do this

I checked for perl cpan module Exporter, but could find it can export subroutines,I only want my array to be exported not any subroutines

Thank you in advance.

È stato utile?

Soluzione

Exporter can handle any package symbol, be it a subroutine or variable:

package My::Package;
use parent 'Exporter';

our @EXPORT_OK = qw/@foo/;

our @foo = (42, 666);

Then:

use My::Package qw/@foo/;

print "@foo\n";

Output: 42 666.

However, package variables (aka. “globals”) are frowned upon. Usually, techniques like object orientation provide a better solution. If you don't want to go there, you could still write a kind of “accessor” or “initializer”:

package My::Package;

sub main {
  my @foo = (42, 666);
  return \@foo;
}

Then:

use My::Package;

my $foo = My::Package::main();
print "@$foo\n";

Altri suggerimenti

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.

Yeah, I have to echo amon's deprecation of exporting. You can create it as a constant

package TheData;
use constant BIG_ARRAY => [ 0..300_000 ];

And then other packages can reference it as if it were a static member:

package DataUser;

my $big_array_data = TheData->BIG_ARRAY;

Much cleaner for a more modern Perl. (Note that constant just is shorthand for what amon specified.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top