Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Moose: Perl Objects

38 views

Published on

Unashamedly great OO Perl.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Moose: Perl Objects

  1. 1. MooseUnashamedly Great OO Perl Presenter: Lambert Lum
  2. 2. Warning Case insensitive file systems may fail on Name.pm Windows, maybe MAC OSX
  3. 3. Code Coloring Note Green: from the .pm file Black: from the .pl file
  4. 4. For the Moose Beginner Just the basics No fancy stuff Ignoring the adv users.
  5. 5. Module package SaveLoadModule; # A module starts with a first line that mentions package. # filename is package name plus “.pm” # # Add some perl subroutines # # The 1 at the bottom is a Perl quirk. 1;
  6. 6. Module package SaveLoadModule; use warnings; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(save load); sub save { ... } sub load { ... } 1;
  7. 7. New to Objects? Combine data and functions. Class: declare what data and functions go together Object: create an instance of a class.
  8. 8. Mostly Interchangeable Terminology Subroutine Function Method
  9. 9. What is Moose? New Object System of Perl 2200 CPAN distributions depend on Moose Not part of the core language. Built on top of Perl's existing object system (Backward compatible)
  10. 10. Without Object sub save { my ($filename, $hash) = @_; ... } sub load { my ($filename) = @_; ... return $hash; } save ("/tmp/data.dat", $hash); $hash = load ("/tmp/data.dat");
  11. 11. [Open without_object.pl]
  12. 12. With Object my $obj = new SaveLoad (filename => "/tmp/data.dat"); $obj->save ($hash); $hash = $obj->load ();
  13. 13. With Objectpackage SaveLoad; use Moose; has 'filename' => ( is => 'rw', isa => 'Str', ); sub save { my ($self, $hash) = @_; ... } sub load { my ($self) = @_; ... return $hash; } 1;
  14. 14. Open SaveLoad.pm Run with_object.pl
  15. 15. Attributes: Accessors has 'filename' => ( is => 'rw', isa => 'Str', ); my $filename = $obj->filename(); $obj->filename("/tmp/data.dat");
  16. 16. Accessor has 'filename' => ( is => 'rw', isa => 'Str', reader => 'get_filename', writer => 'set_filename', ); my $filename = $obj->get_filename(); $obj->set_filename("/tmp/data.dat");
  17. 17. Attribute: Read-only has 'filename' => ( is => 'ro', isa => 'Str', ); my $filename = $obj->filename();
  18. 18. Constructors has 'filename' => ( is => 'rw', isa => 'Str', ); my $obj = new SaveLoad (filename => "/tmp/data.dat");
  19. 19. Constructor: Required has 'filename' => ( is => 'rw', isa => 'Str', required => 1, ); my $obj = new ConstructorRequired (filename => "/tmp/data.dat");
  20. 20. Constructor: Default has 'filename' => ( is => 'rw', isa => 'Str', default => "/tmp/data.dat", ); my $obj = new ConstructorDefault(); print $obj->filename, "n";
  21. 21. Attributes: Lazy If you don't use the attribute, the initialization routine is never called
  22. 22. Attributes: Lazy has 'mtime' => ( is => 'ro', isa => 'Str', lazy => 1, builder => '_build_mtime', init_arg => undef, # unsettable via constructor ); sub _build_mtime { my ($self) = @_; my @stats = stat($self->filename); my $mtime = $stats[9]; return $mtime; }
  23. 23. Attributes: Delegation has 'mtime_dt' => ( is => 'ro', isa => 'DateTime', lazy => 1, builder => '_build_mtime_dt', handles => ['year', 'month'], ); sub _build_mtime_dt { my ($self) = @_; my $mtime_dt = DateTime->from_epoch ( epoch => $self->mtime() ); return $mtime_dt; } print "year: " . $obj->year() . "n"; print "year: " . $obj->mtime_dt()->year() . "n"; print "month: " . $obj->month() . "n"; print "month: " . $obj->mtime_dt()->month() . "n";
  24. 24. Constructor: Other BUILDARGS BUILD DEMOLISH
  25. 25. Method Modifiers before after around augment/inner override
  26. 26. Inheritance package AttributeDelegation; use Moose; extends 'AttributeLazy';
  27. 27. Inheritance package AttributeLazy; use Moose; extends 'AttributeReadOnly';
  28. 28. Roles: Like Java Interface package SaveLoadRole; use Moose::Role; requires 'save'; requires 'load'; 1;
  29. 29. Roles package SaveLoadUsingRoles; use Moose; with 'SaveLoadRole'; sub save { ... } sub load { ... } 1;
  30. 30. Roles: Like Ruby mix-inpackage CountRole; use Moose::Role; has 'save_cnt' => ( is => 'rw', isa => 'Int', default => 0, ); sub print_save_cnt { my ($self) = @_; print "saved: " . $self->save_cnt() . "n"; } after 'save' => sub { my ($self) = @_; $self->save_cnt( $self->save_cnt + 1 ); }; 1;
  31. 31. SaveLoadUsingRoles package SaveLoadUsingRoles; use Moose; with 'SaveLoadRole'; with 'CountRole'; sub save { ... } sub load { ... } 1;
  32. 32. SaveLoadUsingRoles my $obj = new SaveLoadUsingRoles ( filename => "/tmp/data.dat" ); $obj->save($hash); $obj->print_save_cnt(); % perl save_load_using_roles.pl saved: 1
  33. 33. Moose optimization package PackageName; use Moose; use namespace::autoclean; # more perl code __PACKAGE__->meta->make_immutable; 1;
  34. 34. Strict/Warnings # Moose will load strict/warnings use warnings; use strict;
  35. 35. Smaller Moose Moo
  36. 36. Moose performance MongoDB::get_collection() # creates new collection object each time
  37. 37. perlmodstyle perldoc perlmodstyle
  38. 38. perlmodstyle advice Gratuitous object orientation results in complex APIs which are difficult for the average module user to understand or use.
  39. 39. perlmodstyle advice When to go OO? You have a piece of data on which many different operations are applied.
  40. 40. Lazy Graph lazy_init.svg
  41. 41. Adv. Homework Build your own lazy_depend 30 lines of code Extra 15 lines for obfuscated names See lazy_init.svg

×