Perl 6 OOP
What is OOP ?
Larry Wall Says:
What is OOP ?
Position
Out Of Position
OOP
Man With Clue
Read That !
Damian Says:
Object-oriented
programming ...
many opinions,
theories, and even
ideologies have
been formulated on
the subject. ... Most
are mutually
inconsistent.
OOP
Classes / Prototypes
(Multiple) Inheritance / Roles
MMD + Delegation
Types + Subtypes
Introspection / Metaobj.
His Opinion
TIMTOWTDI
All There in Beauty
In Search Of Perf.
Starts With A Class
Class
class
Class
class
instanceable name space
Class
class module package
Class
class Excalibur;
class Babylon;
Class
class
instanceable name space
NS in Braces
class Excalibur {
...
}
Object
Object

my $obj =
Class.new();
Ops Changed

my $obj =
Class.new();
Create New
Clone Existing
Object

my $obj =
$old.clone();
Object

my $obj =
$old.clone(...);
Positional Paramters

clone($pos1, $pos2);
Named Parameters

clone( :key('value'),);
With Autoquoting
clone( :key<value>,);
Old School Works Too

clone( key=>'value',);
Object

new & clone
bless stayed
Attributes
+
Methods
Space Ship
Class
class Spaceship {
has Int $.speed;
method stop {
$speed = 0
}
}
I can do that too !
In Perl 5
package Spaceship;
use Moose;
has 'speed' => (
is => 'ro';
isa => 'Int';
);
sub stop {
$self = shift;
$self->speed = 0;
}
Me too !
In Perl 5
package Spaceship;
use Moo;
has 'speed' => (
is => 'ro';
isa => sub { die "…"
unless looks_like_number($_[0]);
});
sub stop {
$self = shift;
$self->speed = 0;
}
In Perl 5
use MooseX::Declare;
class Spaceship {
has 'speed' => (
is => 'ro';
isa => 'Int';
);
method stop {
$self->speed = 0;
}
}
Class
class Spaceship {
has Int $.speed;
method stop {
$.speed = 0;
}
}
Attribute Usage
P5
$self->speed
shift->speed

P6
$.speed
self.speed
$!speed
Twigil of
Accessors
.
!

public
private
Twigil of
Accessors
.
!

public
private

has $!speed; # private
Twigil of
Accessors
.
!
has $speed;

