Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning Moose Houston Perl Mongers, April 10 th 2014.

Similar presentations


Presentation on theme: "Beginning Moose Houston Perl Mongers, April 10 th 2014."— Presentation transcript:

1 Beginning Moose Houston Perl Mongers, April 10 th 2014

2

3 What is Moose? Moose is a complete Object Oriented system for Perl Moose allows the developer to define your class declaratively. It allows for objects to have attributes, roles, methods, types and more.

4 Why Moose? Moose is easy to use and allows the developer to focus more on what the object should be doing and not how to implement it. Moose does not require the knowledge blessing hashrefs and accessor methods. It also provides introspection for classes in order to learn about attributes, methods etc.

5 Creating an Object Attribute package Human; use Moose; has 'name' => ( is => 'rw', isa => 'Str', required => 1, predicate => ‘has_name’ ); 1; Attributes are declared with the has keyword

6 No More of this! Yay! package Thing; sub new { my $class = shift; return bless {}, $class; }

7 About Moose Attribute Properties An attribute is a property of a class that defines it. It should have a name and can have many other properties. Potential Moose attributes can include: Is Isa Required Predicate

8 Other Attribute Properties Clearer Builder Lazy Sub type Delegation Coercion

9 Example of Attribute clearer and builder has 'weight' => ( is => 'rw', isa => 'Int', builder => '_build_weight', clearer => 'clear_weight', ); sub _build_weight { return 155; } 1; Within our Human module from previous example.

10 Example of Sub Type use Moose; use Moose::Util::TypeConstraints;...code… subtype 'PositiveInt', as 'Int’; Don’t forget to use Moose::Util::TypeConstriants !

11 Using Our Moose Object #! /usr/bin/perl use Human; my $human = Human->new(name => 'Dan The Man Culver'); print "The human's name is ".$human->name."\n"; print $human->has_name."\n"; print $human->weight."\n"; $human->clear_weight; print $human->weight."weight is now cleared\n"; Because we made the name property required in our Human module, it is required to define the name when we call new Our predicate property ‘has_name’ for our module will return a true or false value

12 Extending our Moose Object package Programmer; use Moose; use DateTime; extends 'Human'; has 'coffee' => ( is => 'rw', isa => 'Str', lazy => 1, ); has 'money' => (# Continued next slide is => 'rw', isa => PostiveInt, ); Extend an object with extends ‘ ’

13 Extending our Moose Object (Cont.) has 'celebration' => ( is => 'rw', isa => 'DateTime', handles => { 'last_made_it_rain' => 'date' } ); sub celebrate { my $self = shift; my $dollars = shift; if ( $self->money ne $dollars ) { return 0; } $self->celebration( DateTime->now() ); return 1; } 1; Here in celebration attribute, handles is an example of delegation in Moose

14 Using our new Object Extension #! /usr/bin/perl use Programmer; my $programmer = Programmer->new( name => 'Dan The Man Culver', coffee => 'black', money => 1000, ); $programmer->celebrate($programmer->money); print $programmer->name." made it rain on ". $programmer->last_made_it_rain. " with ". $programmer->money." dollars while drinking ". $programmer->coffee." coffee.";

15 Using Moose Roles #! /usr/bin/perl package Coding; use Moose::Role; has 'is_coding' => ( is => 'rw', isa => 'Bool', ); sub code { my $self = shift; print "I'm trying to code..\n"; $self->is_coding(1); } 1; It helps to think of roles as something that can be done, or ask yourself it an ‘ing’ or ‘able’ can be added to the end of the desired role. In this example, a programmer can be coding

16 Using Moose Roles ( Cont. ) package Programmer; use Moose; use DateTime; extends 'Human'; with 'Coding'; … To use a role, simply add with ‘ ’ in the module that you intend on using the role with.

17 Using our Moose Role #! /usr/bin/perl use Programmer; my $programmer = Programmer->new( name => 'Dan The Man Culver', coffee => 'black', money => 1000, ); $programmer->code; I’m trying to code.. ( Output )

18 Recommended Resources CPAN Moose Newsletter/Mailing List http://perldoc.perl.org/perlobj.html

19 Any Questions?


Download ppt "Beginning Moose Houston Perl Mongers, April 10 th 2014."

Similar presentations


Ads by Google