简体   繁体   中英

FInding relative path in Perl

I have the following code in a perl module,

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.

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". 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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