在Perl中获取当前工作目录路径

问题描述:

我使用$FindBin::RealBin获取perl脚本位置。现在我有一个使用这个问题。 我从一个Perl脚本调用Perl脚本。 在调用者脚本中,$FindBin::RealBin工作正常,但在调用Perl脚本中,它没有给出位置。 我错过了什么吗?在Perl中获取当前工作目录路径

+1

这是否工作http://perl.active-venture.com/lib/Cwd.html? – huon 2012-03-29 05:08:42

+0

'$ FindBin'用于查找Perl脚本所在的目录。这与Perl进程的当前工作目录不同。你真的在找哪个? – 2012-03-29 05:09:26

+0

我想只获得perl脚本位置..我如何在被调用的perl脚本中获取它..当我使用$ FindBin :: RealBin时它是空的。另一种方式来获得它? – siri 2012-03-29 05:15:41

由于您没有提供完整的代码示例,因此更多的是猜测。
按照documentation,你需要调用

FindBin::again(); 

,因为这是FindBin的一个已知的限制。

如果我理解你的问题,你可以使用realpathCwd

$ cat ./mycode 
#!/usr/bin/env perl 
use strict; 
use warnings; 
use Cwd; 
print "called as '$0'\n"; 
print "lives in '", Cwd::realpath($0), "'\n"; 

$ ./mycode 
called as './mycode' 
lives in '/Users/jrf/Sandbox/mycode' 

这是我一直使用:

my ($vol,$script_path, $prog) = File::Spec->splitpath(File::Spec->rel2abs(__FILE__)); 

检查,如果它在你的情况。如果您将内部脚本作为shell调用调用,它应该可以工作。我不知道如果你用do来调用它,它是否会起作用。

一些有关此读数:

看到How do I get the full path to a Perl script that is executing?

FindBin ::彬被打破http://use.perl.org/~Aristotle/journal/33995(或谷歌的缓存http://webcache.googleusercontent.com/search?q=cache:y-5OZsxdTT8J:use.perl.org/~Aristotle/journal/33995

文件::基名http://perldoc.perl.org/File/Basename.html更成问题

希望它有帮助