140
Perl 6 OOP

P6 OO vs Moose (&Moo)

Embed Size (px)

DESCRIPTION

Part of a series of talk to help you write your first Perl 6 program today. So its basic syntax and concepts of its object orientation and a comparison to the widely used P5 OO system Moose which is similar by no accident.

Citation preview

Page 1: P6 OO vs Moose (&Moo)

Perl 6 OOP

Page 2: P6 OO vs Moose (&Moo)

What is OOP ?

Page 3: P6 OO vs Moose (&Moo)

Larry Wall Says:

Page 4: P6 OO vs Moose (&Moo)

What is OOP ?

Page 5: P6 OO vs Moose (&Moo)

Position

Page 6: P6 OO vs Moose (&Moo)

Out Of Position

Page 7: P6 OO vs Moose (&Moo)

O O P

Page 8: P6 OO vs Moose (&Moo)

Man With Clue

Page 9: P6 OO vs Moose (&Moo)

Read That !

Page 10: P6 OO vs Moose (&Moo)

Damian Says:

Object-orientedprogramming ... many opinions, theories, and even ideologies have been formulated on the subject. ... Most are mutually inconsistent.

Page 11: P6 OO vs Moose (&Moo)

O O P

Classes / Prototypes(Multiple) Inheritance / Roles

MMD + DelegationTypes + Subtypes

Introspection / Metaobj.

Page 12: P6 OO vs Moose (&Moo)

His Opinion

Page 13: P6 OO vs Moose (&Moo)

TIMTOWTDI

Page 14: P6 OO vs Moose (&Moo)

All There in Beauty

Page 15: P6 OO vs Moose (&Moo)

In Search Of Perf.

Page 16: P6 OO vs Moose (&Moo)

Starts With A Class

Page 17: P6 OO vs Moose (&Moo)

Class

class

Page 18: P6 OO vs Moose (&Moo)

Class

class

instanceable name space

Page 19: P6 OO vs Moose (&Moo)

Class

class module package

Page 20: P6 OO vs Moose (&Moo)

Class

class Excalibur; class Babylon;

Page 21: P6 OO vs Moose (&Moo)

Class

class

instanceable name space

Page 22: P6 OO vs Moose (&Moo)

NS in Braces

class Excalibur { ... }

Page 23: P6 OO vs Moose (&Moo)

Object

Page 24: P6 OO vs Moose (&Moo)

Object

my $obj = Class.new();

Page 25: P6 OO vs Moose (&Moo)

Ops Changed

my $obj = Class.new();

Page 26: P6 OO vs Moose (&Moo)

Create New

Page 27: P6 OO vs Moose (&Moo)

Clone Existing

Page 28: P6 OO vs Moose (&Moo)

Object

my $obj = $old.clone();

Page 29: P6 OO vs Moose (&Moo)

Object

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

Page 30: P6 OO vs Moose (&Moo)

Positional Paramters

clone($pos1, $pos2);

Page 31: P6 OO vs Moose (&Moo)

Named Parameters

clone( :key('value'),);

Page 32: P6 OO vs Moose (&Moo)

With Autoquoting

clone( :key<value>,);

Page 33: P6 OO vs Moose (&Moo)

Old School Works Too

clone( key=>'value',);

Page 34: P6 OO vs Moose (&Moo)

Object

new & clone

bless stayed

Page 35: P6 OO vs Moose (&Moo)

Attributes

+

Methods

Page 36: P6 OO vs Moose (&Moo)

Space Ship

Page 37: P6 OO vs Moose (&Moo)

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

Page 38: P6 OO vs Moose (&Moo)

I can do that too !

Page 39: P6 OO vs Moose (&Moo)

In Perl 5package Spaceship;use Moose;has 'speed' => ( is => 'ro'; isa => 'Int';);sub stop { $self = shift; $self->speed = 0;}

Page 40: P6 OO vs Moose (&Moo)

Me too !

Page 41: P6 OO vs Moose (&Moo)

In Perl 5package Spaceship;use Moo;has 'speed' => ( is => 'ro'; isa => sub { die "…" unless looks_like_number($_[0]);});sub stop { $self = shift; $self->speed = 0;}

Page 42: P6 OO vs Moose (&Moo)

In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}

Page 43: P6 OO vs Moose (&Moo)

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

Page 44: P6 OO vs Moose (&Moo)

Attribute Usage

P5 P6

$self->speed $.speedshift->speed self.speed $!speed

Page 45: P6 OO vs Moose (&Moo)

Twigil of Accessors

. public ! private

Page 46: P6 OO vs Moose (&Moo)

Twigil of Accessors

. public ! private

has $!speed; # private

Page 47: P6 OO vs Moose (&Moo)

Twigil of Accessors

. public ! private

has $speed; # private too

Page 48: P6 OO vs Moose (&Moo)

trusts

Page 49: P6 OO vs Moose (&Moo)

trusts

