Q-Bert has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get the PWD or CWD (current working directory) in a Perl script that is to be run on both Linux and NT4.0
I currently use
chop(my $application_pwd = `pwd`);
but of course, this doesn't work on Win32.

I checked out CPAN and found nothing that can help me.
Suggestions?
Pat.

Replies are listed 'Best First'.
Re: Platform independant PWD
by davemabe (Monk) on Nov 16, 2000 at 20:59 UTC
    In case you missed the excitement in the chatterbox, try

    perl -MCwd -e "print cwd();"

    That should work.
    davemabe
Re: Platform independant PWD
by Q-Bert (Novice) on Nov 16, 2000 at 20:57 UTC
    To answer my own question:
    As briliantly reported by davemab

    Use Cwd; my $cwd_var = cwd(); print "My current working directory: $cwd_var\n";
    This works on both Unix and Win32.

    Pat.

Re: Platform independant PWD
by curtisb (Monk) on Nov 16, 2000 at 22:10 UTC
    The Cwd module is a good start. I recommend using it. The Cwd module will work in Unix, Linux, and Win32.
    Cwd module has three functions: cwd, getcwd, and fastcwd. You can a brief summury of this in the "Perl in a Nutshell" book. (Not a plug for OReilly)
    curtisb -- "Trying to help others"
      It works in MacPerl as well.
Re: Platform independant PWD
by Albannach (Monsignor) on Nov 16, 2000 at 20:56 UTC
    Well you can use `cd` in Win32, but unless you want to install a pwd binary on Win32 I can't think of a real platform independent way to do it.

    Update: D'oh! Ok, I forgot about cwd(), but to justify this update I'll just note that `cd` and cwd() give you different directory slashes, which might matter depending on what you wanted to do with the results.

      although, you could write a little trap for OS.... I don't use win32, so I don't know the value of $^O on win32, but you could do something like this:
      if ($^O =~ /linux/) { $working_directory = `pwd`; } elsif ($^O =~ /win32/) { # or value for win32 $working_directory = `cd`; } else { $working_directory = "./"; # :) }