Initializing a local hash in the perl module results in an empty hash

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?

+4
source share
2 answers

our makes the lexical alias of a variable with a dynamic region, the type of which can refer to $ Fully :: Qualified :: name.

my , $name.

@Schwern, "" , (, - , , my $lexical_var, ).

, my %h , (Test.pm?). our %h, , , (), %Test::h. sub test . .

( , local , , eval. "local" , , perl.)

+4

- ? " 5.014002;"? , , - - , perl. , , ?

OO perl . .

package myModule;

sub new
{
   $class = shift;
   my $self = {};
   bless $self, $class;

   return $self;
}

sub someMethod
{
   my $self = shift;
   $self->{some} = "value";

   return $self;
}

1;
-4

Source: https://habr.com/ru/post/1570216/


All Articles