Perl6 Object Oriented Cookbook (v0.2.1)  
Section 2: Attributes and Methods  
 
Recipe 2.12: Calling Methods Only if They Exist
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)
4.00 Rating, 2 Votes  

Problem:  

You want to call an attribute or method only if the class or object actually supports it, to avoid "no such attribute or method" errors.

Solution:  

Use CAN:

$obj.foo if $obj.CAN.foo;

MyClass.foo if MyClass.CAN.foo;

Discussion:


Log In to Comment


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