OO Perl with Moose

     Nelo Onyiah
OO Perl with Moose

What is Moose?
OO Perl with Moose

What is Moose?
  Moose is a postmodern object system for Perl 5 that takes
  the tedium out of writing...
OO Perl with Moose

Why Moose?
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
  offer...
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
  offer...
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
  offer...
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
  offer...
OO Perl with Moose

Why Moose?

  makes Perl 5 OO both simpler and more powerful
  define your class declaratively
  offer...
OO Perl with Moose

package Person;      To make a class you start
1;
                     with a package
OO Perl with Moose

package Person;      To make a class you start
use Moose;
                     with a package and just...
OO Perl with Moose

package Person;      This is a complete class
use Moose;
                     definition
1;
OO Perl with Moose

package Person;      This is a complete class
use Moose;
                     definition
1;
          ...
OO Perl with Moose

package Person;      Under the hood Moose is
use Moose;
                     doing a lot
1;
OO Perl with Moose

package Person;      Under the hood Moose is
use Moose;
                     doing a lot
1;
          ...
OO Perl with Moose

package Person;      Classes have zero or more
use Moose;
                     attributes
1;
OO Perl with Moose

package Person;         Attributes are declared using
use Moose;
                        the has funct...
OO Perl with Moose

package Person;         Attributes are declared using
use Moose;
                        the has funct...
OO Perl with Moose

package Person;         Attributes are declared using
use Moose;
                        the has funct...
OO Perl with Moose

package Person;         Can be provided with
use Moose;
                        accessors
has 'birth_d...
OO Perl with Moose

package Person;         Can be provided with
use Moose;
                        accessors by stating t...
OO Perl with Moose

package Person;         or read-only
use Moose;

has 'birth_date' => (
    is => 'ro',
);

1;
OO Perl with Moose

package Person;                or you can provide your own
use Moose;
                               r...
OO Perl with Moose

package Person;         You can specify a type for
use Moose;
                        your attribute
h...
OO Perl with Moose

package Person;         Only values that pass the
use Moose;
                        type check will b...
OO Perl with Moose

package Person;         Built in types include:
use Moose;
                           Str
has 'birth_d...
OO Perl with Moose

package Person;          Class names are treated as
use Moose;
use DateTime;
                         ...
OO Perl with Moose
package Person;
use Moose;                        You can   create your own
use Moose::Util::TypeConstr...
OO Perl with Moose
package Person;
use Moose;                        You can   create your own
use Moose::Util::TypeConstr...
OO Perl with Moose
package Person;
use Moose;                           You can create your own
use Moose::Util::TypeConst...
OO Perl with Moose
package Person;
use Moose;                          You can create your own
use Moose::Util::TypeConstr...
OO Perl with Moose
package Person;
use Moose;                        You can also coerce   one
use Moose::Util::TypeConstr...
OO Perl with Moose
package Person;
use Moose;                        See Moose::Manual::Types
use Moose::Util::TypeConstra...
OO Perl with Moose
package Person;
use Moose;                        A person with   no birth date
use Moose::Util::TypeCo...
OO Perl with Moose
package Person;
use Moose;                          A person with no birth date
use Moose::Util::TypeCo...
OO Perl with Moose
package Person;
use Moose;                        You can also set default
use Moose::Util::TypeConstra...
OO Perl with Moose
package Person;
use Moose;                        Complex defaults need   to
use Moose::Util::TypeConst...
OO Perl with Moose
package Person;
use Moose;                       or you could write a
                                 ...
OO Perl with Moose
package Person;
use Moose;                       and make it lazy

