Skip to Content
Perl 6 Deep Dive
book

Perl 6 Deep Dive

by Andrew Shitov
September 2017
Beginner
402 pages
9h 52m
English
Packt Publishing
Content preview from Perl 6 Deep Dive

Public and private attributes

In the previous code examples, the class attribute was declared with the dot sigil—$.rooms or $.street. A dot at that position means that the attribute is public and may be accessed by code that does not belong to the class.

There is another twigil, !, which makes attributes private. This means that the only way to read or change the value of an attribute is to access it from methods.

Let us return to the House class and change all the twigils of its methods to !:

class House {    has $!rooms;    has $!area;    has $!height;}

Creating a house can be done in the same way as before:

my $house = House.new(    rooms  => 2,     area   => 100,    height => 3,);

However, it is now not possible to read the value of the attributes. An attempt ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
Learning Perl 6

Learning Perl 6

brian d foy
Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Pro Perl

Pro Perl

Peter Wainwright

Publisher Resources

ISBN: 9781787282049Supplemental Content