class Dog { trusts Cat; has $!Bone;}

Page 50: P6 OO vs Moose (&Moo)

trusts

class Cat { method steal { my $carlo = Dog.new(); $carlo!Bone = 0; ...

Page 51: P6 OO vs Moose (&Moo)

Twigils . punlic access. ! private access. ^ pos. auto para. : named auto p. * global var ? compiler info = POD ~ sublang

Page 52: P6 OO vs Moose (&Moo)

Sigils

$ Scalar @ Array % Hash

Page 53: P6 OO vs Moose (&Moo)

Sigils

has $.speed;has @.shuttle;has %.crew;

Page 54: P6 OO vs Moose (&Moo)

In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}

Page 55: P6 OO vs Moose (&Moo)

In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; }}

Page 56: P6 OO vs Moose (&Moo)

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

Page 57: P6 OO vs Moose (&Moo)

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

Page 58: P6 OO vs Moose (&Moo)

In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; }}

Page 59: P6 OO vs Moose (&Moo)

Perl 6 Attribute

no:isa default (just Syntax)predicate required coercereader writer init_argclearer builder lazy_build

Page 60: P6 OO vs Moose (&Moo)

That was my idea!

Page 61: P6 OO vs Moose (&Moo)

Perl 6 & Moose

has is

Page 62: P6 OO vs Moose (&Moo)

Subtypes

Page 63: P6 OO vs Moose (&Moo)

Moose

subtype 'Slogan' => as 'Str' => where {length $_< 50};

Page 64: P6 OO vs Moose (&Moo)

Perl 6

my subset Slogan of Str where {$_.chars < 50};

Page 65: P6 OO vs Moose (&Moo)

Delegation

Page 66: P6 OO vs Moose (&Moo)

Excalibur

Page 67: P6 OO vs Moose (&Moo)

Perl 6class Excalibur;has $.clock handles 'now';

$excalibur = Excalibur.new;$excalibur.clock.now;

Page 68: P6 OO vs Moose (&Moo)

Perl 6class Excalibur;has DateTime $.clock handles 'now';

$excalibur = Excalibur.new;$excalibur.now;

Page 69: P6 OO vs Moose (&Moo)

Moose

has 'clock' => ( handles => 'now'; );

Page 70: P6 OO vs Moose (&Moo)

Moose Rename

has 'clock' => ( handles => { now => 'time'

}; );

Page 71: P6 OO vs Moose (&Moo)

Perl 6 Renameclass Spaceship;has DateTime $.clock handles { :time<now>};

Page 72: P6 OO vs Moose (&Moo)

Methods

Page 73: P6 OO vs Moose (&Moo)

Methods

method stop { … }

Page 74: P6 OO vs Moose (&Moo)

Methods

method !stop { … }

Page 75: P6 OO vs Moose (&Moo)

Methods

method !stop { … }

submethod

Page 76: P6 OO vs Moose (&Moo)

Methods

method !stop { … }

submethod # !inherit

Page 77: P6 OO vs Moose (&Moo)

MMD

?

Page 78: P6 OO vs Moose (&Moo)

MMD

MultiMethodDispatch

Page 79: P6 OO vs Moose (&Moo)

MMD

only multi proto

Page 80: P6 OO vs Moose (&Moo)

MMD

only # default anywaymulti # look at !proto # later

Page 81: P6 OO vs Moose (&Moo)

MMD

multi method navigate (Coord $place) {}

multi method navigate (Str $cmd) {};

Page 82: P6 OO vs Moose (&Moo)

MMD

$excalibur.navigate('back');

Page 83: P6 OO vs Moose (&Moo)

MMD

only # default anywaymulti # MMDproto # own handling

Page 84: P6 OO vs Moose (&Moo)

Inheritance

Page 85: P6 OO vs Moose (&Moo)

MooseX::Declare

class WhiteStar extends Spaceship;

Page 86: P6 OO vs Moose (&Moo)

Inheritance

extends => is

Page 87: P6 OO vs Moose (&Moo)

Perl 6

class WhiteStar is Spaceship;

Page 88: P6 OO vs Moose (&Moo)

Multiple Inheritance

class WhiteStaris Spaceship is Minbari;

Page 89: P6 OO vs Moose (&Moo)

Vererbung später

extends => also is

Page 90: P6 OO vs Moose (&Moo)

MooseX::Declare

class WhiteStar;...extends Spaceship;

Page 91: P6 OO vs Moose (&Moo)

Perl 6

