2

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?

melpomene
  • 84,125
  • 8
  • 85
  • 148
mo-ta-to
  • 248
  • 1
  • 5
  • 16
  • The directory in which the `DEBTOR...` resides is itself not in `@INC`, so `ENVRC.pl` is indeed not found in `@INC` ((unless there is a link to that `/apps/...` in some directory in `@INC`?) How is `DEBTOR...` program started? – zdim Oct 04 '18 at 04:42
  • Usually we run this 'DEBTOR_VERIFICATION.pl' program by connecting through putty. It runs fine without any issues. lincacsgqld.ams.com:/apps/CACSGDEV/springbatch/perlscript> DEBTOR_VERIFICATION.pl Executing program DebtorVerification return_code == 0 All transactions are processed without any validation errors Job Finished – mo-ta-to Oct 04 '18 at 05:45
  • Ah. When you run the program directly there is no need for `@INC`. When a file is to be loaded that's when `@INC` is searched for it. So, the directory of `ENVRC.pl` is not in `@INC` and the program can't be found. The other one runs OK because it is directly executed, not needing `@INC`. – zdim Oct 04 '18 at 05:48
  • [See also](https://stackoverflow.com/q/46549671/589924) – ikegami Oct 04 '18 at 08:28

1 Answers1

3

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.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • Re "*But if you know for fact that this program (DEBTOR_VERIFICATION.pl) will always stay in this directory, [...] it is better to use FindBin*", Other way around. The FindBin approach will continue to work if you move the files. – ikegami Oct 04 '18 at 08:31
  • @ikegami If both are moved, yes. The comment is on the possibility that the program is moved but the required file isn't. But how I put it isn't clear, editing.. – zdim Oct 04 '18 at 08:32
  • That's far less likely. Or rather, if one of the files is moved and not the other, it's probably the module, in which case both solutions will break – ikegami Oct 04 '18 at 08:33