Working with attributes
In the previous section, we created the House
class, which did not contain anything. Real houses do have some parameters, such as address, area in square meters, number of rooms, height, and so on. All that can be expressed using Perl 6.
Let us start adding the details to the class. We start with the most simple element, the number of rooms. This parameter can be described by an integer value that is attached to the object of the House
type. In Perl 6, such data elements are called attributes and are declared with the has
keyword, as shown in the following example:
class House { has $.rooms; }
What has been done here? The House
class got an attribute $.rooms
. This is a scalar value that belongs to the class object. Notice the dot after the dollar sigil. It is a twigil that describes the access level to the attribute; we will talk about it later in this section.
Now, try to create a house as we did in the previous section:
my $house = House.new;
This time, the object...