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. | [reply] [Watch: Dir/Any] |
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. | [reply] [Watch: Dir/Any] |
|
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")
| [reply] [Watch: Dir/Any] [d/l] |
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";
}
| [reply] [Watch: Dir/Any] [d/l] |
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. | [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] |
|
this way you'll get dir the script is running _on_, and not the path script is running _from_
| [reply] [Watch: Dir/Any] |
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. | [reply] [Watch: Dir/Any] [d/l] |
Re: How do I find what dir the perl script is running from?
by Vennis (Pilgrim) on Feb 27, 2003 at 07:18 UTC
|
$0=~/^(.+[\\\/])[^\\\/]+[\\\/]*$/;
$cgidir= $1 || "./";
| [reply] [Watch: Dir/Any] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |