simple, put this at the end of your main script...
print "Clean up\n"
# clean up code here
you can put END in the modules as well, they will be executed in
reverse order of definition, so you must put main END before your use
clause as the use
is parsed before anything else.