SlideShare ist ein Scribd-Unternehmen logo
1 von 103
Downloaden Sie, um offline zu lesen
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 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.
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
  offers "sugar" for object construction, attributes, e.t.c
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
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
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
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
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 use
1;                   Moose
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;
                     not terribly useful though
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;
                     (won't go into that though)
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 function
has 'birth_date' => (
);

1;
OO Perl with Moose

package Person;         Attributes are declared using
use Moose;
                        the has function
has 'birth_date' => (
);                      Attributes have properties
1;
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
OO Perl with Moose

package Person;         Can be provided with
use Moose;
                        accessors
has 'birth_date' => (
);

1;
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;
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;
                               reader and/or writer
has 'birth_date' => (
    is => 'ro',
    writer => '_set_birth_date',
);

1;
OO Perl with Moose

package Person;         You can specify a type for
use Moose;
                        your attribute
has 'birth_date' => (
    is => 'ro',
    isa => 'Str',
);

1;
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;
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 ...
OO Perl with Moose

package Person;          Class names are treated as
use Moose;
use DateTime;
                         types

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

1;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
OO Perl with Moose

package Employee;       Inheritance is achieved with
use Moose;
extends qw( Person );
                        the extends function

1;
OO Perl with Moose

package Employee;       Inheritance is achieved with
use Moose;
extends qw( Person );
                        the extends function

1;                      Moose supports multiple
                        inheritance
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
OO Perl with Moose

package Employee;              Override parent methods with
use Moose;
extends qw( Person );
                               the override function

override '_build_birth_date' => sub {
    # ...
}
1;
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;
OO Perl with Moose

package Employee;        You can also override
use Moose;
extends qw( Person );
                         attributes

has '+birth_date' => (
    # ...
);
1;
OO Perl with Moose

package Employee;        You can also override
use Moose;
extends qw( Person );
                         attributes (carefully)

has '+birth_date' => (
    # ...
);
1;
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;
                     Declared by using Moose::
                     Role
OO Perl with Moose

package Science;     Similar to Smalltalk traits,
use Moose::Role;
                     Ruby Mixins and Java
1;                   interfaces
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
OO Perl with Moose

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

package Science;        A collection of reusable traits
use Moose::Role;
                        (attributes)
has 'speciality' => (
    # ...
);
1;
OO Perl with Moose

package Science;        A collection of reusable traits
use Moose::Role;
                        (attributes) and behaviour
has 'speciality' => (   (methods)
    # ...
);

sub research {
    # ...
}
1;
OO Perl with Moose

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

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

sub research {
    # ...
}
1;
OO Perl with Moose

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

has 'speciality' => (      cannot instantiate a role
    # ...
);

sub research {
    # ...
}
1;
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;
OO Perl with Moose

package Science;        Roles are another option to
use Moose::Role;
                        inheritance
has 'speciality' => (
    # ...
);

sub research {
    # ...
}
1;
OO Perl with Moose

package Science;        Roles are composed into
use Moose::Role;
                        consuming classes/roles
has 'speciality' => (
    # ...
);

sub research {
    # ...
}
1;
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;
OO Perl with Moose

package Science;        Roles can insist that
use Moose::Role;
                        consuming classes
has 'speciality' => (   implement certain methods
    # ...
);

sub research {
    # ...
}
1;
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;
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
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;
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
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
OO Perl with Moose

package Scientist;      Class methods and attributes
use Moose;
extends qw( Person );
                        are prioritized
with qw( Science );

sub research { ... }
1;
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;
OO Perl with Moose

package Scientist;      See Moose::Manual::Roles
use Moose;
extends qw( Person );
                        for details
with qw( Science );

sub research { ... }
1;
OO Perl with Moose

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

sub research { ... }
1;
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;
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
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
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;
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;
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
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;
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;
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
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 free
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
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
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
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
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
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;
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
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
OO Perl with Moose

use MooseX::Declare;
                               Declaring classes becomes
class Person {                 even more declarative
    # attributes and methods
}
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
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
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
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:
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 ...
        }
    }
}
Thank you

 Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Moose workshop
Moose workshopMoose workshop
Moose workshopYnon Perek
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginnersleo lapworth
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helperslicejack
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressAlena Holligan
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010leo lapworth
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsMichael Pirnat
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?Maksym Hopei
 
