Reputation: 91
I am trying to learn how to use a .pm file. I created 2 files:
MyScript.pl
use strict;
BEGIN {
unshift(@INC,"./firstdir");
}
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 seconddir::MyModule qw(func1) ;
print func1(@list),"\n"; #line 21
print MyModule::func2(@list),"\n";
MyModule.pm
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;
the structure of the directories is as following:
--------------- ------------ ---------------
| firstdir ---|------> |seconddir--|-> | MyModule.pm |
| MyScript.pl | ------------ ---------------
---------------
note: firstdir and seconddir are directories
when I run the command Perl MyScript.pl
I receive the following error:
Undefined subroutine &main::func1 called at MyScript.pl line 21
can you help me figure out what is wrong please?
Upvotes: 2
Views: 79
Reputation: 1342
@EXPORT = qw(func1 func2);
You need to add the names of the symbols in the @EXPORT
array in order to access them from you script.
Upvotes: 0
Reputation: 7912
Your package name is wrong, it should be:
package seconddir::MyModule
Then you should call func2
with:
print seconddir::MyModule::func2(@list),"\n";
or by exporting it, as with func1
.
Upvotes: 2
Reputation: 2308
func1
should be in the @EXPORT
array in the module MyModule.pm if you want to call it directly as func1
in your main script.
Upvotes: 1