我在文件中有一个模块,它导出一个作为数组引用的常量。我可以在其定义模块中使用该常量,但导入后我无法使用它。错误消息说Can't use bareword ("AR") as an ARRAY ref while "strict refs" in use at mod.pl line 28.
。
考虑这个演示代码:
#!/usr/bin/perl
require 5.018_000;
use warnings;
use strict;
package Test;
use warnings;
use strict;
BEGIN {
require Exporter;
our $VERSION = 1.00; # for version checking
# Inherit from Exporter to export functions and variables
our @ISA = qw(Exporter);
our @EXPORT = qw(); # exported by default
our @EXPORT_OK = qw(AR); # can be optionally exported
}
use constant AR => [1,2,3];
print AR->[1], "\n";
1;
package main;
Test->import(qw(AR));
print AR->[1], "\n";
#Can't use bareword ("AR") as an ARRAY ref while "strict refs" in use at mod.pl line 28.
我该如何解决?
您需要在编译对常量的引用之前执行import
。
你可以使用另一个BEGIN
块来做到这一点,但这意味着我们现在有两个黑客。我建议使用以下方法,而不是对模块的用户和模块本身进行frankensteining。它使内联包保持尽可能多的真实模块。
该方法包括以下内容:
BEGIN
块中。1;
替换尾随的$INC{"Foo/Bar.pm"} = 1;
(对于Foo::Bar
)。而已。这允许您正常地将use
模块。
因此,如果您的模块如下:
package Test;
use strict;
use warnings;
use Exporter qw( import );
our $VERSION = 1.00;
our @EXPORT_OK = qw(AR);
use constant AR => [1,2,3];
1;
如果您的脚本如下:
#!/usr/bin/perl
use 5.018;
use warnings;
use Test qw( AR );
say AR->[1];
您可以使用以下内容:
#!/usr/bin/perl
BEGIN {
package Test;
use strict;
use warnings;
use Exporter qw( import );
our $VERSION = 1.00;
our @EXPORT_OK = qw(AR);
use constant AR => [1,2,3];
$INC{__PACKAGE__ .'.pm'} = 1; # Tell Perl the module is already loaded.
}
use 5.018;
use warnings;
use Test qw( AR );
say AR->[1];
如你所见,我做了一些清理工作。特别,
required 5.018;
取代use 5.018;
来完成的
我们不需要明确使用use strict;
,因为use 5.012;
和更高版本可以使用限制。
我们可以使用say
因为use 5.010;
能够实现它。@EXPORT
。BEGIN
和@ISA
的初始化周围不需要@EXPORT_OK
块。print AR->[1]
语句在编译期间被解析,但是常量AR
在运行时之前不会导入main
命名空间。
修复是为了确保AR
在编译时导入main
BEGIN { Test->import( qw(AR) ) }
还有运行时解决方法
print &AR->[1], "\n";
print AR()->[1], "\n";
我变了
BEGIN {
require Exporter;
our $VERSION = 1.00; # for version checking
# Inherit from Exporter to export functions and variables
our @ISA = qw(Exporter);
our @EXPORT = qw(); # exported by default
our @EXPORT_OK = qw(AR); # can be optionally exported
}
至
# Inherit from Exporter to export functions and variables
use parent 'Exporter';
our $VERSION = 1.00; # for version checking
our @EXPORT = qw(); # exported by default
our @EXPORT_OK = qw(AR); # can be optionally exported
你的代码现在可以使用了
我怀疑在设置@EXPORT_OK
变量之前必须在Exporter中运行一些代码