简体   繁体   English

在Perl中查找相对路径

[英]FInding relative path in Perl

I have the following code in a perl module, 我在perl模块中有以下代码,

package Foo;
our $pathToScript = "/home/Lucas/project841/python_script.py";

It is frequently called by other modules in the same file directory through 同一文件目录中的其他模块经常通过以下方式调用它

$output = `$Foo::pathToScript`;
# etc

I would like to remove the hard coding of the actual path and use relative path, Eg. 我想删除实际路径的硬编码,并使用相对路径,例如。 ./python_script.py to call the script from other modules. ./python_script.py从其他模块调用脚本。

What would be the ideal way? 理想的方法是什么?

You said your Perl script is "frequently" called from the same directory where the Python script resides, not "always". 您说过,“经常”从Python脚本所在的目录中调用Perl脚本,而不是“始终”调用。 If you remove the absolute path, you'll need to change that "frequently" to "always", and just change $pathToScript to the Python script name (no path). 如果删除绝对路径,则需要将其“经常”更改为“始终”,只需将$pathToScript更改$pathToScript Python脚本名称(无路径)。

You could also consider setting the environment PATH (in the Perl script) so that the Python script (without the full path in $pathToScript ) is always found, regardless of where the user is running from or where the Perl script is located. 您还可以考虑设置环境PATH(在Perl脚本中),以便始终找到Python脚本(在$pathToScript没有完整路径),无论用户从何处运行或Perl脚本位于何处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM