Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从另一个perl包访问我的%hash中的元素值_Perl_Hash_Hashtable - Fatal编程技术网

如何从另一个perl包访问我的%hash中的元素值

如何从另一个perl包访问我的%hash中的元素值,perl,hash,hashtable,Perl,Hash,Hashtable,我在包名称中有一个哈希: /lib.pm #!/usr/bin/perl -w use strict; use warnings; package lib; #my %hash = ( our %hash = ( Addr => { 'aa' => 0x11223344, 'bb' => 0x55667788, 'cc' => 0xaabbccdd, }, ); #!/usr/bin/perl

我在包名称中有一个哈希: /lib.pm

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

package lib;

#my %hash = (
our %hash = (
    Addr =>
    {
        'aa' => 0x11223344,
        'bb' => 0x55667788,
        'cc' => 0xaabbccdd,
    },
);
#!/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");
我尝试通过以下方式从另一个包访问它们的所有元素: /check.pm

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

package lib;

#my %hash = (
our %hash = (
    Addr =>
    {
        'aa' => 0x11223344,
        'bb' => 0x55667788,
        'cc' => 0xaabbccdd,
    },
);
#!/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");
我得到的结果是:

========================
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
但当我将散列声明的方式从“我们的%hash”更改为“我的%hash”时,结果就消失了:

========================
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
从“my%hash”获取数据时是否需要更改?

声明词汇变量,并且不能扩展词汇变量的范围,即在包外共享它

为什么不能使用变量

您不能共享词法变量,但可以共享其值。例如,您可以在包中提供一个公开值的闭包:

#!/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";
声明一个词法变量,不能扩展词法变量的范围,即在包外共享它

为什么不能使用变量

您不能共享词法变量,但可以共享其值。例如,您可以在包中提供一个公开值的闭包:

#!/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";

lib
是一个;对于你自己的软件包来说,这不是一个好名字。谢谢你的解释。我只是举个例子。我永远不会在实际任务中使用它。您必须理解您正在编写的代码,并在此基础上选择要编写的内容。写东西“因为它以前工作过”或“因为我见过它做过”是不正确的。请证明
#/usr/bin/perl-w
beyond“这是我在每个perl程序顶部编写的内容”是的,当然。我正在用一些试用代码来了解散列在perl中是如何工作的:)对于-w选项,它是否用于在代码内部存在问题时给出详细的消息/警告?
lib
是一个函数的名称;对于你自己的软件包来说,这不是一个好名字。谢谢你的解释。我只是举个例子。我永远不会在实际任务中使用它。您必须理解您正在编写的代码,并在此基础上选择要编写的内容。写东西“因为它以前工作过”或“因为我见过它做过”是不正确的。请证明
#/usr/bin/perl-w
beyond“这是我在每个perl程序顶部编写的内容”是的,当然。我正在使用一些试用代码来了解散列在perl中是如何工作的:)对于-w选项,当代码中存在一些问题时,它是否用于给出详细的消息/警告?