sid_com
sid_com

Reputation: 25137

"require" doesn't work where "use" works

The My_Module-module (used in this package) uses Signals::XSIG and sets $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'}
    # ...
}

When I use this I get a lot of error-messages like:

"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
...

When I use use instead of require it works fine.
Why does this not work with require?

Upvotes: 1

Views: 251

Answers (2)

Axeman
Axeman

Reputation: 29854

Because when you do this:

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

you're passing the contents of that hash to the import routine. As NUM63 and TRAP are signal names, and they are not valid exports for Signals::XSIG, you're getting those errors.

You need to do this:

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

Because it recognizes Exporter recognizes the string '%XSIG' as one of the things it does export.

Upvotes: 4

ikegami
ikegami

Reputation: 386561

use Signals::XSIG qw( %XSIG );

is equivalent to

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

instead of

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

you do

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

import is complaining about all the incorrect values you passed to it.

(You also got rid of the BEGIN, but that's unrelated to the errors you are currently getting.)

Upvotes: 3

Related Questions