SlideShare a Scribd company logo
Moose Workshop

                     Ynon Perek
                     ynonperek@yahoo.com
                     http://ynonperek.com




Friday, July 6, 12
Agenda

    • Moose           Overview          • Moose    Extensions

    • Classes                           • Design   Patterns

    • Roles

    • Attributes

    • Meta           Object System

    • Type           System

Friday, July 6, 12
Assumptions


    • You   know how to write a
         Moose classes

    • You   know how to define
         methods and what $self
         means



Friday, July 6, 12
Moose Overview


    • Developed      by Stevan Little
         (on the right)

    • Started        2006

    • Goal: Change          the world



Friday, July 6, 12
Moose Features

    • From            perl to OO

    • No             boilerplate code

    • Optional            type constraints

    • Inheritance           and Mixins

    • Design            Patterns


Friday, July 6, 12
Organizations Using Moose

    • Cisco

    • IMDb

    • Infinity         Interactive

    • MusicBrainz

    • Symantec

    • And    others: http://moose.iinteractive.com/
         about.html#organizations
Friday, July 6, 12
Moose Alternatives


    • Mouse

    • Moo

    • Mo

    • Class::Builder




Friday, July 6, 12
Moose HELP


    • IRC:           irc://irc.perl.org/#moose


    • Mailing           List: mailto:moose-
         subscribe@perl.org


    • Youtube: search                for “perl
         moose”



Friday, July 6, 12
Moose Classes




Friday, July 6, 12
Moose Classes

                            • Import sugar functions:
                             extends, has, with, ...

     package Foo;           • Enable   strict & warnings
     use Moose;             • Subclassof Moose::Object
                             for default ctor and dtor

                            • Create   Moose::Meta::Class
                             object

Friday, July 6, 12
Moose::Object

    • new            ( %params )

    • BUILDARGS             ( %params )

    • DESTROY

    • does(           $role_name )

    • DOES(            $class_or_role_name )

    • dump(            $maxdepth )

Friday, July 6, 12
Example: Getting Info
        package Pet;
        use Moose;

        has 'name',        is => 'ro', isa => 'Str', default => 'Lassy';
        has 'past_owners', is => 'ro', isa => 'ArrayRef[Str]';

        package main;

        my $dog = Pet->new( past_owners => ['James', 'Mike'] );

        # show dog's info. No need to import Data::Dumper
        warn $dog->dump;

        # DOES returns true for objects of the class, subclasses or
        # implementors of the role
        print "Good boy" if $dog->DOES('Pet');


Friday, July 6, 12
Class Construction


                                    package main;
 • new    method is automatically   # Pass a hash ref to prevent copying
      generated.                    my $enterprise = Starship->new( {
                                            captain => 'James T Kirk',
                                            crew    => ['Dr. McCoy',
 • Takes    parameters hash or      'Scott', 'Lt. Uhura'],
      hash ref                          });




Friday, July 6, 12
Class Construction


    • Construction        hooks:

          • BUILD

          • BUILDARGS

          • attribute   builders



Friday, July 6, 12
Class Construction

    • BUILD           is called every time a new object is created

    • If  inheritance is in effect, parent’s BUILD is called before child
         BUILD is called automatically

    • Used           for:

          • Object          state validation (whole object)

          • Tracking         objects creation

Friday, July 6, 12
Object State Validation
          package Starship;
          use Moose;

          has 'captain', is => 'ro', isa => 'Str', required => 1;
          has 'crew', is => 'rw', isa => 'ArrayRef[Str]', required => 1;

          sub BUILD {
              my $self = shift;
              if ( $self->captain ~~ $self->crew ) {
                  my $captain = $self->captain;
                  die "Validation Error: Cannot use $captain for both Captain and Crew";
              }
          }

          package main;

          # Validation error
          my $enterprise = Starship->new( {
                  captain => 'James T Kirk',
                  crew    => ['Dr. McCoy', 'Scott', 'Lt. Uhura', 'James T Kirk'],
              });



Friday, July 6, 12
BUILDARGS

    • Used           to manipulate arguments before object creation

    • Takes          the arguments hash as input, returns hashref

    • Wrap           in ‘around’ modifier to change

    • Used           for:

          • Single     arguments ctor


Friday, July 6, 12
BUILDARGS Example
                     package Person;
                     use Moose;

                     has 'name', is => 'ro', isa => 'Str', required => 1;

                     around   BUILDARGS   => sub {
                         my   $orig   =   shift;
                         my   $class =    shift;
                         my   @params =   @_;

                         # Sole parameter that is not a ref
                         # is considered the name
                         if ( ( @params == 1 ) && ( ! ref $params[0] ) ) {
                             return $class->$orig( name => $params[0] );
                         } else {
                             return $class->$orig ( @params );
                         }
                     }; # Watch the semicolon

Friday, July 6, 12
Class Destruction



    • Moose  implemented DESTROY, which will call your
         DEMOLISH

    • It       handles inheritance correctly: demolish child before super




Friday, July 6, 12
Class Destruction
                             package Foo;
                             use Moose;

                             sub DEMOLISH { warn 'Foo::Demolish' }
    • When      program      package Bar;
         ends, prints out:   use Moose;
                             extends 'Foo';

         Bar::Demolish       sub DEMOLISH { warn 'Bar::Demolish' }
         Foo::Demolish       package main;

                             my $b = Bar->new;




Friday, July 6, 12
Construction
             Destruction
                     Do’s and Don’ts




Friday, July 6, 12
Do



    • Provide        reasonable validations with BUILD




Friday, July 6, 12
Do



    • Use            warn( $obj->dump ) for debug




Friday, July 6, 12
Do



    • Consider    namespace::autoclean to remove Moose sugar
         methods from your classes (has, with, etc.)




Friday, July 6, 12
Do


    • Consider             using:
         __PACKAGE__->meta->make_immutable;


    • To             improve performance of objects creation

    • Consider             MooseX::AutoImmute



Friday, July 6, 12
Don’t



    • Never          override new ( it’ll break stuff down the road )




Friday, July 6, 12
Don’t



    • Don’t          use BUILD when attribute builders are sufficient




Friday, July 6, 12
Don’t



    • Never          call $self->SUPER::BUILD

    • Moose          does that for you




Friday, July 6, 12
Don’t



    • Don’t    apply a method modifier
         (before, after, around) to BUILD




Friday, July 6, 12
Don’t



    • Don’t    write BUILD method for your roles ( Moose
         ignores them )




Friday, July 6, 12
after
                                        package Secret;
                                        use Mouse;

                                        has 'message', is => 'ro',
                                                       required => 1,
                                                       clearer => 'reset';

    • Add    code after a method is     has 'counter', is => 'rw', default => 3;

         executed                       after 'message' => sub {
                                            my $self = shift;
                                            $self->counter( $self->counter - 1 );
    • Receives: method       name and       if ( $self->counter <= 0 ) {
                                                $self->reset;
         subroutine to add                  }
                                        };

                                        package main;
                                        my $secret = Secret->new(
                                          message => 'This message will self destroy');
                                        print $secret->message, "n" for (1..5);



Friday, July 6, 12
What Is Printed ?
                     package Foo;
                     use Moose;

                     sub DEMOLISH { warn 'Foo::Demolish' }
                     sub BUILD    { warn 'Foo::Build' }

                     package Bar;
                     use Moose;
                     extends 'Foo';

                     sub DEMOLISH { warn 'Bar::Demolish' }
                     sub BUILD    { warn 'Bar::Build' }

                     package main;

                     my $b = Bar->new;


Friday, July 6, 12
Method Modifiers



    • Alter  code by injecting other code before or after the
         modified method

    • Can            use from roles, subclasses or class itself




Friday, July 6, 12
before

                                            package Logger;
                                            use Mouse;

                                            sub log {
                                                my $self = shift;
    • Before    lets us inject code             my ($msg) = @_;

         before a method is called              print $msg;
                                            }

                                            before 'log' => sub { select *STDERR };
    • Spot           the bug on the right   after 'log' => sub { select *STDOUT };

                                            package main;
                                            my $log = Logger->new;
                                            $log->log("hellon");




Friday, July 6, 12
around


    • around          has two more advantages:

          • Can      use return value of original method

          • Can      skip calling original method altogether

          • You      have the power



Friday, July 6, 12
around

    • First  parameter: original        package AroundExample;
                                        use Mouse;
         method as CODE ref             use feature ':5.10';

                                        sub foo { print "In Foon" }

    • Second         parameter is the   around 'foo' => sub {
                                            my $orig = shift;
         object                             my $self = shift;

                                            say "Around: before calling method";

    • Can    call $self->$orig to get
                                            $self->$orig(@_);
                                            say "Around: after calling method";

         requested functionality        };




Friday, July 6, 12
around

                                         package User;
                                         use Mouse;
                                         use DateTime;
                                         sub login { warn 'Welcome' }

                                         around 'login' => sub {
                                             my $now = DateTime->now;
                                             if ( $now->hour < 12 ) {
    • Forbid         login before noon           my $orig = shift;
                                                 my $self = shift;
                                                 $self->$orig(@_);
                                             }
                                         };




Friday, July 6, 12
Friday, July 6, 12
Moose Roles
                     An alternative to deep hierarchies and base classes




Friday, July 6, 12
Role


    • Encapsulates        behavior. Something that classes do

    • Cannot         be instansiated

    • Classes    consume roles - which means everything in the role is
         copied into the class



