X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=98e190a312cc6d8648c6f7a310e8299b57c392fb;hb=ba3e6f3b43767d159a22c4884f0a7e3720bda4c2;hp=cbbf3a110208ef5fb1df8b2808c65c063efffbc8;hpb=d6c8786deaf97484afa544488d5e0340d131c274;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index cbbf3a1..98e190a 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -40,7 +40,7 @@ img#me05 {top: 43px;left: 36px;} @@ -341,7 +354,7 @@ has blog_uri => (

Why Moose?

@@ -885,10 +898,9 @@ use Moose; sub BUILDARGS { my $class = shift; - if ( @_ == 1 && ! ref $_[0] ) { + if ( @_ == 1 && ! ref $_[0] ) { return { ssn => $_[0] }; } - return $class->SUPER::BUILDARGS(@_); } @@ -925,6 +937,30 @@ sub BUILD {
+

Object Construction a la Moose

+ +
Person->new(@_)
+ +
    +
  1. Calls Person->BUILDARGS(@_) to turn @_ into a hashref
  2. +
  3. Blesses a reference
  4. +
  5. Populates attributes based on the hashref from #1
  6. +
  7. Calls $new_object->BUILDALL($constructor_args) +
    ... which calls all BUILD methods
  8. +
  9. Returns the object
  10. +
+
+ +
+

The Object is Opaque

+ + +
+ +

DEMOLISH

-

overrides and super

+

override and super

-

overrides and super

+

override and super

package Employee;
 use Moose;
 
 extends 'Person';
 
-overrides work => sub {
+override work => sub {
     my $self = shift;
 
-    die "Pay me first" unless $self->got_paid;
+    die "Pay me first"
+        unless $self->got_paid;
     super();
 };
@@ -1103,6 +1142,7 @@ Person->can('extends'); @@ -1148,7 +1188,7 @@ use Moose;
  • use Moose
  • Class->meta
  • Moose::Object base class
  • -
  • extends, overrides, and super
  • +
  • extends, override, and super
  • Simple attributes: has, is => 'ro', & is => 'rw'
  • no Moose
  • __PACKAGE__->meta->make_immutable
  • @@ -1163,12 +1203,15 @@ use Moose;

    Exercises

    # cd exercises
    -$ perl bin/prove -lv t/00-prereq.t
     
    -Missing anything? Install it. (see tarballs/)
    +# perl bin/prove -lv t/00-prereq.t
    +
    +# perl install-moose (if needed)
     
     # perl bin/prove -lv t/01-classes.t
     
    +# edit lib/Person.pm and lib/Employee.pm
    +
     Iterate til this passes all its tests
    @@ -1186,11 +1229,10 @@ Iterate til this passes all its tests
    -

    Roles Can Have State and Behavior

    +

    Roles - State and Behavior

    package HasPermissions;
     use Moose::Role;
    -
     # state
     has access_level => ( is => 'rw' );
     
    @@ -1199,7 +1241,8 @@ sub can_access {
         my $self     = shift;
         my $required = shift;
     
    -    return $self->access_level >= $required;
    +    return $self->access_level
    +             >= $required;
     }
    @@ -1243,8 +1286,8 @@ with 'HasPermissions';

    Classes Consume Roles

    my $person = Person->new(
    -    first_name => 'Kenichi',
    -    last_name => 'Asai',
    +    first_name   => 'Kenichi',
    +    last_name    => 'Asai',
         access_level => 42,
     );
     
    @@ -1300,7 +1343,7 @@ sub print {
     
     # or ...
     
    -if ( Person->meta->does('Printable') ) { ... }
    +Person->meta->does('Printable') @@ -1354,7 +1397,7 @@ use Moose;

    Conflict Resolution

    @@ -1366,10 +1409,10 @@ use Moose; use Moose; with 'IsFragile' => - { alias => + { -alias => { break => 'break_bone' } }, 'CanBreakdance' => - { alias => + { -alias => { break => 'break_it_down' } };