Introduction to Perl Best Practices
Introduction to Perl Best PracticesIntroduction to Perl Best Practices
Introduction to Perl Best PracticesJosé Castro
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perlgarux
 
Banishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHPBanishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHPDavid Hayes
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeProf. Wim Van Criekinge
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners PerlDave Cross
 
Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)Bozhidar Batsov
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet
 

Was ist angesagt? (20)

Moose workshop
Moose workshopMoose workshop
Moose workshop
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Moose
MooseMoose
Moose
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Introduction to Perl Best Practices
Introduction to Perl Best PracticesIntroduction to Perl Best Practices
Introduction to Perl Best Practices
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
Banishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHPBanishing Loops with Functional Programming in PHP
Banishing Loops with Functional Programming in PHP
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 
Perl
PerlPerl
Perl
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
 

Andere mochten auch

Extending Moose
Extending MooseExtending Moose
Extending Moosesartak
 
Introduction to OO Perl with Moose
Introduction to OO Perl with MooseIntroduction to OO Perl with Moose
Introduction to OO Perl with MooseDave Cross
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Junichi Ishida
 
Introduction to .NET Core
Introduction to .NET CoreIntroduction to .NET Core
Introduction to .NET CoreMarco Parenzan
 
Five Tips To Help You Tackle Programming
Five Tips To Help You Tackle ProgrammingFive Tips To Help You Tackle Programming
Five Tips To Help You Tackle ProgrammingWiley
 
Data communication and network Chapter -1
Data communication and network Chapter -1Data communication and network Chapter -1
Data communication and network Chapter -1Zafar Ayub
 
Chapter 1: Introduction to Data Communication and Networks
Chapter 1: Introduction to Data Communication and NetworksChapter 1: Introduction to Data Communication and Networks
Chapter 1: Introduction to Data Communication and NetworksShafaan Khaliq Bhatti
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with DataSeth Familian
 

Andere mochten auch (9)

Extending Moose
Extending MooseExtending Moose
Extending Moose
 
Introduction to OO Perl with Moose
Introduction to OO Perl with MooseIntroduction to OO Perl with Moose
Introduction to OO Perl with Moose
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Introduction to .NET Core
Introduction to .NET CoreIntroduction to .NET Core
Introduction to .NET Core
 
Five Tips To Help You Tackle Programming
Five Tips To Help You Tackle ProgrammingFive Tips To Help You Tackle Programming
Five Tips To Help You Tackle Programming
 
Data communication and network Chapter -1
Data communication and network Chapter -1Data communication and network Chapter -1
Data communication and network Chapter -1
 
Chapter 1: Introduction to Data Communication and Networks
Chapter 1: Introduction to Data Communication and NetworksChapter 1: Introduction to Data Communication and Networks
Chapter 1: Introduction to Data Communication and Networks
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 

Ähnlich wie OO Perl with Moose

P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)lichtkind
 
To infinity and beyond
To infinity and beyondTo infinity and beyond
To infinity and beyondclintongormley
 
Moose: Perl Objects
Moose: Perl ObjectsMoose: Perl Objects
Moose: Perl ObjectsLambert Lum
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsMatt Follett
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perldaoswald
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)Dave Cross
 
Constructive Destructor Use
Constructive Destructor UseConstructive Destructor Use
Constructive Destructor Usemetaperl
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate WorksGoro Fuji
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Wilson Su
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::ClassCurtis Poe
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
Getting groovy (ODP)
Getting groovy (ODP)Getting groovy (ODP)
Getting groovy (ODP)Nick Dixon
 

Ähnlich wie OO Perl with Moose (20)

P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)
 
To infinity and beyond
To infinity and beyondTo infinity and beyond
To infinity and beyond
 
Perl 101
Perl 101Perl 101
Perl 101
 
Moose: Perl Objects
Moose: Perl ObjectsMoose: Perl Objects
Moose: Perl Objects
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Json perl example
Json perl exampleJson perl example
Json perl example
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perl
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Constructive Destructor Use
Constructive Destructor UseConstructive Destructor Use
Constructive Destructor Use
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1
 
Ruby Programming
Ruby ProgrammingRuby Programming
Ruby Programming
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
Moose
MooseMoose
Moose
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
Getting groovy (ODP)
Getting groovy (ODP)Getting groovy (ODP)
Getting groovy (ODP)
 

