fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I have a sample Perl routine into which I want to import a module with data such that I can access the data in the main routine.
use strict; use warnings; use DBI; require db_data; print "length = " . $#LoH . "\n"; foreach (0..$#LoH) { &connect($_, $LoH[$_]{database}, $LoH[$_]{hostname}, $LoH[$_]{servicename}, $LoH[$_]{port}, $LoH[$_]{username}, $LoH[$_]{password}, ); } sub connect { my ($count, $database, $hostname, $servicename, $port, $username, +$password) = @_; print "\n\n$count -> $database\n"; print "\t$hostname\n"; print "\t$servicename\n"; print "\t$port\n"; print "\t$username\n"; print "\t$password\n"; my $dbh = DBI->connect( "dbi:Oracle:host=$hostname; service_name=$servicename; port=$port", "$username", "$password" ) || die "Database connection not made"; print "connected to $database\n"; $dbh->disconnect(); } ########################################################### # db_data.pm use strict; use warnings; package db_data; my @LoH = ( { database => 'Pxxxxx', hostname => 'wpprd0.com', servicename => 'pqxxxx', port => '9010', username => 'mmmmmm'', password => 'pl', }, ) ; 1;
the data is contained in db_data.pm...there are two distinct files

Replies are listed 'Best First'.
Re: require a module and import array data
by Corion (Patriarch) on Apr 09, 2015 at 14:09 UTC
      ########################################################## # db_data.pm use strict; use warnings; package db_data; use Exporter; my @ISA = qw(Exporter); my @EXPORT = qw(@LoH); my @LoH = ( { database => 'Pxxxxx', hostname => 'wpprd0.com', servicename => 'pqxxxx', port => '9010', username => 'mmmmmm', password => 'pl', }, ) ; 1;
      @LoH array is still not available to main routine

        So, have you read the other link I provided?

        Alternatively, have you read the Exporter documentation and examples? They do not mention lexical variables, neither for @EXPORT nor for the exported variables. Maybe you want to explain why you chose to use lexical variables there.

        Take a closer look at Exporter, my isn't used for any of the vars , they are supposed to be globals ... my vars (not globals) cannot be exported