如何将包符号导出到Perl中的命名空间?

Mik*_*ike 9 perl module export

我无法理解如何将包符号导出到命名空间.我几乎完全遵循文档,但它似乎不知道任何导出符号.

mod.pm

#!/usr/bin/perl

package mod;

use strict;
use warnings;

require Exporter;

@ISA = qw(Exporter);
@EXPORT=qw($a);


our $a=(1);

1;
Run Code Online (Sandbox Code Playgroud)

test.pl

$ cat test.pl
#!/usr/bin/perl

use mod;

print($a);
Run Code Online (Sandbox Code Playgroud)

这是运行它的结果

$ ./test.pl
Global symbol "@ISA" requires explicit package name at mod.pm line 10.
Global symbol "@EXPORT" requires explicit package name at mod.pm line 11.
Compilation failed in require at ./test.pl line 3.
BEGIN failed--compilation aborted at ./test.pl line 3.

$ perl -version
This is perl, v5.8.4 built for sun4-solaris-64int
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 17

它没有告诉你出口问题$a.它告诉你,你有一个问题声明@ISA@EXPORT.@ISA并且@EXPORT是包变量和under strict,它们需要使用our关键字声明(或从其他模块导入 - 但这不可能与这两个模块一起).它们在语义上不同 - 但在功能上不同 - 来自$a.

保姆注意: @EXPORT不被认为是礼貌的.通过Exporter它将其符号转储到使用包中.如果认为某些东西有利于出口,那就很有可能- 而且它 - 然后用户要求它是值得的.请@EXPORT_OK改用.


FMc*_*FMc 15

试试这个:

package mod;                # Package name same as module.

use strict;
use warnings;

use base qw(Exporter);

our @ISA    = qw(Exporter); # Use our.
our @EXPORT = qw($z);       # Use our. Also $a is a bad variable name
                            # because of its special role for sort().

our $z = 1;

1;
Run Code Online (Sandbox Code Playgroud)


too*_*lic 7

其他人已正确识别问题并提供解决方案.我认为指出一个调试技巧会很有用.要将问题隔离到给定文件,您可以尝试使用perl -c(参考perlrun)编译该文件:

perl -c mod.pm
Run Code Online (Sandbox Code Playgroud)

这会给你相同的错误信息,导致你意识到问题出在你的.pm文件中,而不是你的.pl文件.