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

Please help how to access multiple hash variables defined in a module. I am not aware of the syntax for accessing the hash variables populated in a module from main file

<sample code>
XYZ.pm ======= #!/usr/bin/perl package XYZ; @ISA = qw(Exporter); @EXPORT_OK = qw(%Hash1 %Hash2 %Hash3) sub populateHashRoutine { # open the file "raw_file.txt" # Populate %Hash1 %Hash2 %Hash3 } 1; main.pl ======== use XYZ; XYZ::populateHashRoutine("raw_file.txt"); How to print Hash1,Hash2,Hash3 values here ?

Replies are listed 'Best First'.
Re: How to access multiple hash variables defined in a module
by zwon (Abbot) on Feb 22, 2010 at 07:29 UTC
    use Data::Dumper; warn Dumper \%XYZ::Hash1;
    Note, that hash variables should be declared as package variables in XYZ. Like this:
    our (%Hash1, %Hash2, %Hash3);
    though you seems aren't using strict...
Re: How to access multiple hash variables defined in a module (Exporter)
by shmem (Chancellor) on Feb 22, 2010 at 08:02 UTC

    As zwon writes above, you can always access the hashes with their fully qualified notation %XYZ::Hash1, but you don't need Exporter for that in package XYZ.

    However,if you make the hashes available to the caller via @EXPORT_OK, you can import them stating

    use XYZ qw(%Hash1 %Hash2);

    which makes them accessible as %Hash1 and %Hash2 in the importing namespace.

    print "$_ => $Hash1{$_}\n" for keys %Hash1;

      I tried both the methods, I am getting able to get only empty hash :(

        Have you tried:

        use strict; use warnings;
        I see require Exporter; line is missed in your example.

        Update: Here's the working code:

        package XYZ; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(%Hash1); our ( %Hash1, %Hash2 ); sub populateHashRoutine { %Hash1 = ( a => 1, b => 2, ); %Hash2 = ( c => 3, d => 4, ); } 1;
        use strict; use warnings; use Data::Dumper; use XYZ qw(%Hash1); XYZ::populateHashRoutine; warn Dumper \%Hash1, \%XYZ::Hash2; __END__ $VAR1 = { 'a' => 1, 'b' => 2 }; $VAR2 = { 'c' => 3, 'd' => 4 };

        Then it must be empty. Try outputting the hash inside the package.

Re: How to access multiple hash variables defined in a module
by cdarke (Prior) on Feb 22, 2010 at 09:14 UTC
    Is this a good approach? Presumably you are putting the hashes in the module for a reason, and for that same reason would it not be better to access the hashes from the outside using a subroutine? This would be a first stage towards encapsulation.

    I know it sounds like more work, but it will pay off in the long run.For example:
    package XYZ; { my %Hash1; my %Hash2; my %Hash3; sub populateHashRoutine { # open the file "raw_file.txt" open (my $fh, '<', 'raw_file.txt') or die "raw_file: $!"; # Populate %Hash1 %Hash2 %Hash3 while (<$fh>) { # mumble, mumble, mumble } close ($fh); } sub GetH1 { my $key = shift; return $Hash1{$key} } sub GetH2 { my $key = shift; return $Hash2{$key} } sub GetH3 { my $key = shift; return $Hash3{$key} } } 1; #main.pl #======== use XYZ; XYZ::populateHashRoutine("raw_file.txt"); #How to print Hash1,Hash2,Hash3 values here ? print XYZ::GetH1('key1'),"\n"; print XYZ::GetH2('key2'),"\n"; print XYZ::GetH3('key3'),"\n";
Re: How to access multiple hash variables defined in a module
by Khen1950fx (Canon) on Feb 22, 2010 at 09:46 UTC
    Here's a different way of handling @EXPORT_OK
    package XYZ; use strict; use warnings; use Data::Dumper; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( Hash1 Hash2 Hash3 ) ], ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
Re: How to access multiple hash variables defined in a module
by Anonymous Monk on Feb 22, 2010 at 07:04 UTC

      Thanks for the tutorial. But it doesn't provide any explanation on using accessing multiple hashes