Perl 6 OOP
Was ist OOP ?

Larry
Wall
Du meinst OPP ?
Was ist OOP ?
Position
Out Of Position
OOP
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.
OOP
    Klassen / Prototypen
(Multiple) Vererbung / Rollen
     MMD + Delegation
     Typen + 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 5
package Spaceship;
use Moose;
has 'speed' => (
   is => 'ro';
   isa => 'Int';
);
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;
   }
}
Klasse
class Spaceship {
   has Int $.speed;
   method stop {
     $.speed = 0;
   }
}
Attributbenutzung
    P5           P6

$self->speed   $.speed
shift->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;
MooseX
use MooseX::Declare;
class Raumschiff {
   has 'speed' => (
      is => 'ro';
      isa => 'Int';
   );
   method stop {
      $self->speed = 0;
   }
}
MooseX
use 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;
    }
}
MooseX
use 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 coerce
reader writer init_arg
clearer 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 6
class spaceship;
has DateTime $.clock;


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

$excalibur.clock.now;
Perl 6
class 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 vererbt
method !stop { … }
# nicht erbbar
submethod go { … }
MMD


?
MMD
Multi
Method
Dispatch
Schlüsselworte

only
multi
proto
Schlüsselworte

only #sowieso default
multi # anschaun !
proto # später
MMD
multi method go
 (Coord $place) {}

multi method go
 (Str $cmd) {};
MMD


$excalibur.go('back');
MMD

only #sowieso default
multi # MMD
proto # selber regeln
Vererbung
Vererbung
 Moose    P6

extends => is
MooseX::Declare

class WhiteStar
  extends Spaceship;
Perl 6

class WhiteStar
 is Spaceship;
Mehrfachvererbung

class WhiteStar
is 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 context
 WHERE memory address
 HOW   object of meta class
 WHEN (reserved for events?)
 WHY   (reserved for documentation)
 WHENCE autovivification of closures
Interessanteste
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
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 Objekt
Befehle sind Methoden
Introspektion
  Alles is ein Objekt
Befehle sind Methoden
  (Operatoren auch)
Introspektion
  Alles is ein Objekt
Befehle sind Methoden
  (Operatoren auch)
   MMD ist überall
Introspektion
  Alles is ein Objekt
Befehle 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 { … }
}
Grammatiken
 Klassen deren
Methoden Regex
 anwenden und
  Matchobjekte
  zurückgeben
Grammatiken

grammar + MMD
= Perl 6 Interna
Lern Mehr
S12: Objekte,S14: Rollen

perl6.org/documentation
http://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

P6oo