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__

:-(




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to