class WhiteStar { ... also is Spaceship;

Page 92: P6 OO vs Moose (&Moo)

Roles

Page 93: P6 OO vs Moose (&Moo)

Class Hierarchy

Page 94: P6 OO vs Moose (&Moo)

Where to insert ?

Page 95: P6 OO vs Moose (&Moo)

Solution

Role:Unit Of Reusable

Functionality

Page 96: P6 OO vs Moose (&Moo)

Therefore

Role:Unit Of Reusable

Functionality

Outside Any Hierarchy

Page 97: P6 OO vs Moose (&Moo)

Solution

Role:Unit Of Reusable

Functionality

Trait Elsewhere

Page 98: P6 OO vs Moose (&Moo)

Therefore

Role:Unit Of Reusable

Functionality

Roles have Atributes, Traits not

Page 99: P6 OO vs Moose (&Moo)

Therefore

Role:Reusable => Small

Page 100: P6 OO vs Moose (&Moo)

Remember?

Role:Reusable => Small

Class:instanceable name space

Page 101: P6 OO vs Moose (&Moo)

How To Solve That

Role:Reusable => Small

Class:Complete => Big

Page 102: P6 OO vs Moose (&Moo)

Class Do Can't Both

Role:Reusable => Small

!=Class:

Complete => Big

Page 103: P6 OO vs Moose (&Moo)

Roles

may be inherited !

if mixed into a class

& remove @ run time

Page 104: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

Page 105: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

No global overwrite likeRuby Mixins

Page 106: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

No global overwrite likeRuby Mixins

Refinements doesn't solve it all

Page 107: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

Roles > multiple inheritance(conflicts remain unhandled

- in intelligent way)

Page 108: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

except when method is empty

Page 109: P6 OO vs Moose (&Moo)

Rolesconflicts throw exceptions

except when method is empty

then is has to be overwritten (interface)

Page 110: P6 OO vs Moose (&Moo)

Roles role Spaceship { has Int $.speed; method stop { $.speed = 0 } }

Page 111: P6 OO vs Moose (&Moo)

Roles role Clock { has DateTime $.time; method alarm { ... } }

Page 112: P6 OO vs Moose (&Moo)

Apply Roles

with => does

Page 113: P6 OO vs Moose (&Moo)

Moose

class Excalibur extends WhiteStar with Clock;

Page 114: P6 OO vs Moose (&Moo)

Moo too !

Page 115: P6 OO vs Moose (&Moo)

Moo::Role

package Excalibur;extends 'WhiteStar';with 'Clock';

Page 116: P6 OO vs Moose (&Moo)

Perl 6

class Excalibur is WhiteStar does Clock;

Page 117: P6 OO vs Moose (&Moo)

Perl 6

class Excalibur is Whitestar;

also does Clock;

Page 118: P6 OO vs Moose (&Moo)

Perl 6

class Excalibbur is WhiteStar;

also does Clock does PlasmaGun;

Page 119: P6 OO vs Moose (&Moo)

Perl 6

$excalibur does Clock;

Page 120: P6 OO vs Moose (&Moo)

Introspection

Page 121: P6 OO vs Moose (&Moo)

MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures

Page 122: P6 OO vs Moose (&Moo)

MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures

Page 123: P6 OO vs Moose (&Moo)

MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures

Page 124: P6 OO vs Moose (&Moo)

Introspection

Class.HOW.methods($obj)

Class.^methods()

Page 125: P6 OO vs Moose (&Moo)

Metaobjectmethods

identifier name authority version author description subject language licensed parents roles

Page 126: P6 OO vs Moose (&Moo)

Deeper & Deeper

$obj.^methods()[$which].signature

Page 127: P6 OO vs Moose (&Moo)

Introspection

All is an Object

Page 128: P6 OO vs Moose (&Moo)

Introspection

All is an Object

„objects are stupid“.uc

Page 129: P6 OO vs Moose (&Moo)

Introspection

All is an ObjectCommands are Methods

Page 130: P6 OO vs Moose (&Moo)

Introspection

All is an ObjectCommands are Methods

(Operators too)

Page 131: P6 OO vs Moose (&Moo)

Introspection

All is an ObjectCommands are Methods

(Operators too)MMD is everywhere

Page 132: P6 OO vs Moose (&Moo)

Introspection

All is an ObjectCommands are Methods

(Operators too)MMD is everywhere

also in Regexes

Page 133: P6 OO vs Moose (&Moo)

Name Spaces

package module

class

Page 134: P6 OO vs Moose (&Moo)

A Kind Of Class

package module

class grammar

Page 135: P6 OO vs Moose (&Moo)

Grammars

grammar { token { … } rule { … } regex { … }}

Page 136: P6 OO vs Moose (&Moo)

Learn MoreS12: Objekte,S14: Rollen

perl6.org/documentationhttp://perlcabal.org/syn/

opt. Precise & Volume

Page 137: P6 OO vs Moose (&Moo)

Learn More

Perl 6 Docs

doc.perl6.org/language/objects

Opt.: Short & Precise

Page 138: P6 OO vs Moose (&Moo)

Learn More

Perl 6 Tablets

tablets.perl6.org

opt.: Hypertext & Volume

Page 139: P6 OO vs Moose (&Moo)

Cockaigne

Page 140: P6 OO vs Moose (&Moo)

Thank You