Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, can't locate packgeName.pm in @INC error

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?

like image 413
abc Avatar asked Jun 05 '15 05:06

abc


1 Answers

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

like image 198
serenesat Avatar answered Sep 22 '22 14:09

serenesat