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

Anyone know a super-easy way for your program to determine at run-time the directory in which it is running? E.g. perl special variable, etc.

Oh, and in case you're wondering why I want to know, I use several different machines while developing and want to not have to change directories in the test scripts.

Thanks,

Jon

  • Comment on Directory in which your program is running...

Replies are listed 'Best First'.
•Re: Directory in which your program is running...
by merlyn (Sage) on Jan 26, 2003 at 18:05 UTC
    It's not clear to me whether you mean the directory in which the program is located, or the current directory of the process that invoked the program, or the current directory of the process as possibly modified by calls to chdir. Those are three entirely different things. You can get the first using FindBin, and the last by Cwd, but the middle one you have to do early enough in the program. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Oops. I should have specified: the directory in which the program is located; which should be the same as the directory of the process since I'm not using any chdir calls.

      Thanks,

      Jon

      Update: further clarification per Merlyn's advice below: I will only be running the scripts from the directory they are in.

        Oops. I should have specified: the directory in which the program is located; which should be the same as the directory of the process since I'm not using any chdir calls.
        No, that's precisely the thinking that will get you into trouble, which is why I asked the question I did.

        When I invoke "some/path/to/script", my current directory hasn't changed, but the script is located in some other directory. So I wanted to know which of the two you wanted: the current directory, or the directory of the script.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        Another way to do it:
        use File::Basename; my $dir = dirname $0;
        Or, if you are *sure* that you will only be running the scripts from the directory that they are in, you can specify relative paths starting from ".".
Re: Directory in which your program is running...
by broquaint (Abbot) on Jan 26, 2003 at 18:08 UTC
Re: Directory in which your program is running...
by pfaut (Priest) on Jan 26, 2003 at 18:07 UTC

    If you want the user's current default directory, look at perldoc Cwd.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';