1

このスレッドに戻って参照すると、モジュールからデータをエクスポートする方法に苦労しています。1つの方法は機能していますが、実装したい他の方法は機能していません。

問題は、スクリプトの2番目のメソッドが機能しない理由です。(これは配布専用だと思うので、モジュールを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__  

事前にThx。

4

1 に答える 1

5

ハッシュではなく、ハッシュのコピーを返します。関数に出入りするすべてのハッシュは、キーと値のペアリストにデハッシュされます。したがって、コピー。

代わりにハッシュへの参照を返します。

 return \%debug_hash;

しかし、これはあなたの内面を外の世界に明らかにします。あまりきれいなことではありません。

%debug_hashリストに追加することもできます@EXPORTが、それはさらに危険なことです。機能的なインターフェースのみを使用してください。後悔することはありません。さらに重要なことは、他の誰もそうしないことです。:)

于 2011-02-17T01:48:55.210 に答える