Friday, July 6, 12
Classes & Roles

                     Person       Computer      Chicken


                     Alive                       Alive


                     Think         Think



Friday, July 6, 12
Roles Example

  package Breakable;
  use Moose::Role;
                                                 package Glass;
  has 'is_broken', is => 'rw', isa => 'Bool';
                                                 use Moose;
  sub break {
      my $self = shift;                          with 'Breakable';
      print "Ouchn" if ! $self->is_broken;
      $self->is_broken(1);
  }                                              package main;
                                                 my $g = Glass->new;
  sub fix {
      my $self = shift;
      print "Works nown" if $self->is_broken;   $g->break;
      $self->is_broken(0);                       $g->fix;
  }




Friday, July 6, 12
Moose Roles


    • Use            Moose::Role to define a role

    • Use ‘with’ to         consume a role

    • Inside          a role, define methods, attributes and modifiers

    • Use ‘does’ to          find out if an object implements a role



Friday, July 6, 12
Partial Implementation

    • Use ‘requires’ in         a role to force your consumer to define a
         method

    • Useful         for:

          • Partial    implementations (template method)

          • Abstract        Base Class


Friday, July 6, 12
Partial Implementation
        package MultipleFileUploader;
        use Moose::Role;

        requires 'upload_file';

        sub upload_files {
            my $self = shift;

            my @success;

            foreach my $f ( @_ ) {
                die "Invalid file: $f" if ! $f->DOES('File');
                $self->upload_file ( $f ) && push @success, $f;
            }

            return @success;
        }


Friday, July 6, 12
Method Conflicts


    • Consuming             two roles with the same method names results in
         a conflict

    • Class           must then implement the conflicted method on its own

    • Can            call role implementation using their namespace



Friday, July 6, 12
Method Conflicts

                     package R1;          package Test;
                     use Moose::Role;     use Moose;

                     sub foo {            with qw/R1 R2/;
                         warn 'R1::foo'
                     }

                     package R2;
                                          Compilation Error
                     use Moose::Role;

                     sub foo {
                         warn 'R2::foo'
                     }




Friday, July 6, 12
Method Conflict


    • Can    use -alias to make a copy of a role’s method by
         another name

    • Can            use -excludes to avoid consuming a specific method

    • Combine            both to work around a conflict



Friday, July 6, 12
Method Conflict


                     with 'Breakable' => {
                          -alias    => { break => 'break_bone' },
                          -excludes => 'break',
                          },

                          'Breakdancer' => {
                          -alias    => { break => 'break_dance' },
                          -excludes => 'break',
                          };




Friday, July 6, 12
Dynamic Roles

    • Roles          can be added to instances after creation

    • Usage: debug    tracing on specific obejct, dynamically change
         objects by configuration

    • Code:

           use Moose::Util qw( apply_all_roles );

           my $car = Car->new;
           apply_all_roles( $car, 'Breakable' );



Friday, July 6, 12
Lab

    • Implement     a Comparable role which requires a single method:
         compare($other) - returns -1 if $other is greater than $self; 0 if
         they are equal and +1 if $self is greater.

    • Use    compare to implement the following:
         greater_than, greater_or_equal, less_than, less_or_equal

    • Implement      a class that consumes the role


Friday, July 6, 12
Friday, July 6, 12
Attributes




Friday, July 6, 12
Moose Attributes



    • An             attribute is a property that every member of a class has

    • Some             attributes are optional (e.g. some people have a middle
         name)




Friday, July 6, 12
Attribute Options


    • is, reader, writer

    • isa

    • required, default, builder

    • lazy




Friday, July 6, 12
Readers & Writers

                                   package Product;
                                   use Moose;
    • Use ‘is’ to auto generate
                                   has 'name'   =>   (
         reader/writer                 is       =>   'rw',
                                       reader   =>   'get_name',
    • Use ‘writer’ to    specify       writer   =>   '_set_name',
                                   );
         writer’s name
                                   has 'price' => (
                                       is     => 'rw',
    • Use ‘reader’ to    specify       reader => 'get_price',
         reader’s name                 writer => 'set_price',
                                   );



Friday, July 6, 12
Isa

    • Use            isa to force a type constraint

    • Available Types include: Bool, Str, Num, Int, ScalarRef, ArrayRef,
         HashRef, CodeRef

    • Can            use another object as type constraint

    • Many    more type constraints with option to extend the list
         yourself


Friday, July 6, 12
Isa

          package Store;
          use Moose;
          use Client;
          use Product;

          has 'owner', is => 'ro', isa => 'Str';

          has 'clients', is => 'rw', isa => 'ArrayRef[Client]';
          has 'products', is => 'rw', isa => 'ArrayRef[Product]';

          has 'revenue',   is => 'rw', isa => 'Num';

          1;




Friday, July 6, 12
Subtypes

    • Use            subtypes to easily define new constraints:


                     use Moose::Util::TypeConstraints;

                     subtype 'Age',
                         as 'Int',
                         where { $_ >= 0 && $_ <= 120 },
                         message { "Invalid Age: $_ "};




Friday, July 6, 12
Enumerations



    • Use            enum function to declare an enum subtype

    • An             enum takes a single value from a predefined list

                     enum 'EyeColor', [qw/green blue brown gray/];




Friday, July 6, 12
Required / Default / Builder



    • Use            required for fields that take their value from “outside”

    • Use            default / builder for everything else




Friday, July 6, 12
Builder
                                        package Person;
                                        use Moose;

                                        has 'pet', is => 'ro', builder =>
                                        '_build_pet';


    • Use   builder for more            has 'age', is => 'rw', required => 1;


         complex initialization logic   sub _build_pet {
                                            my $self = shift;
                                            if ( $self->age < 13 ) {
                                                return "None";
    • builder    works better than          } else {
                                                return "Dog";
         default for inheritance            }
                                        }

                                        package main;
                                        my $p = Person->new(age => 10);
                                        print $p->pet;




Friday, July 6, 12
lazy
                                                  package Person;
                                                  use Moose;

                                                  has 'pet', is => 'ro', lazy_build => 1;
                                                  has 'age', is => 'rw', required => 1;

                                                  sub _build_pet {
    • Create   your attributes only                   my $self = shift;
                                                      if ( $self->age < 13 ) {
         when they are needed                             return "None";
                                                      } else {
                                                          return "Dog";
    • Use            lazy_build to type less          }
                                                  }

                                                  package main;
                                                  my $p = Person->new(age => 10);
                                                  print $p->pet;




Friday, July 6, 12
Dependency Injection


    •A     technique used in testing to build more testable versions of
         your classes

    • If   an attribute has both a builder AND was passed externally,
         external value wins




Friday, July 6, 12
Lab

    • Implement      a Logger class with one method: log. In the ctor,
         logger can take a file name

    • If  no arguments passed, create a screen logger (write all
         output to screen)

    • If      a file name was provided, write to that file

    • Use            dependency injection to test your Logger

           Solution: https://gist.github.com/3029901
Friday, July 6, 12
Delegation


    •A     relationship between classes. A class attribute is an object of
         a different class

    • Can     then redirect all calls on containing object to the attribute
         - thus delegating specific methods




Friday, July 6, 12
Delegation


               Send Mail             Send Mail
                           Contact               Email
            Call
                                          Call

                                                 Phone




Friday, July 6, 12
Delegation

                                          package Contact;
                                          use Moose;
    • Moose          handles delegation
         for you                          has 'email' => (
                                              is      => 'ro',
                                              handles => [ qw/send_mail/ ]
    • Attribute    should declare         );

         “handles” option




Friday, July 6, 12
Delegate


    • Another     option is to      has 'uri' => (
                                       is      => 'ro',
         delegate an entire role       isa     => 'URI',
                                       handles => 'HasURI',
                                    );
    • Moose    will delegate all
         methods in the role
         automatically



Friday, July 6, 12
Native Delegation


    • Give   your object “native” feel by using standard data type
         methods

    • Currently         supported: Array, Hash, Number, String, Bool,
         Counter

    • Useful         for: Fields that should “work like” the native data type



Friday, July 6, 12
Native Delegation
                                        has 'q' => (
                                            is => 'ro',
                                            isa => 'ArrayRef[Int]',
                                            default => sub { [] },
    • Native     arrays have push,          traits => [qw/Array/],
         pop, shift, unshift and more       handles => {
                                                add_item => 'push',
                                                next_item => 'shift',
    • Can     now use:                      },
         $q->add_item                   );

         to add an item to
                                        package main;
         the queue                      my $q = Queue->new;
                                        $q->add_item(10, 20);




Friday, July 6, 12
Native Delegation


    • Array          functions:
         Moose::Meta::Attribute::Native::Trait::Array
    • Hash   functions:
         Moose::Meta::Attribute::Native::Trait::Hash



Friday, July 6, 12
Friday, July 6, 12
Attributes: Advanced Topics


    • Predicates         & Clearers

    • Constructor          Parameters

    • Weak            References

    • Triggers




Friday, July 6, 12
Predicates & Clearers

    • User    can upload photos,            Uploading Image
         other users can “like”              No likes yet

    • Every          photo starts with 0
         likes
                                              Image Online
    • How      many “likes” do you               0 Likes.
         have before the image is
         online ?                          Go find more friends

