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

Private methods

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()

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