Home: Perl Programming Help: mod_perl:
Browser waits for Perl script to exit before displaying prints



hem
User

Sep 6, 2012, 9:20 AM


Views: 51870
Browser waits for Perl script to exit before displaying prints

Hello

I am calling a perl script from a PHP server side script. In the Perl Script I have a bunch of prints and sleeps. Problem is that the browser waits for the Perl Script to complete to display the prints. The prints are intented to give the user a live status of whats going on. But browser waits till the script exits and then displays everything

I tried adding $|=1; but it did not help

I am using:
Perl:
This is perl, v5.8.8 built for i386-linux-thread-multi

Apache:
Server version: Apache/2.2.6 (Unix)


Linux:
Fedora release 8




Code
#!/usr/bin/perl  
use CGI qw(:standard);
$|=1;
my $r = CGI->new();
print header('text/html');
print start_html;
$|=1;
$r->print ("Processing ... <br>");
$|=1;
$r->print ("Processing TWO ... <br>");
foreach $i (1..2) {
sleep 10;
$r->print ("Item .. $i <br>");
}
$r->print ("DONE <br>");
print end_html;




FishMonger
Veteran / Moderator

Sep 6, 2012, 9:29 AM


Views: 51869
Re: [hem] Browser waits for Perl script to exit before displaying prints

You should always be using the strict and warnings pragmas in all of your Perl scripts.

You only need the first $|=1; statement to disable the buffering.

Since the buffering is already disabled in the Perl script, The problem is probably in the way you're calling the script from PHP and the buffering is occurring on that end.


hem
User

Sep 6, 2012, 10:14 AM


Views: 51864
Re: [FishMonger] Browser waits for Perl script to exit before displaying prints

Yes i always use strict and warnings . This is just a test code .

May be its Apache server?

on php side its a simple call:

Code
elseif (isset($_GET["reboot"])) 
{
$x = $_GET["reboot"];
$y = $_GET["ip"];
$Command = "perl /var/www/cgi-bin/PerlScript.cgi $x $y";
passthru($Command,$ret);
//echo $ret;
}



FishMonger
Veteran / Moderator

Sep 6, 2012, 10:43 AM


Views: 51859
Re: [hem] Browser waits for Perl script to exit before displaying prints

It's more likely that the passthru() function is buffering the output it receives.


hem
User

Sep 6, 2012, 7:54 PM


Views: 51827
Re: [FishMonger] Browser waits for Perl script to exit before displaying prints

I realize this might not be the right forum but do you happen to know how can I call the perl script from php to not buffer output.

Appreciate your help


FishMonger
Veteran / Moderator

Sep 7, 2012, 6:53 AM


Views: 51794
Re: [hem] Browser waits for Perl script to exit before displaying prints

I don't have a lot of experience with php. Personally, I hate it and only use it when required by my boss.

If you read the user comments on php.net about the passthru() function you'll find postings from others with the same problem. Their recommendation was to use popen() instead of passthru(). I have not tried it myself, but it's worth a shot.


hem
User

Sep 8, 2012, 12:18 PM


Views: 51771
Re: [FishMonger] Browser waits for Perl script to exit before displaying prints

I wll try to do some more digging on PHP

Will post a solution if i find one

Thanks for your time and looking into this