# subtype ...
has 'birth_date' => (
...
OO Perl with Moose
package Person;
use Moose;                     or in one step

# subtype ...
has 'birth_date' => (
    ...
OO Perl with Moose
package Person;
use Moose;                       Attributes handle
                                 del...
OO Perl with Moose
package Person;
use Moose;                       Attributes handle
                                 del...
OO Perl with Moose
package Person;
use Moose;                       Delegation is one option to
                          ...
OO Perl with Moose
package Person;
use Moose;                        Delegation is one option to
                         ...
OO Perl with Moose

package Employee;       Inheritance is achieved with
use Moose;
extends qw( Person );
                ...
OO Perl with Moose

package Employee;       Inheritance is achieved with
use Moose;
extends qw( Person );
                ...
OO Perl with Moose

package Employee;       Inheritance is achieved with
use Moose;
extends qw( Person );
                ...
OO Perl with Moose

package Employee;              Override parent methods with
use Moose;
extends qw( Person );
         ...
OO Perl with Moose

package Employee;              Call the parent method with
use Moose;
extends qw( Person );
          ...
OO Perl with Moose

package Employee;        You can also override
use Moose;
extends qw( Person );
                      ...
OO Perl with Moose

package Employee;        You can also override
use Moose;
extends qw( Person );
                      ...
OO Perl with Moose

package Science;     Moose also has a concept of
1;
                     roles
OO Perl with Moose

package Science;     Moose also has a concept of
use Moose::Role;
                     roles
1;
      ...
OO Perl with Moose

package Science;     Similar to Smalltalk traits,
use Moose::Role;
                     Ruby Mixins an...
OO Perl with Moose

package Science;     Similar to Smalltalk traits,
use Moose::Role;
                     Ruby Mixins an...
OO Perl with Moose

package Science;     A collection of reusable traits
use Moose::Role;
                     (attributes...
OO Perl with Moose

package Science;        A collection of reusable traits
use Moose::Role;
                        (attr...
OO Perl with Moose

package Science;        A collection of reusable traits
use Moose::Role;
                        (attr...
OO Perl with Moose

package Science;        Roles are not classes
use Moose::Role;

has 'speciality' => (
    # ...
);

su...
OO Perl with Moose

package Science;        Roles are not classes
use Moose::Role;

has 'speciality' => (      cannot inst...
OO Perl with Moose

package Science;        Roles are not classes
use Moose::Role;

has 'speciality' => (      cannot inst...
OO Perl with Moose

package Science;        Roles are another option to
use Moose::Role;
                        inheritan...
OO Perl with Moose

package Science;        Roles are composed into
use Moose::Role;
                        consuming cla...
OO Perl with Moose

package Science;        Roles are composed into
use Moose::Role;
                        consuming cla...
OO Perl with Moose

package Science;        Roles can insist that
use Moose::Role;
                        consuming class...
OO Perl with Moose

package Science;           Roles can insist that
use Moose::Role;
                           consuming...
OO Perl with Moose

package Science;           Roles can insist that
use Moose::Role;
                           consuming...
OO Perl with Moose

package Scientist;      Roles are consumed into
use Moose;
extends qw( Person );
                     ...
OO Perl with Moose

package Scientist;      Roles are consumed into
use Moose;
extends qw( Person );
                     ...
OO Perl with Moose

package Scientist;      Roles are consumed into
use Moose;
extends qw( Person );
                     ...
OO Perl with Moose

package Scientist;      Class methods and attributes
use Moose;
extends qw( Person );
                ...
OO Perl with Moose

package Scientist;      Class methods and attributes
use Moose;
extends qw( Person );
                ...
OO Perl with Moose

package Scientist;      See Moose::Manual::Roles
use Moose;
extends qw( Person );
                    ...
OO Perl with Moose

package Scientist;      Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science );

sub...
OO Perl with Moose

package Scientist;      Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science );     ...
OO Perl with Moose

package Scientist;      Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science );     ...
OO Perl with Moose

package Scientist;      Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science );     ...
OO Perl with Moose

package Scientist;
use Moose;                     Make your classes immutable
extends qw( Person );
wi...
OO Perl with Moose

package Scientist;
use Moose;                      Make your classes immutable
extends qw( Person );
w...
OO Perl with Moose

package Scientist;
use Moose;                      Make your classes immutable
extends qw( Person );
w...
OO Perl with Moose

package Scientist;
use Moose;                      Also you are adviced to clean
extends qw( Person );...
OO Perl with Moose

package Scientist;
use Moose;                      Also you are adviced to clean
extends qw( Person );...
OO Perl with Moose

package Scientist;
use Moose;                      Also you are adviced to clean
use namespace::clean ...
OO Perl with Moose

  Moose is also extensible
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

  Moose is also extensible
  Done by manipulating metaclass objects
  This is where the wizards roam f...
OO Perl with Moose

package Person;
use Moose;                     Lastly you will note that
use namespace::clean         ...
OO Perl with Moose

package Person;
use Moose;                     Lastly you will note that
use namespace::clean         ...
OO Perl with Moose

package Person;
use Moose;                     Lastly you will note that
use namespace::clean         ...
OO Perl with Moose

use MooseX::Declare;
                               Declaring classes becomes
class Person {          ...
OO Perl with Moose

use MooseX::Declare;
                               Combines the power of
class Person {              ...
OO Perl with Moose

use MooseX::Declare;
                                Combines the power of
class Person {             ...
OO Perl with Moose

use MooseX::Declare;
                                Combines the power of
class Person {             ...
OO Perl with Moose

use MooseX::Declare;
                                 Combines the power of
class Person {            ...
OO Perl with Moose
use MooseX::Declare;

class Scientist extends Person with Science {
    use Duration; # fictional class...
Thank you

 Questions?
Upcoming SlideShare
Loading in...5
×

OO Perl with Moose

8,939

Published on

0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,939
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
139
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "OO Perl with Moose"

  1. 1. OO Perl with Moose Nelo Onyiah
  2. 2. OO Perl with Moose What is Moose?
  3. 3. OO Perl with Moose What is Moose? Moose is a postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl. It borrows all the best features from Perl 6, CLOS (LISP), Smalltalk, Java, BETA, OCaml, Ruby and more, while still keeping true to its Perl 5 roots.
  4. 4. OO Perl with Moose Why Moose?
  5. 5. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful
  6. 6. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively
  7. 7. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively offers "sugar" for object construction, attributes, e.t.c
  8. 8. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively offers "sugar" for object construction, attributes, e.t.c don't need to care how they are implemented
  9. 9. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively offers "sugar" for object construction, attributes, e.t.c don't need to care how they are implemented concentrate on the logical structure of your classes
  10. 10. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively offers "sugar" for object construction, attributes, e.t.c don't need to care how they are implemented concentrate on the logical structure of your classes don't need to be a wizard to use it
  11. 11. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively offers "sugar" for object construction, attributes, e.t.c don't need to care how they are implemented concentrate on the logical structure of your classes don't need to be a wizard to use it but if you are, lets you dig about in the guts and extend it
  12. 12. OO Perl with Moose package Person; To make a class you start 1; with a package
  13. 13. OO Perl with Moose package Person; To make a class you start use Moose; with a package and just use 1; Moose
  14. 14. OO Perl with Moose package Person; This is a complete class use Moose; definition 1;
  15. 15. OO Perl with Moose package Person; This is a complete class use Moose; definition 1; not terribly useful though
  16. 16. OO Perl with Moose package Person; Under the hood Moose is use Moose; doing a lot 1;
  17. 17. OO Perl with Moose package Person; Under the hood Moose is use Moose; doing a lot 1; (won't go into that though)
  18. 18. OO Perl with Moose package Person; Classes have zero or more use Moose; attributes 1;
  19. 19. OO Perl with Moose package Person; Attributes are declared using use Moose; the has function has 'birth_date' => ( ); 1;
  20. 20. OO Perl with Moose package Person; Attributes are declared using use Moose; the has function has 'birth_date' => ( ); Attributes have properties 1;
  21. 21. OO Perl with Moose package Person; Attributes are declared using use Moose; the has function has 'birth_date' => ( ); Attributes have properties 1; probably the most powerful feature of Moose
  22. 22. OO Perl with Moose package Person; Can be provided with use Moose; accessors has 'birth_date' => ( ); 1;
  23. 23. OO Perl with Moose package Person; Can be provided with use Moose; accessors by stating that has 'birth_date' => ( attribute is read-writeable is => 'rw', ); 1;
  24. 24. OO Perl with Moose package Person; or read-only use Moose; has 'birth_date' => ( is => 'ro', ); 1;
  25. 25. OO Perl with Moose package Person; or you can provide your own use Moose; reader and/or writer has 'birth_date' => ( is => 'ro', writer => '_set_birth_date', ); 1;
  26. 26. OO Perl with Moose package Person; You can specify a type for use Moose; your attribute has 'birth_date' => ( is => 'ro', isa => 'Str', ); 1;
  27. 27. OO Perl with Moose package Person; Only values that pass the use Moose; type check will be accepted has 'birth_date' => ( for the attribute is => 'ro', isa => 'Str', ); 1;
  28. 28. OO Perl with Moose package Person; Built in types include: use Moose; Str has 'birth_date' => ( Num is => 'ro', ArrayRef isa => 'Str', CodeRef ); Any 1; more ...
  29. 29. OO Perl with Moose package Person; Class names are treated as use Moose; use DateTime; types has 'birth_date' => ( is => 'ro', isa => 'DateTime', ); 1;
  30. 30. OO Perl with Moose package Person; use Moose; You can create your own use Moose::Util::TypeConstraints; types use DateTime; subtype 'ModernDateTime' has 'birth_date' => ( is => 'ro', isa => 'DateTime', ); 1;
  31. 31. OO Perl with Moose package Person; use Moose; You can create your own use Moose::Util::TypeConstraints; types use DateTime; from existing types subtype 'ModernDateTime' => as 'DateTime' has 'birth_date' => ( is => 'ro', isa => 'DateTime', ); 1;
  32. 32. OO Perl with Moose package Person; use Moose; You can create your own use Moose::Util::TypeConstraints; types use DateTime; from existing types subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; and apply your own constraints on them has 'birth_date' => ( is => 'ro', isa => 'DateTime', ); 1;
  33. 33. OO Perl with Moose package Person; use Moose; You can create your own use Moose::Util::TypeConstraints; types use DateTime; from existing types subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 };and apply your own constraints on them has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', and use them ); 1;
  34. 34. OO Perl with Moose package Person; use Moose; You can also coerce one use Moose::Util::TypeConstraints; type into another use DateTime; subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', ); 1;
  35. 35. OO Perl with Moose package Person; use Moose; See Moose::Manual::Types use Moose::Util::TypeConstraints; for more details use DateTime; subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', ); 1;
  36. 36. OO Perl with Moose package Person; use Moose; A person with no birth date use Moose::Util::TypeConstraints; seems odd use DateTime; subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', ); 1;
  37. 37. OO Perl with Moose package Person; use Moose; A person with no birth date use Moose::Util::TypeConstraints; seems odd use DateTime; so it can be made subtype 'ModernDateTime' => as 'DateTime' compulsary with the => where { $_->year >= 2000 };required flag has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, ); 1;
  38. 38. OO Perl with Moose package Person; use Moose; You can also set default use Moose::Util::TypeConstraints; values for the attribute use DateTime; subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, default => '2000-01-01', ); 1;
  39. 39. OO Perl with Moose package Person; use Moose; Complex defaults need to use Moose::Util::TypeConstraints; be set in a sub ref use DateTime; subtype 'ModernDateTime' => as 'DateTime' => where { $_->year >= 2000 }; has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, default => sub { DateTime->now }, ); 1;
  40. 40. OO Perl with Moose package Person; use Moose; or you could write a separate builder method # subtype ... has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, builder => '_build_birth_date', ); sub _build_birth_date { DateTime->now; } 1;
  41. 41. OO Perl with Moose package Person; use Moose; and make it lazy # subtype ... has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, builder => '_build_birth_date', lazy => 1, ); sub _build_birth_date { DateTime->now; } 1;
  42. 42. OO Perl with Moose package Person; use Moose; or in one step # subtype ... has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, lazy_build => 1, ); sub _build_birth_date { DateTime->now; } 1;
  43. 43. OO Perl with Moose package Person; use Moose; Attributes handle delegation # subtype ... has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, lazy_build => 1, handles => { birth_year => 'year' }, ); sub _build_birth_date { DateTime->now; } 1;
  44. 44. OO Perl with Moose package Person; use Moose; Attributes handle delegation # subtype ... has 'birth_date' => ( Calling $person- is => 'ro', isa => 'ModernDateTime', >birth_year delegates to required => 1, $person->birth_date->year lazy_build => 1, handles => { birth_year => 'year' }, ); sub _build_birth_date { DateTime->now; } 1;
  45. 45. OO Perl with Moose package Person; use Moose; Delegation is one option to inheritance # subtype ... has 'birth_date' => ( is => 'ro', isa => 'ModernDateTime', required => 1, lazy_build => 1, handles => { birth_year => 'year' }, ); sub _build_birth_date { DateTime->now; } 1;
  46. 46. OO Perl with Moose package Person; use Moose; Delegation is one option to inheritance # subtype ... has 'birth_date' => ( Especially when inheriting is => 'ro', isa => 'ModernDateTime', from non-Moose based required => 1, classes lazy_build => 1, handles => { birth_year => 'year' }, ); sub _build_birth_date { DateTime->now; } 1;
  47. 47. OO Perl with Moose package Employee; Inheritance is achieved with use Moose; extends qw( Person ); the extends function 1;
  48. 48. OO Perl with Moose package Employee; Inheritance is achieved with use Moose; extends qw( Person ); the extends function 1; Moose supports multiple inheritance
  49. 49. OO Perl with Moose package Employee; Inheritance is achieved with use Moose; extends qw( Person ); the extends function 1; Moose supports multiple inheritance just pass more class names to extends
  50. 50. OO Perl with Moose package Employee; Override parent methods with use Moose; extends qw( Person ); the override function override '_build_birth_date' => sub { # ... } 1;
  51. 51. OO Perl with Moose package Employee; Call the parent method with use Moose; extends qw( Person ); the super function override '_build_birth_date' => sub { # ... super(); } 1;
  52. 52. OO Perl with Moose package Employee; You can also override use Moose; extends qw( Person ); attributes has '+birth_date' => ( # ... ); 1;
  53. 53. OO Perl with Moose package Employee; You can also override use Moose; extends qw( Person ); attributes (carefully) has '+birth_date' => ( # ... ); 1;
  54. 54. OO Perl with Moose package Science; Moose also has a concept of 1; roles
  55. 55. OO Perl with Moose package Science; Moose also has a concept of use Moose::Role; roles 1; Declared by using Moose:: Role
  56. 56. OO Perl with Moose package Science; Similar to Smalltalk traits, use Moose::Role; Ruby Mixins and Java 1; interfaces
  57. 57. OO Perl with Moose package Science; Similar to Smalltalk traits, use Moose::Role; Ruby Mixins and Java 1; interfaces Most similar to Perl 6 Roles
  58. 58. OO Perl with Moose package Science; A collection of reusable traits use Moose::Role; (attributes) 1;
  59. 59. OO Perl with Moose package Science; A collection of reusable traits use Moose::Role; (attributes) has 'speciality' => ( # ... ); 1;
  60. 60. OO Perl with Moose package Science; A collection of reusable traits use Moose::Role; (attributes) and behaviour has 'speciality' => ( (methods) # ... ); sub research { # ... } 1;
  61. 61. OO Perl with Moose package Science; Roles are not classes use Moose::Role; has 'speciality' => ( # ... ); sub research { # ... } 1;
  62. 62. OO Perl with Moose package Science; Roles are not classes use Moose::Role; has 'speciality' => ( cannot instantiate a role # ... ); sub research { # ... } 1;
  63. 63. OO Perl with Moose package Science; Roles are not classes use Moose::Role; has 'speciality' => ( cannot instantiate a role # ... cannot inherit from a role ); sub research { # ... } 1;
  64. 64. OO Perl with Moose package Science; Roles are another option to use Moose::Role; inheritance has 'speciality' => ( # ... ); sub research { # ... } 1;
  65. 65. OO Perl with Moose package Science; Roles are composed into use Moose::Role; consuming classes/roles has 'speciality' => ( # ... ); sub research { # ... } 1;
  66. 66. OO Perl with Moose package Science; Roles are composed into use Moose::Role; consuming classes/roles has 'speciality' => ( # ... Attributes and methods from ); role are flattened into sub research { consuming class/role # ... } 1;
  67. 67. OO Perl with Moose package Science; Roles can insist that use Moose::Role; consuming classes has 'speciality' => ( implement certain methods # ... ); sub research { # ... } 1;
  68. 68. OO Perl with Moose package Science; Roles can insist that use Moose::Role; consuming classes requires qw( research ); implement certain methods has 'speciality' => ( # ... Use the requires function ); 1;
  69. 69. OO Perl with Moose package Science; Roles can insist that use Moose::Role; consuming classes requires qw( research ); implement certain methods has 'speciality' => ( # ... Use the requires function ); 1; Consuming classes must now implement the research function
  70. 70. OO Perl with Moose package Scientist; Roles are consumed into use Moose; extends qw( Person ); classes by using the with with qw( Science ); keyword sub research { ... } 1;
  71. 71. OO Perl with Moose package Scientist; Roles are consumed into use Moose; extends qw( Person ); classes by using the with with qw( Science ); keyword sub research { ... } More than one role can be 1; consumed into a class
  72. 72. OO Perl with Moose package Scientist; Roles are consumed into use Moose; extends qw( Person ); classes by using the with with qw( Science ); keyword sub research { ... } More than one role can be 1; consumed into a class just pass more roles to with
  73. 73. OO Perl with Moose package Scientist; Class methods and attributes use Moose; extends qw( Person ); are prioritized with qw( Science ); sub research { ... } 1;
  74. 74. OO Perl with Moose package Scientist; Class methods and attributes use Moose; extends qw( Person ); are prioritized with qw( Science ); Conflicts are resolved at sub research { ... } compile time 1;
  75. 75. OO Perl with Moose package Scientist; See Moose::Manual::Roles use Moose; extends qw( Person ); for details with qw( Science ); sub research { ... } 1;
  76. 76. OO Perl with Moose package Scientist; Moose is not perfect use Moose; extends qw( Person ); with qw( Science ); sub research { ... } 1;
  77. 77. OO Perl with Moose package Scientist; Moose is not perfect use Moose; extends qw( Person ); with qw( Science ); Biggest caveat is start up time sub research { ... } 1;
  78. 78. OO Perl with Moose package Scientist; Moose is not perfect use Moose; extends qw( Person ); with qw( Science ); Biggest caveat is start up time sub research { ... } 1; Actively being worked on
  79. 79. OO Perl with Moose package Scientist; Moose is not perfect use Moose; extends qw( Person ); with qw( Science ); Biggest caveat is start up time sub research { ... } 1; Actively being worked on But you can help
  80. 80. OO Perl with Moose package Scientist; use Moose; Make your classes immutable extends qw( Person ); with qw( Science ); sub research { ... } __PACKAGE__->meta->make_immutable(); 1;
  81. 81. OO Perl with Moose package Scientist; use Moose; Make your classes immutable extends qw( Person ); with qw( Science ); This lets Moose create an inline constructor for your sub research { ... } class __PACKAGE__->meta->make_immutable(); 1;
  82. 82. OO Perl with Moose package Scientist; use Moose; Make your classes immutable extends qw( Person ); with qw( Science ); This lets Moose create an inline constructor for your sub research { ... } class __PACKAGE__->meta->make_immutable(); 1; Greatly speeding up start up time
  83. 83. OO Perl with Moose package Scientist; use Moose; Also you are adviced to clean extends qw( Person ); up after your class with qw( Science ); i.e remove all Moose sugar sub research { ... } __PACKAGE__ from packages using your ->meta->make_immutable(); classes 1;
  84. 84. OO Perl with Moose package Scientist; use Moose; Also you are adviced to clean extends qw( Person ); up after your class with qw( Science ); i.e remove all Moose sugar sub research { ... } __PACKAGE__ from packages using your ->meta->make_immutable(); classes no Moose; 1;
  85. 85. OO Perl with Moose package Scientist; use Moose; Also you are adviced to clean use namespace::clean up after your class -except => [qw( meta )]; extends qw( Person ); i.e remove all Moose sugar with qw( Science ); from packages using your sub research { ... } classes __PACKAGE__ ->meta->make_immutable(); 1; Alternatively
  86. 86. OO Perl with Moose Moose is also extensible
  87. 87. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects
  88. 88. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free
  89. 89. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free A lot of extensions exist in the MooseX:: namespace
  90. 90. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free A lot of extensions exist in the MooseX:: namespace New ideas usually start life here
  91. 91. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free A lot of extensions exist in the MooseX:: namespace New ideas usually start life here Good ones get incorporated into Moose
  92. 92. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free A lot of extensions exist in the MooseX:: namespace New ideas usually start life here Good ones get incorporated into Moose An example is MooseX::AttributeHelpers
  93. 93. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free A lot of extensions exist in the MooseX:: namespace New ideas usually start life here Good ones get incorporated into Moose An example is MooseX::AttributeHelpers These were incorporated in the Moose::Meta::Attribute:: Native namespace
  94. 94. OO Perl with Moose package Person; use Moose; Lastly you will note that use namespace::clean Moose introduces its own -except => [qw( meta )]; boiler plate code # attributes and methods __PACKAGE__->meta->make_immutable(); 1;
  95. 95. OO Perl with Moose package Person; use Moose; Lastly you will note that use namespace::clean Moose introduces its own -except => [qw( meta )]; boiler plate code # attributes and methods __PACKAGE__->meta->make_immutable(); is an extension There that 1; reduces this
  96. 96. OO Perl with Moose package Person; use Moose; Lastly you will note that use namespace::clean Moose introduces its own -except => [qw( meta )]; boiler plate code # attributes and methods __PACKAGE__->meta->make_immutable(); is an extension There that 1; reduces this MooseX::Declare
  97. 97. OO Perl with Moose use MooseX::Declare; Declaring classes becomes class Person { even more declarative # attributes and methods }
  98. 98. OO Perl with Moose use MooseX::Declare; Combines the power of class Person { Moose with Devel::Declare to # attributes and methods produce keywords for Perl 5 } written in Perl 5
  99. 99. OO Perl with Moose use MooseX::Declare; Combines the power of class Person { Moose with Devel::Declare to # attributes and methods produce keywords for Perl 5 method research() { ... } written in Perl 5 } Keywords include class, role, method
  100. 100. OO Perl with Moose use MooseX::Declare; Combines the power of class Person { Moose with Devel::Declare to # attributes and methods produce keywords for Perl 5 method research() { ... } written in Perl 5 } Keywords include class, role, method Note that the methods have signatures complete with type checking
  101. 101. OO Perl with Moose use MooseX::Declare; Combines the power of class Person { Moose with Devel::Declare to # attributes and methods produce keywords for Perl 5 method research() { ... } written in Perl 5 } Keywords include class, role, method Note that the methods have signatures complete with type checking So using MooseX::Declare the Scientist class example could look like the following:
  102. 102. OO Perl with Moose use MooseX::Declare; class Scientist extends Person with Science { use Duration; # fictional class one could write has 'funding' => ( is => 'rw', isa => 'Num', lazy_build => 1, ); method research( Duration $contract_duration ) { unless ( $self->has_funding ) { confess 'need funding to work'; } while ( not $contract_duration->expired ) { # do your research ... } } }
  103. 103. Thank you Questions?
  1. A particular slide catching your eye?

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

×