Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl 6 OOP

lichtkind
September 05, 2015

Perl 6 OOP

features and syntax of OO in Perl 6, whcih might be much more than you expect

lichtkind

September 05, 2015
Tweet

More Decks by lichtkind

Other Decks in Programming

Transcript

  1. Perl 6 OOP

  2. What is OOP ?

  3. Larry Wall Says:

  4. What is OOP ?

  5. Position

  6. Out Of Position

  7. O O P

  8. Man With Clue

  9. Read That !

  10. Damian Says: Object-oriented programming ... many opinions, theories, and even

    ideologies have been formulated on the subject. ... Most are mutually inconsistent.
  11. O O P Classes / Prototypes (Multiple) Inheritance / Roles

    MMD + Delegation Types + Subtypes Introspection / Metaobj.
  12. His Opinion

  13. TIMTOWTDI

  14. All There in Beauty

  15. In Search Of Perf.

  16. Starts With A Class

  17. Class class

  18. Class class instanceable name space

  19. Class class module package

  20. Class class Excalibur; class Babylon;

  21. Class class instanceable name space

  22. NS in Braces class Excalibur { ... }

  23. Object

  24. Object my $obj = Class.new();

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

  26. Create New

  27. Clone Existing

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

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

  30. Positional Paramters clone($pos1, $pos2);

  31. Named Parameters clone( :key('value'),);

  32. With Autoquoting clone( :key<value>,);

  33. Old School Works Too clone( key=>'value',);

  34. Object new & clone bless stayed

  35. Attributes + Methods

  36. Space Ship

  37. Class class Spaceship { has Int $.speed; method stop {

    $speed = 0 } }
  38. I can do that too !

  39. In Perl 5 package Spaceship; use Moose; has 'speed' =>

    ( is => 'ro'; isa => 'Int'; ); sub stop { $self = shift; $self->speed = 0; }
  40. Me too !

  41. 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; }
  42. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed'

    => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; } }
  43. Class class Spaceship { has Int $.speed; method stop {

    $.speed = 0; } }
  44. Attribute Usage P5 P6 $self->speed $.speed shift->speed self.speed $!speed

  45. Twigil of Accessors . public ! private

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

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

    private too
  48. trusts

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

  50. trusts class Cat { method steal { my $carlo =

    Dog.new(); $carlo!Bone = 0; ...
  51. Twigils . punlic access. ! private access. ^ pos. auto

    para. : named auto p. * global var ? compiler info = POD ~ sublang
  52. Sigils $ Scalar @ Array % Hash

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

  54. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed'

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

    => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; } }
  56. Class class Spaceship { has Int $.speed is rw; method

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

    0; method stop { $.speed = 0; } }
  58. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed'

    => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; } }
  59. Perl 6 Attribute no: isa default (just Syntax) predicate required

    coerce reader writer init_arg clearer builder lazy_build
  60. That was my idea!

  61. Perl 6 & Moose has is

  62. Subtypes

  63. Moose subtype 'Slogan' => as 'Str' => where {length $_<

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

    50};
  65. Delegation

  66. Excalibur

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

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

    = Excalibur.new; $excalibur.now;
  69. Moose has 'clock' => ( handles => 'now'; );

  70. Moose Rename has 'clock' => ( handles => { now

    => 'time' }; );
  71. Perl 6 Rename class Spaceship; has DateTime $.clock handles {

    :time<now>};
  72. Methods

  73. Methods method stop { … }

  74. Methods method !stop { … }

  75. Methods method !stop { … } submethod

  76. Methods method !stop { … } submethod # !inherit

  77. MMD ?

  78. MMD Multi Method Dispatch

  79. MMD only multi proto

  80. MMD only # default anyway multi # look at !

    proto # later
  81. MMD multi method navigate (Coord $place) {} multi method navigate

    (Str $cmd) {};
  82. MMD $excalibur.navigate('back');

  83. MMD only # default anyway multi # MMD proto #

    own handling
  84. Inheritance

  85. MooseX::Declare class WhiteStar extends Spaceship;

  86. Inheritance extends => is

  87. Perl 6 class WhiteStar is Spaceship;

  88. Multiple Inheritance class WhiteStar is Spaceship is Minbari;

  89. Vererbung später extends => also is

  90. MooseX::Declare class WhiteStar; ... extends Spaceship;

  91. Perl 6 class WhiteStar { ... also is Spaceship;

  92. Roles

  93. Class Hierarchy

  94. Where to insert ?

  95. Solution Role: Unit Of Reusable Functionality

  96. Therefore Role: Unit Of Reusable Functionality Outside Any Hierarchy

  97. Therefore Role: Unit Of Reusable Functionality Called Trait Elsewhere

  98. Therefore Role: Reusable => Small

  99. Remember? Role: Reusable => Small Class: instanceable name space

  100. How To Solve That Role: Reusable => Small Class: Complete

    => Big
  101. Class Do Can't Both Role: Reusable => Small != Class:

    Complete => Big
  102. Roles may be inherited ! if mixed into a class

    & remove @ run time
  103. Roles conflicts throw exceptions

  104. Roles conflicts throw exceptions No global overwrite like Ruby Mixins

  105. Roles conflicts throw exceptions No global overwrite like Ruby Mixins

    Refinements doesn't solve it all
  106. Roles conflicts throw exceptions Roles > multiple inheritance (conflicts remain

    unhandled - in intelligent way)
  107. Roles conflicts throw exceptions except when method is empty

  108. Roles conflicts throw exceptions except when method is empty then

    is has to be overwritten (interface)
  109. Roles role Spaceship { has Int $.speed; method stop {

    $.speed = 0 } }
  110. Roles role Clock { has DateTime $.time; method alarm {

    ... } }
  111. Apply Roles with => does

  112. Moose class Excalibur extends WhiteStar with Clock;

  113. Moo too !

  114. Moo::Role package Excalibur; extends 'WhiteStar'; with 'Clock';

  115. Perl 6 class Excalibur is WhiteStar does Clock;

  116. Perl 6 class Excalibur is Whitestar; also does Clock;

  117. Perl 6 class Excalibbur is WhiteStar; also does Clock does

    PlasmaGun;
  118. Perl 6 $excalibur does Clock;

  119. Introspection

  120. 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
  121. 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
  122. 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
  123. Introspection Class.HOW.methods($obj) Class.^methods()

  124. Metaobjectmethods identifier name authority version author description subject language licensed

    parents roles
  125. Deeper & Deeper $obj.^methods()[$which].signature

  126. Introspection All is an Object

  127. Introspection All is an Object „objects are stupid“.uc

  128. Introspection All is an Object Commands are Methods

  129. Introspection All is an Object Commands are Methods (Operators too)

  130. Introspection All is an Object Commands are Methods (Operators too)

    MMD is everywhere
  131. Introspection All is an Object Commands are Methods (Operators too)

    MMD is everywhere also in Regexes
  132. Name Spaces package module class

  133. A Kind Of Class package module class grammar

  134. Grammars grammar { token { … } rule { …

    } regex { … } }
  135. Learn More S12: Objekte,S14: Rollen perl6.org/documentation http://perlcabal.org/syn/ opt. Precise &

    Volume
  136. Learn More Perl 6 Docs doc.perl6.org/language/objects Opt.: Short & Precise

  137. Learn More Perl 6 Tablets tablets.perl6.org opt.: Hypertext & Volume

  138. Cockaigne

  139. Thank You