We recently inherited a somewhat legacy perl application and we’re working to migrate to a new server as well as setup a sandbox environment so we can start sorting the application out. The issue we’re having is the code currently uses some relative paths for the open
method. And currently works in their shared hosting environment (we have little access to)
open(HANDLE,"<../relative/path/to/file.txt")
We pulled all of the code, paths, etc. over and for the most part have the application up and running until we run into one of the scripts that does the above, opens a file with a relative path. Then it fails.
If we run the code via the command line, the relative paths work. If we modify the path to be the full path it works both via command line and through Apache (navigating to the page in the browser).
This makes me think there is some module or configuration option we need to set in Apache to allow for the perl scripts to access or use the open
command with relative paths?
4
Answers
As mentioned in the comment above. Our final solution was to use:
This allowed us to get the code working while also kept additional code modifications to a minimum.
Relative paths are relative to the process’s current work directory, which is not necessarily the same as a the directory containing the program. It fact, it’s often
/
for daemons (and thus their children).For paths relative to the program’s location, use
It is bad practice to specify file’s path in open with a fixed string unless the path is predefined and never change – as for example with /etc/fstab in linux.
You should change the code to use variable(s) instead.
Define variable at the top of the script — in feature if you need to change base and path you will know that you find it at the few first lines of the code.
In such situation add temporary in the code something as
Once you run your script from webserver you should look for the file my_uniq_tile.txt — it’s location will be default base of web server for the file(s) location.
Now adjust variables with file path accordingly.
I was running a script out of cgi-bin and wanted to open a template in htdocs/templates so I did this in my script…