djneely has asked for the wisdom of the Perl Monks concerning the following question:

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 path works. 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? After reading a few other posts that seem to have the same issue (but never saw a final solution) we added in a couple print statements
print `pwd`; print `ls -l`;
And the script despite being in /srv/site/scripts/script.pl and accessible via example.com/scripts/script.pl prints out
/ the contents of / of the server... /etc, /var, /home, etc...
Thoughts on what we're doing wrong here and why the script does not see its actual path and therefore able to access another file up a directory and into another?

Replies are listed 'Best First'.
Re: Perl - open with Apache
by choroba (Cardinal) on Feb 04, 2020 at 17:32 UTC
    Just a comment: pwd is the current working directory, not the directory where the program resides. These two can be identical, but they don't have to, and relying on such a fact is dangerous.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl - open with Apache
by hippo (Archbishop) on Feb 04, 2020 at 17:37 UTC

    How does Apache invoke the script? mod_perl? CGI? FastCGI? It may simply be a setting in the configuration of one of these within Apache. If you just have a script which only prints those diagnostics and then exits, doing literally nothing else, then you can confirm that the PWD is being set outside the script itself. I would warn() instead of print() for this (so it will appear in the error log) but YMMV:

    #!/usr/bin/env perl use strict; use warnings; warn "Directory is " . `pwd`; warn "Contents are \n" . `ls -al`; exit;

    If that confirms it you might also want to dump %ENV to see what else might be going on.

      Interesting. Using your code in another script in the same directory. I'm getting the following. Not sure where that gets me but results I did not expect.
      ==> /var/log/apache2/error.log <== Directory is /srv/site/scripts Contents are total 36 drwxr-xr-x 2 derek derek 4096 Feb 4 17:49 . drwxrwxr-x 10 derek derek 4096 Feb 4 17:04 .. -rw------- 1 derek derek 12288 Feb 4 17:49 .test3.pl.swp -rw-r--r-- 1 derek derek 7 Feb 4 17:09 config.txt -rw-r--r-- 1 derek derek 434 Feb 4 17:41 test.pl -rw-r--r-- 1 derek derek 394 Feb 4 17:41 test2.pl -rw-r--r-- 1 derek derek 118 Feb 4 17:49 test3.pl
      As far as Apache goes the config is just simply:
      <Files ~ "\.(pl|cgi)$"> SetHandler perl-script PerlResponseHandler ModPerl::PerlRun Options +ExecCGI PerlSendHeader On </Files>

      Quick update and curious as to the difference.

      The header of the file on the old server is: #!/usr/local/bin/perl

      The scripts that we've been testing with are: #!/usr/bin/perl

      And the one provided in your test script was: #!/usr/bin/env perl

      When we update the scripts in questions to use the #!/usr/bin/env perl they seem to work.

      Thanks for your help been a long time since I've done perl and coming back at this as a noob.