Riaan Badenhorst Riaan Badenhorst - 6 months ago 18
Perl Question

Export HASH from module

Can some explain how to pass a HASH from a module to main. I've read posts like How to share/export a global variable between two different perl scripts? and understand the basic concept behind it but this is a little different and I can't find any examples.

This module works if called from main, but how do I just pass the hash to main so that I can print in there, instead of printing it from the module. Or so I can write the key values to file or something.

package My::Module;
use strict;
use warnings;

use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(get_proc_info);

sub get_proc_info
{

my %processor;
Win32::SystemInfo::ProcessorInfo(%processor);
for (my $i=0;$i<$processor{NumProcessors};$i++)
{
print "Processor $i\n";
print "Processor Name: " . $processor{"Processor$i"}{ProcessorName} . "\n";
print "Processor Info: " . $processor{"Processor$i"}{Identifier} . "\n";
print "Processor Speed: " . $processor{"Processor$i"}{MHZ} . "MHz\n\n";
}

}

Answer

Return a reference to it.

The module:

package My::Module;

use strict;
use warnings;

use Exporter          qw( import );
use Win32::SystemInfo qw( );

our @EXPORT_OK = qw( get_processors );

sub get_processors {
    my %processors;
    Win32::SystemInfo::ProcessorInfo(%processors);
    return \%processors;
}

1;

The caller:

use My::Module qw( get_processors );

my $processors = get_processors();

for my $i (0 .. $processors->{NumProcessors}-1)
    my $processor = $processors->{"Processor$i"};

    print "Processor $i\n";
    print "Processor Name:  $processor->{ProcessorName}\n";
    print "Processor Info:  $processor->{Identifier}\n";
    print "Processor Speed: $processor->{MHZ} MHz\n"; 
    print "\n";
}

The above already "fixes" some of Win32::SystemInfo::ProcessorInfo's weird interfaces. The following fixes it further:

The module:

package My::Module;

use strict;
use warnings;

use Exporter          qw( import );
use Win32::SystemInfo qw( );

our @EXPORT_OK = qw( get_processors );

sub get_processors {
    my %processors;
    Win32::SystemInfo::ProcessorInfo(%processors);
    return
        map { $processors{"Processor$_"} }
            0 .. $processors{NumProcessors}-1;
}

1;

The caller:

use My::Module qw( get_processors );

my @processors = get_processors();

for my $i (0..$#processors)
    my $processor = $processors[$i];

    print "Processor $i\n";
    print "Processor Name:  $processor->{ProcessorName}\n";
    print "Processor Info:  $processor->{Identifier}\n";
    print "Processor Speed: $processor->{MHZ} MHz\n"; 
    print "\n";
}