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?
use lib
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With