Class data variables are called attributes. They are declared with the has keyword. An attribute’s scope is defined via its twigil. As usual, the first character of the twigil indicates the type of the container (thus, a scalar, an array, or a hash). The second character is either . if a variable is public or … Continue reading “📘 Class attributes in Perl 6”
Category: Chapter 4. Classes
📘 Submethods in Perl 6 classes
Perl 6 defines the so-called submethods for classes. These are the methods which are not propagating to the subclass’s definition. The submethods may be either private or public, but they will not be inherited by the children. class A { submethod submeth { say “A.submeth” } } class B is A { … Continue reading “📘 Submethods in Perl 6 classes”
Multiple inheritance in Perl 6
When more than one class is mentioned in the list of base classes, we have multiple inheritance. class A { method a { say “A.a” } } class B { method b { say “B.b”; } } class C is A is B { } my $c = C.new; … Continue reading “Multiple inheritance in Perl 6”
📘 Chapter 4. Classes
We have already seen elements of the object-oriented programming in Perl 6. Methods may be called on those variables, which do not look like real objects from the first view. Even more, methods may be called on constants. The types that were used earlier (like Int or Str) are container types. Variables of a container … Continue reading “📘 Chapter 4. Classes”
📘 Class methods in Perl 6
The method keyword defines a method, similarly to how we define subroutines with sub. A method has access to all attributes of the class, both public and private. The method itself can be private. We will return to this later after talking about inheritance. In the following short example, two methods are created, and each … Continue reading “📘 Class methods in Perl 6”
📘 Inheritance in Perl 6
Inheritance is easy. Just say is Baseclass when declaring a class. Having said that, your class will be derived from the base class. class A { method x { say “A.x” } method y { say “A.y” } } class B is A { method x { … Continue reading “📘 Inheritance in Perl 6”
📘 Private (closed) methods in Perl 6 classes
Now, after we have discussed inheritance, let us return to the private (or closed) methods. These methods may only be used within the class itself. Thus, you cannot call them from the programme that uses an instance of the class. Nor are they accessible in the derived classes. An exclamation mark is used to denote … Continue reading “📘 Private (closed) methods in Perl 6 classes”
📘 Constructors in Perl 6 classes
You may have noticed in the previous examples that two different approaches to creating a typed variable were used. The first was via an explicit call of the new constructor. In this case, a new instance was created. my $a = A.new; In the second, a variable was declared as a typed variable. Here, a … Continue reading “📘 Constructors in Perl 6 classes”
📘 Roles in Perl 6
Apart from the bare classes, the Perl 6 language allows roles. These are what are sometimes called interfaces in other object-oriented languages. Both the methods and the data, which are defined in a role, are available for “addition” (or mixing-in) to a new class with the help of the does keyword. A role looks like … Continue reading “📘 Roles in Perl 6”