in reply to nested subs cause DESTROY block to not get hit

perls man page perltoot tells you:

Because while a constructor is explicitly called, a destructor is not. Destruction happens automatically via Perl's garbage collection (GC) system, which is a quick but somewhat lazy reference-based GC system. To know what to call, Perl insists that the destructor be named DESTROY. Perl's notion of the right time to call a destructor is not well-defined currently, which is why your destructors should not rely on when they are called.

and also ;)

in really good object-oriented programming languages, the user doesn't care when the destructor is called. It just happens when it's supposed to. In low-level languages without any GC at all, there's no way to depend on this happening at the right time, so the programmer must explicitly call the destructor to clean up memory and state, crossing their fingers that it's the right time to do so. Unlike C++, an object destructor is nearly never needed in Perl, and even when it is, explicit invocation is uncalled for. In the case of our Person class, we don't need a destructor because Perl takes care of simple matters like memory deallocation.

MP

  • Comment on Re: nested subs cause DESTROY block to not get hit

Replies are listed 'Best First'.
Re: Re: nested subs cause DESTROY block to not get hit
by John M. Dlugosz (Monsignor) on Aug 16, 2001 at 18:26 UTC
    Unlike C++, an object destructor is nearly never needed in Perl, and even when it is, explicit invocation is uncalled for. In the case of our Person class, we don't need a destructor because Perl takes care of simple matters like memory deallocation.
    I quite disagree. GC is great for memory, but doesn't do a thing for other resources such as window handles. If an object represents a window, the object being destructed closes the window. Do you want the window to stay visible until the system notices it can collect it, or promptly when the last reference is dropped?

      hmmm... but I also don't want to destruct the object just to close the window.
      IMHO the destructor of the object should call the destroy/close function of the window and thats it....
      but it seems we are getting of topic ,)

      MP