为什么我不能在 Perl 程序中调用导出的子例程?
我是 Perl 新手,我面临以下问题,不知道为什么以下不起作用。
我的 Perl 模块包含:
package PACK2;
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK=('whom');
sub why(){
print "why\n";
}
sub whom(){
print "whom\n";
}
1;
我的 Perl 文件包含:
#!/usr/bin/perl -w
use pack;
use pack2 ('whom');
PACK::who();
&whom();
我运行这个程序,但找不到 whom
:
perl use_pack_pm.pl
who
Undefined subroutine &main::whom called at use_pack_pm.pl line 7.
I am new to Perl and I face following issue, having no clue why following is not working.
My Perl module contains:
package PACK2;
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK=('whom');
sub why(){
print "why\n";
}
sub whom(){
print "whom\n";
}
1;
My Perl file contains:
#!/usr/bin/perl -w
use pack;
use pack2 ('whom');
PACK::who();
&whom();
I run this program and can't find whom
:
perl use_pack_pm.pl
who
Undefined subroutine &main::whom called at use_pack_pm.pl line 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl 是一种区分大小写的语言。我不认为模块“pack2”和“PACK2”是相同的。
(但我还没有实际测试过。)
Perl is a case-sensitive language. I don't think modules "pack2" and "PACK2" are the same.
(But I haven't actually tested this.)
内部
use pack2 ('whom');
被翻译成类似的东西,除了 perl 会检查它是否可以在
pack2
上调用import
之前它试图调用它。在您的示例中,没有名为
pack2
的包,因此没有要调用的import
函数。如果你的包名和文件名匹配,perl 就会找到
Exporter
提供的import
函数。对此没有任何警告,因为 Perl 很难判断何时是故意这样做的。
大多数 OO 模块不会导出任何函数或变量,因此它们不提供
import
函数。Internally
use pack2 ('whom');
is translated to something likeExcept that perl will check to see if it can call
import
onpack2
before it tries to call it.In your example there is no package named
pack2
and so noimport
function to call.If your package name and file name match then perl would find the
import
function provided byExporter
.There is no warning for this because Perl has a hard time telling when this was done deliberately.
Most OO modules won't export any functions or variables and so they don't privide an
import
function.使用子文件夹树中的模块时出现相同的错误,而没有在包中声明完整路径。
您应该编写包语句及其路径。
对于位于子目录
Dir
中的模块,请编写package Dir::Module;
,而不是package Module ;
。然后就可以了。Got same error using module from subfolder tree without declaring full path in package.
You should write package statement with its path.
For module located in subdirectory
Dir
writepackage Dir::Module;
, notpackage Module ;
. Then it works.