Perl script doesn't see modules .pm [duplicate] - perl

This question already has answers here:
Doesn't Perl include current directory in #INC by default?
(3 answers)
Closed 2 years ago.
I have a perl scripts (proj_perl.pl , and proj_perl_client.pl) , I also created module proj_library.pm. My .pm looks:
package proj_library;
use base 'Exporter';
our #EXPORT = qw(help);
sub help{#my code}
1;
In my .pl scripts I invoke sub help and it looks:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use IO::Socket;
use JSON;
use proj_library qw(help); #LOOK HERE!
Then I invoke help somewhere in the code.
The problem is when I wan to run my script: ./pro_perl.pl Terminal shows me sth like this:
Can't locate proj_library.pm in #INC (you may need to install the proj_library module) (#INC
contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0
/usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30
/usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at
./proj_perl.pl line 8.
BEGIN failed--compilation aborted at ./proj_perl.pl line 8.
So I don;t know what is going on!

Add the following to tell Perl to search in the script's directory:
use FindBin qw( $RealBin );
use lib $RealBin;

Did you check that your pm is in your #INC? If you need to load a custom PM folder, check perldoc.perl.org/lib
use as:
use lib "."
or
use lib "my_libs/"

Related

Perl package file Can't locate [duplicate]

This question already has answers here:
Doesn't Perl include current directory in #INC by default?
(3 answers)
Closed 4 years ago.
I have integrate sample function in perl. Everything working fine but i am not able to connect Perl module package file(.pm)
Please review below code
sample.pl
#!/usr/bin/perl
use strict;
use warnings;
use Sample;
print Sample->test_function();
Sample.pm
package Sample;
sub test_function
{
return 'Welcome';
}
Once after run the sample.pl file. It's return the error "Can't locate Sample.pm " but the package file availale in same folder.
The reason of the issue is installed perl module location could not found . add the below line your sample.pl file
use strict;
use warnings;
use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path($0));
use Sample;
print Sample->test_function();
First of all, your Sample.pm file must finish returning a true value. The common way to do that is appending 1; and the end of that file.
package Sample;
use strict;
use warnings;
sub test_function {
return 'Welcome';
}
1;
After Perl 5.26, current directory was removed from #INC. There are several ways to deal with this. For example, you can use the -I option when running the script:
perl -I. sample.pl
For a more deeper lecture on how to deal with #INC, please read this SO post.

Can't locate custom module in #INC

I have a basic Perl script that runs code from a module. After switching to a new computer, I am no longer able to run this script. The directory tree is set up like so:
SatanicBot
src
SatanicBot
Bot.pm
Utils.pm
run.pl <-- The script that uses the module
The use statements in run.pl are like so:
use warnings;
use strict;
use diagnostics;
use Bot::BasicBot;
use Cwd qw(abs_path);
use FindBin;
use lib abs_path("$FindBin::Bin/SatanicBot");
# Without this, I get an error that is essentially caused by it not being to find the module.
use SatanicBot::Bot;
When I run run.pl, I get the following error:
Can't locate SatanicBot/Bot.pm in #INC (you may need to install the SatanicBot::Bot module) (#INC contains: /Users/elifoster/Desktop/Dev/SatanicBot/src/SatanicBot /Users/elifoster/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/darwin-2level /Users/elifoster/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0 /Users/elifoster/perl5/perlbrew/perls/perl-5.22.0/lib/5.22.0/darwin-2level /Users/elifoster/perl5/perlbrew/perls/perl-5.22.0/lib/5.22.0 .) at src/run.pl line 12.
As you can see, it states that #INC contains the directory that the SatanicBot::Bot module is in, yet it cannot find it. I'm seriously stumped.
#INC contains:
/Users/elifoster/Desktop/Dev/SatanicBot/src/SatanicBot
but to find the SatanicBot::Bot package in the file /Users/elifoster/Desktop/Dev/SatanicBot/src/SatanicBot/Bot.pm, you want #INC to contain
/Users/elifoster/Desktop/Dev/SatanicBot/src

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?
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
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

How to create own modules in Perl

Module
package ExampleModule;
use strict;
use base "Exporter";
our #export = qw/hello_world/;
sub hello_world
{
print "Hello,world";
}
1
Test:
perl -c examplemodule.pm
examplemodule.pm syntax OK
Script:
use ExampleModule;
hello_world();
exit;
but I'm getting error
Error:
Can't locate ExampleModule.pm in #INC (#INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at until.pl line 229.
BEGIN failed--compilation aborted at until.pl line 229.
What is the wrong in my Module please let me know
EDIT
package ExampleModule;
use strict;
use base "Exporter";
our #EXPORT = qw(hello_world);
sub hello_world {
print "Hello,world";
}
1;
saved with ExampleModule.pm
#!/usr/bin/perl -w
use strict;
use lib "/home/Admin/Desktop/Perl_Work_Space/ExampleModule.pm/";
use ExampleModule ;
hell_world();
exit;
saved with first.pl
getting error
[Admin#localhost Perl_Work_Space]$ perl first.pl
Undefined subroutine &main::hell_world called at first.pl line 5.
You're missing path to your module in #INC. To fix it you can tell perl where to look for modules,
use lib "lib/"; # relative or absolute path to ExampleModule.pm
use ExampleModule;
hello_world();
Also ExampleModule.pm should have package line at the top, and use but don't inherit Exporter module,
package ExampleModule;
use strict;
use Exporter "import";
our #EXPORT = qw/hello_world/;
sub hello_world {
print "Hello,world";
}
1;
In addition to the use lib ...; statement, the packagename and the filename have to have the same spelling including the capitalization. So rename the file to ExampleModule.pm.
Although I'm wondering why perl -c examplemodule.pm does not emit a warning.
And you have to use our #EXPORT = qw/hello_world/;

Can't locate WWW/Mechanize.pm in #INC

I am trying to get this sample code that I found on a site to work:
#!/usr/bin/perl
use v5.10;
use WWW::Mechanize;
use WWW::Mechanize::TreeBuilder;
my $mech = WWW::Mechanize->new;
WWW::Mechanize::TreeBuilder->meta->apply($mech);
$mech->get( 'http://htmlparsing.com/' );
# Find all <h1> tags
my #list = $mech->find('h1');
# or this way
# Now just iterate and process
foreach (#list) {
say $_->as_text();
}
When I run it I get this message:
Can't locate WWW/Mechanize.pm in #INC (#INC contains: /Library/Perl/5.16/darwin-thread-multi-2level /Library/Perl/5.16 /Network/Library/Perl/5.16/darwin-thread-multi-2level /Network/Library/Perl/5.16 /Library/Perl/Updates/5.16.2 /System/Library/Perl/5.16/darwin-thread-multi-2level /System/Library/Perl/5.16 /System/Library/Perl/Extras/5.16/darwin-thread-multi-2level /System/Library/Perl/Extras/5.16 .) at test2.pl line 4.
BEGIN failed--compilation aborted at test2.pl line 4.
Does anyone have any ideas on what is happening?
WWW::Mechanize is a Perl module. You need to install it. The easiest way is to run cpanm:
cpanm WWW::Mechanize
Note that your code says use WWW::Mechanize::TreeBuilder, which means you need to install that library as well. As its name suggests, it depends on WWW::Mechanize, which will be automatically installed if you run
cpanm WWW::Mechanize::TreeBuilder