SlideShare una empresa de Scribd logo
1 de 22
Introduction to Object Oriented Programming in Perl with Moose.




  Introduction to Object
  Oriented Programming
       with Moose
What is an Object?

• Holds information about it’s self
• Does stuff (functions)
• defined by a class (not an object)
OO in Perl?

• DateTime
• WWW::Mechanize
• DBI
• IO::Handle
Often created with
         new

• my $mech = WWW::Mechanize->new
• my $dt = DateTime->new(year => 2000,
  month => ...);
But not always


• my $dt = DateTime->now(time_zone =>
  ‘America/Chicago’);
Holds information

• my $mech = WWW::Mechanize->new
• $mech->get($url)
• my $content = $mech->content()
Not an object.


• use Cwd;
• my $dir = getcwd();
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;
}
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;
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’);
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’);
Moose out of the box

• Type constraints (                           )
                  http://tinyurl.com/moose-types


• use strict;
• use warnings
• don’t have to define new
• Delegation
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;
Why Moose?


• See slide 25 - Intro to Moose
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
No Moose

•no Moose at the end of a package is a
  best practice
• Just do it
Immutability

  Stevan's Incantation of Fleet-
  Footedness
package Person;
use Moose;


__PACKAGE__->meta->make_immutable;
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
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;
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;
Roles

• Usually what you want when you try to do
  inheritance
• lets 1 class take on the features of multiple
  packages.
More Reading
• Moose Cookbook (heavy on the
  inheritance)
 • http://search.cpan.org/dist/Moose/lib/
    Moose/Cookbook.pod
• Moose Website
 • http://moose.perl.org

Más contenido relacionado

La actualidad más candente

The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
Jacob Kaplan-Moss
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
Kris Wallsmith
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
 

La actualidad más candente (19)

Moose workshop
Moose workshopMoose workshop
Moose workshop
 
To infinity and beyond
To infinity and beyondTo infinity and beyond
To infinity and beyond
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORM
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
Nubilus Perl
Nubilus PerlNubilus Perl
Nubilus Perl
 
Perl Web Client
Perl Web ClientPerl Web Client
Perl Web Client
 
Keeping It Small with Slim
Keeping It Small with SlimKeeping It Small with Slim
Keeping It Small with Slim
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to Services
 
Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)Introducing Assetic (NYPHP)
Introducing Assetic (NYPHP)
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Simple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with PerlSimple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with Perl
 
Going crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPGoing crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHP
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
WordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know queryWordPress London 16 May 2012 - You don’t know query
WordPress London 16 May 2012 - You don’t know query
 

Destacado

Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 

Destacado (10)

Firewall in Perl by Chankey Pathak
Firewall in Perl by Chankey PathakFirewall in Perl by Chankey Pathak
Firewall in Perl by Chankey Pathak
 
Moose Hacking Guide
Moose Hacking GuideMoose Hacking Guide
Moose Hacking Guide
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012
 
Introduction to Perl MAGIC
Introduction to Perl MAGICIntroduction to Perl MAGIC
Introduction to Perl MAGIC
 
Introduction to OO Perl with Moose
Introduction to OO Perl with MooseIntroduction to OO Perl with Moose
Introduction to OO Perl with Moose
 
YAPC::Asia 2014 - 半端なPHPDisでPHPerに陰で笑われないためのPerl Monger向け最新PHP事情
YAPC::Asia 2014 - 半端なPHPDisでPHPerに陰で笑われないためのPerl Monger向け最新PHP事情YAPC::Asia 2014 - 半端なPHPDisでPHPerに陰で笑われないためのPerl Monger向け最新PHP事情
YAPC::Asia 2014 - 半端なPHPDisでPHPerに陰で笑われないためのPerl Monger向け最新PHP事情
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
10 books that every developer must read
10 books that every developer must read10 books that every developer must read
10 books that every developer must read
 
The Programmer
The ProgrammerThe Programmer
The Programmer
 

Similar a Intro To Moose

MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
Six Apart KK
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
Damien Seguy
 
Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP Extension
Adam Trachtenberg
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 

Similar a Intro To Moose (20)

Perl object ?
Perl object ?Perl object ?
Perl object ?
 
Blog Hacks 2011
Blog Hacks 2011Blog Hacks 2011
Blog Hacks 2011
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
php2.pptx
php2.pptxphp2.pptx
php2.pptx
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 
Wp query
Wp queryWp query
Wp query
 
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
 
DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちDBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たち
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
 
About Data::ObjectDriver
About Data::ObjectDriverAbout Data::ObjectDriver
About Data::ObjectDriver
 
You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)You Don't Know Query (WordCamp Netherlands 2012)
You Don't Know Query (WordCamp Netherlands 2012)
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP Extension
 
Webdriver
WebdriverWebdriver
Webdriver
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Intro To Moose

  • 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

Notas del editor