将哈希从模块导出到脚本

hor*_*bzz 1 perl module exporter

回到这个线程,我正在努力解决如何从我的模块导出数据的方法.一种方法是工作,但不是我想要实现的另一种方式.

问题是为什么脚本中的第二种方法不起作用?(我没有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;  
Run Code Online (Sandbox Code Playgroud)

脚本

#!/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__  
Run Code Online (Sandbox Code Playgroud)

Thx提前.

tch*_*ist 5

你返回的不是哈希,而是哈希的副本.传入或传出函数的所有哈希值都会变为键值值的pairlist.因此,副本.

返回对哈希的引用:

 return \%debug_hash;
Run Code Online (Sandbox Code Playgroud)

但这揭示了你对外面世界的内在.不是很干净的事情.

你也可以添加%debug_hash到你的@EXPORT列表中,但这是一个更奇怪的事情.请仅请使用功能界面,您不会后悔 - 更重要的是,其他任何人都不会.:)