简体   繁体   中英

How to access element value in my %hash from an another package perl

I have a hash inside a package name: ./lib.pm

#!/usr/bin/perl -w
use strict;
use warnings;

package lib;

#my %hash = (
our %hash = (
    Addr =>
    {
        'aa' => 0x11223344,
        'bb' => 0x55667788,
        'cc' => 0xaabbccdd,
    },
);

And i tried to access all of their elements from another package with some ways as below: ./check.pm

#!/usr/bin/perl -w
use strict;
use warnings;

use Cwd;
my $dir = getcwd;
unshift( @INC, $dir );
require lib;

print "========================\n";
print "1: $lib::hash{Addr}{'aa'}\n";
print "2: $lib::hash{Addr}{'bb'}\n";
print "3: $lib::hash{Addr}{'cc'}\n";

print "========================\n";
printf (sprintf("%8X\n", $lib::hash{Addr}{aa}));
printf (sprintf("%8X\n", $lib::hash{Addr}{bb}));
printf (sprintf("%8X\n", $lib::hash{Addr}{cc}));

print "========================\n";
my ($Address_a, $Address_b, $Address_c);
my (@Address_a, @Address_b, @Address_c, @names);
$Address_a = sprintf("%8X", $lib::hash{Addr}{'aa'});
$Address_b = sprintf("%8X", $lib::hash{Addr}{'bb'});
$Address_c = sprintf("%8X", $lib::hash{Addr}{'cc'});
@Address_a = ( $Address_a =~ m/../g );
@Address_b = ( $Address_b =~ m/../g );
@Address_c = unpack("(A2)*", $Address_c);

printf "========================\n";
printf ("@Address_a \t\n");
printf ("@Address_b \t\n");
printf ("@Address_c \t\n");

printf "========================\n";
printf ("$Address_a[0], $Address_a[1], $Address_a[2], $Address_a[3]\n");
printf ("$Address_b[0], $Address_b[1], $Address_b[2], $Address_b[3]\n");
printf ("$Address_c[0], $Address_c[1], $Address_c[2], $Address_c[3]\n");

And i got result normally:

========================
1: 287454020
2: 1432778632
3: 2864434397
========================
11223344
55667788
AABBCCDD
========================
========================
11 22 33 44
55 66 77 88
AA BB CC DD
========================
11, 22, 33, 44
55, 66, 77, 88
AA, BB, CC, DD

But when i change the way of hash declaration from " our %hash " to " my %hash ", the result is gone:

========================
Use of uninitialized value in concatenation (.) or string at ./check.pm line 10.
1:
Use of uninitialized value in concatenation (.) or string at ./check.pm line 11.
2:
Use of uninitialized value in concatenation (.) or string at ./check.pm line 12.
3:
========================
Use of uninitialized value in sprintf at ./check.pm line 14.
       0
Use of uninitialized value in sprintf at ./check.pm line 15.
       0
Use of uninitialized value in sprintf at ./check.pm line 16.
       0
========================
Use of uninitialized value in sprintf at ./check.pm line 20.
Use of uninitialized value in sprintf at ./check.pm line 21.
Use of uninitialized value in sprintf at ./check.pm line 22.
========================
          0
          0
    0
========================
  ,   ,   ,  0
  ,   ,   ,  0
, , ,  0

Is there any change i need when getting data from "my %hash"?

my declares a lexical variable, and you can't extend the scope of a lexical variable, ie share it outside the package.

Why can't you use an our variable?

You can't share the lexical variable, but you can share its value. For example, you can provide a closure in the package that exposes the value:

#!/usr/bin/perl
use warnings;
use strict;

{   package MyLib;

    my %hash = (
        Addr => {
            aa => 0x11223344,
            bb => 0x55667788,
            cc => 0xaabbccdd,
        },
    );
    sub get_hash { %hash }

}


my %hash = MyLib::get_hash();
print "========================\n";
print "1: $hash{Addr}{'aa'}\n";
print "2: $hash{Addr}{'bb'}\n";
print "3: $hash{Addr}{'cc'}\n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM