Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, how can I access a scalar defined in another package?

I seem to be stuck trying to access a scalar which is defined in another package, and have narrowed down an example to a simple test case where I can reproduce the issue. What I wish to be able to do it access a reference to a list which is in defined within the package 'Example', using the our mechanism, however, Dumper is showing that the variable is always undefined within example.pl:

Example.pm looks like the following:

#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;

package Example;
use Data::Dumper;

my $exported_array = [ 'one', 'two', 'three' ];
print Dumper $exported_array;

1;

And the code which uses this package looks like this:

#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;
use Data::Dumper;

use lib '.';
use Example;

{ package Example;
  use Data::Dumper;
  our $exported_array;
  print Dumper $exported_array;
}

exit 0;

Upon running this code, the first Dumper runs and things look normal, after this, the second Dumper, example.pl runs and the reference is then undefined:

$VAR1 = [
          'one',
          'two',
          'three'
        ];
$VAR1 = undef;
like image 875
user441369 Avatar asked Dec 07 '10 16:12

user441369


People also ask

How do you refer variable within a package in Perl?

The package stays in effect until either another package statement is invoked, or until the end of the current block or file. You can explicitly refer to variables within a package using the :: package qualifier.

What is the difference between package and module in Perl?

A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension . pm. Two different modules may contain a variable or a function of the same name.

What is a scalar in Perl?

A scalar is a variable that stores a single unit of data at a time. The data that will be stored by the scalar variable can be of the different type like string, character, floating point, a large group of strings or it can be a webpage and so on. Example : Perl.

What is a Perl namespace?

In perl namespaces are called "packages" and the package declaration tells the compiler which namespace to prefix to our variables and unqualified dynamic names.


2 Answers

A my declaration does not create a package level variable and does not enter anything onto the symbol table for any namespace.

To do what you look like you are trying to do, you will have to change the declaration in the first file to

our $exported_array = [ ... ];

You can then access it in another file as

$Example::exported_array
like image 169
mob Avatar answered Oct 21 '22 04:10

mob


Even if $exported_array weren't lexically scoped in the Example package, Example's $exported_array and main's $exported_array are two different things. The easiest way to change the example you've given is to 1. change my to our in the Example declaration and explicitly qualify the variable name.

our $exported_array;

...

print Dumper $Example::exported_array;

Otherwise, you need to make Example an Exporter. (Or just write an Example::import routine--but I' not going to cover that.)

package Example;
our $exported_array = ...;
our @EXPORT_OK = qw<$exported_array>;
use parent qw<Exporter>;

And in the script:

use Example qw<$exported_array>;

However, as you can actually export arrays (not just refs), I would make that:

our @exported_array = (...);
our @EXPORT_OK      = qw<@exported_array>;
...
use Example qw<@exported_array>;
...
print Dumper( \@exported_array );
like image 38
Axeman Avatar answered Oct 21 '22 04:10

Axeman