1 / 19

Beginning Moose

Beginning Moose. Houston Perl Mongers, April 10 th 2014. 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. Why Moose?.

branxton
Download Presentation

Beginning Moose

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Beginning Moose Houston Perl Mongers, April 10th 2014

  2. 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.

  3. 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.

  4. 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

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

  6. 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

  7. Other Attribute Properties • Clearer • Builder • Lazy • Sub type • Delegation • Coercion

  8. 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.

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

  10. 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

  11. 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 ‘<objectname>’

  12. Extending our Moose Object (Cont.) Here in celebration attribute, handlesis an example of delegation in Moose 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;

  13. 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.";

  14. Using Moose Roles • 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 #! /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;

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

  16. 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 )

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

  18. Any Questions?

More Related