X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=b065633f79232569c9117da03886f1ee22399a65;hb=6960f4e1f61fa7cdb54cae31ce4e60d513469d6a;hp=ddc3e774c85f0afab38f7b2182bfcb07c6e4dc53;hpb=232c184d204fd0e834ee16c60414555b2b383134;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index ddc3e77..b065633 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -40,8 +40,8 @@ img#me05 {top: 43px;left: 36px;}
-

Before & After

+

Before and After

before 'foo'
     => sub { warn 'About to call foo()' };
@@ -235,7 +245,10 @@ after  'foo'
     my @return =
         $self->$real_foo( @_, bar => 42 );
 
-    return ( @return, 'modify return values' );
+    return (
+        @return,
+        'modify return values'
+    );
 };
@@ -262,7 +275,7 @@ has weight => ( ); # kaboom -Person->new( weight => 'fat' ); +Person->new( weight => 'heavy' );
@@ -284,10 +297,10 @@ use Moose; has blog_uri => ( is => 'rw', isa => 'URI', - handles => { 'blog_hostname' => 'host' }, + handles => { 'blog_host' => 'host' }, ); -$person->blog_hostname; +$person->blog_host; # really calls $person->blog_uri->host
@@ -341,7 +354,7 @@ has blog_uri => (

Why Moose?

@@ -814,7 +827,7 @@ sub last_name {

Typo?

- $self->{last_nane} + $self->{last_nane}
@@ -829,6 +842,16 @@ has last_name => ( );
+
+

More Why Moose?

+ + +
+

Part 1: Moose Classes

@@ -866,10 +889,103 @@ use Moose;
+

BUILDARGS

+ + +
+ +
+

BUILDARGS Example

+ +
package Person;
+use Moose;
+
+sub BUILDARGS {
+    my $class = shift;
+
+    if ( @_ == 1 && ! ref $_[0] ) {
+        return { ssn => $_[0] };
+    }
+    return $class->SUPER::BUILDARGS(@_);
+}
+
+Person->new('123-45-6789')
+
+ +
+

BUILD

+ + +
+ +
+

BUILD Example

+ +
package Person;
+use Moose;
+
+sub BUILD {
+    my $self = shift;
+
+    if ( $self->country_of_residence
+         eq 'USA' ) {
+        die 'All US residents'
+            . ' must have an SSN'
+            unless $self->has_ssn;
+    }
+}
+
+ +
+

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

+ + +
+ +

extends

package Employee;
@@ -884,14 +1000,14 @@ use Moose;
     
  • Each call to extends resets your parents
  • -

    WRONG

    +

    Wrong

    package EvilEmployee;
     use Moose;
     extends 'Person';
     extends 'Thief';
    -

    RIGHT

    +

    Right

    package EvilEmployee;
     use Moose;
    @@ -918,26 +1034,27 @@ extends 'LWP';
    -

    overrides and super

    +

    override and super

    -

    overrides and super

    +

    override and super

    package Employee;
     use Moose;
     
    -extends 'Person';
    +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();
     };
    @@ -949,11 +1066,12 @@ use Moose;
  • Mostly like $self->SUPER::work(@_)
  • But cannot change @_!
  • Binds the parent's method at compile time
  • +
  • Parent determined by checking Child->meta()->superclasses()
  • -

    Attributes (Part 1)

    +

    Minimal Attributes

    @@ -1080,7 +1200,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
  • @@ -1088,15 +1208,22 @@ use Moose;
    +

    Questions?

    +
    + +

    Exercises

    -
    $ cd exercises
    -$ perl bin/prove -lv t/00-prereq.t
    +  
    # cd exercises
    +
    +# perl bin/prove -lv t/00-prereq.t
     
    -Missing anything? Install it. (see tarballs/)
    +# 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
    @@ -1114,12 +1241,11 @@ Iterate til this passes all its tests
    -

    Roles Can Have State and Behavior

    +

    Roles - State and Behavior

    package HasPermissions;
     use Moose::Role;
    -
    -# state
    +# state
     has access_level => ( is => 'rw' );
     
     # behavior
    @@ -1127,7 +1253,8 @@ sub can_access {
         my $self     = shift;
         my $required = shift;
     
    -    return $self->access_level >= $required;
    +    return $self->access_level
    +             >= $required;
     }
    @@ -1171,8 +1298,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,
     );
     
    @@ -1228,7 +1355,7 @@ sub print {
     
     # or ...
     
    -if ( Person->meta->does('Printable') ) { ... }
    +Person->meta->does('Printable') @@ -1282,7 +1409,7 @@ use Moose;

    Conflict Resolution

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