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

I've had expected results with a .pl script that had $|=1 in it running through mod_perl2, up until a recent fresh RedHat EL 5 installation where I had to reinstall my environment/software (via yum / rpms). Here is some environment-related info: Embedded Perl version v5.8.8 for Apache/2.2.3 (mod_perl v2.0.4) ... all x86_64.

/etc/httpd/conf.d/perl.conf:

LoadModule perl_module modules/mod_perl.so PerlRequire "/etc/httpd/mod_perl-startup.pl" Alias /perl/ "/var/www/cgi-bin/" <Directory /var/www/cgi-bin> SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders Options +ExecCGI </Directory>

/etc/httpd/mod_perl-startup.pl:

if ( ! $ENV{MOD_PERL}) { die "GATEWAY_INTERFACE not Perl!"; } use lib qw(/usr/local/lib); use ModPerl::Util (); #for CORE::GLOBAL::exit use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::RequestUtil (); use Apache2::ServerRec (); use Apache2::ServerUtil (); use Apache2::Connection (); use Apache2::Log (); use APR::Table (); use ModPerl::Registry (); use Apache2::Const -compile => ':common'; use APR::Const -compile => ':common'; 1;

The test script being accessed via Firefox/IE (doesn't matter what browser):

#!/usr/bin/perl -W use strict; print "Content-type: text/html\n\n"; $|=1; for (my $x=1; $x<=10; $x++) { print($x); sleep 1; } exit(0);

Apache starts normally... all other functions that I've used in the mod_perl environment work as expected... The $|=1 command doesn't do anything--printing still behaves in a buffered manner. I have a similar installation in RHEL 4 where unbuffered printing works as expected (via $|=1). I found a few interesting posts on Google... first, this one: http://www.nntp.perl.org/group/perl.beginners.cgi/2006/07/msg12693.html, which basically describes that perl's STDOUT is redirected to Socket. Looking at the mod_perl2 documentation (http://perl.apache.org/docs/2.0/api/Apache2/RequestIO.html#C_print_, it references setting $|=1 as one would expect to create the desired unbuffered behavior.

What am I missing... I feel like it's some configuration within mod_perl itself (mod_perl-startup.pl / perl.conf) or Apache's config (httpd.conf)? Any help is GREATLY appreciated! Thank you in advance, and please let me know if additional environment info is needed...

Replies are listed 'Best First'.
Re: mod_perl2 unbuffered printing
by aitap (Curate) on Jul 24, 2012 at 07:06 UTC
    Are you sure it's not caused by browser? If you try some sniffer (for example, Wireshark), will it show that you receive buffered output? Modern browsers seem to cache the page and download it fully before rendering.
    Sorry if my advice was wrong.
Re: mod_perl2 unbuffered printing
by Anonymous Monk on Jul 24, 2012 at 07:24 UTC

      Your reference specifically helped... http://honglus.blogspot.com/2010/08/resolving-perl-cgi-buffering-issue.html. Now, the code works as I expect (see below). I guess this is normal behavior and something was screwed up on my RHEL 4 server... hm. Also, since i'm doing my own "buffer management", I removed the $|=1; line.

      #!/usr/bin/perl -W use strict; my $r = shift; print "Content-type: text/html\n\n"; for (my $x=1; $x<=10; $x++) { print($x); sleep 1; $r->rflush; } exit(0);

      Thank you!