Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Show information before end of script

by Opalenet (Acolyte)
on Sep 15, 2005 at 13:06 UTC ( #492199=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://492199]
Approved by ChrisR
Front-paged by kwaping
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-16 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (62 votes). Check out past polls.

    Notices?