Can't locate custom module in #INC - perl

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

Related

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

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/"

plink- Can't locate .pl in #INC error while running a perl script located on a remote linux server using a python program from windows

I'm trying to run a perl script that is located in a remote linux server from windows using the python program. I'm using the subprocess.call method.
This is the python line of code
returnCd = subprocess.call(['plink', '-ssh', \
'%s#%s' %('******','*****.***.com'), '-pw', '****', \
'perl', '/apps/CACSGDEV/springbatch/perlscript/DEBTOR_VERIFICATION.pl'], \
shell=True)
And this is what i see in the console (broken over lines for readability)
Can't locate ENVRC.pl 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 /apps/CACSGDEV/springbatch/perlscript/DEBTOR_VERIFICATION.pl line 16.
The file DEBTOR_VERIFICATION.pl has the following # line 16
require "ENVRC.pl";
The file ENVRC.pl is in the same path as that of the DEBTOR_VERIFICATION.pl. Can anyone please suggest?
The program DEBTOR_VERIFICATION.pl is directly executed and thus it doesn't need to have its directory in #INC. On the other hand, when a file need be loaded via require (or use, which uses require) then it is precisely the directories in #INC that are searched for it. Since /apps/.., where ENVRC.pl resides, is not there the file cannot be found.
You need to add that directory to #INC and a good way to do it is via lib pragma
use warnings;
use strict;
...
use lib "/apps/CACSGDEV/springbatch/perlscript";
...
But if this program (DEBTOR_VERIFICATION.pl) and the required file ENVRC.pl will always stay in the same directory it is better to use FindBin instead of hard coding the path
use warnings;
use strict;
...
use FindBin qw($RealBin);
use lib $RealBin;
In either case the files in that directory can now be found by use and require (and do).
The DEBTOR program works when executed right from its directory, in older Perls, since the . (the current working directory) used to be included in #INC by default. We see that this is the case from #INC printed in the error message. So . gets searched and ENVRC.pl is found.
Relying on that has really always been a bad practice, but as of perl v5.26.0 (perldelta) (May 2017) the . directory is not in #INC anymore.
So you should add the above use lib statement to the DEBTOR... program anyway.

Apache doesn't see pm file

I have installed Apache 2.2 and strawberry perl. I have textxx.pm. And I prepare simple script
#!/usr/bin/perl
use textxx;
print("HelloWorld!");
When I run it via cmd it works. When I run it via web browser I get and error:
Can't locate textxx.pm in #INC (#INC contains: C:/strawberry/perl/site/lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .)
It's weird because script without "use" works.
My module is located at the same directory as script
You are trying to use the textxx.pm perl module.. but it is not found while running through apache, if you remove the use line it will obviously work since it's not looking for the module.
a quick fix, if your textxx.pm is in let's say C:/strawberry/perl/site/lib then just add this at the top of your script:
#!/usr/bin/perl
use lib "C:/strawberry/perl/site/lib";
use textxx;
print("HelloWorld!");
Lack of permission could be the issue, but I'm betting the problem is that you're expecting Perl to look in the directory containing the script for the module. Perl doesn't do that, at least not by default.
It worked from the console because you set the current work directory happened to the be the same as the script, but that's not the case when you're running it through apache.
To tell Perl to look in the same directory as the script, add the following because use textxx;:
use Cwd qw( realpath );
use File::Basename qw( dirname );
use lib dirname(realpath($0));

"Can't locate inc/nph-globals.pl in #INC (#INC contains: ......" ERROR for my PERL site

I am setting up perl site on XAMPP local server. I have placed the code at proper place and change first line of all .pl and .cgi files as per the path. i.e.
#!"D:\xampp\perl\bin\perl.exe"
for my case. Still I am getting error like
Can't locate inc/nph-globals.pl in
#INC (#INC contains:
D:/xampp/perl/site/lib/
D:/xampp/perl/lib
D:/xampp/perl/site/lib .
D:/xampp/apache) at
D:/xampp/htdocs//cgi-bin/webscripts/nph-home.pl
line 9.
Please suggest.
I suspect the file is located at
D:/xampp/htdocs/cgi-bin/webscripts/inc/nph-globals.pl
If so, it doesn't work because your script incorrectly assume the current working directory is set to the directory that contains the executing script, namely
D:/xampp/htdocs/cgi-bin/webscripts
The following code will address that issue:
use lib 'D:/xampp/htdocs/cgi-bin/webscripts';
Or generically,
use Cwd qw( realpath );
use File::Basename qw( dirname );
use lib dirname(realpath($0));
Looks to me that on the file that give this error, it cannot locate the file inc/nph-globals.pl. probably in that file, you need to specify something like
#!"D:\xampp\perl\bin\perl.exe" -I <path_to_inc_nph-globals.pl>

Can't locate File/Glob.pm in #INC (#INC contains: D:/tools/lib .) at directory.pl line 2

I get this error when running my perl code
Can't locate File/Glob.pm in #INC (#INC contains: D:/tools/lib .) at directory.pl line 2.
line 2: #files=<*>;
When i run the command, I get,
Y:\perl\perl>perldoc -l File::Glob
D:\tools\lib\perl\510\File\Glob.pm
So I think the File::Glob module is installed?
#INC should be set correctly upon installation of Perl. When it doesn't match your configuration, you seem to have messed up something.
However, if the current value of #INC doesn't fit your needs, you have various options:
Add D:\tools\lib\perl\510\ to the
environment variable PERL5LIB (or PERLLIB if this doesn't work)
Specify #INC on startup:
perl -I D:\tools\lib\perl\510\
Instead of writing use libname, you can write use path/to/libname
Using a BEGIN block before the use statements:
BEGIN {
push #INC,"D:\tools\lib\perl\510\";
}
See also http://perldoc.perl.org/perlvar.html for a short introduction.