Re: END ing in a cgi script

[email protected] ("Mumia W.")
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>
On 02/16/2007 10:27 PM, Mary Anderson wrote:
> Hi all,
>    My perl-cgi application creates some temporary files and a temporary 
> table which I would like to clean up as I exit the program.  I tried 
> writing a perl END block, but found that did not work.  It appeared that 
> to the cgi interpreter END{} had no special meaning and the code inside 
> the END block was just executed in turn, instead of at the exit of the 
> program.  Are there techniques to clean up the environment on leaving the 
> application?
> 
> Thanks
> Mary
> 
> 

Normal Perl-CGI programs should respect the END block. What environment 
are you running under? Mod_perl?

You are probably using mod_perl which is not a normal CGI environment.

I did a search for "end block" at the mod_perl website:
http://perl.apache.org/search/swish.cgi?query=end+block&sbm=&submit=search

Look here:
http://clipmarks.com/clipmark/384255C7-7A86-48FD-9B14-9F4E0498A542/

Or look here:
http://perl.apache.org/docs/1.0/guide/debug.html#Safe_Resource_Locking_and_Cleanup_Code

In addition to the tips above, you can put the resources to be cleaned 
up in an object that is stored in a lexical variable. When the variable 
goes out of scope, the object's DESTROY method is invoked, and you can 
do your cleanup in the DESTROY method:


use strict;
use warnings;

{
package Country;
     sub new {
         my $class = shift;
         print "Created: $_[0]\n";
         bless {name => shift}, $class;
     }
     sub DESTROY {
         my $self = shift;
         print "Destroyed: ", $self->{name}, "\n";
         # Delete any resources used by this object.
     }
}

print "Content-Type: text/plain\n";
print "Cache-control: no-cache\n\n";

print "Object creation and destruction:\n";

{
     my $ct = Country->new('Iraq');
}


__HTH__

:-(
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.