Perl cannot find packgeName.pm file in @INC error

This is a module math.pm

adding and multiplying two main functions:

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;

      

These are the script script.pl

that call the add function:

#!/usr/bin/perl
use strict;
use warnings;

use Math qw(add);
print add(19, 23);

      

This gives an error:

cannot find math.pm in @INC <@INC containing: C: / perl / site / lib C: / perl / lib. > in C: \ programs \ script.pl line 5. BEGIN failed - compilation aborted at C: \ programs \ script.pl line 5.

How to solve this problem?

+3


source to share


2 answers


use lib

Adding a use lib statement to the script will add a directory to @INC for that particular script. No matter who launches it and in what environment.

Before loading the module, you just need to make sure you are using the lib statement.

use lib '/path/to/module';
use Math qw(add);

      



See below for details on @INC installation:

How to include a Perl module in another directory

+7


source


Add the following before script.pl

before use Math ...;

:

use FindBin qw( $RealBin );
use lib $RealBin;

      

If script.pl

u are math.pm

not in the same directory, adjust accordingly.



It can also cause problems if the file is named math.pm

and you use use Math;

and package Math;

. Your best bet is to rename the file so the spelling is consistent.

ren math.pm Math.pm

      

0


source







All Articles