Since mod_perl wraps my entire code into a handler subroutine behind
the scenes, I figured out how to access the apache API from a normal
cgi script. Hence, the following example appears to be working under
mod_perl2:

#!/usr/bin/perl

my $r = shift;
use CGI;
my $cgi = new CGI;
print $cgi->header();
print $cgi->start_html();
$r->rflush;
print $cgi->h2("test1");
sleep 1;
$r->rflush;
print $cgi->h2("test2");
sleep 1;
$r->rflush;
print $cgi->h2("test3");
sleep 1;
$r->rflush;
print $cgi->end_html();


With my more complicated example (listed below), I get mixed results.
Sometimes it works, sometimes I get segmentation faults, and sometimes
I get the following error in my Apache error_log which I don't
understand:

"$r->rflush can't be called before the response phase at
/html/perl/test.pl line 107"

The complete code is as follows:

#!/usr/bin/perl

my $r = shift;
use CGI;
local our $cgi = new CGI;

our $action = "";
$action  = $cgi->param("action") || 'test_code';

if ( $action eq "test_code" ) {
        test_code ();
}
elsif ( $action eq "run" ) {
        test_prog ($cgi);
}

sub test_code
{
    # my ($cgi_sub) = @_;

    my $cgi_sub = new CGI;

    print $cgi_sub->header();
    print $cgi_sub->start_html( -title => 'Javascript calls from Perl' );
    print $cgi_sub->h2("test code");
         print $cgi_sub->div($cgi_sub->font({-face=>'Arial'},"Status: "));
    print $cgi_sub->start_form();
    print $cgi_sub->submit(-name=>'action',-value=>'run');
    print $cgi_sub->end_form();
    print $cgi_sub->end_html();
}

sub test_prog
{
    my ($cgi_sub) = @_;
    print $cgi_sub->header();
    print $cgi_sub->start_html( -title => 'Javascript calls from mod_perl' );
    print $cgi_sub->h2("test code");

print <<"EOT" ;
<script language="JavaScript1.2"><!--
if (document.getElementById || document.all || document.layers)
       document.write("<div id=statusbox><font face=Arial>Status:
</font></div>");
function newstatus(s) {
       var e;
       if (document.getElementById) {
               e = document.getElementById("statusbox");
       } else if (document.all) {
               e = document.all["statusbox"];
       } else if (document.layers) {
               e = document.layers["statusbox"];
       }
       e.innerHTML = "<font face=arial>Status: </font><font
face=arial color=blue><b>" + s + "</b></font>";
}
//--></script>
EOT

for (my $i = 1; $i <= 6; $i++){
   if ($i == 6){
       newstatus("Aggregating Data...");
       sleep (2);
       newstatus("Analyzing Data...");
       sleep (2);
       newstatus("Finished \(this text is from the server\)");
   }
   else {
       newstatus("Querying " . "$i" . " out of 5 suppliers");
       sleep (1);
   }
}
    print $cgi_sub->start_form();
    print $cgi_sub->submit(-name=>'action',-value=>'run');
    print $cgi_sub->end_form();
    print $cgi_sub->end_html();
}


sub newstatus {
   my ($s) = @_;

print <<"EOT" ;
<script language="JavaScript1.2"><!--
newstatus("$s");
//--></script>
EOT

$r->rflush;

}












On 7/11/06, zentara <[EMAIL PROTECTED]> wrote:
On Mon, 10 Jul 2006 19:11:08 +0200, [EMAIL PROTECTED] ("Ibrahim Dawud")
wrote:

>The following code works great using normal perl but does not work
>under mod_perl:
>
>#!/usr/bin/perl
>
>use CGI;
>my $cgi = new CGI;
>print $cgi->header();
>print $cgi->start_html();
>$| = 1;
>print $cgi->h2("test1");
>sleep 1;
>$| = 1;
>print $cgi->h2("test2");
>sleep 1;
>$| = 1;
>print $cgi->h2("test3");
>print $cgi->end_html();
>
>The purpose of the code is print periodic messages to the browser
>during long processing. Can someone please explain or show an example
>of how I can flush the output buffer when running the same code under
>mod_perl. According to my understanding, I have to do this at the
>level of the Apache API using rflush. The problem is that I don't know
>how to write such a handler and even if i did, i don't know how to
>call it into my existing CGI code in place of $|=1.
>
>Suggestions are highly appreciated.

Try asking this at http://perlmonks.org  where a few mod_perl experts
hang out.

--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to