CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Multiple hashes from module



Apr 16, 2014, 3:59 PM

Post #1 of 5 (33155 views)
Multiple hashes from module Can't Post

Ok I'm back for another question, hopefully not so vague this time.

I want to export multiple hashes and its not working, I can retrieve one(often not the one I want either) but not the other.

Overview: - The perl module reads a directory of files and their contents and creates multiple hashes in different ways for different purposes. The perl file should be able to get both hashes. I've played around with Exporter with no luck. (Note: I've got the hashes declared within the subroutine and outside of it as well, not sure where it should be declared with how I'm calling it. Here's what I've got:

package ReadFile; 

use strict;
use File::Spec;
use Data::Dumper;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(%searchadmin_id %record);
our ( %searchadmin_id, %record );

my $REC_DIR = '../accounts/';

sub Read_Active {
our %record;
our %searchadmin_id;

# Read all files in directory an put in $files array
if ( opendir( DIR, $REC_DIR ) ) {
my @files = readdir(DIR);
@files = File::Spec->no_upwards(@files);
foreach my $file (@files) {
chomp $file;

#Read the lastline of each file and put in array
my $filename = "$REC_DIR/$file";
( open( my $IF, '<', $filename ) );
my @entry = <$IF>;
close $IF;
chomp $lastentry;
my @record = split( ':', $lastentry );
my $mainid = $record[0];
my $uid = $record[1];
my $acct_name = $record[2];
my $type = $record[3];
my $org = $record[4];
my $admin_id = $record[5];

my $hashvars = (
"main_id => $mainid,
uid => $uid,
acctname => $acct_name,
type => $type,
org => $org,
admin_id => $admin_id,

#Hash Indexed by mainid
%{ $record{$mainid} } = $hashvars;

#Hash for admin_id = req (pending)
%{ $searchadmin_id{$admin_id}{$mainid} } = $hashvars;
return (\%searchadmin_id, \%record);


use strict;
use ReadFile qw(Read_Active);
use Data::Dumper;

my (%searchadmin_id, %record) = ReadFile::Read_Active(%record);

print "--- Testing imported Hashes --- \n";

print Dumper(%searchadmin_id) ### This seems to be printing %record
print Dumper(%record); ###This is printing nothing

So what am I missing here?


Veteran / Moderator

Apr 16, 2014, 6:53 PM

Post #2 of 5 (33152 views)
Re: [Iconx] Multiple hashes from module [In reply to] Can't Post

Don't export the hashes.

Don't declare them outside of the sub.

Declare them in the sub as lexical vars, not globals.

When you declare $hashvars, use braces not parens.

Why are you copying $hashvars twice?

Your sub is returning hash refs, not hashes, so when you call the sub you need to assign those to hash refs, not hashes.

my (%searchadmin_id, %record) = ReadFile::Read_Active(%record);

Should be:

my ($searchadmin_id, $record) = ReadFile::Read_Active(%record);


Apr 17, 2014, 8:54 AM

Post #3 of 5 (33134 views)
Re: [FishMonger] Multiple hashes from module [In reply to] Can't Post

Ok that worked. The reason for copying the hashvars twice is cause I'm using it to build 2 arrays with different keys.

So now that dumper prints the hash as expected how come I cant do this:

print "$searchadmin_id{req} \n";

says: Global symbol "%searchadmin_id" requires explicit package name

I was hoping to be able to return all records with the key 'req'. This works in the module but not the script.



Apr 18, 2014, 6:07 AM

Post #4 of 5 (32837 views)
Re: [Iconx] Multiple hashes from module [In reply to] Can't Post

Did you make ALL of the changes that FishMonger suggested? If so, your only error is forgetting to dereference your hash reference $searchadmin_id in your print statement.

You seem to be confusing the concept of argument with that of IMPORT/EXPORT. I suggest that you implement the subroutine Read_Active as a subroutine in main. After you have it working, move it to a module if you still want to. This approach will separate the two issues.
Good Luck,

(This post was edited by BillKSmith on Apr 18, 2014, 7:14 AM)

Veteran / Moderator

Apr 18, 2014, 8:28 AM

Post #5 of 5 (32758 views)
Re: [Iconx] Multiple hashes from module [In reply to] Can't Post

Your module is returning references to hashes (\%searchadmin_id, \%record).

So, assuming your main program has a line calling your module like this:

my ($search_href, $record_href) = Read_Active();

what you get is a pair of references to hashes, not the hashes themselves, so you need to deference $search_href in order to fetch the content of the %searchadmin_id hash populated in the Read_Active function of your module. This could be done this way:

print "$search_href->{req} \n";


print "$$search_href{req} \n";

If you do not understand this or if it still does not work, then please post the code of your module modified along the lines suggested by FishMonger as well as the code of the calling program.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives