in reply to Weird perl behavior regarding unloading class modules

Hmm, are you referring to the following behaviour:
use strict; use warnings; package Foo; our $Obj = Foo->new(); sub new { bless {}, shift; } sub DESTROY { my $this = shift; print "DESTROYING $this\n"; } sub END { print "Ending! (global = $Obj)\n"; } ## OUTPUTS Ending! (global = Foo=HASH(0x1a7f0c8)) DESTROYING Foo=HASH(0x1a7f0c8)
Odd indeed, that DESTOY would be called after END. These problems can usually be solved by explicitly undefing your instance object.

Replies are listed 'Best First'.
(tye)Re2: Weird perl behavior regarding unloading class modules
by tye (Sage) on Jan 20, 2001 at 10:09 UTC

    Depends whether you want to be able to use your global objects from within your END block or not, eh? This was argued back and forth on p5p quite a bit, I think.

    I prefer to code like this:

    #!/usr/bin/perl -w use strict; # use other modules here # global variables here exit( main( @ARGV ) ); # subroutines here sub main { my $object= Widget::->new(); # more code here return 0; }
    so that I don't have an objects in global variables and I don't have to remember to undef things.

    A much worse problem, IMHO, is that once you get into the global destruction phase, objects are destroyed in some arbitrary order so that Z can be destroyed before X even if X holds a reference to Z. This means that X's destructor may find undef where it expects a reference to Z. This really sucks to me.

            - tye (but my friends call me "Tye")
      exit( main( @ARGV ) ); sub main { ...
      That's a C-ish brogue if ever I've heard one :)