Can you think of a way to guarantee that Perl does not unload a class module
*before* it deletes the final instance of this class?
I have written a Perl object module that contains a database connection class variable
(it is a package variable). The class constructor counts each object instance, and
creates a database connection only for the first object instance. The destructor
disconnects from the database only when Perl deletes the *last* instance. However,
when the DESTROY method, on behalf of the final instance, attempts to disconnect
from the database, Perl complains that the database reference is no longer defined!
By having the END subroutine defined in this module print out a debug message, I
learned that Perl unloads the module *before* it deletes the the final object instance.
This prematurely deletes the database connection and renders the database package
variable undefined.
I have avoided this issue by using the END subroutine to disconnect from the
database, but it surprises me that Perl would unload a class module before it deletes
all instances of a class. Do you agree that this seems like incorrect behaviour?