Perl6 Object Oriented Cookbook (v0.2.1)  
Section 5: Constructors, Destructors, and Autochained Methods  
 
Recipe 5.3: Declaring Destructors
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
5.00 Rating, 1 Vote  

How acceptable is the proposed solution?
  (Login to Vote)
5.00 Rating, 1 Vote  

Problem:  

You want to create one or more destructors for your class: methods that are automatically invoked when an object is about to be garbage collected.

Solution:  

Use the DESTROY keyword:

class MyClass is ParentClass {
    DESTROY { ... }
}

Discussion:

Destructors in Perl6 are defined using DESTROY. Destructors are autochained, such that destructors for parent classes are invoked automatically after your subclass destructor is called.

You don't have to put a DESTROY method in a class. Any parent DESTROY methods will still be called, in the proper order.

For the above example, therefore, the destruction of an object happens like this:

  • The DESTROY method of MyClass is invoked.
  • The DESTROY method of ParentClass is invoked.
  • The DESTROY method of Object, the root class, is invoked.


Log In to Comment


Login / Edit User Info -- Copyright © 2002 Cognitivity -- Previous Page   Next Page