Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I have perl code wrapped in a batch file so that it can be run anywhere as long as the batch is in the environmental path. The only problem is that the code employs 2 package modules which it can't find when run from a separate directory. I have used 'perl -x -S' hoping that perl would 'cd' to the directory where the batch file resides and find the packages. This didn't work. I'm thinking i have to include both packages in the batch file as well. What are my options? thanks so much, michael

Replies are listed 'Best First'.
Re: perl wrapped in batch
by Joost (Canon) on Nov 18, 2002 at 16:26 UTC
    If you want a library path relative to the perl-script's location, you can do this:
    use FindBin; use lib "$FindBin::Bin/path/to/modules"; use Other::Module;
    see FindBin for more info.
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Thanks for your help joost. it seems from your inclusion that I need to know the path to the modules beforehand. the client will be setting the path to the modules in the environment (via My Computer) so i will not know this path. please tell me if i misunderstood. Is there anyway in perl to get the path the OS uses (after searching the PATH) to run the script? thanks again, michael
        ah, I see,

        The easiest way for this is to set the environment variable PERL5LIB instead of PATH to point to the module files.

        From perlrun:

        PERL5LIB
        A colon-separated list of directories in which to look for Perl library files before looking in the standard library and the current directory.
        Any architecture-specific directories under the specified locations are automatically included if they exist. If PERL5LIB is not defined, PERLLIB is used.
        -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: perl wrapped in batch
by dingus (Friar) on Nov 18, 2002 at 17:05 UTC
    Hi all. I have perl code wrapped in a batch file so that it can be run anywhere as long as the batch is in the environmental path. The only problem is that the code employs 2 package modules which it can't find when run from a separate directory. I have used 'perl -x -S' hoping that perl would 'cd' to the directory where the batch file resides and find the packages. This didn't work. I'm thinking i have to include both packages in the batch file as well. What are my options? thanks so much, michael

    Maybe you should -I%path% as well? I.e. perl initing line of myscript.bat is

    @perl -I%path% -x -S %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
    Better would be to have a separate variable possibly set in the .bat file and then used in the start line as %path% is liable to be rather big.

    You could also investigate having the -I switch on the

    #! /path/perl
    line

    Dingus


    Enter any 47-digit prime number to continue.
Re: perl wrapped in batch
by John M. Dlugosz (Monsignor) on Nov 18, 2002 at 17:16 UTC
    I run Perl programs from anywhere on the PATH, without using a batch file or anything like that.

    I use the 4NT command-line shell, as an upgrade to the stupid COMMAND.COM/CMD.EXE that comes with Windows. But I understand that those can understand the file associations and run the correct application too, if you launch the command shell program itself with the /X option.

    —John

      thanks to all who replied. with all your suggestions, I came up with this:
      use lib split(';',$ENV{PATH});
      which correctly finds the required packages. it ain't pretty (it includes alot of useless junk), but it works. thanks again. michael