Your SlideShare is downloading. ×
Intro To Moose
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro To Moose

2,727
views

Published on

Published in: Technology

1 Comment
1 Like
Statistics
Notes
  • On slide 13, doesn't second 'handles' delegation override the first one?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,727
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
1
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide






















  • Transcript

    • 1. Introduction to Object Oriented Programming in Perl with Moose. Introduction to Object Oriented Programming with Moose
    • 2. What is an Object? • Holds information about it’s self • Does stuff (functions) • defined by a class (not an object)
    • 3. OO in Perl? • DateTime • WWW::Mechanize • DBI • IO::Handle
    • 4. Often created with new • my $mech = WWW::Mechanize->new • my $dt = DateTime->new(year => 2000, month => ...);
    • 5. But not always • my $dt = DateTime->now(time_zone => ‘America/Chicago’);
    • 6. Holds information • my $mech = WWW::Mechanize->new • $mech->get($url) • my $content = $mech->content()
    • 7. Not an object. • use Cwd; • my $dir = getcwd();
    • 8. Some times globals point the need for an object #!/usr/bin/env perl use strict; use warnings; use WWW::Mechanize; my $base = ‘http://www.google.com/search?q=’; my $mech = $mech->new; print query(“perl”) print query(“moose”) sub query { my $query = shift; $mech->get($url . $query); return $mech->content; }
    • 9. Becomes.... package SearchEngine; use Moose; use WWW::Mechanize; has ‘url_base’ => (isa => Str, is => ‘rw’, default => ‘http://www.google.com/search?q=’); has ‘mech’ => (isa => Object, is => ‘ro’, default => sub {WWW::Mechanize->new}); sub query { my $self = shift; my $query = shift; $self->mech->get($self->url_base . $query); return $self->mech->content; } 1;
    • 10. Now I can query away use SearchEngine; my $google = SearchEngine->new; my $alta = SearchEngine->new(url_base => ‘http://www.altavista.com/web/results?q=’); print $google->query(‘perl’); print $alta->query(‘perl’); print $google->query(‘Moose’); print $alta->query(‘Moose’);
    • 11. Change engines after new use SearchEngine; my $search = SearchEngine->new; # defaults to google $search->url_base(‘http://www.altavista.com/web/results?q=’); print $search->query(‘perl’);
    • 12. Moose out of the box • Type constraints ( ) http://tinyurl.com/moose-types • use strict; • use warnings • don’t have to define new • Delegation
    • 13. package SearchEngine; Delegation use Moose; use WWW::Mechanize; has ‘url_base’ => (isa => Str, is => ‘rw’, default => ‘http://www.google.com/search?q=’); has ‘mech’ => (isa => Object, is => ‘ro’, default => sub {WWW::Mechanize->new}, handles => { ‘content’ => ‘content’ } handles => { ‘get’ => ‘get’ } ); sub query { my $self = shift; my $query = shift; #$self->mech->get($self->url_base . $query); $self->get($self->url_base . $query); #return $self->mech->content; return $self->content; } 1;
    • 14. Why Moose? • See slide 25 - Intro to Moose
    • 15. Why Moose • Moose means less code • (less code == less bugs) • Less tests • (why test what moose already does) • over 6000 unit tests • 0.24 seconds load time • ~ 3mb for perl+moose
    • 16. No Moose •no Moose at the end of a package is a best practice • Just do it
    • 17. Immutability Stevan's Incantation of Fleet- Footedness package Person; use Moose; __PACKAGE__->meta->make_immutable;
    • 18. What make_immutable does Magic • Uses eval to "inline" a constructor • Memoizes a lot of meta-information • Makes loading your class slower • Makes object creation much faster
    • 19. no immutable Moose; package SearchEngine; use Moose; use WWW::Mechanize; has ‘url_base’ => (isa => Str, is => ‘rw’, default => ‘http://www.google.com/search?q=’); has ‘mech’ => (isa => Object, is => ‘ro’, default => sub {WWW::Mechanize->new}, handles => { ‘content’ => ‘content’ } handles => { ‘get’ => ‘get’ } ); sub query { my $self = shift; my $query = shift; #$self->mech->get($self->url_base . $query); $self->get($self->url_base . $query); #return $self->mech->content; return $self->content; } __PACKAGE__->meta->make_immutable no Moose; 1;
    • 20. Moose is cool for tests! # no_net.t package pseudomech; sub new{return bless {}} sub get{ my $self = shift; $self->{query} = shift; } sub content { return shift->{query} . “ response”; } package main; use Test::More tests => 2; use SearchEngine; my $google = SearchEngine->new(mech => pseudomech->new); is($google->query(‘perl’), ‘perl response’); is($google->query(‘Moose’), ‘Moose response’); exit;
    • 21. Roles • Usually what you want when you try to do inheritance • lets 1 class take on the features of multiple packages.
    • 22. More Reading • Moose Cookbook (heavy on the inheritance) • http://search.cpan.org/dist/Moose/lib/ Moose/Cookbook.pod • Moose Website • http://moose.perl.org