使用 Perl 创建包

发布于 2024-12-01 08:12:20 字数 1343 浏览 0 评论 0原文

我在制作第一个简单的包时似乎遇到了很多麻烦(实际上这是我的第一个包期)。我正在做我应该做的一切(我认为),但它仍然不起作用。这是包(我猜你可以称它为模块):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-12-08 08:12:20

您的包名称和您的使用名称不匹配。如果您的模块位于名为 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 call

use MyModule

另类 2024-12-08 08:12:20

应该是

package Mine::MyModule;

并且应该在 Perl/site/lib 下的 Mine 目录中。

It should be

package Mine::MyModule;

And it should be in the Mine directory under Perl/site/lib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文