In early 1.3 versions of Apache - it was enough to simply do $| = 1. In later versions - and especially in Apache 2 versions you must flush the buffer your self. Your example above would turn into the following:
#!/usr/bin/perl
use strict;
my $r = Apache->request; # mod_perl 1
print "Content-type: text/html\n\n";
print "<html><head></head><body>";
print "START\n";
$r->rflush;
sleep 5;
print "End\n";
print "</body></html>\n";
If you want everything to be buffered I would make a sub like the following:
my $my_print = sub {
print @_;
$r->rflush;
};
# call it like this
$my_print->("<html><head>....");
If your script is going to be migrating from CGI to mod_perl 1 to mod_perl 2 I would recommend using the CGI::Ex module which lets you handle all three without modifying your script.
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Ex;
my $cgix = CGI::Ex->new;
my $my_flush;
if (! $cgix->mod_perl_version) {
$| = 1;
$my_flush = sub {}; # no op
} else {
$my_flush = sub {
# works on most apache versions
$cgix->apache_request->rflush;
};
}
$cgix->print_content_type;
print "<html>...\n";
$my_flush->();
sleep 5;
print "The rest...\n";
my @a=qw(random brilliant braindead); print $a[rand(@a)];
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.