P6 OO vs Moose (&Moo)

1,220 views

Published on

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.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,220
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

P6 OO vs Moose (&Moo)

  1. 1. Perl 6 OOP
  2. 2. What is OOP ?
  3. 3. Larry Wall Says:
  4. 4. What is OOP ?
  5. 5. Position
  6. 6. Out Of Position
  7. 7. OOP
  8. 8. Man With Clue
  9. 9. Read That !
  10. 10. Damian Says: Object-oriented programming ... many opinions, theories, and even ideologies have been formulated on the subject. ... Most are mutually inconsistent.
  11. 11. OOP Classes / Prototypes (Multiple) Inheritance / Roles MMD + Delegation Types + Subtypes Introspection / Metaobj.
  12. 12. His Opinion
  13. 13. TIMTOWTDI
  14. 14. All There in Beauty
  15. 15. In Search Of Perf.
  16. 16. Starts With A Class
  17. 17. Class class
  18. 18. Class class instanceable name space
  19. 19. Class class module package
  20. 20. Class class Excalibur; class Babylon;
  21. 21. Class class instanceable name space
  22. 22. NS in Braces class Excalibur { ... }
  23. 23. Object
  24. 24. Object my $obj = Class.new();
  25. 25. Ops Changed my $obj = Class.new();
  26. 26. Create New
  27. 27. Clone Existing
  28. 28. Object my $obj = $old.clone();
  29. 29. Object my $obj = $old.clone(...);
  30. 30. Positional Paramters clone($pos1, $pos2);
  31. 31. Named Parameters clone( :key('value'),);
  32. 32. With Autoquoting clone( :key<value>,);
  33. 33. Old School Works Too clone( key=>'value',);
  34. 34. Object new & clone bless stayed
  35. 35. Attributes + Methods
  36. 36. Space Ship
  37. 37. Class class Spaceship { has Int $.speed; method stop { $speed = 0 } }
  38. 38. I can do that too !
  39. 39. In Perl 5 package Spaceship; use Moose; has 'speed' => ( is => 'ro'; isa => 'Int'; ); sub stop { $self = shift; $self->speed = 0; }
  40. 40. Me too !
  41. 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. 42. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; } }
  43. 43. Class class Spaceship { has Int $.speed; method stop { $.speed = 0; } }
  44. 44. Attribute Usage P5 $self->speed shift->speed P6 $.speed self.speed $!speed
  45. 45. Twigil of Accessors . ! public private
  46. 46. Twigil of Accessors . ! public private has $!speed; # private
  47. 47. Twigil of Accessors . ! has $speed; public private # private too
  48. 48. trusts
  49. 49. trusts class Dog { trusts Cat; has $!Bone; }
  50. 50. trusts class Cat { method steal { my $carlo = Dog.new(); $carlo!Bone = 0; ...
  51. 51. . ! ^ : * ? = ~ Twigils punlic access. private access. pos. auto para. named auto p. global var compiler info POD sublang
  52. 52. Sigils $ @ % Scalar Array Hash
  53. 53. Sigils has $.speed; has @.shuttle; has %.crew;
  54. 54. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; } }
  55. 55. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; } }
  56. 56. Class class Spaceship { has Int $.speed is rw; } method stop { $.speed = 0; }
  57. 57. Class class Spaceship { has Int $.speed is rw = 0; } method stop { $.speed = 0; }
  58. 58. In Perl 5 use MooseX::Declare; class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; } }
  59. 59. Perl 6 Attribute no: isa default (just Syntax) predicate required coerce reader writer init_arg clearer builder lazy_build
  60. 60. That was my idea!
  61. 61. Perl 6 & Moose has is
  62. 62. Subtypes
  63. 63. Moose subtype 'Slogan' => as 'Str' => where {length $_< 50};
  64. 64. Perl 6 my subset Slogan of Str where {$_.chars < 50};
  65. 65. Delegation
  66. 66. Excalibur
  67. 67. Perl 6 class Excalibur; has $.clock handles 'now'; $excalibur = Excalibur.new; $excalibur.clock.now;
  68. 68. Perl 6 class Excalibur; has DateTime $.clock handles 'now'; $excalibur = Excalibur.new; $excalibur.now;
  69. 69. Moose has 'clock' => ( handles => 'now'; );
  70. 70. Moose Rename has 'clock' => ( handles => { now => 'time' }; );
  71. 71. Perl 6 Rename class Spaceship; has DateTime $.clock handles { :time<now>};
  72. 72. Methods
  73. 73. Methods method stop { … }
  74. 74. Methods method !stop { … }
  75. 75. Methods method !stop { … } submethod
  76. 76. Methods method !stop { … } submethod # !inherit
  77. 77. MMD ?
  78. 78. MMD Multi Method Dispatch
  79. 79. MMD only multi proto
  80. 80. MMD only # default anyway multi # look at ! proto # later
  81. 81. MMD multi method navigate (Coord $place) {} multi method navigate (Str $cmd) {};
  82. 82. MMD $excalibur.navigate('back');
  83. 83. MMD only # default anyway multi # MMD proto # own handling
  84. 84. Inheritance
  85. 85. MooseX::Declare class WhiteStar extends Spaceship;
  86. 86. Inheritance extends => is
  87. 87. Perl 6 class WhiteStar is Spaceship;
  88. 88. Multiple Inheritance class WhiteStar is Spaceship is Minbari;
  89. 89. Vererbung später extends => also is
  90. 90. MooseX::Declare class WhiteStar; ... extends Spaceship;
  91. 91. Perl 6 class WhiteStar { ... also is Spaceship;
  92. 92. Roles
  93. 93. Class Hierarchy
  94. 94. Where to insert ?
  95. 95. Solution Role: Unit Of Reusable Functionality
  96. 96. Therefore Role: Unit Of Reusable Functionality Outside Any Hierarchy
  97. 97. Solution Role: Unit Of Reusable Functionality Trait Elsewhere
  98. 98. Therefore Role: Unit Of Reusable Functionality Roles have Atributes, Traits not
  99. 99. Therefore Role: Reusable => Small
  100. 100. Remember? Role: Reusable => Small Class: instanceable name space
  101. 101. How To Solve That Role: Reusable => Small Class: Complete => Big
  102. 102. Class Do Can't Both Role: Reusable => Small != Class: Complete => Big
  103. 103. Roles may be inherited ! if mixed into a class & remove @ run time
  104. 104. Roles conflicts throw exceptions
  105. 105. Roles conflicts throw exceptions No global overwrite like Ruby Mixins
  106. 106. Roles conflicts throw exceptions No global overwrite like Ruby Mixins Refinements doesn't solve it all
  107. 107. Roles conflicts throw exceptions Roles > multiple inheritance (conflicts remain unhandled - in intelligent way)
  108. 108. Roles conflicts throw exceptions except when method is empty
  109. 109. Roles conflicts throw exceptions except when method is empty then is has to be overwritten (interface)
  110. 110. Roles role Spaceship { has Int $.speed; method stop { $.speed = 0 } }
  111. 111. Roles role Clock { has DateTime $.time; method alarm { ... } }
  112. 112. Apply Roles with => does
  113. 113. Moose class Excalibur extends WhiteStar with Clock;
  114. 114. Moo too !
  115. 115. Moo::Role package Excalibur; extends 'WhiteStar'; with 'Clock';
  116. 116. Perl 6 class Excalibur is WhiteStar does Clock;
  117. 117. Perl 6 class Excalibur is Whitestar; also does Clock;
  118. 118. Perl 6 class Excalibbur is WhiteStar; also does Clock does PlasmaGun;
  119. 119. Perl 6 $excalibur does Clock;
  120. 120. Introspection
  121. 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. 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. 123. 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
  124. 124. Introspection Class.HOW.methods($obj) Class.^methods()
  125. 125. Metaobjectmethods identifier name authority version author description licensed subject parents language roles
  126. 126. Deeper & Deeper $obj.^methods()[$which].signature
  127. 127. Introspection All is an Object
  128. 128. Introspection All is an Object „objects are stupid“.uc
  129. 129. Introspection All is an Object Commands are Methods
  130. 130. Introspection All is an Object Commands are Methods (Operators too)
  131. 131. Introspection All is an Object Commands are Methods (Operators too) MMD is everywhere
  132. 132. Introspection All is an Object Commands are Methods (Operators too) MMD is everywhere also in Regexes
  133. 133. Name Spaces package module class
  134. 134. A Kind Of Class package module class grammar
  135. 135. Grammars grammar { token { … } rule { … } regex { … } }
  136. 136. Learn More S12: Objekte,S14: Rollen perl6.org/documentation http://perlcabal.org/syn/ opt. Precise & Volume
  137. 137. Learn More Perl 6 Docs doc.perl6.org/language/objects Opt.: Short & Precise
  138. 138. Learn More Perl 6 Tablets tablets.perl6.org opt.: Hypertext & Volume
  139. 139. Cockaigne
  140. 140. Thank You

×