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

I have a webpage that kicks off a perl script that has the potential to take a long time (30 seconds or so). The success of this script is of great interest to the client and so ideally, I would like to display an animated gif (Processing... or something like that) until I know whether or not the script failed or succeeded. The problem is of course what to do when the script returns. I know I could use AJAX, and Ive done it before, but I think I would prefer a simpler solution in this case. I would like to just redirect to a new page with the message of success or failure, but how do you redirect when you've already sent the HTTP header?

Replies are listed 'Best First'.
Re: CGI redirection after headers sent
by merlyn (Sage) on Jan 15, 2007 at 22:29 UTC
Re: CGI redirection after headers sent
by Cody Pendant (Prior) on Jan 16, 2007 at 01:49 UTC
    I do a quick-and-dirty version, no redirect, with javascript.

    I output this as soon as the script starts (setting $| to 1 first of course):

    <img name="progress" src="animated.gif">

    which is an animated progress-bar type image -- then when the script is finished/starting to produce output I print this to the browser:

    <script> document.images.progress.src='finished.gif'; </script>

    Which changes the gif to one which says "done" or is just transparent.

    Although of course if the perl script itself times out/fails and produces no further output they're left with the animated gif forever. That could be handled with a setTimeout() in javascript though, allowing two or three minutes then resetting the image and giving some kind of error. Your <script> block would in that case include a clearTimeout() as well. Exercise for the reader.

    Please note I only ever did this for internal apps where I could be absolutely sure the users had javascript and had it turned on.



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: CGI redirection after headers sent
by SFLEX (Chaplain) on Jan 16, 2007 at 13:39 UTC
    I think Image::ParseGIF would be a nice module to use for your animated gif, check it out.

    For your HTTP header issue, maybe you can settup that page to refresh in some amount of time after refreshing it checks to see what it should do, if it should start all over or do the next action.

    Good Luck