OO Perl with Moose

  • 1. OO Perl with Moose Nelo Onyiah
  • 2. OO Perl with Moose What is Moose?
  • 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. OO Perl with Moose Why Moose?
  • 5. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful
  • 6. OO Perl with Moose Why Moose? makes Perl 5 OO both simpler and more powerful define your class declaratively
  • 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. 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. 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. 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. 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. OO Perl with Moose package Person; To make a class you start 1; with a package
  • 13. OO Perl with Moose package Person; To make a class you start use Moose; with a package and just use 1; Moose
  • 14. OO Perl with Moose package Person; This is a complete class use Moose; definition 1;
  • 15. OO Perl with Moose package Person; This is a complete class use Moose; definition 1; not terribly useful though
  • 16. OO Perl with Moose package Person; Under the hood Moose is use Moose; doing a lot 1;
  • 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. OO Perl with Moose package Person; Classes have zero or more use Moose; attributes 1;
  • 19. OO Perl with Moose package Person; Attributes are declared using use Moose; the has function has 'birth_date' => ( ); 1;
  • 20. OO Perl with Moose package Person; Attributes are declared using use Moose; the has function has 'birth_date' => ( ); Attributes have properties 1;
  • 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. OO Perl with Moose package Person; Can be provided with use Moose; accessors has 'birth_date' => ( ); 1;
  • 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. OO Perl with Moose package Person; or read-only use Moose; has 'birth_date' => ( is => 'ro', ); 1;
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. OO Perl with Moose package Employee; Inheritance is achieved with use Moose; extends qw( Person ); the extends function 1;
  • 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. 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. 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. 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. OO Perl with Moose package Employee; You can also override use Moose; extends qw( Person ); attributes has '+birth_date' => ( # ... ); 1;
  • 53. OO Perl with Moose package Employee; You can also override use Moose; extends qw( Person ); attributes (carefully) has '+birth_date' => ( # ... ); 1;
  • 54. OO Perl with Moose package Science; Moose also has a concept of 1; roles
  • 55. OO Perl with Moose package Science; Moose also has a concept of use Moose::Role; roles 1; Declared by using Moose:: Role
  • 56. OO Perl with Moose package Science; Similar to Smalltalk traits, use Moose::Role; Ruby Mixins and Java 1; interfaces
  • 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. OO Perl with Moose package Science; A collection of reusable traits use Moose::Role; (attributes) 1;
  • 59. OO Perl with Moose package Science; A collection of reusable traits use Moose::Role; (attributes) has 'speciality' => ( # ... ); 1;
  • 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. OO Perl with Moose package Science; Roles are not classes use Moose::Role; has 'speciality' => ( # ... ); sub research { # ... } 1;
  • 62. OO Perl with Moose package Science; Roles are not classes use Moose::Role; has 'speciality' => ( cannot instantiate a role # ... ); sub research { # ... } 1;
  • 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. OO Perl with Moose package Science; Roles are another option to use Moose::Role; inheritance has 'speciality' => ( # ... ); sub research { # ... } 1;
  • 65. OO Perl with Moose package Science; Roles are composed into use Moose::Role; consuming classes/roles has 'speciality' => ( # ... ); sub research { # ... } 1;
  • 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. OO Perl with Moose package Science; Roles can insist that use Moose::Role; consuming classes has 'speciality' => ( implement certain methods # ... ); sub research { # ... } 1;
  • 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. 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. 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. 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. 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. OO Perl with Moose package Scientist; Class methods and attributes use Moose; extends qw( Person ); are prioritized with qw( Science ); sub research { ... } 1;
  • 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. OO Perl with Moose package Scientist; See Moose::Manual::Roles use Moose; extends qw( Person ); for details with qw( Science ); sub research { ... } 1;
  • 76. OO Perl with Moose package Scientist; Moose is not perfect use Moose; extends qw( Person ); with qw( Science ); sub research { ... } 1;
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. OO Perl with Moose Moose is also extensible
  • 87. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects
  • 88. OO Perl with Moose Moose is also extensible Done by manipulating metaclass objects This is where the wizards roam free
  • 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. 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. 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. 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. 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. 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. 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. 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. OO Perl with Moose use MooseX::Declare; Declaring classes becomes class Person { even more declarative # attributes and methods }
  • 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. 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. 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. 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. 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 ... } } }