0

list.pl

my @array1 = qw ( l2  l3 l4 l5 );
my @array2 = qe ( l6  l2 l3 );

Pgm.pl

use list.pl 

print @array1; 

is it possible ?

1
  • Not directly. The whole point of my is to make it impossible to access a variable outside of its scope. Commented Feb 25, 2011 at 16:22

2 Answers 2

3

try require, require 'list.pl'. You might also need to change the scope prefix my to something more global.

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to do something like this, you should setup a module:

List.pm:

 package List;
 use Exporter;
 our @ISA    = 'Exporter';
 our @EXPORT = qw(@array1 @array2);     

 our @array1 = qw(12 13 14 15);
 our @array2 = qw(16 12 13);

Pgm.pl:

 use List;
 print @array1;

But in general it is better to either code this using fully qualified names (removing the need for Exporter):

 use List ();
 print @List::array1;

Or to create an accessor method:

List.pm:

package List;

my @array1 = qw(12 13 14 15); # my makes these arrays private to this file
my @array2 = qw(16 12 13);

sub array1 {\@array1}  # accessor methods provide ways to change your 
sub array2 {\@array2}  # implementation if needed

Pgm.pl:

use List;

my $array1 = List->array1;

print @$array1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.