“require”在“使用”工作的地方不起作用

时间:2012-03-22 17:59:51

标签: perl require perl-module overwrite

My_Module - 模块(在此包中使用)use s Signals::XSIG并设置$XSIG{'WINCH'}[1] = sub { ... };

use warnings;
use 5.014;
package Auto_sid;
use Exporter 'import';
our @EXPORT = qw(something);

use My_Module;

no warnings qw(redefine);

sub My_Module::list_to_big {
    my ( @arguments ) = @_;
    require Signals::XSIG;
    Signals::XSIG->import(%Signals::XSIG::XSIG);
    @{$Signals::XSIG::XSIG{'WINCH'}} = ();
    no Signals::XSIG;
    # ...
    # do something that My_Module normaly doesn't do and
    # which doesn't work when modified $SIG{'WINCH'}
    # ...
}

当我使用它时,我收到很多错误消息,如:

"NUM63" is not exported by the Signals::XSIG module
"ARRAY(0xc23180)" is not exported by the Signals::XSIG module
"TRAP" is not exported by the Signals::XSIG module
"ARRAY(0xc119c8)" is not exported by the Signals::XSIG module
...

当我使用use代替require时,它可以正常使用 为什么这不适用于require

2 个答案:

答案 0 :(得分:4)

因为当你这样做时:

Signals::XSIG->import(%Signals::XSIG::XSIG);

您将该哈希的内容传递给导入例程。由于NUM63TRAP是信号名称,并且它们不是Signals :: XSIG的有效导出,因此您会收到这些错误。

你需要这样做:

Signals::XSIG->import('%XSIG');

因为它识别Exporter将字符串'%XSIG'识别为 导出的内容之一。

答案 1 :(得分:3)

use Signals::XSIG qw( %XSIG );

相当于

BEGIN {
   require Signals::XSIG;
   Signals::XSIG->import(qw( %XSIG ));
}

而不是

# Passes the string '%XSIG'
Signals::XSIG->import(qw( %XSIG ));
你做了

# Passes the contents of %Signals::XSIG::XSIG
Signals::XSIG->import(%Signals::XSIG::XSIG);

import抱怨您传递给它的所有错误值。

(你也摆脱了BEGIN,但这与你目前得到的错误无关。)