Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I find what directory the perl script is running from?

by punksk8er (Initiate)
on Dec 16, 2000 at 13:05 UTC ( [id://47035]=perlquestion: print w/replies, xml ) Need Help??

punksk8er has asked for the wisdom of the Perl Monks concerning the following question: (directories)

How do I find what directory the perl script is running from?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I find what dir the perl script is running from?
by Fastolfe (Vicar) on Dec 16, 2000 at 23:42 UTC
    Since you put this in the CGI section, I'm assuming that by Perl script you mean CGI script. Web servers typically run these out of your cgi-bin directory, using that directory as the script's current working directory. Unfortunately your question is a bit vague. Do you mean the script's current working directory, or where the script physically resides on the filesystem?

    If it's the former, use the Cwd module for a portable way to do this. If it's the latter, this isn't really a CGI issue and is already discussed elsewhere. See: How do I get the full path to the script executing? The only catch is that this information can be faked or made unreliable, but it's usually OK so long as you're not relying on it as a security mechanism.

Re: How do I find what dir the perl script is running from?
by merlyn (Sage) on Dec 16, 2000 at 13:53 UTC
    No. There are partial answers based on examining $0 and $ENV{PATH}, with algorithms rolled up into File::FindBin, but nothing will ever be completely unspoofable or accurate.

      See FindBin is broken (RE: How do I get the full path to the script executing?) for why this is completely wrong. (:

      Update: Just FYI, I interpret the original question as wanting to know about getcwd() and use Cwd, rather than wanting the full path to the script (which is what File::FindBin tries to find in a very perverse way). But it is hard to say for sure.

              - tye (but my friends call me "Tye")
Re: How do I find what dir the perl script is running from?
by cei (Monk) on Dec 19, 2000 at 01:39 UTC
    I picked up this cross-platform directory checker somewhere... Works on both NT and *NIX
    if ($0=~m#^(.*)\\#) { $cgi_dir = "$1"; } elsif ($0=~m#^(.*)/# ) { $ cgi_dir = "$1"; } else {`pwd` =~ /(.*)/; $ cgi_dir = "$1"; }
Re: How do I find what dir the perl script is running from?
by Theodorus (Initiate) on Dec 29, 2000 at 20:31 UTC
    I use:

    use FindBin qw($Bin);

    $Bin is set to the value of the script's path.
      Even better (for Unix), you can try one of the following...

      $var = `/usr/bin/pwd`; # put the current dir in to $var
      

      print `/usr/bin/pwd`; # print the cwd to STDOUT

        this way you'll get dir the script is running _on_, and not the path script is running _from_
Re: How do I find what dir the perl script is running from?
by tachyon (Chancellor) on Feb 27, 2003 at 11:02 UTC
    use Cwd; print "The current working directory is: ", cwd();

    Originally posted as a Categorized Answer.

Re: How do I find what dir the perl script is running from?
by Vennis (Pilgrim) on Feb 27, 2003 at 07:18 UTC
    This works quite well:
    $0=~/^(.+[\\\/])[^\\\/]+[\\\/]*$/; $cgidir= $1 || "./";
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://47035]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found