From the documentation on twigils:
Attributes are variables that exist per instance of a class. They may be directly accessed from within the class via !:
class Point {
has $.x;
has $.y;
method Str() {
"($!x, $!y)"
}
}
Note how the attributes are declared as $.x
and $.y
but are still accessed via $!x
and $!y
. This is because in Perl 6 all attributes are private and can be directly accessed within the class by using $!attribute-name
. Perl 6 may automatically generate accessor methods for you though. For more details on objects, classes and their attributes see object orientation.
Public attributes have the .
twigil, private ones the !
twigil.
class YourClass {
has $!private;
has @.public;
# and with write accessor
has $.stuff is rw;
method do_something {
if self.can('bark') {
say "Something doggy";
}
}
}