回到这个thread,我正在努力解决从模块中导出数据的方法。一种方法是工作,但不是我想要实现的另一种方式。
问题是为什么脚本中的第二种方法不起作用? (我没有h2xs模块,因为我猜这只是为了分发)
Perl 5.10 / Linux发行版
模块 my_common_declarations.pm
#!/usr/bin/perl -w
package my_common_declarations;
use strict;
use warnings;
use parent qw(Exporter);
our @EXPORT_OK = qw(debugme);
# local datas
my ( $tmp, $exec_mode, $DEBUGME );
my %debug_hash = ( true => 1, TRUE => 1, false => 0, FALSE => 0, tmp=>$tmp, exec=>$exec_mode, debugme=>$DEBUGME );
# exported hash
sub debugme {
return %debug_hash;
}
1;
脚本
#!/usr/bin/perl -w
use strict;
use warnings;
use my_common_declarations qw(debugme);
# 1st Method: WORKS
my %local_hash = &debugme;
print "\n1st method:\nTRUE: ". $local_hash{true}. " ou : " . $local_hash{TRUE} , "\n";
# 2nd Method: CAVEATS
# error returned : "Global symbol "%debug_hash" requires explicit package name"
print "2nd method \n " . $debug_hash{true};
__END__
提前谢谢。
答案 0 :(得分:6)
您返回的不是哈希,而是哈希的副本。传入或函数的所有哈希值都会被删除为键值值的pairlist。因此,副本。
返回对哈希的引用:
return \%debug_hash;
但这揭示了你对外面世界的内在。不是很干净的事情。
您还可以将%debug_hash
添加到您的@EXPORT
列表中,但这是一个更加愚蠢的事情。请仅请使用功能界面,您不会后悔 - 更重要的是,其他任何人都不会。 :)