Perl6 Object Oriented Cookbook (v0.2.1)  
Section 2: Attributes and Methods  
 
Recipe 2.2: Getting and Setting Attribute Values
Last Updated: Sep 8, 2003
Status: Very Likely
      Previous Page   Next Page

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

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

Problem:  

You want to get or set the value of an attribute.

Solution:  

From inside the class, like this:

class Dog {
    has $.fleas;            # declare the attribute
    
    method more_fleas {
        .fleas++;           # increment $.fleas
    }
}
From outside the class, do it like this:
my $num_fleas = $dog.fleas;  # get the number of fleas

$dog.fleas = 50;             # set the number of fleas

Discussion:

Assuming that you have permission to do so, accessing an attribute is straightforward.

Every attribute of a class has an automatically generated accessor method allowing the attribute to be used as if it were an lvalue method (a method whose return result can be both read and written to.) Though it sounds complicated, the net result is that you can access the attribute as a simple value or as a method call, interchangeably.

From within a method of the class, you may refer to the attribute either directly, or through the accessor method:

method more_fleas {
    $.fleas++;         # (1) refer to the attribute directly, and increment
    
    .fleas++;          # (2) call the lvalue accessor
    
    .fleas(.fleas+1);  # (3) method syntax, identical to (2)
}
Note that if the accessor method is overriden by a subclass, (2) will use the subclass method, whereas (1) will always operate on the attribute directly. Both mechanisms can be useful, depending on the circumstance.

Outside the class, you may only refer to the attribute by going through the accessor method. By default, attributes are considered to be private to a class, meaning that their accessor method cannot be called except from within the methods of the class itself. To make an attribute public, use the is public property:

has int $.counter is public;
This won't actually make the attribute variable itself "public", but will make the attribute's accessor method publicly available, so that users of the object can get and set the attribute.

Note that whether an element of an object is implemented as a public attribute or as a public method is almost entirely invisible to the users of that object, since the syntax is identical:

my $obj = MyClass.new;
    
    $obj.pub_attrib;       # calling a public attribute
    $obj.pub_method;       # calling a public method
    
    $obj.pub_attrib = 5;   # setting a public attribute
    $obj.pub_method = 5;   # setting an lvalue method
This is a useful feature; it allows subclasses to reimplement attributes as methods, and vice versa, without altering the overall usage of the class.

Issue: Need to demonstrate the exact signature of the autocreated accessors.


Log In to Comment


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