p6oo
DESCRIPTION
Eine praktische Einleitung in the OO-Syntax von Perl 6 mit ein wenig TheorieTRANSCRIPT
Perl 6 OOP
Was ist OOP ?
LarryWall
Du meinst OPP ?
Was ist OOP ?
Position
Out Of Position
O O P
Was ist OOP ?
Lies Das !
Lies Das !
Object-oriented programming ... many opinions, theories, and even ideologies have
been formulated on the subject. ... Most are
mutually inconsistent.
O O P
Klassen / Prototypen(Multiple) Vererbung / Rollen
MMD + DelegationTypen + Subtypen
Introspektion / Metaobj.
Seine Meinung
TIMTOWTDI
Alles da in Schönheit
Suche Perfektion
Klasse
Klasse
class
Klasse
class
instanzierbarer Namensraum
Klasse
class module package
Klasse
class Excalibur; class Babylon;
Klasse
class
Instanzierbarer Namensraum
NR in Klammern
class Excalibur { ... }
Objekt
Objekt
my $obj = Klasse.new();
Objekt
my $obj = Klasse.new();
Neu Erschaffen
Bestehendes Klonen
Objekt
my $obj = $alt.clone();
Objekt
my $obj = $alt.clone(...);
Positionale Paramter
clone($pos1, $pos2);
Benannte Parameter
clone( :key('value'),);
Mit Autoquoting
clone( :key<value>,);
Old School Geht Auch
clone( key=>'value',);
Objekt
new & clone
bless blieb
Attribute
+
Methoden
Klasse class Spaceship { has Int $.speed; method stop { $.speed = 0 } }
Kann Ich auch !
In Perl 5package Spaceship;use Moose;has 'speed' => ( is => 'ro'; isa => 'Int';);sub stop { $self = shift; $self->speed = 0;}
In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}
Klasse class Spaceship { has Int $.speed; method stop { $.speed = 0; } }
Attributbenutzung
P5 P6
$self->speed $.speedshift->speed self.speed $!speed
Twigil der Accessoren
. öffentlich ! privat
Twigil der Accessoren
. öffentlich ! privat
has $!speed; # privat
Twigil der Accessoren
. öffentlich ! privat
has $speed; # auch privat
trusts
trusts
class Hund { trusts Katze; has $!Knochen;}
trusts
my $carlo = Hund.new();my $mine = Katze.new();
$mine!Knochen = 0;
Twigils . öffentliche A. ! private A. ^ pos. auto para. : ben. Auto para. * globale ? compiler info = POD ~ sublang
Sigils
$ Skalar
@ Array
% Hash
Sigils
has $.speed;
has @.shuttle;
has %.crew;
Keine Typ Hashref
has $.speed;
has @.shuttle;
has %.crew;
MooseXuse MooseX::Declare;class Raumschiff { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}
MooseXuse MooseX::Declare;class Raumschiff { has 'speed' => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; }}
Perl 6 class Raumschiff { has Int $.speed is rw; method stop { $.speed = 0; } }
Perl 6 class Raumschiff { has Int $.speed is rw = 0; method stop { $.speed = 0; } }
MooseXuse MooseX::Declare;class Raumschiff { has 'speed' => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; }}
Perl 6 Attribute
kein:isa default (nur Syntax)predicate required coercereader writer init_argclearer builder lazy_build
Hab ich mir ausgedacht!
Perl 6 & Moose
has is
Subtypen
Moose
subtype 'Slogan' => as 'Str' => where {length $_< 50};
Perl 6
my subset Slogan of Str where {$_.chars < 50};
Delegation
Perl 6class spaceship;has DateTime $.clock;
$excalibur.clock.now;
Perl 6class spaceship;has DateTime $.clock handles 'now';
$excalibur.clock.now;
Perl 6class spaceship;has DateTime $.clock handles 'now';
$excalibur.clock.now; # ==$excalibur.now;
Moose
has 'clock' => ( handles => 'now'; );
Moose++, Nicht P6
has 'clock' => ( handles => { now => 'time'
}; );
Methoden
Methode
class Spaceship;
method stop { … }
Private Methode
method !stop { … }
Methoden
method !stop { … }
submethod go { … }
Methoden
# wird vererbtmethod !stop { … }# nicht erbbarsubmethod go { … }
MMD
?
MMD
MultiMethodDispatch
Schlüsselworte
only multi proto
Schlüsselworte
only #sowieso defaultmulti # anschaun !proto # später
MMD
multi method go (Coord $place) {}
multi method go (Str $cmd) {};
MMD
$excalibur.go('back');
MMD
only #sowieso defaultmulti # MMDproto # selber regeln
Vererbung
Vererbung
Moose P6
extends => is
MooseX::Declare
class WhiteStar extends Spaceship;
Perl 6
class WhiteStar is Spaceship;
Mehrfachvererbung
class WhiteStaris Spaceship is Membari;
Vererbung später
Moose P6
extends => also is
MooseX::Declare
class WhiteStar;...extends Spaceship;
Perl 6
class WhiteStar { ... also is Spaceship;
Rollen
Klassenhierarchie
Wo kommt die Neue rein?
Rollen
werden nicht vererbt !
geht nur ins Objekt
& zur Laufzeit raus
Rollen
werden vererbt !
wenn in eine Klasse gemischt
Rollen
Konflikte werfen Ausnahme
Rollen
Konflikte werfen Ausnahme
Überschreiben nicht global wie Ruby Mixins
Rollen
Konflikte werfen Ausnahme
Rollen > Mehrfachvererbung(dort bleiben Konflikte auch
unbemerkt)
Rollen
Konflikte werfen Ausnahme
außer wenn Methode leer
Rollen
Konflikte werfen Ausnahme
außer wenn Methode leer
dann muß überschrieben werden (Interface)
Rollen role Spaceship { has Int $.speed; method stop { $.speed = 0 } }
Rollen role Clock { has DateTime $.time; method alarm { ... } }
Rollen anwenden
Moose P6
with => does
Moose
class WhiteStar extends Spaceship with Clock;
Perl 6
class WhiteStar is Spaceship does Clock;
Perl 6
class WhiteStar is Spaceship;
also does Clock;
Perl 6
class WhiteStar is Spaceship;
also does Clock does PlasmaGun;
Laufzeiteinbindung
$excalibur does Clock;
Introspektion
Methoden jedes Objektes
WHAT 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
InteressantesteWHAT 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
Introspektion
Class.HOW.methods($obj)
Class.^methods()
Metaobjektmethoden
identifier name authority version author description subject language licensed parents roles
Immer tiefer
$obj.^methods()[$which].signature
Introspektion
Alles is ein Objekt
Introspektion
Alles is ein Objekt
„Objekte sind doof“.uc
(wie in Ruby)
Introspektion
Alles is ein ObjektBefehle sind Methoden
Introspektion
Alles is ein ObjektBefehle sind Methoden
(Operatoren auch)
Introspektion
Alles is ein ObjektBefehle sind Methoden
(Operatoren auch)MMD ist überall
Introspektion
Alles is ein ObjektBefehle sind Methoden
(Operatoren auch)MMD ist überall
Auch in den Regex
Namenräume
package module
class
Auch 'ne Art Klasse
package module
class grammar
Grammatiken
grammar { token { … } rule { … } regex { … }}
GrammatikenKlassen deren
Methoden Regex anwenden und Matchobjekte zurückgeben
Grammatiken
grammar + MMD
= Perl 6 Interna
Lern MehrS12: Objekte,S14: Rollen
perl6.org/documentationhttp://perlcabal.org/syn/
opt. Präzision & Umfang
Lern Mehr
Perl 6 Docs
doc.perl6.org/language/objects
optimiert: Kürze & Genauigkeit
Lern Mehr
Perl 6 Tablets
tablets.perl6.org
opt.: Hypertext & Umfang
Wann Kommt Perl 6 ?
Thank You