Access to (exported?) variabes of a module
jeanluca
created: 2006-06-01 05:42:03
Dear Monks,

I have this module which needs to export $variables instead of subs. Can some explain to me how todo this or point me to a manual that deals with this subject ?

Thnx a lot
Luca
Re: Access to (exported?) variabes of a module
created: 2006-06-01 05:54:58

Variables are exported in exactly the same way as subroutines are (tho' you'll need to ensure that they are package variables, not lexical variables).

See Exporter for more details.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re: Access to (exported?) variabes of a module
created: 2006-06-01 05:55:04

You can export variables using exactly the same [module://Exporter] mechanism as you can with subroutines. E.g:

(PackVar.pm):
package PackVar;
                                                                                
use base Exporter;
                                                                                
use vars qw(@EXPORT $Test);
                                                                                
@EXPORT = qw($Test);
                                                                                
$Test = 1;
                                                                                
1;


(test.pl):
use strict;
                                                                                
use PackVar;
                                                                                
print $Test,"\n";
which works fine. Note that the exported variables must be package and not lexical variables: you need to declare them in the module with use vars or our (or use the fully package qualified name) and not declare them with my.

However you really, really, really do want to think carefully about exporting variables like this even more than you do with subroutines as this constitutes a more serious pollution of the callers namespace with a higher risk of collision, unless you have absolutely no way around this you should use fully package qualified names (e.g. $PackVar::Test) in the caller and document this usage.

/J\

Re^2: Access to (exported?) variabes of a module
created: 2006-06-01 08:37:18

It's somewhat safer if you use @EXPORT_OK rather than @EXPORT and make the caller explicitly use PackVar qw( $Test ) to get the variable in their namespace. That also documents where $Test is coming from in the using code.

But seconded on the recommendation to prefer fully qualified names; if I don't I make sure to use the explicit import (then again I almost always use an explicit import list for subs as well . . .).

Re: Access to (exported?) variabes of a module
created: 2006-06-01 09:08:02
Alternatively, you could write your own get/set functions for the module.

let say I have a module
my @AoA = (
  #Array of Array Stuff
);

my %fundHash = (
 # Hash Stuff
);


# return array reference
sub return_AoA {
	return \@AoA;
}

# return hash reference
sub return_fundHash {
	return \%fundHash;
}


In my main program I will have
use perlMod;

my $AoA = perlMod::return_AoA();
my $fundHash = perlMod::return_fundHash();


And since you are passing by reference in this case, you will need to dereference in order to actually use the variable values...
my $toMail = $$AoA[$i][2] . "\@mail.host";


Tobin

perlmonks.org content © perlmonks.org and davorg, Fletch, gellyfish, jeanluca, Tobin Cataldo

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03