Friday, July 6, 12
Predicates & Clearers
                                       package Photo;
                                       use Moose;

                                       has 'likes' => (
    • Provide     two new methods          is => 'rw',
         on $self: unpublish and           clearer => 'unpublish',
                                           predicate => 'is_published',
         is_published                  );

    • Setting    value to undef does   sub publish {
                                           my $self = shift;
         not affect predicate              $self->likes ( 0 );
                                       }




Friday, July 6, 12
Predicates & Clearers


                     sub like {
                         my $self = shift;

                         die 'Cannot like an Unpublished photo'
                             if ! $self->is_published;

                         $self->likes ( $self->likes + 1 );
                     }




Friday, July 6, 12
Constructor Parameters

    • Sometimes     the name of the attribute is not the same as the
         name of the constructor param

    •A          possible workaround is BUILDARGS, but that’s too tedious

    •A          better way: Use init_arg

    • Usage: modify         constructor param name, prevent dependency
         injection


Friday, July 6, 12
Example: init_arg


    • Use    to modify constructor
         parameter name
                                         has 'bigness' => (
                                             is       => 'ro',
    • Attribute    name is size, while       init_arg => 'size',
                                         );
         object creation is performed
         with:
         Cls->new( bigness => 7 )



Friday, July 6, 12
Example: init_arg


    • Use     init_arg => undef to     has '_genetic_code' => (
         prevent dependency                is         => 'ro',
                                           lazy_build => 1,
         injection                         init_arg   => undef,
                                       );
    • Use            with caution




Friday, July 6, 12
Weak References


                                   Student       Student   Student


                                             Learns At
                     Course




Friday, July 6, 12
Weak References


    • When           an object leaves scope, it’s ref-count decreases

    • Circular        references cause objects to remain in memory

    • Weak           references to the rescue




Friday, July 6, 12
Weak Ref

    • When       a Course object
         leaves the last scope - it will
                                           package Student;
         now be deleted                    use Moose;
                                           has 'name',
    • When      Course object leaves         is => 'ro', required => 1;

         scope, Moose will                 has 'learns_at',
         automatically clear all             is => 'rw', weak_ref => 1;

         “learns_at” attributes of
         students
                     Full Example: https://gist.github.com/3031636
Friday, July 6, 12
Triggers

• Called             when attribute value   has 'size' => (
    is set                                     is      => 'rw',
                                               trigger => &_size_set,
                                            );
• Called   when set from new
    or explicitly                           sub _size_set {
                                              my ( $self, $size, $old_size ) = @_;
                                            }
• Is  not called when set from
    default or builder


Friday, July 6, 12
Friday, July 6, 12
Lab



    • Improve           Students/Course example to use native delegation

    • Use            method modifiers to add custom logic




Friday, July 6, 12
Meta Moose




Friday, July 6, 12
What is MOP


    • An     abstraction to build abstractions - or simply put - an API to
         build an object system

    • Moose          is one object system built upon Class::MOP

    • Understanding     Class::MOP and Moose’s use of it reveals new
         features in Moose



Friday, July 6, 12
MOP Parts


    • The            Class protocol

    • The Attribute           protocol

    • The            Method protocol

    • The            Instance protocol



Friday, July 6, 12
Moose and Class::MOP


    • Moose            is built on top of Class::MOP

    • Prefixed            Moose::Meta (for example Moose::Meta::Class)

    • Get            with $self->meta




Friday, July 6, 12
What Meta Can Do For You


    • Class          and Object Introspection

    • Modify     objects and classes dynamically (add/remove methods,
         attributes, roles)

    • Add            more information to attributes (label, persistent)



Friday, July 6, 12
Object Introspection
         package main;

         my $z = Zombie->new;

         for my $attr ( $z->meta->get_all_attributes ) {
             say $attr->name;
         }

         for my $method ( $z->meta->get_all_methods ) {
             say $method->fully_qualified_name;
         }

         if ( $z->meta->has_method( 'eat_brain' ) ) {
             $z->eat_brain;
         }

                         Full Source: https://gist.github.com/3032056
Friday, July 6, 12
Object Introspection


    • All            meta methods listed under:

         Class::MOP::Class and Moose::META::Class

    • In   most cases, using roles is a better idea than dynamic
         checking



Friday, July 6, 12
Validate Type Constraints

    • Use    $self->meta->get_attribtue(attr)->type_constraint to get
         meta object of type constraints

    • $constraint->check(         $value )

    • $constraint->validate(         $value )
                 or die $constraint->get_message( $value );

    • $constraint->assert_valid(        $value )


Friday, July 6, 12
Class Modification

    • $meta->add_attribute

    • $meta->remove_attribute

    • $meta->add_method

    • $meta->remove_method

    • $meta->make_immutable



Friday, July 6, 12
Moose::Util



    •A      bundle of useful functions that take away some of the pain
         of working with meta

    • Start          here before implementing your own.




Friday, July 6, 12
Moose::Util


    • find_meta(       $class_or_obj )

    • does_role(      $class_or_obj, $role )

    • apply_all_roles(    $applicant, @roles )

    • english_list(   @items )



Friday, July 6, 12
Friday, July 6, 12
Moose Types
                     Customizable Type System




Friday, July 6, 12
Moose Type System

    • Verify         attribute values are “valid” - of a certain type

    • Types          have names, so they can be reused

    • Type     checking is just sugar for method arguments validation.
         Perl does not associate types with variables

    • Earlier        error detection


Friday, July 6, 12
Stock Types


    • Bool, Maybe[‘a], Str, Num, Int, ClassName, RoleName

    • Ref, ScalarRef[‘a], ArrayRef[‘a], HashRef[‘a], CodeRef

    • RegexpRef, GlobRef, FileHandle

    • Object




Friday, July 6, 12
Type Registry


    •A     type is an instance of
         Moose::Meta::TypeConstraint

    • All  types are stored in the type registry. Use
         get_type_constraint_registry from
         Moose::Util::TypeConstraints to get it



Friday, July 6, 12
Example: Print All Constraints


         use v5.14;
         use Data::Dumper;
         use Moose::Util::TypeConstraints;

         my $registry =
         Moose::Util::TypeConstraints::get_type_constraint_registry();

         print Dumper($registry->type_constraints);




Friday, July 6, 12
Extending The Type System


    • Every          Moose object is a new type

    • There          are also helper methods to create new types

    •A          new type can be named or anonymous




Friday, July 6, 12
Named Subtypes: enum
              use v5.14;

              package Person::Types;
              use Moose::Util::TypeConstraints;

              enum 'Person::Types::EyeColor', [qw/gray brown green blue/];

              package Person;
              use Moose;
              use Moose::Util::TypeConstraints;

              has 'eyecolor' => (
                  is => 'ro',
                  isa => 'Person::Types::EyeColor',
              );



Friday, July 6, 12
Anonymous Subtypes: enum

                      use v5.14;

                      package Person;
                      use Moose;
                      use Moose::Util::TypeConstraints;

                      has 'eyecolor' => (
                          is => 'ro',
                          isa => enum [qw/gray blue brown green/],
                      );




Friday, July 6, 12
More Subtypes


    • subtype(       %opts ) - Create a new subtype

    • role_type ‘barks’, {   role => ‘Some::Library::Role::Barks’ }

    • union ‘UnionName’, [qw/Str ArrayRef/]; -     Create a new
         subtype that can hold either string or an array



Friday, July 6, 12
Subtypes

    • Provide ‘as’ to   specify base
         type                          subtype 'NaturalLessThanTen',
                                           as 'Natural',
                                           where { $_ < 10 },
    • Provide ‘where’ to    add            message {
         constraint on the base type          "This number ($_) is
                                               not less than ten!"
                                           };
    • Provide   your own error
         message with ‘message’


Friday, July 6, 12
Subtypes Do’s



    • Define     all your subtype in a single module for code reuse. Use
         that module from every Moose class




Friday, July 6, 12
Subtypes Do’s


    • Prefer  namespaced subtypes:
         ZombieApocalipse::Human::EyeColor is better than
         EyeColor

    • Zombies        may have different eye color ...




Friday, July 6, 12
Type Coercion
                      Proceed With Caution




Friday, July 6, 12
Type Coercion

    • Automatically             convert invalid data to valid

    • Int            ------> ArrayRef[Int]

    • Str            ------> Person

    • High     risk - an invalid value could coerce thus skipping type
         validation


Friday, July 6, 12
Type Coercion
        use v5.14;

        package Student;
        use Moose;
        use Moose::Util::TypeConstraints;

        subtype 'GradesArray', as 'ArrayRef[Int]';
        coerce 'GradesArray', from 'Int', via { [ $_ ] };

        has 'grades', is => 'ro', isa => 'GradesArray', coerce => 1;


        package main;
        my $s = Student->new( grades => 77 );

        print $s->dump;


Friday, July 6, 12
Coercion Don’ts



    • Don’t    add coercion on Moose’s subtypes
         (unfortunately it’ll work)

    • Generally, try   to avoid coercions




Friday, July 6, 12
Friday, July 6, 12
Subtypes Lab



    • Define     a new subtype for hex numbers (numbers of the
         format 0x22)

    • Add            a coercion from HexNum to Int




Friday, July 6, 12
MooseX
                     More Than Moose




Friday, July 6, 12
eXtending Moose


    • Moose          is (relatively) easy to change and extend

    • Writing         extensions can take some time and effort BUT

    • There          are tons of Moose Extensions on CPAN

    • Prefixed          MooseX, they provide extra or modified functionality



