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

I'm trying to run the following line of code in my Perl script my $output = qx "php -l -d log_errors=0 $pName"; This code works fine on another Windows server but not on the second server I moved it to. I can go into command prompt and type in echo %PATH% and the path to the php directory is correct and I can run php scripts directly from anywhere on that server while in command prompt. When running my CGI Perl script the command php cannot be found. If I print out the $ENV{"PATH"} the path to the PHP directory isn't there. What am I missing here? Supposedly I don't need to do a reboot so what else can I do? TIA for any "helpful" advice.

Replies are listed 'Best First'.
Re: Execute command
by hippo (Archbishop) on Apr 18, 2019 at 15:17 UTC
    When running my CGI Perl script the command php cannot be found. If I print out the $ENV{"PATH"} the path to the PHP directory isn't there.

    Either set $ENV{"PATH"} so that it does include the path to the php binary or else just call php with an absolute path instead.

    The very fact that you are using a Perl CGI script which is shelling out to PHP (on Windows no less) suggests that whatever it is you are actually trying to do could be better achieved some other way. But you probably knew that already.

Re: Execute command
by haukex (Archbishop) on Apr 18, 2019 at 18:31 UTC

    Most likely your web server is providing a different environment to the scripts it runs. If you check the web server's configuration, you may be able to find its settings on that environment, including PATH. However, it's usually best to use absolute pathnames, as already mentioned by hippo.

    my $output = qx "php -l -d log_errors=0 $pName";

    What is $pName? If it's anything derived from user input, then this is a massive security hole, see my post about this here. Even if it's not user input, it should be escaped properly to avoid potential issues with shell metacharacters. Since you say this is Windows, I would at the very least use Win32::ShellQuote to quote that command! For example:

    use Win32::ShellQuote qw/quote_system_string/; my $output = readpipe(quote_system_string("php","-l","-d","log_errors= +0",$pName));
Re: Execute command
by talexb (Chancellor) on Apr 18, 2019 at 14:51 UTC
      This code works fine on another Windows server but not on the second server I moved it to.

    Can you explain in more detail what your definition of 'not fine' is? A self-contained example would be lovely.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.