http://www.perlmonks.org?node_id=1059563

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

What would cause a perl script to behave differently in different directories on one system? The system is running RedHat 3.4.3-9, and perl version 5.8.5.

The following perl script runs correctly when run from /home/test. After copying this script to /var/www/cgi-bin, it runs wrong: 1) Its stdout is directed somewhere other than back to the user, and 2)It does not receive the output from the call to pread.

Here is the perl script:

#!/usr/bin/perl my @cc = (); @cc = `pread cm 98`; print "Content-type: text/html\n\n"; print <<HTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML lang='en'> <HEAD> <TITLE> Hello World Status </TITLE> </HEAD> <BODY> hello world: $cc[0] </BODY> </HTML> HTML _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ When run from the directory /home/test as [xyz@daystar test]$ ./tst.pl the output is correct: Content-type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML lang='en'> <HEAD> <TITLE> Hello World Status </TITLE> </HEAD> <BODY> hello world: RETAIL WEBSALE </BODY> </HTML> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ There is no output when run from directory /var/www/cgi-bin as [xyz@daystar test]$ ./tst.pl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ However, when run and piping output to 'more' using the command [xyz@daystar test]$ ./tst.pl | more the output is: Content-type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML lang='en'> <HEAD> <TITLE> Hello World Status </TITLE> </HEAD> <BODY> hello world: </BODY> </HTML>

Replies are listed 'Best First'.
Re: perl stdout and backtick execute
by dasgar (Priest) on Oct 25, 2013 at 00:13 UTC

    I'm more of a Windows guy, but that /var/www/cgi-bin looks like it's a directory used by a web server. If so, there may be different permissions for that directory and the STDOUT of the script when running from that directory might not be STDOUT of the shell (i.e. it's expecting to be run via web browser request through the web server, in which case STDOUT is HTTP response back to requesting client browser). Also, you might want to double-check the permissions for the script file after copying it to that directory.

    That's my initial thoughts. But like I said, I'm more familiar with Windows, which means I could be wrong.

Re: perl stdout and backtick execute
by sundialsvc4 (Abbot) on Oct 25, 2013 at 00:16 UTC

    Just to clarify ... you are executing this program from the command-line, as a logged-on user, and not as “a CGI (web...) request ...?   Even though you “just happened to” move it to ... cgi_bin?   You’re not trying to run this script from a web-page, are you?   You’re not trying to diagnose a problem where Apache/CGI won’t run this program, are you?

      I am executing this script (tst.pl) from the command-line as low-level user xyz. The script is not owned by xyz; it is owned by user abc. Currently I am trying to prove that the pieces operate correctly together. Ultimately, the script will by executed from a web-page via Apache/CGI, hence it does a database inquiry (pread), and will return the output to the browser.
Re: perl stdout and backtick execute
by Eily (Monsignor) on Oct 25, 2013 at 08:50 UTC

    What is @cc = `pread cm 98`; supposed to do? I have found the C function but not the shell one. If this is executed in bash, 'cm' is more likely a file than a file handle, so you have to be in the same directory as the file to see it with its relative path.

    It would probably be a good idea to check $?.