如何使用"use strict"导入常量,避免“不能使用beadword ...作为ARRAY ref"; [英] How to import a constant with "use strict", avoiding "Can't use bareword ... as an ARRAY ref"
问题描述
我在文件中有一个模块,该模块导出一个常量,该常量是一个数组引用.我可以在其定义模块中使用该常量,但是在导入后不能使用它.错误消息显示Can't use bareword ("AR") as an ARRAY ref while "strict refs" in use at mod.pl line 28.
.
I have a module in a file that exports a constant that is an array reference. I can use that constant inside its defining module, but I cannot use it after importing it. The error message says 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
.
You need to execute the import
before a reference to the constant is compiled.
您可以使用另一个BEGIN
块来执行此操作,但这意味着我们现在有两个技巧.我建议不要采用以下方法,而不是弗兰肯斯坦对模块的用户和模块本身的看法.它使内联的程序包看起来尽可能多地成为一个真实的模块.
You could use yet another BEGIN
block to do that, but that means we have now two hacks. Rather than frankensteining both the module's user and the module itself, I suggest the following approach. It keeps the inlined package looking as much as a real module as possible.
该方法包括以下内容:
- 在脚本开头的
BEGIN
块中按原样放置整个模块. - 将尾随的
1;
替换为$INC{"Foo/Bar.pm"} = 1;
(用于Foo::Bar
).
- Place the entire module as-is in a
BEGIN
block at the start of the script. - Replace the trailing
1;
with$INC{"Foo/Bar.pm"} = 1;
(forFoo::Bar
).
就是这样.这使您可以像往常一样use
模块.
That's it. This allows you to use
the module as normal.
因此,如果您的模块如下:
So if your module is the following:
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];
如您所见,我进行了一些清理.具体来说,
As you can see, I've made some cleanups. Specifically,
- 如果您需要5.18,则最好启用它提供的语言功能.这是通过将
required 5.018;
替换为use 5.018;
来完成的- 我们不需要显式使用
use strict;
,因为use 5.012;
及更高版本可以实现严格要求. - 我们可以使用
say
,因为use 5.010;
启用了它.
- If you're going to requires 5.18, might as well enable the language features it provides. This was done by replacing
required 5.018;
withuse 5.018;
- We don't need to use
use strict;
explicitly becauseuse 5.012;
and higher enable strictures. - We can use
say
becauseuse 5.010;
enables it.
这篇关于如何使用"use strict"导入常量,避免“不能使用beadword ...作为ARRAY ref";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- We don't need to use
- 我们不需要显式使用