Perl6 Object Oriented Cookbook (v0.2.1)  
Section 3: Working with Classes  
 
Recipe 3.11: Creating AUTOLOADed Attributes and Methods
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

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

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

Problem:  

You want to intercept calls to undefined methods before an exception is thrown, so that you can dispatch them yourself and avoid the exception.

Solution:  

Use the AUTOLOAD method, defined in the base class, Class:

class MyClass {
    method foo { ... }
    method zap { ... }
    
    method AUTOLOAD {
    	...
    }
}

Discussion:

(Can be filled out more after ApocN)

If an AUTOLOAD method exists for a given object, it will be called whenever an attribute or method of the object cannot be found, instead of the offending (non-existant) method. This allows you to intercept these calls, and do something about it.

AUTOLOAD is defined is interface: it does not require (or accept) a method signature. The standard signature must be used in all cases.

AUTOLOAD methods chain automatically: subclass AUTOLOAD methods are called before their parent AUTOLOAD methods (in the same manner as DESTROY.)


Log In to Comment


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