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

Gotta quickie question (all hands on buzzers...)

What's the quickest way in Perl to get the full path of current directory (where the script was called from)?

My current kludge is:
$currdir = `cd`;

But I have this feeling that there's some hieroglyphic-ish $_doodad for the same thing.
thx Desert coder

Replies are listed 'Best First'.
(Ovid) Re: Getting current directory
by Ovid (Cardinal) on Feb 09, 2001 at 22:45 UTC
    I think what you are looking for is the following:
    use Cwd qw/getcwd/; # Current working directory my $cwd = getcwd();

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Getting current directory
by gryphon (Abbot) on Feb 09, 2001 at 22:46 UTC

    Greetings JojoLinkyBob,

    use Cwd; my $cwd = cwd();

    You'll end up with the full directory path. On Win32, the "\"s are changed to "/"s. You also get the "C:" at the front.

    -Gryphon.

      Awesome, thanks!
      Desert coder
Re: Getting current directory
by oakley (Scribe) on Feb 09, 2001 at 22:51 UTC
    To get the current working directory, you could use:

    $currdir = $ENV{PWD};

    or

    $currdir = $0; <-- (gives full execution path AND program name though)

    - oakley
    Embracing insanity - one twitch at a time >:)
      Whoa, careful with that advice:
      print $ENV{PWD}, "\n"; chdir('perl'); print $ENV{PWD}, "\n"; print `ls`;
      Yeah, I wouldn't normally use the backticks there, but it proves my point. Also, $0 is spoofable. Cwd is a more reliable solution.

      Update: You can spoof $0 by using symlinks, at least if your OS supports them.

        Unlike argv[0] in C, $0 in Perl is not spoofable. See FindBin is broken (RE: How do I get the full path to the script executing?) for more details.

        But $0 is certainly inappropriate for getting the full path to "the current working directory" or "where the script was called from" (though I could see misinterpretting this last phrase to mean the full path to the script). $0 often tells you the full path to where the script resides.

                - tye (but my friends call me "Tye")
Re: Getting current directory
by tune (Curate) on Feb 10, 2001 at 08:34 UTC
    Probably the dumbest way, but nobody mentioned before:
    $current_path = `pwd`;
    BEAT ME BEAT ME, SPANK ME SPANK ME, but don't (--) me please :-)))

    -- tune