X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=3266122613b555693f6c76648a6974cc8bd86ee9;hb=255fe70078ad34620996c42945b563a5471cceee;hp=2a1be675a85dc02978b3eedf1402fcc12e14a3f1;hpb=de668480a7241b4aca987a0a252bfb738ea10f0d;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index 2a1be67..3266122 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -40,7 +40,7 @@ img#me05 {top: 43px;left: 36px;}
@@ -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
@@ -829,6 +842,16 @@ has last_name => ( ); +
+

More Why Moose?

+ + +
+

Part 1: Moose Classes

@@ -885,10 +908,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 +947,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

@@ -1103,6 +1152,7 @@ Person->can('extends'); @@ -1163,12 +1213,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 +1239,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 +1251,8 @@ sub can_access {
     my $self     = shift;
     my $required = shift;
 
-    return $self->access_level >= $required;
+    return $self->access_level
+             >= $required;
 }
@@ -1243,8 +1296,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 +1353,7 @@ sub print {
 
 # or ...
 
-if ( Person->meta->does('Printable') ) { ... }
+Person->meta->does('Printable') @@ -1366,10 +1419,10 @@ use Moose; use Moose; with 'IsFragile' => - { alias => + { -alias => { break => 'break_bone' } }, 'CanBreakdance' => - { alias => + { -alias => { break => 'break_it_down' } };