morgon has asked for the wisdom of the Perl Monks concerning the following question:

Venerable fellow monks,

I have a strange thing happening that I would like to understand.

Here is the code in it's essentials:

package X; use strict; use Y; my $y = Y->new; 1;
So here we have a package that keeps an instance of a class Y in a "static" variable.

Here now the implementation of Y:

package Y; use Moose; sub DEMOLISH {} 1;
As you can see it's just an (empty) Moose-based class that provides an (in this case even empty) destructor.

Now I have a little driver script:

use strict; use X; print "done\n";
All this trivial stuff now results in this warning when the script exits:

(in cleanup) Can't call method "default" on an undefined value at (eval 36) line 7 during global destruction.

So evidently during the global destruction phase (that I have never fully understood) something gets cleaned up in a way that seems to confuse Moose or what is happening here?

The problem is not just this warning but the fact that the destructor (code in DEMOLISH) then never gets run so this means that I cannot rely on destructors to clean up resources when the Moose-instance is held in a static variable and gets garbage collected only during global destruction...

Regardless of whether or not using destructors in such a way is good or bad style - could someone explain what exactly is causing this problem?

Many thanks!

PS: I am running 5.10 on Linux with Moose 0.62

Replies are listed 'Best First'.
Re: global destruction and Moose
by FunkyMonk (Bishop) on Jun 21, 2009 at 22:05 UTC
    You code works fine here with Moose 0.80:

    Name "Moose::Object::DEMOLISH" used only once: possible typo at /usr/s +hare/perl5/Moose/Object.pm line 82. done

    Can you upgrade to a newer Moose? Yours is about 6 months old and current Moose is version 0.82.