September 2017
Beginner
402 pages
9h 52m
English
To hide an attribute, you need to change the twigil to !. Similarly, it is possible to hide methods by making them private with the same exclamation mark. Private methods cannot be called on the object; they only can be used from other methods of the class. Examine the following example:
class X { method !a() { say 'Private method'; } method b() { say 'Public method'; self!a(); }}my $x = X.new;
This class has two methods, a and b. The first of them is declared as private so an attempt to call it as $x.a() causes a runtime error:
No such method 'a' for invocant of type 'X'
The b method is public and thus can be called:
$x.b();Inside, this method is calling the private method a with the help of the self keyword—self!a()