When I initialize a local hash (using "mine") in the perl module, the hash appears empty from inside the module functions.
Here is the perl module code:
package Test;
use 5.014002;
use strict;
use warnings;
use Exporter qw(import);
our %EXPORT_TAGS = (
'all' => [ qw(test) ]
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.01';
my %h = ( "1" => "one" );
BEGIN
{
}
sub test
{
my $a = shift;
print $Test::h{$a} . "\n";
}
1;
__END__
Here the test sees an empty hash.
If instead I first declare a hash, but initialize it to BEGIN, then it works fine. Here is the modified code:
package Test;
use 5.014002;
use strict;
use warnings;
use Exporter qw('import');
our %EXPORT_TAGS = (
'all' => [ qw(test) ]
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.01';
my %h;
BEGIN
{
%Test::h = ( "1" => "one" );
}
sub test
{
my $a = shift;
print $Test::h{$a} . "\n";
}
1;
__END__
Also, if I declare a hash instead of “ours,” then it works great in both cases.
What am I missing?
source
share