我似乎被困在尝试访问另一个包中定义的标量,并将示例缩小到一个简单的测试用例,我可以重现这个问题.我希望能够使用我们的机制访问对包'Example'中定义的列表的引用,但是,Dumper显示该变量在example.pl中始终未定义:
Example.pm如下所示:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
package Example;
use Data::Dumper;
my $exported_array = [ 'one', 'two', 'three' ];
print Dumper $exported_array;
1;
Run Code Online (Sandbox Code Playgroud)
使用此包的代码如下所示:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use lib '.';
use Example;
{ package Example;
use Data::Dumper;
our $exported_array;
print Dumper $exported_array;
}
exit 0;
Run Code Online (Sandbox Code Playgroud)
在运行此代码时,第一个Dumper运行并且事情看起来正常,此后,第二个Dumper,example.pl运行,然后引用未定义:
$VAR1 = [
'one',
'two',
'three'
];
$VAR1 = undef;
Run Code Online (Sandbox Code Playgroud) 我在使用配置文件中定义的常量时遇到问题.这是我的包裹:
package myPackage;
require "APIconfig.pl";
APIconfig::import(APIconfig);
use constant SERVICE_URL => APIconfig::SERVICE_URL();
Run Code Online (Sandbox Code Playgroud)
配置如下所示:
package APIconfig;
use constant SERVICE_URL => 'http://api.example.org/blah';
1;
Run Code Online (Sandbox Code Playgroud)
运行此代码时,我收到以下错误:
Undefined subroutine &APIconfig::SERVICE_URL called at API.pl line 4.
Run Code Online (Sandbox Code Playgroud)
我不能使用'use'而不是'require',因为这需要将配置文件命名为.pm,并且它在我们网络上的许多服务器上都被称为.pl.如何在不重命名文件的情况下使用包?