The system call forks a process and that process inherits the STDOUT
from perl. So since your perl is the persistent interpreter in Apache,
it would make sense that that's where most of the STDOUT messages will go.
I'm not sure if there is a way to intercept the STDOUT of the forked
process in this context, but from the perldoc on system:
"The return value is the exit status of the program
as returned by the "wait" call. To get the actual
exit value shift right by eight (see below). See
also "exec". This is _not_ what you want to use to
capture the output from a command, for that you
should use merely backticks or "qx//", as described
in "`STRING`" in perlop."
Niels Larsen wrote:
Greetings,
I have moved a big collection of modules to mod_perl2 under PerlRun
and can see a great speedup, as hoped. But in some of these modules
programs are run that write something to STDOUT that is then captured
and processed - which doesnt work since STDOUT is tie'ed to Apache.
I saw suggestions to use the $r-> interface, but I prefer to not be
mod_perl specific (write handlers or use the request object etc), at
least not until later. So to "borrow" STDOUT temporarily I did the
attempt below (found in Stas Bekmans 1.0 practical mod_perl), which
opens a temporary STDOUT into a string. Printing to STDOUT works as
hoped, but output from other programs run by system do not appear
in $out_str; instead they appear - most of the time, sometimes not -
in the console where Apache was started (with httpd -X). I can put
the command line to run in backticks and do print [EMAIL PROTECTED], and
that too works, but would like to use system, Proc::SafeExec, etc.
I use Apache 2.2.9, mod_perl 2.0.4 and Linux. Below the example is
the mod_perl part of httpd.conf.
Any ideas? (I'm sure its something obvious i missed, but it has
become an obstacle for me)
Niels L
------------------------------ test.cgi
<pre>
#!/usr/bin/env perl
use strict;
use warnings FATAL => qw ( all );
use Symbol;
my ( $out_fh, $out_str );
{
$out_str = "";
$out_fh = Symbol::gensym();
open $out_fh, '>', \$out_str or die "Can't open stdout to string: $!";
$| = 1;
local *STDOUT = $out_fh;
# Print always puts output into always goes into $out_str,
print "hello\n";
# But commands like date, ls etc goes into $out_str sometimes,
# sometimes at console where apache was started with httpd -X
# system( "date" );
# print `date`; # works
close $out_fh;
}
#print "Content-type: text/html\n\n";
print "<pre>\n";
print $out_str;
print "</pre>\n";
print $ENV{"MOD_PERL"} ."<br>";
print $ENV{"GATEWAY_INTERFACE"} ."<br>";
print $ENV{"SERVER_PROTOCOL"} ."<br>";
</pre>
--------------- httpd.conf
SetEnv TERM "dumb"
PerlSetEnv TERM "dumb"
LoadModule perl_module modules/mod_perl.so
PerlModule ModPerl::PerlRun
<LocationMatch "/test.cgi">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options SymLinksifOwnerMatch ExecCGI
</LocationMatch>
--
Jim Brandt
Administrative Computing Services
University at Buffalo