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

hello
does perl has a command for knowing the current directory on runtime (i'm sure there is, i just don't remember)

Hotshot

Replies are listed 'Best First'.
Re: knowing the current directory
by guha (Priest) on Nov 29, 2001 at 14:14 UTC
    The module Cwd in standard distribution (AFAIK) comes to rescue

    Use Cwd; my $current_dir = cwd(); print "The current working directory is $current_dir\n";
    Cwd works cross platform

    HTH

Re: knowing the current directory
by xornand (Initiate) on Nov 29, 2001 at 16:07 UTC
    yes Cwd Works fine. but it is very expensive and has to maintain the working directory all time. So use it carefully and only when required. xornand

      I've had problems with Cwd in the unix environment whenever your process is non-root and is working in a subdirectory of a directory that has the execute bit witout the read bit set.

      My understanding is that Cwd essentially just does a CD .. until it gets to / and then CD's back to where it came from. But, if it can not read every single directory on the way back to / it gets confused.

      There is a way to get Cwd make a system call to /bin/pwd, but if that's all the module is good for, then you might as well just do something like this:

      chomp ( my $dir = `/bin/pwd` );

      There probably is some way to make Cwd always do the right thing. If anybody knows what that is, I'd love to know

Re: knowing the current directory
by r3b3lxd (Initiate) on Nov 29, 2001 at 22:37 UTC
    The special variable $0 contains the full path of the script including the script name. If you knew the name of the script then to work out the directory the script is in you could use something like:
    ($cur_dir = $0) =~ s/$name_of_script//;
    Rob
Re: knowing the current directory
by Dogma (Pilgrim) on Nov 29, 2001 at 18:41 UTC
    If your just trying to make sure you know what the current directory is your could simply chdir("/some/known/dir"); at the top of your code. Else go with Cwd and unload it when your done with it.
      sorry about the ignorance but what do you mean by unloading Cwd?

      Hotshot
Re: knowing the current directory
by IlyaM (Parson) on Nov 30, 2001 at 23:33 UTC