Friday, July 6, 12
Useful MooseX

  • MooseX::StrictConstructor   • MooseX::MultiMethods

  • MooseX::Singleton           • MooseX::HasDefaults

  • MooseX::Declare             • MooseX::APIRole

  • MooseX::FollowPBP

  • MooseX::Privacy

  • MooseX::SingleArg

Friday, July 6, 12
Simple eXtensions

    • MooseX::StrictConstructor

    • MooseX::Singleton

    • MooseX::FollowPBP

    • MooseX::SingleArg

    • MooseX::HasDefaults

    • MooseX::Privacy

Friday, July 6, 12
MooseX::StrictConstructor
                                     package Contact;
                                     use Moose;
                                     use MooseX::StrictConstructor;

                                     has 'email', is => 'ro';
                                     has 'name', is => 'ro';
    • Throw     exception if
         constructor was passed an   package main;

         unexpected argument         # Throw an exception
                                     Contact->new(
                                         name => 'Bob',
                                         emial => 'bob@gmail.com');




Friday, July 6, 12
MooseX::Singleton

                                            package App;
                                            use MooseX::Singleton;

                                            package main;
    • Create         only one instance of
         a class                            {
                                                my $app = App->instance;
                                            }
    • Has    initialization method to
         pass arguments if needed           {
                                                # same one
                                                my $app = App->instance;
                                            }




Friday, July 6, 12
MooseX::FollowPBP



    • Use    set_x and get_x as
         default reader and writer

    • SEE ALSO: Perl::Critic




Friday, July 6, 12
MooseX::SingleArg

                                   use v5.14;

                                   package Contact;
                                   use Moose;
                                   use MooseX::SingleArg;
    • Easily   create single arg
                                   single_arg 'name';
         constructor (without      has 'name', is => 'ro';
         wrapping BUILDARGS)
                                   package main;
                                   my $c = Contact->new('Mike');

                                   say $c->name;




Friday, July 6, 12
MooseX::HasDefaults


    • Automatically    use:
                                use v5.14;

         is => ‘ro’             package Point;
                                use Moose;
                                use MooseX::HasDefaults::RO;
         or:
                                has ['x', 'y', 'z'];

         is => ‘rw’



Friday, July 6, 12
MooseX::Privacy
                                             use MooseX::Privacy;
    • Restrict       visibility of methods
                                             has config   =>   (
                                                 is       =>   'rw',
    • private     can only be called             isa      =>   'Some::Config',
         from within the class                   traits
                                             );
                                                          =>   [qw/Private/],


    • protected         can only be          private_method foo => sub {
                                                 return 23;
         called from within the class        };
         or any of its subclasses
                                             protected_method bar => sub {
                                                 return 42;
    • Doesn’t        work for roles          };



Friday, July 6, 12
Heavy Lifting


    • Logging: Log4perl

    • MooseX::APIRole

    • MooseX::Declare

    • MooseX::MultiMethods




Friday, July 6, 12
Log4perl


    • Logging          is all about keeping a record of your info at runtime

    • Log4perl          lets you control how your application logs its data

    • Perl’s         implementation of log4j




Friday, July 6, 12
Log4perl alternatives


    • print/warn     debug messages: Too simple for real apps

    • roll-your-own: Too    much work...

    • Log4perl     is currently the best logging solution for medium-
         large perl applications



Friday, July 6, 12
Log4perl and Moose

                           package MyApp;
    • Use                  use Moose;

         MooseX::Log::     with 'MooseX::Log::Log4perl';
         Log4perl role     sub go {
         in your class         my $self = shift;

                               $self->log->debug('Starting method go');
    • New                      $self->log->info('Go go go');
         attributes: log
                               $self->log('IO')->info('reading data');
         and logger        }




Friday, July 6, 12
Log4perl Output

    • Completely         customizable

    • Output         log to: Screen, File, Socket, DBI, and more

    • Example:

       [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (9) MyApp - Starting method go
       [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (10) MyApp - Go go go
       [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (12) IO - reading data




Friday, July 6, 12
Log4perl Configuration

           log4perl.logger = DEBUG, Screen, Logfile

           log4perl.appender.Screen = Log::Log4perl::Appender::Screen
           log4perl.appender.Screen.layout = PatternLayout
           log4perl.appender.Screen.layout.ConversionPattern= 
                    [%r] %F %l %c - %m%n

           log4perl.appender.Logfile = Log::Log4perl::Appender::File
           log4perl.appender.Logfile.filename = my.log
           log4perl.appender.Logfile.layout = PatternLayout
           log4perl.appender.Logfile.layout.ConversionPattern = 
                     [%d] %F %l %c - %m%n




Friday, July 6, 12
Log4perl Initialization



                                        package main;
    • Load     configuration file on      use Log::Log4perl;
         startup with:
                                        Log::Log4perl::init('log.conf');
         Log::Log4perl::init(filename)




Friday, July 6, 12
Log4perl Docs


    • Usage:
         perldoc Log::Log4perl

    • Conversion      Patterns Layout:
         http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/
         PatternLayout.html



Friday, July 6, 12
MooseX::APIRole

                                          package Logger;
                                          use Moose;
    • Automatically       create a role   use MooseX::APIRole;
         out of a class                   sub info    {   }
                                          sub error   {   }
    • all   subroutines become            make_api_role 'Logger::API';
         ‘requires’
                                          package Test;
                                          use Moose;
    • Easy  to use and very               # Fails - Test does not implement
         powerful                         # required methods
                                          with 'Logger::API';



Friday, July 6, 12
MooseX::Declare


    • Use            modern OO syntax for your moose objects

    • ‘class’ keywordsdeclares a class. Inside,
         MooseX::Method::Signatures is in effect.

    • ‘role’ keyword         declares a role



Friday, July 6, 12
MooseX::Declare
    use MooseX::Declare;

    class BankAccount {
        has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );

        method deposit (Num $amount) {
            $self−>balance( $self−>balance + $amount );
        }

        method withdraw (Num $amount) {
            my $current_balance = $self−>balance();
            ( $current_balance >= $amount )
            || confess "Account overdrawn";
            $self−>balance( $current_balance − $amount );
        }
    }


Friday, July 6, 12
MooseX::Declare


    • Still          experimental, API could change

    • Inside   a method $self is already defined for you, as well as
         other input parameters

    • Awesome. perldoc             MooseX::Declare for more



Friday, July 6, 12
MooseX::MultiMethods



    • Allow          multi methods dispatcher based on input arguments

    • Define          multiple handlers instead of long if-else blocks




Friday, July 6, 12
MooseX::MultiMethods
              package Paper;    use Moose;
              package Scissors; use Moose;
              package Rock;     use Moose;

              package Game;
              use Moose;
              use MooseX::MultiMethods;

              multi   method   play   (Paper      $x,   Rock       $y)   {   1   }
              multi   method   play   (Scissors   $x,   Paper      $y)   {   1   }
              multi   method   play   (Rock       $x,   Scissors   $y)   {   1   }
              multi   method   play   (Any        $x,   Any        $y)   {   0   }

              my $game = Game->new;

              # 1, Paper covers Rock
              print $game->play(Paper->new, Rock->new);
Friday, July 6, 12
Friday, July 6, 12
Thanks For Listening


    • Ynon           Perek

    • ynonperek@yahoo.com

    • ynonperek.com




Friday, July 6, 12

More Related Content

What's hot

Drupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the islandDrupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the island
Angela Byron
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
None
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
Rebecca Murphey
 
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
Michael Pirnat
 
Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)
Bozhidar Batsov
 
Coffee Script
Coffee ScriptCoffee Script
Coffee Script
Michal Taberski
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
Chris Neale
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
Prof. Wim Van Criekinge
 
Understanding Prototypal Inheritance
Understanding Prototypal InheritanceUnderstanding Prototypal Inheritance
Understanding Prototypal Inheritance
Guy Royse
 
API Design
API DesignAPI Design
API Design
James Gray
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
RORLAB
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
Brian Mann
 
Objective-C & iPhone for .NET Developers
Objective-C & iPhone for .NET DevelopersObjective-C & iPhone for .NET Developers
Objective-C & iPhone for .NET Developers
Ben Scheirman
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
Karsten Dambekalns
 
Introduction to Ruby MagLev
Introduction to Ruby MagLevIntroduction to Ruby MagLev
Introduction to Ruby MagLev
rengelbrecht
 
Exhibition of Atrocity
Exhibition of AtrocityExhibition of Atrocity
Exhibition of Atrocity
Michael Pirnat
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
Learning How To Use Jquery #5
Learning How To Use Jquery #5Learning How To Use Jquery #5
Learning How To Use Jquery #5
Takahiro Yoshimura
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
Andrew Dupont
 

What's hot (20)

Drupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the islandDrupal 8: A story of growing up and getting off the island
Drupal 8: A story of growing up and getting off the island
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
 
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
 
Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)Модерни езици за програмиране за JVM (2011)
Модерни езици за програмиране за JVM (2011)
 
Coffee Script
Coffee ScriptCoffee Script
Coffee Script
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
Understanding Prototypal Inheritance
Understanding Prototypal InheritanceUnderstanding Prototypal Inheritance
Understanding Prototypal Inheritance
 
API Design
API DesignAPI Design
API Design
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
 
Objective-C & iPhone for .NET Developers
Objective-C & iPhone for .NET DevelopersObjective-C & iPhone for .NET Developers
Objective-C & iPhone for .NET Developers
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
Introduction to Ruby MagLev
Introduction to Ruby MagLevIntroduction to Ruby MagLev
Introduction to Ruby MagLev
 
