The webserver should normally terminate the CGI script in case the browser prematurely closes the connection. So, you could try putting your cleanup code in an END { ... } block, which should execute upon termination of the CGI script.
In case that doesn't work, you'll probably also have to set up a signal handler (most likely for SIGTERM, but I haven't checked1) calling die or exit, which in turn will make the script terminate in a controlled fashion, so the END block executes. (Details depend on the webserver, OS, etc.)
___
1 in a private msg Perlbotics noted (thanks!) that according to his observations, it would rather be SIGPIPE — which makes perfect sense, as the script would be trying to output to a broken pipe (presuming it hasn't already finished writing at the time...).
|