开发者

Creating Packages With Perl

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!


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


It should be

package Mine::MyModule;

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