Exhibition of Atrocity
Exhibition of AtrocityExhibition of Atrocity
Exhibition of Atrocity
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Learning How To Use Jquery #5
Learning How To Use Jquery #5Learning How To Use Jquery #5
Learning How To Use Jquery #5
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 

Similar to Moose workshop

iPhone Memory Management
iPhone Memory ManagementiPhone Memory Management
iPhone Memory Management
Vadim Zimin
 
Rspec group
Rspec groupRspec group
Rspec group
thefonso
 
Immutability
ImmutabilityImmutability
Immutability
Yung-Luen Lan
 
Enterprise javascriptsession3
Enterprise javascriptsession3Enterprise javascriptsession3
Enterprise javascriptsession3
Troy Miles
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
Daniel Cousineau
 
Cloud, Cache, and Configs
Cloud, Cache, and ConfigsCloud, Cache, and Configs
Cloud, Cache, and Configs
Scott Taylor
 
Parse Heroku for Mobiles
Parse Heroku for MobilesParse Heroku for Mobiles
Parse Heroku for Mobiles
zezzi Castillo
 
Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.
lrdesign
 
Puppet Module Writing 201
Puppet Module Writing 201Puppet Module Writing 201
Puppet Module Writing 201
eshamow
 
PuppetCamp NYC - Building Scalable Modules
PuppetCamp NYC - Building Scalable ModulesPuppetCamp NYC - Building Scalable Modules
PuppetCamp NYC - Building Scalable Modules
Puppet
 
How We Look At Software
How We Look At SoftwareHow We Look At Software
How We Look At Software
Devrim Yasar
 
Upgrades and migrations
Upgrades and migrationsUpgrades and migrations
Upgrades and migrations
David Lanier
 
Effective Scala @ Jfokus
Effective Scala @ JfokusEffective Scala @ Jfokus
Effective Scala @ Jfokus
Henrik Engström
 
What I Hate About Wordpress
What I Hate About WordpressWhat I Hate About Wordpress
What I Hate About Wordpress
Mark Jaquith
 
Enterprise javascriptsession2
Enterprise javascriptsession2Enterprise javascriptsession2
Enterprise javascriptsession2
Troy Miles
 
Optimizing WordPress Performance on Shared Web Hosting
Optimizing WordPress Performance on Shared Web HostingOptimizing WordPress Performance on Shared Web Hosting
Optimizing WordPress Performance on Shared Web Hosting
Jon Brown
 

Similar to Moose workshop (16)

iPhone Memory Management
iPhone Memory ManagementiPhone Memory Management
iPhone Memory Management
 
Rspec group
Rspec groupRspec group
Rspec group
 
Immutability
ImmutabilityImmutability
Immutability
 
Enterprise javascriptsession3
Enterprise javascriptsession3Enterprise javascriptsession3
Enterprise javascriptsession3
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
 
Cloud, Cache, and Configs
Cloud, Cache, and ConfigsCloud, Cache, and Configs
Cloud, Cache, and Configs
 
Parse Heroku for Mobiles
Parse Heroku for MobilesParse Heroku for Mobiles
Parse Heroku for Mobiles
 
Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.Ruby 2.0 / Rails 4.0, A selection of new features.
Ruby 2.0 / Rails 4.0, A selection of new features.
 
Puppet Module Writing 201
Puppet Module Writing 201Puppet Module Writing 201
Puppet Module Writing 201
 
PuppetCamp NYC - Building Scalable Modules
PuppetCamp NYC - Building Scalable ModulesPuppetCamp NYC - Building Scalable Modules
PuppetCamp NYC - Building Scalable Modules
 
How We Look At Software
How We Look At SoftwareHow We Look At Software
How We Look At Software
 
Upgrades and migrations
Upgrades and migrationsUpgrades and migrations
Upgrades and migrations
 
Effective Scala @ Jfokus
Effective Scala @ JfokusEffective Scala @ Jfokus
Effective Scala @ Jfokus
 
What I Hate About Wordpress
What I Hate About WordpressWhat I Hate About Wordpress
What I Hate About Wordpress
 
Enterprise javascriptsession2
Enterprise javascriptsession2Enterprise javascriptsession2
Enterprise javascriptsession2
 
Optimizing WordPress Performance on Shared Web Hosting
Optimizing WordPress Performance on Shared Web HostingOptimizing WordPress Performance on Shared Web Hosting
Optimizing WordPress Performance on Shared Web Hosting
 

More from Ynon Perek

Regexp
RegexpRegexp
Regexp
Ynon Perek
 
Html5 intro
Html5 introHtml5 intro
Html5 intro
Ynon Perek
 
09 performance
09 performance09 performance
09 performance
Ynon Perek
 
Mobile Web Intro
Mobile Web IntroMobile Web Intro
Mobile Web Intro
Ynon Perek
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
Ynon Perek
 
Vimperl
VimperlVimperl
Vimperl
Ynon Perek
 
Syllabus
SyllabusSyllabus
Syllabus
Ynon Perek
 
Mobile Devices
Mobile DevicesMobile Devices
Mobile Devices
Ynon Perek
 
Network
NetworkNetwork
Network
Ynon Perek
 
Architecture app
Architecture appArchitecture app
Architecture app
Ynon Perek
 
Cryptography
CryptographyCryptography
Cryptography
Ynon Perek
 
Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
Ynon Perek
 
How to write easy-to-test JavaScript
How to write easy-to-test JavaScriptHow to write easy-to-test JavaScript
How to write easy-to-test JavaScript
Ynon Perek
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and Ruby
Ynon Perek
 
Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application Testing
Ynon Perek
 
Accessibility
AccessibilityAccessibility
Accessibility
Ynon Perek
 
Angularjs
AngularjsAngularjs
Angularjs
Ynon Perek
 
Js memory
Js memoryJs memory
Js memory
Ynon Perek
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design Patterns
Ynon Perek
 
Web Application Security
Web Application SecurityWeb Application Security
Web Application Security
Ynon Perek
 

More from Ynon Perek (20)

Regexp
RegexpRegexp
Regexp
 
Html5 intro
Html5 introHtml5 intro
Html5 intro
 
09 performance
09 performance09 performance
09 performance
 
Mobile Web Intro
Mobile Web IntroMobile Web Intro
Mobile Web Intro
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
Vimperl
VimperlVimperl
Vimperl
 
Syllabus
SyllabusSyllabus
Syllabus
 
Mobile Devices
Mobile DevicesMobile Devices
Mobile Devices
 
Network
NetworkNetwork
Network
 
Architecture app
Architecture appArchitecture app
Architecture app
 
Cryptography
CryptographyCryptography
Cryptography
 
Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
 
How to write easy-to-test JavaScript
How to write easy-to-test JavaScriptHow to write easy-to-test JavaScript
How to write easy-to-test JavaScript
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and Ruby
 
Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application Testing
 
Accessibility
AccessibilityAccessibility
Accessibility
 
Angularjs
AngularjsAngularjs
Angularjs
 
Js memory
Js memoryJs memory
Js memory
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design Patterns
 
Web Application Security
Web Application SecurityWeb Application Security
Web Application Security
 

Recently uploaded

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 

Recently uploaded (20)

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 

