Reputation: 91
This is a module math.pm
with 2 basic functions add and multiply:
package Math;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(add multiply);
sub add {
my ($x, $y) = @_;
return $x + $y;
}
sub multiply {
my ($x, $y) = @_;
return $x * $y;
}
1;
This is script script.pl
that call the add function:
#!/usr/bin/perl
use strict;
use warnings;
use Math qw(add);
print add(19, 23);
It gives an error:
can't locate math.pm in @INC <@INC contain: C:/perl/site/lib C:/perl/lib .> at C:\programs\script.pl line 5. BEGIN failed--compilation aborted at C:\programs\script.pl line 5.
How to solve this problem?
Upvotes: 8
Views: 23126
Reputation: 386551
Add the following to script.pl
before use Math ...;
:
use FindBin qw( $RealBin );
use lib $RealBin;
If script.pl
and math.pm
aren't in the same directory, adjust accordingly.
Also, you might have problems if the file is named math.pm
and you use use Math;
and package Math;
. It would be best to rename the file so the spelling is consistent.
ren math.pm Math.pm
Upvotes: 0
Reputation: 4709
Adding a use lib statement to the script will add the directory to @INC for that specific script. Regardless who and in what environment runs it.
You just have to make sure to have the use lib statement before trying to load the module:
use lib '/path/to/module';
use Math qw(add);
For more details to set @INC look at this:
How do I include a Perl module that's in a different directory
Upvotes: 11