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

I have several perl programs:

mainApp.pl
subApp1.pl
subApp2.pl

they are all in one directory, say, /path/abc/bin/

One of them is the main application program: mainApp.pl, and all the others are called by the main script,but they are not libraries (in the format of functions), they are independent script and can be run individually as well. They are called in this way:

...
$command = "subApp1.pl -f $config_file";
$status = system( $command);
...

In Unix, I made all the scripts into executable, and add the path to the directory to PATH enviornment variable,then I can run the mainApp.pl from anywhere on the machine. IT works.

Then I tested it on windows, I also added the directory where all the scripts stays to the PATH enviornment variable, and run the mainApp.pl from an arbitrary directory. But as I run mainApp.pl to the point where subApp.pl is called, it said:

Can't open perl script "subApp1.pl": No such file or directory.

I don't want to hardcode the absolute path to my code. What can I do?

Thanks ahead.
  • Comment on Path to a perl executable file on windows

Replies are listed 'Best First'.
Re: Path to a perl executable file on windows
by BrowserUk (Patriarch) on Jul 11, 2003 at 18:08 UTC

    There is no need to use the hooky FindBin method. :)

    If the scripts are in a directory in your path, there are a couple of further steps needed.

    1. Tell the OS that you wish it to consider .pl (or whatever extension you choose) to be considered and 'executable' file.

      You do this by adding your chosen extension to the PATHEXT environment variable. The best way is to do this through the Start->Settings->ControlPanel->System-> Environment tab. Where you can choose to make this so system wide (if you have appropriate privaledges) or on a user by user basis.

    2. Tell the OS what executable you which to be invoked for .pl files through the usual ftype and assoc mechanism.

    It a different (but quite flexible) mechanism from *nix, but it does have advantages as well as disadvantages.

    I currently use 3 differnt extensions:

    1. .pl for 5.6.1 scripts.
    2. .pl8 for 5.8 scripts.
    3. .plt for my experimental (test) build of perl.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Thanks to all who replied to my question.

      I tried the easiest answer, which is changing the PATHEXT environment variable and it works very well!!! :)

Re: Path to a perl executable file on windows
by davorg (Chancellor) on Jul 11, 2003 at 15:30 UTC

    You could try using the FindBin module.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Path to a perl executable file on windows
by halley (Prior) on Jul 11, 2003 at 16:49 UTC
    I've used this in my own projects.
    use FindBin; use lib $FindBin::Bin;
    A side-note: when running perl as root, the '.' entry is automatically removed from the initial @INC value. Likewise, if your scripts are to be run from root authority, do try to validate the @INC so you're sure you're not drawing in some unexpected version of subApp1.pl. What checking you do will depend on your circumstances.

    --
    [ e d @ h a l l e y . c c ]