Moose workshop

  • 1. Moose Workshop Ynon Perek ynonperek@yahoo.com http://ynonperek.com Friday, July 6, 12
  • 2. Agenda • Moose Overview • Moose Extensions • Classes • Design Patterns • Roles • Attributes • Meta Object System • Type System Friday, July 6, 12
  • 3. Assumptions • You know how to write a Moose classes • You know how to define methods and what $self means Friday, July 6, 12
  • 4. Moose Overview • Developed by Stevan Little (on the right) • Started 2006 • Goal: Change the world Friday, July 6, 12
  • 5. Moose Features • From perl to OO • No boilerplate code • Optional type constraints • Inheritance and Mixins • Design Patterns Friday, July 6, 12
  • 6. Organizations Using Moose • Cisco • IMDb • Infinity Interactive • MusicBrainz • Symantec • And others: http://moose.iinteractive.com/ about.html#organizations Friday, July 6, 12
  • 7. Moose Alternatives • Mouse • Moo • Mo • Class::Builder Friday, July 6, 12
  • 8. Moose HELP • IRC: irc://irc.perl.org/#moose • Mailing List: mailto:moose- subscribe@perl.org • Youtube: search for “perl moose” Friday, July 6, 12
  • 10. Moose Classes • Import sugar functions: extends, has, with, ... package Foo; • Enable strict & warnings use Moose; • Subclassof Moose::Object for default ctor and dtor • Create Moose::Meta::Class object Friday, July 6, 12
  • 11. Moose::Object • new ( %params ) • BUILDARGS ( %params ) • DESTROY • does( $role_name ) • DOES( $class_or_role_name ) • dump( $maxdepth ) Friday, July 6, 12
  • 12. Example: Getting Info package Pet; use Moose; has 'name', is => 'ro', isa => 'Str', default => 'Lassy'; has 'past_owners', is => 'ro', isa => 'ArrayRef[Str]'; package main; my $dog = Pet->new( past_owners => ['James', 'Mike'] ); # show dog's info. No need to import Data::Dumper warn $dog->dump; # DOES returns true for objects of the class, subclasses or # implementors of the role print "Good boy" if $dog->DOES('Pet'); Friday, July 6, 12
  • 13. Class Construction package main; • new method is automatically # Pass a hash ref to prevent copying generated. my $enterprise = Starship->new( {         captain => 'James T Kirk',         crew => ['Dr. McCoy', • Takes parameters hash or 'Scott', 'Lt. Uhura'], hash ref     }); Friday, July 6, 12
  • 14. Class Construction • Construction hooks: • BUILD • BUILDARGS • attribute builders Friday, July 6, 12
  • 15. Class Construction • BUILD is called every time a new object is created • If inheritance is in effect, parent’s BUILD is called before child BUILD is called automatically • Used for: • Object state validation (whole object) • Tracking objects creation Friday, July 6, 12
  • 16. Object State Validation package Starship; use Moose; has 'captain', is => 'ro', isa => 'Str', required => 1; has 'crew', is => 'rw', isa => 'ArrayRef[Str]', required => 1; sub BUILD {     my $self = shift;     if ( $self->captain ~~ $self->crew ) {         my $captain = $self->captain;         die "Validation Error: Cannot use $captain for both Captain and Crew";     } } package main; # Validation error my $enterprise = Starship->new( {         captain => 'James T Kirk',         crew => ['Dr. McCoy', 'Scott', 'Lt. Uhura', 'James T Kirk'],     }); Friday, July 6, 12
  • 17. BUILDARGS • Used to manipulate arguments before object creation • Takes the arguments hash as input, returns hashref • Wrap in ‘around’ modifier to change • Used for: • Single arguments ctor Friday, July 6, 12
  • 18. BUILDARGS Example package Person; use Moose; has 'name', is => 'ro', isa => 'Str', required => 1; around BUILDARGS => sub {     my $orig = shift;     my $class = shift;     my @params = @_;     # Sole parameter that is not a ref     # is considered the name     if ( ( @params == 1 ) && ( ! ref $params[0] ) ) {         return $class->$orig( name => $params[0] );     } else {         return $class->$orig ( @params );     } }; # Watch the semicolon Friday, July 6, 12
  • 19. Class Destruction • Moose implemented DESTROY, which will call your DEMOLISH • It handles inheritance correctly: demolish child before super Friday, July 6, 12
  • 20. Class Destruction package Foo; use Moose; sub DEMOLISH { warn 'Foo::Demolish' } • When program package Bar; ends, prints out: use Moose; extends 'Foo'; Bar::Demolish sub DEMOLISH { warn 'Bar::Demolish' } Foo::Demolish package main; my $b = Bar->new; Friday, July 6, 12
  • 21. Construction Destruction Do’s and Don’ts Friday, July 6, 12
  • 22. Do • Provide reasonable validations with BUILD Friday, July 6, 12
  • 23. Do • Use warn( $obj->dump ) for debug Friday, July 6, 12
  • 24. Do • Consider namespace::autoclean to remove Moose sugar methods from your classes (has, with, etc.) Friday, July 6, 12
  • 25. Do • Consider using: __PACKAGE__->meta->make_immutable; • To improve performance of objects creation • Consider MooseX::AutoImmute Friday, July 6, 12
  • 26. Don’t • Never override new ( it’ll break stuff down the road ) Friday, July 6, 12
  • 27. Don’t • Don’t use BUILD when attribute builders are sufficient Friday, July 6, 12
  • 28. Don’t • Never call $self->SUPER::BUILD • Moose does that for you Friday, July 6, 12
  • 29. Don’t • Don’t apply a method modifier (before, after, around) to BUILD Friday, July 6, 12
  • 30. Don’t • Don’t write BUILD method for your roles ( Moose ignores them ) Friday, July 6, 12
  • 31. after package Secret; use Mouse; has 'message', is => 'ro', required => 1, clearer => 'reset'; • Add code after a method is has 'counter', is => 'rw', default => 3; executed after 'message' => sub {     my $self = shift;     $self->counter( $self->counter - 1 ); • Receives: method name and     if ( $self->counter <= 0 ) {         $self->reset; subroutine to add     } }; package main; my $secret = Secret->new( message => 'This message will self destroy'); print $secret->message, "n" for (1..5); Friday, July 6, 12
  • 32. What Is Printed ? package Foo; use Moose; sub DEMOLISH { warn 'Foo::Demolish' } sub BUILD { warn 'Foo::Build' } package Bar; use Moose; extends 'Foo'; sub DEMOLISH { warn 'Bar::Demolish' } sub BUILD { warn 'Bar::Build' } package main; my $b = Bar->new; Friday, July 6, 12
  • 33. Method Modifiers • Alter code by injecting other code before or after the modified method • Can use from roles, subclasses or class itself Friday, July 6, 12
  • 34. before package Logger; use Mouse; sub log {     my $self = shift; • Before lets us inject code     my ($msg) = @_; before a method is called     print $msg; } before 'log' => sub { select *STDERR }; • Spot the bug on the right after 'log' => sub { select *STDOUT }; package main; my $log = Logger->new; $log->log("hellon"); Friday, July 6, 12
  • 35. around • around has two more advantages: • Can use return value of original method • Can skip calling original method altogether • You have the power Friday, July 6, 12
  • 36. around • First parameter: original package AroundExample; use Mouse; method as CODE ref use feature ':5.10'; sub foo { print "In Foon" } • Second parameter is the around 'foo' => sub {     my $orig = shift; object     my $self = shift;     say "Around: before calling method"; • Can call $self->$orig to get     $self->$orig(@_);     say "Around: after calling method"; requested functionality }; Friday, July 6, 12
  • 37. around package User; use Mouse; use DateTime; sub login { warn 'Welcome' } around 'login' => sub {     my $now = DateTime->now;     if ( $now->hour < 12 ) { • Forbid login before noon         my $orig = shift;         my $self = shift;         $self->$orig(@_);     } }; Friday, July 6, 12
  • 39. Moose Roles An alternative to deep hierarchies and base classes Friday, July 6, 12
  • 40. Role • Encapsulates behavior. Something that classes do • Cannot be instansiated • Classes consume roles - which means everything in the role is copied into the class Friday, July 6, 12
  • 41. Classes & Roles Person Computer Chicken Alive Alive Think Think Friday, July 6, 12
  • 42. Roles Example package Breakable; use Moose::Role; package Glass; has 'is_broken', is => 'rw', isa => 'Bool'; use Moose; sub break {     my $self = shift; with 'Breakable';     print "Ouchn" if ! $self->is_broken;     $self->is_broken(1); } package main; my $g = Glass->new; sub fix {     my $self = shift;     print "Works nown" if $self->is_broken; $g->break;     $self->is_broken(0); $g->fix; } Friday, July 6, 12
  • 43. Moose Roles • Use Moose::Role to define a role • Use ‘with’ to consume a role • Inside a role, define methods, attributes and modifiers • Use ‘does’ to find out if an object implements a role Friday, July 6, 12
  • 44. Partial Implementation • Use ‘requires’ in a role to force your consumer to define a method • Useful for: • Partial implementations (template method) • Abstract Base Class Friday, July 6, 12
  • 45. Partial Implementation package MultipleFileUploader; use Moose::Role; requires 'upload_file'; sub upload_files {     my $self = shift;     my @success;     foreach my $f ( @_ ) {         die "Invalid file: $f" if ! $f->DOES('File');         $self->upload_file ( $f ) && push @success, $f;     }     return @success; } Friday, July 6, 12
  • 46. Method Conflicts • Consuming two roles with the same method names results in a conflict • Class must then implement the conflicted method on its own • Can call role implementation using their namespace Friday, July 6, 12
  • 47. Method Conflicts package R1; package Test; use Moose::Role; use Moose; sub foo { with qw/R1 R2/;     warn 'R1::foo' } package R2; Compilation Error use Moose::Role; sub foo {     warn 'R2::foo' } Friday, July 6, 12
  • 48. Method Conflict • Can use -alias to make a copy of a role’s method by another name • Can use -excludes to avoid consuming a specific method • Combine both to work around a conflict Friday, July 6, 12
  • 49. Method Conflict with 'Breakable' => { -alias => { break => 'break_bone' }, -excludes => 'break', }, 'Breakdancer' => { -alias => { break => 'break_dance' }, -excludes => 'break', }; Friday, July 6, 12
  • 50. Dynamic Roles • Roles can be added to instances after creation • Usage: debug tracing on specific obejct, dynamically change objects by configuration • Code: use Moose::Util qw( apply_all_roles ); my $car = Car->new; apply_all_roles( $car, 'Breakable' ); Friday, July 6, 12
  • 51. Lab • Implement a Comparable role which requires a single method: compare($other) - returns -1 if $other is greater than $self; 0 if they are equal and +1 if $self is greater. • Use compare to implement the following: greater_than, greater_or_equal, less_than, less_or_equal • Implement a class that consumes the role Friday, July 6, 12
  • 54. Moose Attributes • An attribute is a property that every member of a class has • Some attributes are optional (e.g. some people have a middle name) Friday, July 6, 12
  • 55. Attribute Options • is, reader, writer • isa • required, default, builder • lazy Friday, July 6, 12
  • 56. Readers & Writers package Product; use Moose; • Use ‘is’ to auto generate has 'name' => ( reader/writer     is => 'rw',     reader => 'get_name', • Use ‘writer’ to specify     writer => '_set_name', ); writer’s name has 'price' => (     is => 'rw', • Use ‘reader’ to specify     reader => 'get_price', reader’s name     writer => 'set_price', ); Friday, July 6, 12
  • 57. Isa • Use isa to force a type constraint • Available Types include: Bool, Str, Num, Int, ScalarRef, ArrayRef, HashRef, CodeRef • Can use another object as type constraint • Many more type constraints with option to extend the list yourself Friday, July 6, 12
  • 58. Isa package Store; use Moose; use Client; use Product; has 'owner', is => 'ro', isa => 'Str'; has 'clients', is => 'rw', isa => 'ArrayRef[Client]'; has 'products', is => 'rw', isa => 'ArrayRef[Product]'; has 'revenue', is => 'rw', isa => 'Num'; 1; Friday, July 6, 12
  • 59. Subtypes • Use subtypes to easily define new constraints: use Moose::Util::TypeConstraints; subtype 'Age',     as 'Int',     where { $_ >= 0 && $_ <= 120 },     message { "Invalid Age: $_ "}; Friday, July 6, 12
  • 60. Enumerations • Use enum function to declare an enum subtype • An enum takes a single value from a predefined list enum 'EyeColor', [qw/green blue brown gray/]; Friday, July 6, 12
  • 61. Required / Default / Builder • Use required for fields that take their value from “outside” • Use default / builder for everything else Friday, July 6, 12
  • 62. Builder package Person; use Moose; has 'pet', is => 'ro', builder => '_build_pet'; • Use builder for more has 'age', is => 'rw', required => 1; complex initialization logic sub _build_pet {     my $self = shift;     if ( $self->age < 13 ) {         return "None"; • builder works better than     } else {         return "Dog"; default for inheritance     } } package main; my $p = Person->new(age => 10); print $p->pet; Friday, July 6, 12
  • 63. lazy package Person; use Moose; has 'pet', is => 'ro', lazy_build => 1; has 'age', is => 'rw', required => 1; sub _build_pet { • Create your attributes only     my $self = shift;     if ( $self->age < 13 ) { when they are needed         return "None";     } else {         return "Dog"; • Use lazy_build to type less     } } package main; my $p = Person->new(age => 10); print $p->pet; Friday, July 6, 12
  • 64. Dependency Injection •A technique used in testing to build more testable versions of your classes • If an attribute has both a builder AND was passed externally, external value wins Friday, July 6, 12
  • 65. Lab • Implement a Logger class with one method: log. In the ctor, logger can take a file name • If no arguments passed, create a screen logger (write all output to screen) • If a file name was provided, write to that file • Use dependency injection to test your Logger Solution: https://gist.github.com/3029901 Friday, July 6, 12
  • 66. Delegation •A relationship between classes. A class attribute is an object of a different class • Can then redirect all calls on containing object to the attribute - thus delegating specific methods Friday, July 6, 12
  • 67. Delegation Send Mail Send Mail Contact Email Call Call Phone Friday, July 6, 12
  • 68. Delegation package Contact; use Moose; • Moose handles delegation for you has 'email' => (     is => 'ro',     handles => [ qw/send_mail/ ] • Attribute should declare ); “handles” option Friday, July 6, 12
  • 69. Delegate • Another option is to has 'uri' => ( is => 'ro', delegate an entire role isa => 'URI', handles => 'HasURI', ); • Moose will delegate all methods in the role automatically Friday, July 6, 12
  • 70. Native Delegation • Give your object “native” feel by using standard data type methods • Currently supported: Array, Hash, Number, String, Bool, Counter • Useful for: Fields that should “work like” the native data type Friday, July 6, 12
  • 71. Native Delegation has 'q' => (     is => 'ro',     isa => 'ArrayRef[Int]',     default => sub { [] }, • Native arrays have push,     traits => [qw/Array/], pop, shift, unshift and more     handles => {         add_item => 'push',         next_item => 'shift', • Can now use:     }, $q->add_item ); to add an item to package main; the queue my $q = Queue->new; $q->add_item(10, 20); Friday, July 6, 12
  • 72. Native Delegation • Array functions: Moose::Meta::Attribute::Native::Trait::Array • Hash functions: Moose::Meta::Attribute::Native::Trait::Hash Friday, July 6, 12
  • 74. Attributes: Advanced Topics • Predicates & Clearers • Constructor Parameters • Weak References • Triggers Friday, July 6, 12
  • 75. Predicates & Clearers • User can upload photos, Uploading Image other users can “like” No likes yet • Every photo starts with 0 likes Image Online • How many “likes” do you 0 Likes. have before the image is online ? Go find more friends Friday, July 6, 12
  • 76. Predicates & Clearers package Photo; use Moose; has 'likes' => ( • Provide two new methods     is => 'rw', on $self: unpublish and     clearer => 'unpublish',     predicate => 'is_published', is_published ); • Setting value to undef does sub publish {     my $self = shift; not affect predicate     $self->likes ( 0 ); } Friday, July 6, 12
  • 77. Predicates & Clearers sub like {     my $self = shift;     die 'Cannot like an Unpublished photo'         if ! $self->is_published;     $self->likes ( $self->likes + 1 ); } Friday, July 6, 12
  • 78. Constructor Parameters • Sometimes the name of the attribute is not the same as the name of the constructor param •A possible workaround is BUILDARGS, but that’s too tedious •A better way: Use init_arg • Usage: modify constructor param name, prevent dependency injection Friday, July 6, 12
  • 79. Example: init_arg • Use to modify constructor parameter name has 'bigness' => ( is => 'ro', • Attribute name is size, while init_arg => 'size', ); object creation is performed with: Cls->new( bigness => 7 ) Friday, July 6, 12
  • 80. Example: init_arg • Use init_arg => undef to has '_genetic_code' => ( prevent dependency is => 'ro', lazy_build => 1, injection init_arg => undef, ); • Use with caution Friday, July 6, 12
  • 81. Weak References Student Student Student Learns At Course Friday, July 6, 12
  • 82. Weak References • When an object leaves scope, it’s ref-count decreases • Circular references cause objects to remain in memory • Weak references to the rescue Friday, July 6, 12
  • 83. Weak Ref • When a Course object leaves the last scope - it will package Student; now be deleted use Moose; has 'name', • When Course object leaves is => 'ro', required => 1; scope, Moose will has 'learns_at', automatically clear all is => 'rw', weak_ref => 1; “learns_at” attributes of students Full Example: https://gist.github.com/3031636 Friday, July 6, 12
  • 84. Triggers • Called when attribute value has 'size' => ( is set is => 'rw', trigger => &_size_set, ); • Called when set from new or explicitly sub _size_set { my ( $self, $size, $old_size ) = @_; } • Is not called when set from default or builder Friday, July 6, 12
  • 86. Lab • Improve Students/Course example to use native delegation • Use method modifiers to add custom logic Friday, July 6, 12
  • 88. What is MOP • An abstraction to build abstractions - or simply put - an API to build an object system • Moose is one object system built upon Class::MOP • Understanding Class::MOP and Moose’s use of it reveals new features in Moose Friday, July 6, 12
  • 89. MOP Parts • The Class protocol • The Attribute protocol • The Method protocol • The Instance protocol Friday, July 6, 12
  • 90. Moose and Class::MOP • Moose is built on top of Class::MOP • Prefixed Moose::Meta (for example Moose::Meta::Class) • Get with $self->meta Friday, July 6, 12
  • 91. What Meta Can Do For You • Class and Object Introspection • Modify objects and classes dynamically (add/remove methods, attributes, roles) • Add more information to attributes (label, persistent) Friday, July 6, 12
  • 92. Object Introspection package main; my $z = Zombie->new; for my $attr ( $z->meta->get_all_attributes ) {     say $attr->name; } for my $method ( $z->meta->get_all_methods ) {     say $method->fully_qualified_name; } if ( $z->meta->has_method( 'eat_brain' ) ) {     $z->eat_brain; } Full Source: https://gist.github.com/3032056 Friday, July 6, 12
  • 93. Object Introspection • All meta methods listed under: Class::MOP::Class and Moose::META::Class • In most cases, using roles is a better idea than dynamic checking Friday, July 6, 12
  • 94. Validate Type Constraints • Use $self->meta->get_attribtue(attr)->type_constraint to get meta object of type constraints • $constraint->check( $value ) • $constraint->validate( $value ) or die $constraint->get_message( $value ); • $constraint->assert_valid( $value ) Friday, July 6, 12
  • 95. Class Modification • $meta->add_attribute • $meta->remove_attribute • $meta->add_method • $meta->remove_method • $meta->make_immutable Friday, July 6, 12
  • 96. Moose::Util •A bundle of useful functions that take away some of the pain of working with meta • Start here before implementing your own. Friday, July 6, 12
  • 97. Moose::Util • find_meta( $class_or_obj ) • does_role( $class_or_obj, $role ) • apply_all_roles( $applicant, @roles ) • english_list( @items ) Friday, July 6, 12
  • 99. Moose Types Customizable Type System Friday, July 6, 12
  • 100. Moose Type System • Verify attribute values are “valid” - of a certain type • Types have names, so they can be reused • Type checking is just sugar for method arguments validation. Perl does not associate types with variables • Earlier error detection Friday, July 6, 12
  • 101. Stock Types • Bool, Maybe[‘a], Str, Num, Int, ClassName, RoleName • Ref, ScalarRef[‘a], ArrayRef[‘a], HashRef[‘a], CodeRef • RegexpRef, GlobRef, FileHandle • Object Friday, July 6, 12
  • 102. Type Registry •A type is an instance of Moose::Meta::TypeConstraint • All types are stored in the type registry. Use get_type_constraint_registry from Moose::Util::TypeConstraints to get it Friday, July 6, 12
  • 103. Example: Print All Constraints use v5.14; use Data::Dumper; use Moose::Util::TypeConstraints; my $registry = Moose::Util::TypeConstraints::get_type_constraint_registry(); print Dumper($registry->type_constraints); Friday, July 6, 12
  • 104. Extending The Type System • Every Moose object is a new type • There are also helper methods to create new types •A new type can be named or anonymous Friday, July 6, 12
  • 105. Named Subtypes: enum use v5.14; package Person::Types; use Moose::Util::TypeConstraints; enum 'Person::Types::EyeColor', [qw/gray brown green blue/]; package Person; use Moose; use Moose::Util::TypeConstraints; has 'eyecolor' => (     is => 'ro',     isa => 'Person::Types::EyeColor', ); Friday, July 6, 12
  • 106. Anonymous Subtypes: enum use v5.14; package Person; use Moose; use Moose::Util::TypeConstraints; has 'eyecolor' => (     is => 'ro',     isa => enum [qw/gray blue brown green/], ); Friday, July 6, 12
  • 107. More Subtypes • subtype( %opts ) - Create a new subtype • role_type ‘barks’, { role => ‘Some::Library::Role::Barks’ } • union ‘UnionName’, [qw/Str ArrayRef/]; - Create a new subtype that can hold either string or an array Friday, July 6, 12
  • 108. Subtypes • Provide ‘as’ to specify base type subtype 'NaturalLessThanTen', as 'Natural', where { $_ < 10 }, • Provide ‘where’ to add message { constraint on the base type "This number ($_) is not less than ten!" }; • Provide your own error message with ‘message’ Friday, July 6, 12
  • 109. Subtypes Do’s • Define all your subtype in a single module for code reuse. Use that module from every Moose class Friday, July 6, 12
  • 110. Subtypes Do’s • Prefer namespaced subtypes: ZombieApocalipse::Human::EyeColor is better than EyeColor • Zombies may have different eye color ... Friday, July 6, 12
  • 111. Type Coercion Proceed With Caution Friday, July 6, 12
  • 112. Type Coercion • Automatically convert invalid data to valid • Int ------> ArrayRef[Int] • Str ------> Person • High risk - an invalid value could coerce thus skipping type validation Friday, July 6, 12
  • 113. Type Coercion use v5.14; package Student; use Moose; use Moose::Util::TypeConstraints; subtype 'GradesArray', as 'ArrayRef[Int]'; coerce 'GradesArray', from 'Int', via { [ $_ ] }; has 'grades', is => 'ro', isa => 'GradesArray', coerce => 1; package main; my $s = Student->new( grades => 77 ); print $s->dump; Friday, July 6, 12
  • 114. Coercion Don’ts • Don’t add coercion on Moose’s subtypes (unfortunately it’ll work) • Generally, try to avoid coercions Friday, July 6, 12
  • 116. Subtypes Lab • Define a new subtype for hex numbers (numbers of the format 0x22) • Add a coercion from HexNum to Int Friday, July 6, 12
  • 117. MooseX More Than Moose Friday, July 6, 12
  • 118. eXtending Moose • Moose is (relatively) easy to change and extend • Writing extensions can take some time and effort BUT • There are tons of Moose Extensions on CPAN • Prefixed MooseX, they provide extra or modified functionality Friday, July 6, 12
  • 119. Useful MooseX • MooseX::StrictConstructor • MooseX::MultiMethods • MooseX::Singleton • MooseX::HasDefaults • MooseX::Declare • MooseX::APIRole • MooseX::FollowPBP • MooseX::Privacy • MooseX::SingleArg Friday, July 6, 12
  • 120. Simple eXtensions • MooseX::StrictConstructor • MooseX::Singleton • MooseX::FollowPBP • MooseX::SingleArg • MooseX::HasDefaults • MooseX::Privacy Friday, July 6, 12
  • 121. MooseX::StrictConstructor package Contact; use Moose; use MooseX::StrictConstructor; has 'email', is => 'ro'; has 'name', is => 'ro'; • Throw exception if constructor was passed an package main; unexpected argument # Throw an exception Contact->new( name => 'Bob', emial => 'bob@gmail.com'); Friday, July 6, 12
  • 122. MooseX::Singleton package App; use MooseX::Singleton; package main; • Create only one instance of a class {     my $app = App->instance; } • Has initialization method to pass arguments if needed {     # same one     my $app = App->instance; } Friday, July 6, 12
  • 123. MooseX::FollowPBP • Use set_x and get_x as default reader and writer • SEE ALSO: Perl::Critic Friday, July 6, 12
  • 124. MooseX::SingleArg use v5.14; package Contact; use Moose; use MooseX::SingleArg; • Easily create single arg single_arg 'name'; constructor (without has 'name', is => 'ro'; wrapping BUILDARGS) package main; my $c = Contact->new('Mike'); say $c->name; Friday, July 6, 12
  • 125. MooseX::HasDefaults • Automatically use: use v5.14; is => ‘ro’ package Point; use Moose; use MooseX::HasDefaults::RO; or: has ['x', 'y', 'z']; is => ‘rw’ Friday, July 6, 12
  • 126. MooseX::Privacy use MooseX::Privacy; • Restrict visibility of methods has config => (     is => 'rw', • private can only be called     isa => 'Some::Config', from within the class     traits ); => [qw/Private/], • protected can only be private_method foo => sub {     return 23; called from within the class }; or any of its subclasses protected_method bar => sub {     return 42; • Doesn’t work for roles }; Friday, July 6, 12
  • 127. Heavy Lifting • Logging: Log4perl • MooseX::APIRole • MooseX::Declare • MooseX::MultiMethods Friday, July 6, 12
  • 128. Log4perl • Logging is all about keeping a record of your info at runtime • Log4perl lets you control how your application logs its data • Perl’s implementation of log4j Friday, July 6, 12
  • 129. Log4perl alternatives • print/warn debug messages: Too simple for real apps • roll-your-own: Too much work... • Log4perl is currently the best logging solution for medium- large perl applications Friday, July 6, 12
  • 130. Log4perl and Moose package MyApp; • Use use Moose; MooseX::Log:: with 'MooseX::Log::Log4perl'; Log4perl role sub go { in your class     my $self = shift;     $self->log->debug('Starting method go'); • New     $self->log->info('Go go go'); attributes: log     $self->log('IO')->info('reading data'); and logger } Friday, July 6, 12
  • 131. Log4perl Output • Completely customizable • Output log to: Screen, File, Socket, DBI, and more • Example: [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (9) MyApp - Starting method go [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (10) MyApp - Go go go [2012/07/06 14:54:34] 130.pl MyApp::go 130.pl (12) IO - reading data Friday, July 6, 12
  • 132. Log4perl Configuration log4perl.logger = DEBUG, Screen, Logfile log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern= [%r] %F %l %c - %m%n log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = my.log log4perl.appender.Logfile.layout = PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = [%d] %F %l %c - %m%n Friday, July 6, 12
  • 133. Log4perl Initialization package main; • Load configuration file on use Log::Log4perl; startup with: Log::Log4perl::init('log.conf'); Log::Log4perl::init(filename) Friday, July 6, 12
  • 134. Log4perl Docs • Usage: perldoc Log::Log4perl • Conversion Patterns Layout: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ PatternLayout.html Friday, July 6, 12
  • 135. MooseX::APIRole package Logger; use Moose; • Automatically create a role use MooseX::APIRole; out of a class sub info { } sub error { } • all subroutines become make_api_role 'Logger::API'; ‘requires’ package Test; use Moose; • Easy to use and very # Fails - Test does not implement powerful # required methods with 'Logger::API'; Friday, July 6, 12
  • 136. MooseX::Declare • Use modern OO syntax for your moose objects • ‘class’ keywordsdeclares a class. Inside, MooseX::Method::Signatures is in effect. • ‘role’ keyword declares a role Friday, July 6, 12
  • 137. MooseX::Declare use MooseX::Declare; class BankAccount {     has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );     method deposit (Num $amount) {         $self−>balance( $self−>balance + $amount );     }     method withdraw (Num $amount) {         my $current_balance = $self−>balance();         ( $current_balance >= $amount )         || confess "Account overdrawn";         $self−>balance( $current_balance − $amount );     } } Friday, July 6, 12
  • 138. MooseX::Declare • Still experimental, API could change • Inside a method $self is already defined for you, as well as other input parameters • Awesome. perldoc MooseX::Declare for more Friday, July 6, 12
  • 139. MooseX::MultiMethods • Allow multi methods dispatcher based on input arguments • Define multiple handlers instead of long if-else blocks Friday, July 6, 12
  • 140. MooseX::MultiMethods package Paper; use Moose; package Scissors; use Moose; package Rock; use Moose; package Game; use Moose; use MooseX::MultiMethods; multi method play (Paper $x, Rock $y) { 1 } multi method play (Scissors $x, Paper $y) { 1 } multi method play (Rock $x, Scissors $y) { 1 } multi method play (Any $x, Any $y) { 0 } my $game = Game->new; # 1, Paper covers Rock print $game->play(Paper->new, Rock->new); Friday, July 6, 12
  • 142. Thanks For Listening • Ynon Perek • ynonperek@yahoo.com • ynonperek.com Friday, July 6, 12