In the previous example, we were using the class attribute for keeping the data that is shared between all instances of the class. We were using a method that is working with that attribute.
The idea of class attributes can also be projected on methods. In Perl 6, classes can contain class methods, which are defined with the sub keyword. Such methods have access to all the class attributes, but do not receive an implicit self reference to the object. Consider an example with two classes:
class Address {
my Int $last_assigned_number = 0;
has Int $.housenumber is rw;
our sub get_next() {
return ++$last_assigned_number;
}
}
class House {
has Address $.address is rw;
}
The get_next class method is declared also with the our keyword. This is needed because we want to access the method from external code. By default, the scope will be limited to the...