P6 OO vs Moose (&Moo)

  • 330 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
330
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Perl 6 OOP
  • 2. What is OOP ?
  • 3. Larry Wall Says:
  • 4. What is OOP ?
  • 5. Position
  • 6. Out Of Position
  • 7. OOP
  • 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. OOP 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 $self->speed shift->speed P6 $.speed self.speed $!speed
  • 45. Twigil of Accessors . ! public private
  • 46. Twigil of Accessors . ! public private has $!speed; # private
  • 47. Twigil of Accessors . ! has $speed; public private # 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. Solution Role: Unit Of Reusable Functionality Trait Elsewhere
  • 98. Therefore Role: Unit Of Reusable Functionality Roles have Atributes, Traits not
  • 99. Therefore Role: Reusable => Small
  • 100. Remember? Role: Reusable => Small Class: instanceable name space
  • 101. How To Solve That Role: Reusable => Small Class: Complete => Big
  • 102. Class Do Can't Both Role: Reusable => Small != Class: Complete => Big
  • 103. Roles may be inherited ! if mixed into a class & remove @ run time
  • 104. Roles conflicts throw exceptions
  • 105. Roles conflicts throw exceptions No global overwrite like Ruby Mixins
  • 106. Roles conflicts throw exceptions No global overwrite like Ruby Mixins Refinements doesn't solve it all
  • 107. Roles conflicts throw exceptions Roles > multiple inheritance (conflicts remain unhandled - in intelligent way)
  • 108. Roles conflicts throw exceptions except when method is empty
  • 109. Roles conflicts throw exceptions except when method is empty then is has to be overwritten (interface)
  • 110. Roles role Spaceship { has Int $.speed; method stop { $.speed = 0 } }
  • 111. Roles role Clock { has DateTime $.time; method alarm { ... } }
  • 112. Apply Roles with => does
  • 113. Moose class Excalibur extends WhiteStar with Clock;
  • 114. Moo too !
  • 115. Moo::Role package Excalibur; extends 'WhiteStar'; with 'Clock';
  • 116. Perl 6 class Excalibur is WhiteStar does Clock;
  • 117. Perl 6 class Excalibur is Whitestar; also does Clock;
  • 118. Perl 6 class Excalibbur is WhiteStar; also does Clock does PlasmaGun;
  • 119. Perl 6 $excalibur does Clock;
  • 120. Introspection
  • 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. 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. Introspection Class.HOW.methods($obj) Class.^methods()
  • 125. Metaobjectmethods identifier name authority version author description licensed subject parents language roles
  • 126. Deeper & Deeper $obj.^methods()[$which].signature
  • 127. Introspection All is an Object
  • 128. Introspection All is an Object „objects are stupid“.uc
  • 129. Introspection All is an Object Commands are Methods
  • 130. Introspection All is an Object Commands are Methods (Operators too)
  • 131. Introspection All is an Object Commands are Methods (Operators too) MMD is everywhere
  • 132. Introspection All is an Object Commands are Methods (Operators too) MMD is everywhere also in Regexes
  • 133. Name Spaces package module class
  • 134. A Kind Of Class package module class grammar
  • 135. Grammars grammar { token { … } rule { … } regex { … } }
  • 136. Learn More S12: Objekte,S14: Rollen perl6.org/documentation http://perlcabal.org/syn/ opt. Precise & Volume
  • 137. Learn More Perl 6 Docs doc.perl6.org/language/objects Opt.: Short & Precise
  • 138. Learn More Perl 6 Tablets tablets.perl6.org opt.: Hypertext & Volume
  • 139. Cockaigne
  • 140. Thank You