use Moose;
&focus_on_actual_problem_solving
Raf Gemmail
@fiqus
Wellington.pm
June 2014
% perldoc perltoot
% perldoc perltoot
% perldoc perltoot
% perldoc perltoot
% perldoc perltoot
Does Not Solve
A
Business
Problem
Class::Accessor
Class::Accessor
Prelude
● Declarative
● Focus TDD(esign) and Single Responsibility
● Standard Paradigm (Mo[ou].* and Perl6)
● Focus on ret...
Moose
Moose
● strict and warnings pragmas enabled
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new({name=>'Raf'})
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new({name=>'Raf'})
● $person->name() immutable
● $pers...
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new
● get_name, get_age, set_age
Moose
● strict and warnings pragmas enabled
● Constructor : Person->new
● get_name (immutable), get_age, set_age
● Type ch...
Attributes
Attributes
● is (mutability)
Attributes
● is (mutability)
– ro
– rw
Attributes
● is (mutability)
● isa (type validation)
Attributes
● is (mutability)
● isa (type validation)
– Class (or Role) name eg. Person
Attributes
● is (mutability)
● isa (type validation)
– Class (or Role) name eg. Person
– Object, Int, Str, Bool, Undef, De...
Attributes
● is (mutability)
● isa (type validation)
– Class (or Role) name eg. Person
– Object, Int, Str, Bool, Undef, De...
Attributes
● default
– default => $scalar || $sub_ref
Attributes
● default
– default => $scalar || $sub_ref
Attributes
● builder, lazy
– builder => method name
Attributes
● builder, lazy
– builder => method name
Attributes
● lazy_build => 1
Attributes
● handles
– Proxies methods
Attributes
Attributes
Attributes
Attributes
● reader, writer
– reader => 'get_vm_config'
– writer => 'persist_vm_config'
Attributes
● reader, writer
– reader => 'get_vm_config'
– writer => 'persist_vm_config'
● clearer => 'clear_attribute_name'
Attributes
● reader, writer
– reader => 'get_vm_config'
– writer => 'persist_vm_config'
● clearer => 'clear_attribute_name...
Attributes
● reader, writer
– reader => 'get_vm_config'
– writer => 'persist_vm_config'
● clearer => 'clear_attribute_name...
Inheritance
● extends
Ape
Person
Method Resolution Order
Animal
RomulanHorse Bird
Pegasus
• Perl depth-first: [Pegasus → Horse → Animal, Bird]
• eg. Animal...
Roles
● Cross cutting characteristics (attribs &
methods)
● Like Interface and Abstract Classes
● Base class which defines...
Roles
Role
Roles
Roles
Roles
Consume role
after attribute
declarations
Method Modifiers
● Aspects – non-core cross-cutting behaviour
● Advice (do this) eg. A sub ref
● Point-cut (do the advice ...
Method Modifiers
● Aspects – non-core cross-cutting behaviour
● Advice (do this) eg. A sub ref
● Point-cut (do the advice ...
Method Modifiers
Method Modifiers
Method Modifiers
Method Modifiers
Method Modifiers
Method Modifiers
Method Modifiers
before
Method Modifiers
Transform
Args
Method Modifiers
Real
Method
Method Modifiers
After
Invocation
Method Modifiers
Method Modifiers
● around [qw(method_1, method_2)]
● before qr/.*all_matching.*/
● HasLogger HasModel HasWebService
MooseX::Method::Signatures
MooseX::Method::Signatures
● Type checking
● Method argument validation
Moops
MooseX::Declare
And they all play together
Avoid Starup Costs with Moo
● Pure Perl
Avoid Starup Costs with Moo
● Pure Perl
● Painless integration with Moose deps (auto
upgrades and does meta-magic)
Avoid Starup Costs with Moo
● Pure Perl
● Painless integration with Moose deps (auto
upgrades and does meta-magic)
● Fast
MooseX
● Lots of Extensions
● MooseX::Types
● MooseX::GetOpt
● MooseX::App
● MooseX::Aspect
MooseX::App::Simple
MooseX::App::Simple
Meta Magic
● Refelection
● __PACKAGE__->meta
– > get_all_subclasses
– > get_all_attributes
– > add_attribute
– > magix
Recommendations
● use namespace::clean
● Use MooseX::FollowPBP
● Test::Routine – test with fixture roles and
various permu...
no Moose;
Upcoming SlideShare
Loading in...5
×

use Moose; &focus_on_actual_problem_solving

160

Published on

Introduction to Moose for wellington.pm

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
160
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

use Moose; &focus_on_actual_problem_solving

  1. 1. use Moose; &focus_on_actual_problem_solving Raf Gemmail @fiqus Wellington.pm June 2014
  2. 2. % perldoc perltoot
  3. 3. % perldoc perltoot
  4. 4. % perldoc perltoot
  5. 5. % perldoc perltoot
  6. 6. % perldoc perltoot Does Not Solve A Business Problem
  7. 7. Class::Accessor
  8. 8. Class::Accessor
  9. 9. Prelude ● Declarative ● Focus TDD(esign) and Single Responsibility ● Standard Paradigm (Mo[ou].* and Perl6) ● Focus on returning business value / solution
  10. 10. Moose
  11. 11. Moose ● strict and warnings pragmas enabled
  12. 12. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new
  13. 13. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new
  14. 14. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new({name=>'Raf'})
  15. 15. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new({name=>'Raf'}) ● $person->name() immutable ● $person->age(5) ● $age = $person->age()
  16. 16. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new ● get_name, get_age, set_age
  17. 17. Moose ● strict and warnings pragmas enabled ● Constructor : Person->new ● get_name (immutable), get_age, set_age ● Type checking (isa)
  18. 18. Attributes
  19. 19. Attributes ● is (mutability)
  20. 20. Attributes ● is (mutability) – ro – rw
  21. 21. Attributes ● is (mutability) ● isa (type validation)
  22. 22. Attributes ● is (mutability) ● isa (type validation) – Class (or Role) name eg. Person
  23. 23. Attributes ● is (mutability) ● isa (type validation) – Class (or Role) name eg. Person – Object, Int, Str, Bool, Undef, Def,
  24. 24. Attributes ● is (mutability) ● isa (type validation) – Class (or Role) name eg. Person – Object, Int, Str, Bool, Undef, Def, – Ref (ScalarRef, ArrayRef, HashRef)
  25. 25. Attributes ● default – default => $scalar || $sub_ref
  26. 26. Attributes ● default – default => $scalar || $sub_ref
  27. 27. Attributes ● builder, lazy – builder => method name
  28. 28. Attributes ● builder, lazy – builder => method name
  29. 29. Attributes ● lazy_build => 1
  30. 30. Attributes ● handles – Proxies methods
  31. 31. Attributes
  32. 32. Attributes
  33. 33. Attributes
  34. 34. Attributes ● reader, writer – reader => 'get_vm_config' – writer => 'persist_vm_config'
  35. 35. Attributes ● reader, writer – reader => 'get_vm_config' – writer => 'persist_vm_config' ● clearer => 'clear_attribute_name'
  36. 36. Attributes ● reader, writer – reader => 'get_vm_config' – writer => 'persist_vm_config' ● clearer => 'clear_attribute_name' ● predicate => 'is_verbose'
  37. 37. Attributes ● reader, writer – reader => 'get_vm_config' – writer => 'persist_vm_config' ● clearer => 'clear_attribute_name' ● predicate => 'is_verbose' ● trigger => &sub_ref
  38. 38. Inheritance ● extends Ape Person
  39. 39. Method Resolution Order Animal RomulanHorse Bird Pegasus • Perl depth-first: [Pegasus → Horse → Animal, Bird] • eg. Animal::brain has higher precedence than Bird::brain • C3 MRO breadth first: [Pegasus, Horse, Bird, Animal] use base qw(Horse Bird) vs extends qw(Horse Bird);
  40. 40. Roles ● Cross cutting characteristics (attribs & methods) ● Like Interface and Abstract Classes ● Base class which defines a behavioural contract with an optional partial implementation
  41. 41. Roles
  42. 42. Role
  43. 43. Roles
  44. 44. Roles
  45. 45. Roles Consume role after attribute declarations
  46. 46. Method Modifiers ● Aspects – non-core cross-cutting behaviour ● Advice (do this) eg. A sub ref ● Point-cut (do the advice at these point of my execution) Before, After and Around
  47. 47. Method Modifiers ● Aspects – non-core cross-cutting behaviour ● Advice (do this) eg. A sub ref ● Point-cut (do the advice at these point of my execution) Before, After and Around
  48. 48. Method Modifiers
  49. 49. Method Modifiers
  50. 50. Method Modifiers
  51. 51. Method Modifiers
  52. 52. Method Modifiers
  53. 53. Method Modifiers
  54. 54. Method Modifiers before
  55. 55. Method Modifiers Transform Args
  56. 56. Method Modifiers Real Method
  57. 57. Method Modifiers After Invocation
  58. 58. Method Modifiers
  59. 59. Method Modifiers ● around [qw(method_1, method_2)] ● before qr/.*all_matching.*/ ● HasLogger HasModel HasWebService
  60. 60. MooseX::Method::Signatures MooseX::Method::Signatures ● Type checking ● Method argument validation
  61. 61. Moops
  62. 62. MooseX::Declare
  63. 63. And they all play together
  64. 64. Avoid Starup Costs with Moo ● Pure Perl
  65. 65. Avoid Starup Costs with Moo ● Pure Perl ● Painless integration with Moose deps (auto upgrades and does meta-magic)
  66. 66. Avoid Starup Costs with Moo ● Pure Perl ● Painless integration with Moose deps (auto upgrades and does meta-magic) ● Fast
  67. 67. MooseX ● Lots of Extensions ● MooseX::Types ● MooseX::GetOpt ● MooseX::App ● MooseX::Aspect
  68. 68. MooseX::App::Simple
  69. 69. MooseX::App::Simple
  70. 70. Meta Magic ● Refelection ● __PACKAGE__->meta – > get_all_subclasses – > get_all_attributes – > add_attribute – > magix
  71. 71. Recommendations ● use namespace::clean ● Use MooseX::FollowPBP ● Test::Routine – test with fixture roles and various permutations of test data. ● __PACKAGE__->meta->make_immutable;
  72. 72. no Moose;
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×