public
private
# private too
trusts
trusts
class Dog {
trusts Cat;
has $!Bone;
}
trusts
class Cat {
method steal {
my $carlo = Dog.new();
$carlo!Bone = 0;
...
.
!
^
:
*
?
=
~

Twigils

punlic access.
private access.
pos. auto para.
named auto p.
global var
compiler info
POD
sublang
Sigils
$
@
%

Scalar
Array
Hash
Sigils
has $.speed;
has @.shuttle;
has %.crew;
In Perl 5
use MooseX::Declare;
class Spaceship {
has 'speed' => (
is => 'ro';
isa => 'Int';
);
method stop {
$self->speed = 0;
}
}
In Perl 5
use MooseX::Declare;
class Spaceship {
has 'speed' => (
is => 'rw';
isa => 'Int';
);
method stop {
$self->speed = 0;
}
}
Class
class Spaceship {
has Int $.speed is rw;

}

method stop {
$.speed = 0;
}
Class
class Spaceship {
has Int $.speed is rw = 0;

}

method stop {
$.speed = 0;
}
In Perl 5
use MooseX::Declare;
class Spaceship {
has 'speed' => (
is => 'rw';
isa => 'Int';
default => 0;
);
method stop {
$self->speed = 0;
}
}
Perl 6 Attribute
no:
isa default (just Syntax)
predicate required coerce

reader writer init_arg
clearer builder lazy_build
That was my idea!
Perl 6 & Moose
has is
Subtypes
Moose
subtype 'Slogan'
=> as 'Str'
=> where {length $_< 50};
Perl 6
my subset Slogan of Str
where {$_.chars < 50};
Delegation
Excalibur
Perl 6
class Excalibur;
has $.clock handles 'now';
$excalibur = Excalibur.new;
$excalibur.clock.now;
Perl 6
class Excalibur;
has DateTime $.clock
handles 'now';
$excalibur = Excalibur.new;
$excalibur.now;
Moose
has 'clock' => (
handles => 'now';
);
Moose Rename
has 'clock' => (
handles => {
now => 'time'
};
);
Perl 6 Rename
class Spaceship;
has DateTime $.clock
handles { :time<now>};
Methods
Methods
method stop { … }
Methods
method !stop { … }
Methods
method !stop { … }
submethod
Methods
method !stop { … }
submethod # !inherit
MMD

?
MMD

Multi
Method
Dispatch
MMD
only
multi
proto
MMD
only # default anyway
multi # look at !
proto # later
MMD
multi method navigate
(Coord $place) {}
multi method navigate
(Str $cmd) {};
MMD
$excalibur.navigate('back');
MMD
only # default anyway
multi # MMD
proto # own handling
Inheritance
MooseX::Declare
class WhiteStar
extends Spaceship;
Inheritance

extends => is
Perl 6
class WhiteStar
is Spaceship;
Multiple Inheritance
class WhiteStar
is Spaceship is Minbari;
Vererbung später
extends => also is
MooseX::Declare
class WhiteStar;
...
extends Spaceship;
Perl 6
class WhiteStar {
...
also is Spaceship;
Roles
Class Hierarchy
Where to insert ?
Solution
Role:
Unit Of Reusable
Functionality
Therefore
Role:
Unit Of Reusable
Functionality
Outside Any Hierarchy
Solution
Role:
Unit Of Reusable
Functionality
Trait Elsewhere
Therefore
Role:
Unit Of Reusable
Functionality
Roles have Atributes, Traits not
Therefore
Role:
Reusable => Small
Remember?
Role:
Reusable => Small
Class:
instanceable name space
How To Solve That
Role:
Reusable => Small
Class:
Complete => Big
Class Do Can't Both
Role:
Reusable => Small
!=
Class:
Complete => Big
Roles
may be inherited !
if mixed into a class
& remove @ run time
Roles
conflicts throw exceptions
Roles
conflicts throw exceptions
No global overwrite like
Ruby Mixins
Roles
conflicts throw exceptions
No global overwrite like
Ruby Mixins
Refinements doesn't solve it all
Roles
conflicts throw exceptions
Roles > multiple inheritance
(conflicts remain unhandled
- in intelligent way)
Roles
conflicts throw exceptions
except when method is empty
Roles
conflicts throw exceptions
except when method is empty

then is has to be overwritten
(interface)
Roles
role Spaceship {
has Int $.speed;
method stop {
$.speed = 0
}
}
Roles
role Clock {
has DateTime $.time;
method alarm {
...
}
}
Apply Roles

with => does
Moose
class Excalibur
extends WhiteStar
with Clock;
Moo too !
Moo::Role
package Excalibur;
extends 'WhiteStar';
with 'Clock';
Perl 6
class Excalibur
is WhiteStar
does Clock;
Perl 6
class Excalibur
is Whitestar;
also does Clock;
Perl 6
class Excalibbur
is WhiteStar;
also does Clock
does PlasmaGun;
Perl 6
$excalibur does Clock;
Introspection
Methods
WHAT short name
WHICH object ID (type)
WHO package, long name in str context
WHERE memory address
HOW
object of meta class
WHEN (reserved for events?)
WHY
(reserved for documentation)
WHENCE autovivification of closures
Methods
WHAT short name
WHICH object ID (type)
WHO package, long name in str context
WHERE memory address
HOW
object of meta class
WHEN (reserved for events?)
WHY
(reserved for documentation)
WHENCE autovivification of closures
Methods
WHAT short name
WHICH object ID (type)
WHO package, long name in str context
WHERE memory address
HOW
object of meta class
WHEN (reserved for events?)
WHY
(reserved for documentation)
WHENCE autovivification of closures
Introspection
Class.HOW.methods($obj)
Class.^methods()
Metaobjectmethods
identifier
name authority version author
description
licensed

subject

parents

language
roles
Deeper & Deeper
$obj.^methods()[$which].signature
Introspection
All is an Object
Introspection
All is an Object
„objects are stupid“.uc
Introspection
All is an Object
Commands are Methods
Introspection
All is an Object
Commands are Methods
(Operators too)
Introspection
All is an Object
Commands are Methods
(Operators too)
MMD is everywhere
Introspection
All is an Object
Commands are Methods
(Operators too)
MMD is everywhere
also in Regexes
Name Spaces
package module
class
A Kind Of Class
package module
class grammar
Grammars
grammar {
token { … }
rule { … }
regex { … }
}
Learn More
S12: Objekte,S14: Rollen
perl6.org/documentation
http://perlcabal.org/syn/
opt. Precise & Volume
Learn More
Perl 6 Docs
doc.perl6.org/language/objects

Opt.: Short & Precise
Learn More
Perl 6 Tablets
tablets.perl6.org
opt.: Hypertext & Volume
Cockaigne
Thank You

P6 OO vs Moose (&Moo)