1

refering back to this thread, I'm strugglying with the way how to export datas from my module. One way is working but not the other one which I would like to implement.

The question is why the second method in the script is not working ? (I did not h2xs the module as I guess this is for distributing only)

Perl 5.10/ Linux distro

Module 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;  

Script

#!/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__  

Thx in advance.

4

1 回答 1

5

您返回的不是哈希,而是哈希的副本。传入或传出函数的所有哈希都被分解为键值对列表。因此,一个副本。

而是返回对哈希的引用:

 return \%debug_hash;

但这向外界揭示了你的内在。这不是一件很干净的事情。

您也可以添加%debug_hash到您的@EXPORT列表中,但这是一个更狡猾的事情。请只使用功能界面,您不会后悔的——更重要的是,其他人也不会后悔。:)

于 2011-02-17T01:48:55.210 回答