Evolving Software with Moose
Dave Cross [email_address] @davorg
Perlanet
Perl + Planet
Web Feed Aggregation
Input: Lots of web feeds
Output: Web page + web feed
 
 
 
Scratching an itch
Wrote it for me
CPAN
github
A few bug fixes
A few enhancements
Perl Ironman
Using Plagger
Wanted to use Perlanet
Didn't scale well
YAML config
NWE.pm Hackday
Enter the Moose
Slash and burn
More easily subclassable
Monolithic vs Overridable
HTML::Scrubber
# deep in the 'run' subroutine my %scrub_rules = (   # stuff ); my %scrub_def = (   # more stuff ) my $scrub = HTML::Scrub...
Overriding this is hard
has 'scrubber' => ( is  => 'rw', lazy_build => 1 ); sub _build_scrubber {   my $self = shift;   # all that previous stuff ...
Overriding this is easy
Repeat half a dozen times
YAML config
YAML  config
DB config
Better scaling
Phase 1 complete
Almost
Need better tests
Release early Release often
Phase 2
Traits
Moose has traits and roles
Difference is subtle
Implementation looks similar
I didn't know the difference
Role is like an interface
Defines methods that class MUST implement
package ARole; use Moose::Role; requires 'do_stuff'; 1;
package AClass; use Moose; with 'ARole'; has name => (   is => 'rw',   isa => 'Str', ); 1;
#!/usr/bin/perl use strict; use warnings; use AClass; my $obj = Aclass->new({   name => 'davorg', });
$ ./role_demo 'ARole' requires the method 'do_stuff' to be implemented by 'AClass' ... other scary stuff ...
Need to implement method
package AClass; use Moose; with 'ARole'; has name => (   is => 'rw',   isa => 'Str', ); sub do_stuff {   my $self = shift;...
Roles impose an interface
(We don't use those in Perlanet)
Traits add functionality
Like a mixin
Remember HTML::Scrubber?
package Perlanet::Trait::Scrubber; use Moose::Role; # yes really – hold that thought use HTML::Scrubber; has scrubber => (...
package Perlanet::Trait::Scrubber; use Moose::Role; # yes really – hold that thought use HTML::Scrubber; has scrubber => (...
around 'clean_html' => sub {   my $orig = shift;   my ($self, $html) = @_;   $html = $self->orig($html);   $self->scrubber...
Perlanet.pm has clean_html method
But it does nothing until traits are added
Perlanet is now a hollow shell of a module
All the fun stuff is in traits
Perlanet::Simple
package Perlanet::Simple; use Moose; extends 'Perlanet'; with qw(   Perlanet::Trait::Cache   Perlanet::Trait::OPML   Perla...
Moose::Role
Used in both roles and traits
This is Perl
Difference is not clear-cut
Roles and traits are the same thing
Depends where the code is
All based on Moose::Role
Perlanet now trait-based
Easy to add functionality
Perlanet trait ecosystem
And I learned about Moose roles and traits
(And now, so have you)
ht tp://search.cpan.org/dist/Perlanet http://theplanetarium.org/ ht tp://perlanet.mag-sol.com/ http://moose.perl.org/
 
Upcoming SlideShare
Loading in...5
×

Evolving Software with Moose

4,383

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,383
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Evolving Software with Moose"

  1. 1. Evolving Software with Moose
  2. 2. Dave Cross [email_address] @davorg
  3. 3. Perlanet
  4. 4. Perl + Planet
  5. 5. Web Feed Aggregation
  6. 6. Input: Lots of web feeds
  7. 7. Output: Web page + web feed
  8. 11. Scratching an itch
  9. 12. Wrote it for me
  10. 13. CPAN
  11. 14. github
  12. 15. A few bug fixes
  13. 16. A few enhancements
  14. 17. Perl Ironman
  15. 18. Using Plagger
  16. 19. Wanted to use Perlanet
  17. 20. Didn't scale well
  18. 21. YAML config
  19. 22. NWE.pm Hackday
  20. 23. Enter the Moose
  21. 24. Slash and burn
  22. 25. More easily subclassable
  23. 26. Monolithic vs Overridable
  24. 27. HTML::Scrubber
  25. 28. # deep in the 'run' subroutine my %scrub_rules = ( # stuff ); my %scrub_def = ( # more stuff ) my $scrub = HTML::Scrubber->new; $scrub->rules(%scrub_rules); $scrub->default(1, scrub_def);
  26. 29. Overriding this is hard
  27. 30. has 'scrubber' => ( is => 'rw', lazy_build => 1 ); sub _build_scrubber { my $self = shift; # all that previous stuff return $scrub; }
  28. 31. Overriding this is easy
  29. 32. Repeat half a dozen times
  30. 33. YAML config
  31. 34. YAML config
  32. 35. DB config
  33. 36. Better scaling
  34. 37. Phase 1 complete
  35. 38. Almost
  36. 39. Need better tests
  37. 40. Release early Release often
  38. 41. Phase 2
  39. 42. Traits
  40. 43. Moose has traits and roles
  41. 44. Difference is subtle
  42. 45. Implementation looks similar
  43. 46. I didn't know the difference
  44. 47. Role is like an interface
  45. 48. Defines methods that class MUST implement
  46. 49. package ARole; use Moose::Role; requires 'do_stuff'; 1;
  47. 50. package AClass; use Moose; with 'ARole'; has name => ( is => 'rw', isa => 'Str', ); 1;
  48. 51. #!/usr/bin/perl use strict; use warnings; use AClass; my $obj = Aclass->new({ name => 'davorg', });
  49. 52. $ ./role_demo 'ARole' requires the method 'do_stuff' to be implemented by 'AClass' ... other scary stuff ...
  50. 53. Need to implement method
  51. 54. package AClass; use Moose; with 'ARole'; has name => ( is => 'rw', isa => 'Str', ); sub do_stuff { my $self = shift; print $self->name, “ does stuff ”; } 1;
  52. 55. Roles impose an interface
  53. 56. (We don't use those in Perlanet)
  54. 57. Traits add functionality
  55. 58. Like a mixin
  56. 59. Remember HTML::Scrubber?
  57. 60. package Perlanet::Trait::Scrubber; use Moose::Role; # yes really – hold that thought use HTML::Scrubber; has scrubber => ( is => 'rw', lazy_build => 1, ); sub _build_scrubber { # all that stuff from before return $scrub; }
  58. 61. package Perlanet::Trait::Scrubber; use Moose::Role; # yes really – hold that thought use HTML::Scrubber; has scrubber => ( is => 'rw', lazy_build => 1, ); sub _build_scrubber { # all that stuff from before return $scrub; }
  59. 62. around 'clean_html' => sub { my $orig = shift; my ($self, $html) = @_; $html = $self->orig($html); $self->scrubber->scrub($html); return $html; } 1;
  60. 63. Perlanet.pm has clean_html method
  61. 64. But it does nothing until traits are added
  62. 65. Perlanet is now a hollow shell of a module
  63. 66. All the fun stuff is in traits
  64. 67. Perlanet::Simple
  65. 68. package Perlanet::Simple; use Moose; extends 'Perlanet'; with qw( Perlanet::Trait::Cache Perlanet::Trait::OPML Perlanet::Trait::Scrubber Perlanet::Trait::Tidy Perlanet::Trait::YAMLConfig Perlanet::Trait::TemplateToolkit Perlanet::Trait::FeedFile );
  66. 69. Moose::Role
  67. 70. Used in both roles and traits
  68. 71. This is Perl
  69. 72. Difference is not clear-cut
  70. 73. Roles and traits are the same thing
  71. 74. Depends where the code is
  72. 75. All based on Moose::Role
  73. 76. Perlanet now trait-based
  74. 77. Easy to add functionality
  75. 78. Perlanet trait ecosystem
  76. 79. And I learned about Moose roles and traits
  77. 80. (And now, so have you)
  78. 81. ht tp://search.cpan.org/dist/Perlanet http://theplanetarium.org/ ht tp://perlanet.mag-sol.com/ http://moose.perl.org/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×