http://www.perlmonks.org?node_id=492199

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

Hey,

I have a Perl script that is rather long to execute (around 20 sec.). I would like it to send some message to the browser, before it ends.

Actually, it would start by showing a "Processing..." message, then do its job, then show the end result.

The problem is that I did made some print in the head of the script, but it always awaits for its end to make the print to the browser.

Do you have any idea?

Thanks,
Andrei

Replies are listed 'Best First'.
Re: Show information before end of script
by merlyn (Sage) on Sep 15, 2005 at 15:31 UTC
      Thank you monks (mean, folks), I really appreciate the help.

      I'll try to implement one or another of your solutions and get back to you.

      Regards,
      Andrei
Re: Show information before end of script
by NateTut (Deacon) on Sep 15, 2005 at 13:20 UTC
    It sounds like you need to turn print buffering off. Try putting $| = 1; before your first print statement. this will turn Auto Flush on.
Re: Show information before end of script
by phaylon (Curate) on Sep 15, 2005 at 13:30 UTC
    If this is something more complex, I'd spawn a working process off the request and present a "Processing." Page with automatic reloads and a "check now" link. But that's more a question of habits, as I hate long running requests :)

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Show information before end of script
by amw1 (Friar) on Sep 15, 2005 at 15:07 UTC
    Not strictly a perl solution, but you could use ajax to handle this. Your CGI would return a page that says processing and exit. This page would have some javascript that would periodically do an xmlhttprequest to a status script on the server. When the job is done the javascript would get the finished status from the status script and update the page accordingly.
    Info on ajax and perl http://www.modernmethod.com/sajax/
Re: Show information before end of script
by ruoso (Curate) on Sep 15, 2005 at 17:23 UTC
    This is a task for Non-Parsed-Headers... See Here.
    daniel
      $|=1 did it just fine !

      Thanks a lot.
      Andrei
        Is it a web application? If it works, I'm really impressed.
        daniel
Re: Show information before end of script
by schweini (Friar) on Sep 15, 2005 at 23:46 UTC
    what i do in long report-generating scripts is to send a
    <script> updateProgressbar( $percentCompleted ) </script>

    to the browser from inside the loop that's doing the computation. Obviously, i define function updateProgressbar(percent) in some globally included .js file.
    Works great for me, but just be careful not to send something like this on every iteration of a fast-iterating loop, since you could easily clog even broadband connections with millions of these instructions. i usually do something like
    &printProgessJavascript( %percent ) if ($i++ % 1000 == 0);

    on the perl side.