I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0
varies and sometimes contains the fullpath+filename
and sometimes just filename
. Because the working directory can vary as well I can't think of a way to reliably get the fullpath+filename
of the script.
我有Perl脚本,需要在执行过程中确定脚本的完整路径和文件名。我发现,根据调用脚本$0的方式不同,有时包含fullpath+文件名,有时只包含文件名。因为工作目录也可以变化,所以我想不出一种可靠地获得全路径+脚本文件名的方法。
Anyone got a solution?
谁有解决方案吗?
20 个解决方案
#1
138
$0 is typically the name of your program, so how about this?
$0通常是程序的名称,那么这个呢?
use Cwd 'abs_path';
print abs_path($0);
Seems to me that this should work as abs_path knows if you are using a relative or absolute path.
在我看来,这应该是有效的,因为abs_path知道您使用的是相对路径还是绝对路径。
Update For anyone reading this years later, you should read Drew's answer below. It's much better than mine.
对于今年晚些时候阅读本文的读者,您应该阅读下面的Drew的答案。比我的好多了。
#2
205
There are a few ways:
有几种方法:
-
$0
is the currently executing script as provided by POSIX, relative to the current working directory if the script is at or below the CWD - $0是POSIX提供的当前执行脚本,相对于当前工作目录(如果脚本在CWD以下)
- Additionally,
cwd()
,getcwd()
andabs_path()
are provided by theCwd
module and tell you where the script is being run from - 此外,cwd()、getcwd()和abs_path()由cwd模块提供,并告诉您脚本是从哪里运行的
- The module
FindBin
provides the$Bin
&$RealBin
variables that usually are the path to the executing script; this module also provides$Script
&$RealScript
that are the name of the script - 模块FindBin提供$Bin和$RealBin变量,这些变量通常是执行脚本的路径;这个模块还提供$Script和$RealScript,它们是脚本的名称
-
__FILE__
is the actual file that the Perl interpreter deals with during compilation, including its full path. - __FILE__是Perl解释器在编译期间处理的实际文件,包括它的完整路径。
I've seen the first three ($0
, the Cwd
module and the FindBin
module) fail under mod_perl
spectacularly, producing worthless output such as '.'
or an empty string. In such environments, I use __FILE__
and get the path from that using the File::Basename
module:
我已经看到,在mod_perl下,前三个($0、cdbin模块和FindBin模块)以惊人的速度失败,产生了“这样的无用输出”。或者一个空字符串。在这样的环境中,我使用__FILE__并从该文件中获取路径::Basename模块:
use File::Basename;
my $dirname = dirname(__FILE__);
#3
34
Use File::Spec;
File::Spec->rel2abs( __FILE__ );
http://perldoc.perl.org/File/Spec/Unix.html
http://perldoc.perl.org/File/Spec/Unix.html
#4
16
I think the module you're looking for is FindBin:
我想你们要找的模块是FindBin:
#!/usr/bin/perl
use FindBin;
$0 = "stealth";
print "The actual path to this is: $FindBin::Bin/$FindBin::Script\n";
#5
10
You could use FindBin, Cwd, File::Basename, or a combination of them. They're all in the base distribution of Perl IIRC.
您可以使用FindBin、Cwd、File: Basename或它们的组合。它们都位于Perl IIRC的基本分布中。
I used Cwd in the past:
我过去使用Cwd:
Cwd:
慢性消耗病:
use Cwd qw(abs_path);
my $path = abs_path($0);
print "$path\n";
#6
9
Getting the absolute path to $0
or __FILE__
is what you want. The only trouble is if someone did a chdir()
and the $0
was relative -- then you need to get the absolute path in a BEGIN{}
to prevent any surprises.
获取到$0或__FILE__的绝对路径是您想要的。唯一的问题是,如果某人执行了chdir(),而$0是相对的——那么您需要在BEGIN{}中获得绝对路径,以防止出现任何意外。
FindBin
tries to go one better and grovel around in the $PATH
for something matching the basename($0)
, but there are times when that does far-too-surprising things (specifically: when the file is "right in front of you" in the cwd.)
FindBin尝试做得更好,并在$PATH中寻找与basename($0)匹配的东西,但有时它会做一些太令人惊讶的事情(特别是:当cwd中的文件“就在您面前”时)。
File::Fu
has File::Fu->program_name
and File::Fu->program_dir
for this.
文件::Fu有文件::Fu->程序名和文件::Fu->程序目录。
#7
7
Some short background:
一些简短的背景:
Unfortunately the Unix API doesn't provide a running program with the full path to the executable. In fact, the program executing yours can provide whatever it wants in the field that normally tells your program what it is. There are, as all the answers point out, various heuristics for finding likely candidates. But nothing short of searching the entire filesystem will always work, and even that will fail if the executable is moved or removed.
不幸的是,Unix API并没有提供一个完整的可执行程序的运行程序。实际上,执行您的程序可以在通常告诉您的程序是什么的字段中提供它想要的任何东西。正如所有的答案所指出的,寻找可能的候选人有各种各样的启发式。但是,除了搜索整个文件系统之外,没有任何东西总是可以工作的,甚至如果可执行文件被移动或删除,也会失败。
But you don't want the Perl executable, which is what's actually running, but the script it is executing. And Perl needs to know where the script is to find it. It stores this in __FILE__
, while $0
is from the Unix API. This can still be a relative path, so take Mark's suggestion and canonize it with File::Spec->rel2abs( __FILE__ );
但是您不需要Perl可执行文件,它实际上正在运行,但是需要它正在执行的脚本。Perl需要知道脚本在哪里找到它。它将其存储在__FILE__中,而$0来自Unix API。这仍然可以是一个相对路径,因此根据Mark的建议,用文件:Spec->rel2abs(__FILE__)将其封为圣徒;
#8
6
Have you tried:
你有试过:
$ENV{'SCRIPT_NAME'}
or
或
use FindBin '$Bin';
print "The script is located in $Bin.\n";
It really depends on how it's being called and if it's CGI or being run from a normal shell, etc.
这取决于它是如何被调用的,如果是CGI或者从正常的shell中运行等等。
#9
5
In order to get the path to the directory containing my script I used a combination of answers given already.
为了获得包含我的脚本的目录的路径,我使用了已经给出的答案的组合。
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;
my $dir = dirname(File::Spec->rel2abs(__FILE__));
#10
2
perlfaq8 answers a very similar question with using the rel2abs()
function on $0
. That function can be found in File::Spec.
perlfaq8在$0上使用rel2abs()函数回答了一个非常类似的问题。该函数可以在文件::Spec中找到。
#11
2
There's no need to use external modules, with just one line you can have the file name and relative path. If you are using modules and need to apply a path relative to the script directory, the relative path is enough.
不需要使用外部模块,只需一行就可以拥有文件名和相关路径。如果您正在使用模块,并且需要对脚本目录应用一个路径,那么相对路径就足够了。
$0 =~ m/(.+)[\/\\](.+)$/;
print "full path: $1, file name: $2\n";
#12
1
#!/usr/bin/perl -w
use strict;
my $path = $0;
$path =~ s/\.\///g;
if ($path =~ /\//){
if ($path =~ /^\//){
$path =~ /^((\/[^\/]+){1,}\/)[^\/]+$/;
$path = $1;
}
else {
$path =~ /^(([^\/]+\/){1,})[^\/]+$/;
my $path_b = $1;
my $path_a = `pwd`;
chop($path_a);
$path = $path_a."/".$path_b;
}
}
else{
$path = `pwd`;
chop($path);
$path.="/";
}
$path =~ s/\/\//\//g;
print "\n$path\n";
:DD
:弟弟
#13
1
Are you looking for this?:
你在找这个吗?
my $thisfile = $1 if $0 =~
/\\([^\\]*)$|\/([^\/]*)$/;
print "You are running $thisfile
now.\n";
The output will look like this:
输出如下所示:
You are running MyFileName.pl now.
It works on both Windows and Unix.
它同时适用于Windows和Unix。
#14
0
use strict ; use warnings ; use Cwd 'abs_path';
sub ResolveMyProductBaseDir {
# Start - Resolve the ProductBaseDir
#resolve the run dir where this scripts is placed
my $ScriptAbsolutPath = abs_path($0) ;
#debug print "\$ScriptAbsolutPath is $ScriptAbsolutPath \n" ;
$ScriptAbsolutPath =~ m/^(.*)(\\|\/)(.*)\.([a-z]*)/;
$RunDir = $1 ;
#debug print "\$1 is $1 \n" ;
#change the \'s to /'s if we are on Windows
$RunDir =~s/\\/\//gi ;
my @DirParts = split ('/' , $RunDir) ;
for (my $count=0; $count < 4; $count++) { pop @DirParts ; }
my $ProductBaseDir = join ( '/' , @DirParts ) ;
# Stop - Resolve the ProductBaseDir
#debug print "ResolveMyProductBaseDir $ProductBaseDir is $ProductBaseDir \n" ;
return $ProductBaseDir ;
} #eof sub
#15
0
The problem with __FILE__
is that it will print the core module ".pm" path not necessarily the ".cgi" or ".pl" script path that is running. I guess it depends on what your goal is.
__FILE__的问题是它将打印核心模块。pm“路径不一定是”。cgi”或“。正在运行的脚本路径。我想这取决于你的目标是什么。
It seems to me that Cwd
just needs to be updated for mod_perl. Here is my suggestion:
在我看来,Cwd只需要为mod_perl更新。这是我的建议:
my $path;
use File::Basename;
my $file = basename($ENV{SCRIPT_NAME});
if (exists $ENV{MOD_PERL} && ($ENV{MOD_PERL_API_VERSION} < 2)) {
if ($^O =~/Win/) {
$path = `echo %cd%`;
chop $path;
$path =~ s!\\!/!g;
$path .= $ENV{SCRIPT_NAME};
}
else {
$path = `pwd`;
$path .= "/$file";
}
# add support for other operating systems
}
else {
require Cwd;
$path = Cwd::getcwd()."/$file";
}
print $path;
Please add any suggestions.
请添加任何建议。
#16
0
Without any external modules, valid for shell, works well even with '../':
没有任何外部模块,适用于shell,即使使用'. /':
my $self = `pwd`;
chomp $self;
$self .='/'.$1 if $0 =~/([^\/]*)$/; #keep the filename only
print "self=$self\n";
test:
测试:
$ /my/temp/Host$ perl ./host-mod.pl
self=/my/temp/Host/host-mod.pl
$ /my/temp/Host$ ./host-mod.pl
self=/my/temp/Host/host-mod.pl
$ /my/temp/Host$ ../Host/./host-mod.pl
self=/my/temp/Host/host-mod.pl
#17
0
The problem with just using dirname(__FILE__)
is that it doesn't follow symlinks. I had to use this for my script to follow the symlink to the actual file location.
使用dirname(__FILE__)的问题是它不遵循符号链接。我必须将它用于我的脚本,以跟踪到实际文件位置的符号链接。
use File::Basename;
my $script_dir = undef;
if(-l __FILE__) {
$script_dir = dirname(readlink(__FILE__));
}
else {
$script_dir = dirname(__FILE__);
}
#18
0
All the library-free solutions don't actually work for more than a few ways to write a path (think ../ or /bla/x/../bin/./x/../ etc. My solution looks like below. I have one quirk: I don't have the faintest idea why I have to run the replacements twice. If I don't, I get a spurious "./" or "../". Apart from that, it seems quite robust to me.
所有没有图书馆的解决方案实际上并不适用于编写路径的几种方法。/或/ bla / x / . . / bin /。/ x / . ./ etc.我的解决方案如下。我有一个怪癖:我一点也不知道为什么我要运行两次替换。如果我不这样做,我就会得到一个假的。/”或“. . /”。除此之外,在我看来,它似乎相当稳健。
my $callpath = $0;
my $pwd = `pwd`; chomp($pwd);
# if called relative -> add pwd in front
if ($callpath !~ /^\//) { $callpath = $pwd."/".$callpath; }
# do the cleanup
$callpath =~ s!^\./!!; # starts with ./ -> drop
$callpath =~ s!/\./!/!g; # /./ -> /
$callpath =~ s!/\./!/!g; # /./ -> / (twice)
$callpath =~ s!/[^/]+/\.\./!/!g; # /xxx/../ -> /
$callpath =~ s!/[^/]+/\.\./!/!g; # /xxx/../ -> / (twice)
my $calldir = $callpath;
$calldir =~ s/(.*)\/([^\/]+)/$1/;
#19
-1
What's wrong with $^X
?
怎么了X $ ^ ?
#!/usr/bin/env perl<br>
print "This is executed by $^X\n";
Would give you the full path to the Perl binary being used.
将为您提供使用的Perl二进制文件的完整路径。
Evert
翻转
#20
-5
On *nix, you likely have the "whereis" command, which searches your $PATH looking for a binary with a given name. If $0 doesn't contain the full path name, running whereis $scriptname and saving the result into a variable should tell you where the script is located.
在*nix上,您可能有“whereis”命令,该命令在您的$PATH中搜索具有给定名称的二进制文件。如果$0不包含完整的路径名,则运行$scriptname并将结果保存到一个变量中应该会告诉您脚本的位置。
#1
138
$0 is typically the name of your program, so how about this?
$0通常是程序的名称,那么这个呢?
use Cwd 'abs_path';
print abs_path($0);
Seems to me that this should work as abs_path knows if you are using a relative or absolute path.
在我看来,这应该是有效的,因为abs_path知道您使用的是相对路径还是绝对路径。
Update For anyone reading this years later, you should read Drew's answer below. It's much better than mine.
对于今年晚些时候阅读本文的读者,您应该阅读下面的Drew的答案。比我的好多了。
#2
205
There are a few ways:
有几种方法:
-
$0
is the currently executing script as provided by POSIX, relative to the current working directory if the script is at or below the CWD - $0是POSIX提供的当前执行脚本,相对于当前工作目录(如果脚本在CWD以下)
- Additionally,
cwd()
,getcwd()
andabs_path()
are provided by theCwd
module and tell you where the script is being run from - 此外,cwd()、getcwd()和abs_path()由cwd模块提供,并告诉您脚本是从哪里运行的
- The module
FindBin
provides the$Bin
&$RealBin
variables that usually are the path to the executing script; this module also provides$Script
&$RealScript
that are the name of the script - 模块FindBin提供$Bin和$RealBin变量,这些变量通常是执行脚本的路径;这个模块还提供$Script和$RealScript,它们是脚本的名称
-
__FILE__
is the actual file that the Perl interpreter deals with during compilation, including its full path. - __FILE__是Perl解释器在编译期间处理的实际文件,包括它的完整路径。
I've seen the first three ($0
, the Cwd
module and the FindBin
module) fail under mod_perl
spectacularly, producing worthless output such as '.'
or an empty string. In such environments, I use __FILE__
and get the path from that using the File::Basename
module:
我已经看到,在mod_perl下,前三个($0、cdbin模块和FindBin模块)以惊人的速度失败,产生了“这样的无用输出”。或者一个空字符串。在这样的环境中,我使用__FILE__并从该文件中获取路径::Basename模块:
use File::Basename;
my $dirname = dirname(__FILE__);
#3
34
Use File::Spec;
File::Spec->rel2abs( __FILE__ );
http://perldoc.perl.org/File/Spec/Unix.html
http://perldoc.perl.org/File/Spec/Unix.html
#4
16
I think the module you're looking for is FindBin:
我想你们要找的模块是FindBin:
#!/usr/bin/perl
use FindBin;
$0 = "stealth";
print "The actual path to this is: $FindBin::Bin/$FindBin::Script\n";
#5
10
You could use FindBin, Cwd, File::Basename, or a combination of them. They're all in the base distribution of Perl IIRC.
您可以使用FindBin、Cwd、File: Basename或它们的组合。它们都位于Perl IIRC的基本分布中。
I used Cwd in the past:
我过去使用Cwd:
Cwd:
慢性消耗病:
use Cwd qw(abs_path);
my $path = abs_path($0);
print "$path\n";
#6
9
Getting the absolute path to $0
or __FILE__
is what you want. The only trouble is if someone did a chdir()
and the $0
was relative -- then you need to get the absolute path in a BEGIN{}
to prevent any surprises.
获取到$0或__FILE__的绝对路径是您想要的。唯一的问题是,如果某人执行了chdir(),而$0是相对的——那么您需要在BEGIN{}中获得绝对路径,以防止出现任何意外。
FindBin
tries to go one better and grovel around in the $PATH
for something matching the basename($0)
, but there are times when that does far-too-surprising things (specifically: when the file is "right in front of you" in the cwd.)
FindBin尝试做得更好,并在$PATH中寻找与basename($0)匹配的东西,但有时它会做一些太令人惊讶的事情(特别是:当cwd中的文件“就在您面前”时)。
File::Fu
has File::Fu->program_name
and File::Fu->program_dir
for this.
文件::Fu有文件::Fu->程序名和文件::Fu->程序目录。
#7
7
Some short background:
一些简短的背景:
Unfortunately the Unix API doesn't provide a running program with the full path to the executable. In fact, the program executing yours can provide whatever it wants in the field that normally tells your program what it is. There are, as all the answers point out, various heuristics for finding likely candidates. But nothing short of searching the entire filesystem will always work, and even that will fail if the executable is moved or removed.
不幸的是,Unix API并没有提供一个完整的可执行程序的运行程序。实际上,执行您的程序可以在通常告诉您的程序是什么的字段中提供它想要的任何东西。正如所有的答案所指出的,寻找可能的候选人有各种各样的启发式。但是,除了搜索整个文件系统之外,没有任何东西总是可以工作的,甚至如果可执行文件被移动或删除,也会失败。
But you don't want the Perl executable, which is what's actually running, but the script it is executing. And Perl needs to know where the script is to find it. It stores this in __FILE__
, while $0
is from the Unix API. This can still be a relative path, so take Mark's suggestion and canonize it with File::Spec->rel2abs( __FILE__ );
但是您不需要Perl可执行文件,它实际上正在运行,但是需要它正在执行的脚本。Perl需要知道脚本在哪里找到它。它将其存储在__FILE__中,而$0来自Unix API。这仍然可以是一个相对路径,因此根据Mark的建议,用文件:Spec->rel2abs(__FILE__)将其封为圣徒;
#8
6
Have you tried:
你有试过:
$ENV{'SCRIPT_NAME'}
or
或
use FindBin '$Bin';
print "The script is located in $Bin.\n";
It really depends on how it's being called and if it's CGI or being run from a normal shell, etc.
这取决于它是如何被调用的,如果是CGI或者从正常的shell中运行等等。
#9
5
In order to get the path to the directory containing my script I used a combination of answers given already.
为了获得包含我的脚本的目录的路径,我使用了已经给出的答案的组合。
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;
my $dir = dirname(File::Spec->rel2abs(__FILE__));
#10
2
perlfaq8 answers a very similar question with using the rel2abs()
function on $0
. That function can be found in File::Spec.
perlfaq8在$0上使用rel2abs()函数回答了一个非常类似的问题。该函数可以在文件::Spec中找到。
#11
2
There's no need to use external modules, with just one line you can have the file name and relative path. If you are using modules and need to apply a path relative to the script directory, the relative path is enough.
不需要使用外部模块,只需一行就可以拥有文件名和相关路径。如果您正在使用模块,并且需要对脚本目录应用一个路径,那么相对路径就足够了。
$0 =~ m/(.+)[\/\\](.+)$/;
print "full path: $1, file name: $2\n";
#12
1
#!/usr/bin/perl -w
use strict;
my $path = $0;
$path =~ s/\.\///g;
if ($path =~ /\//){
if ($path =~ /^\//){
$path =~ /^((\/[^\/]+){1,}\/)[^\/]+$/;
$path = $1;
}
else {
$path =~ /^(([^\/]+\/){1,})[^\/]+$/;
my $path_b = $1;
my $path_a = `pwd`;
chop($path_a);
$path = $path_a."/".$path_b;
}
}
else{
$path = `pwd`;
chop($path);
$path.="/";
}
$path =~ s/\/\//\//g;
print "\n$path\n";
:DD
:弟弟
#13
1
Are you looking for this?:
你在找这个吗?
my $thisfile = $1 if $0 =~
/\\([^\\]*)$|\/([^\/]*)$/;
print "You are running $thisfile
now.\n";
The output will look like this:
输出如下所示:
You are running MyFileName.pl now.
It works on both Windows and Unix.
它同时适用于Windows和Unix。
#14
0
use strict ; use warnings ; use Cwd 'abs_path';
sub ResolveMyProductBaseDir {
# Start - Resolve the ProductBaseDir
#resolve the run dir where this scripts is placed
my $ScriptAbsolutPath = abs_path($0) ;
#debug print "\$ScriptAbsolutPath is $ScriptAbsolutPath \n" ;
$ScriptAbsolutPath =~ m/^(.*)(\\|\/)(.*)\.([a-z]*)/;
$RunDir = $1 ;
#debug print "\$1 is $1 \n" ;
#change the \'s to /'s if we are on Windows
$RunDir =~s/\\/\//gi ;
my @DirParts = split ('/' , $RunDir) ;
for (my $count=0; $count < 4; $count++) { pop @DirParts ; }
my $ProductBaseDir = join ( '/' , @DirParts ) ;
# Stop - Resolve the ProductBaseDir
#debug print "ResolveMyProductBaseDir $ProductBaseDir is $ProductBaseDir \n" ;
return $ProductBaseDir ;
} #eof sub
#15
0
The problem with __FILE__
is that it will print the core module ".pm" path not necessarily the ".cgi" or ".pl" script path that is running. I guess it depends on what your goal is.
__FILE__的问题是它将打印核心模块。pm“路径不一定是”。cgi”或“。正在运行的脚本路径。我想这取决于你的目标是什么。
It seems to me that Cwd
just needs to be updated for mod_perl. Here is my suggestion:
在我看来,Cwd只需要为mod_perl更新。这是我的建议:
my $path;
use File::Basename;
my $file = basename($ENV{SCRIPT_NAME});
if (exists $ENV{MOD_PERL} && ($ENV{MOD_PERL_API_VERSION} < 2)) {
if ($^O =~/Win/) {
$path = `echo %cd%`;
chop $path;
$path =~ s!\\!/!g;
$path .= $ENV{SCRIPT_NAME};
}
else {
$path = `pwd`;
$path .= "/$file";
}
# add support for other operating systems
}
else {
require Cwd;
$path = Cwd::getcwd()."/$file";
}
print $path;
Please add any suggestions.
请添加任何建议。
#16
0
Without any external modules, valid for shell, works well even with '../':
没有任何外部模块,适用于shell,即使使用'. /':
my $self = `pwd`;
chomp $self;
$self .='/'.$1 if $0 =~/([^\/]*)$/; #keep the filename only
print "self=$self\n";
test:
测试:
$ /my/temp/Host$ perl ./host-mod.pl
self=/my/temp/Host/host-mod.pl
$ /my/temp/Host$ ./host-mod.pl
self=/my/temp/Host/host-mod.pl
$ /my/temp/Host$ ../Host/./host-mod.pl
self=/my/temp/Host/host-mod.pl
#17
0
The problem with just using dirname(__FILE__)
is that it doesn't follow symlinks. I had to use this for my script to follow the symlink to the actual file location.
使用dirname(__FILE__)的问题是它不遵循符号链接。我必须将它用于我的脚本,以跟踪到实际文件位置的符号链接。
use File::Basename;
my $script_dir = undef;
if(-l __FILE__) {
$script_dir = dirname(readlink(__FILE__));
}
else {
$script_dir = dirname(__FILE__);
}
#18
0
All the library-free solutions don't actually work for more than a few ways to write a path (think ../ or /bla/x/../bin/./x/../ etc. My solution looks like below. I have one quirk: I don't have the faintest idea why I have to run the replacements twice. If I don't, I get a spurious "./" or "../". Apart from that, it seems quite robust to me.
所有没有图书馆的解决方案实际上并不适用于编写路径的几种方法。/或/ bla / x / . . / bin /。/ x / . ./ etc.我的解决方案如下。我有一个怪癖:我一点也不知道为什么我要运行两次替换。如果我不这样做,我就会得到一个假的。/”或“. . /”。除此之外,在我看来,它似乎相当稳健。
my $callpath = $0;
my $pwd = `pwd`; chomp($pwd);
# if called relative -> add pwd in front
if ($callpath !~ /^\//) { $callpath = $pwd."/".$callpath; }
# do the cleanup
$callpath =~ s!^\./!!; # starts with ./ -> drop
$callpath =~ s!/\./!/!g; # /./ -> /
$callpath =~ s!/\./!/!g; # /./ -> / (twice)
$callpath =~ s!/[^/]+/\.\./!/!g; # /xxx/../ -> /
$callpath =~ s!/[^/]+/\.\./!/!g; # /xxx/../ -> / (twice)
my $calldir = $callpath;
$calldir =~ s/(.*)\/([^\/]+)/$1/;
#19
-1
What's wrong with $^X
?
怎么了X $ ^ ?
#!/usr/bin/env perl<br>
print "This is executed by $^X\n";
Would give you the full path to the Perl binary being used.
将为您提供使用的Perl二进制文件的完整路径。
Evert
翻转
#20
-5
On *nix, you likely have the "whereis" command, which searches your $PATH looking for a binary with a given name. If $0 doesn't contain the full path name, running whereis $scriptname and saving the result into a variable should tell you where the script is located.
在*nix上,您可能有“whereis”命令,该命令在您的$PATH中搜索具有给定名称的二进制文件。如果$0不包含完整的路径名,则运行$scriptname并将结果保存到一个变量中应该会告诉您脚本的位置。