使用 Perl 创建包
我在制作第一个简单的包时似乎遇到了很多麻烦(实际上这是我的第一个包期)。我正在做我应该做的一切(我认为),但它仍然不起作用。这是包(我猜你可以称它为模块):
package MyModule;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(func1 func2);
%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
Both => [qw(&func1 &func2)]);
sub func1 { return reverse @_ }
sub func2 { return map{ uc }@_ }
1;
我在 Perl/site/lib
中将此模块保存为 MyModule(是的,它被保存为 .pm 文件)(这是其中的位置)我的所有非内置模块都被存储)。然后我尝试在 Perl 脚本中使用这个模块:
use strict;
use warnings;
my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !);
use Mine::MyModule qw(&func1 &func2);
print func1(@list),"\n";
print func2(@list),"\n";
我将其保存为 my.pl
。然后我运行 my.pl
并收到此错误:
Undefined subroutine &main::func1 called at C:\myperl\examplefolder\my.pl line 7.
有人可以解释为什么会发生这种情况吗?提前致谢!
注意:是的,我的示例来自 Perl Monks。请参阅Perl Monks“简单模块教程”。谢谢速子!
I seem to be having a lot of trouble with making my first, simple Package (actually it is my first package period). I am doing everything I should be doing (I think) and it still isn't working. Here is the Package (I guess you can call it a Module):
package MyModule;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(func1 func2);
%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
Both => [qw(&func1 &func2)]);
sub func1 { return reverse @_ }
sub func2 { return map{ uc }@_ }
1;
I saved this module as MyModule (yes, it was saved as a .pm file) in Perl/site/lib
(this is where all of my modules that are not built-in are stored). Then I tried using this module inn a Perl script:
use strict;
use warnings;
my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !);
use Mine::MyModule qw(&func1 &func2);
print func1(@list),"\n";
print func2(@list),"\n";
I save this as my.pl
. Then I run my.pl
and get this error:
Undefined subroutine &main::func1 called at C:\myperl\examplefolder\my.pl line 7.
Can someone please explain why this happens? Thanks in advance!
Note:Yes my examples were from Perl Monks. See the Perl Monks "Simple Module Tutorial". Thank You tachyon!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的包名称和您的使用名称不匹配。如果您的模块位于名为
Mine
的文件夹中,那么您需要相应地命名您的包:package Mine::MyModule
如果您在该文件夹中没有它,那么您需要从您的
use
调用use MyModule
中删除它Your package name and your use name don't match. If you have your module in a folder called
Mine
then you need to name your package accordingly:package Mine::MyModule
If you don't have it in that folder then you need to remove that from your
use
calluse MyModule
应该是
并且应该在 Perl/site/lib 下的 Mine 目录中。
It should be
And it should be in the Mine directory under Perl/site/lib.