Add an introduce yourself slide. Expand Native delegation slides. Add a few slides...
[gitmo/moose-presentations.git] / moose-class / slides / index.html
index 5e678db..98e190a 100644 (file)
@@ -40,7 +40,7 @@ img#me05 {top: 43px;left: 36px;}
 <div id="header"></div>
 <div id="footer">
   <div id="license">
-    <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/us/88x31.png" /></a>
+    <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/"><img alt="Creative Commons License" style="border-width:0" src="ui/creative-commons.png" /></a>
     <br /><span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/Text" property="dc:title" rel="dc:type">Introduction to Moose</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">David Rolsky</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/">Creative Commons Attribution-Share Alike 3.0 United States License</a>.
   </div>
 
@@ -56,6 +56,16 @@ img#me05 {top: 43px;left: 36px;}
 </div>
 
 <div class="slide">
+  <h1>Introduce Yourselves</h1>
+
+  <ul>
+    <li>Your name</li>
+    <li>What you do with Perl</li>
+    <li>Why you're here today (optional)</li>
+  </ul>
+</div>
+
+<div class="slide">
   <h1>Moose Summed Up</h1>
 
   <ul>
@@ -126,7 +136,7 @@ img#me05 {top: 43px;left: 36px;}
     <li>
       Attributes have ...
       <ul>
-        <li>Access-control (read-only vs read-write)</li>
+        <li>Mutability (read-only vs read-write)</li>
         <li>An optional type</li>
         <li>Accessor methods</li>
         <li>Delegation methods</li>
@@ -235,7 +245,10 @@ after  'foo'
     my @return =
         $self-&gt;$real_foo( @_, bar =&gt; 42 );
 
-    return ( @return, 'modify return values' );
+    return (
+        @return,
+        'modify return values'
+    );
 };</code></pre>
 </div>
 
@@ -284,10 +297,10 @@ use Moose;
 has blog_uri =&gt; (
     is      =&gt; 'rw',
     isa     =&gt; 'URI',
-    <span class="highlight">handles =&gt; { 'blog_hostname' =&gt; 'host' },</span>
+    <span class="highlight">handles =&gt; { 'blog_host' =&gt; 'host' },</span>
 );
 
-<span class="highlight">$person->blog_hostname;</span>
+<span class="highlight">$person->blog_host;</span>
 # really calls $person->blog_uri->host</code></pre>
 </div>
 
@@ -888,7 +901,6 @@ sub BUILDARGS {
     if ( @_ == 1 &amp;&amp; ! ref $_[0] ) {
         <span class="highlight">return { ssn =&gt; $_[0] };</span>
     }
-
     <span class="highlight">return $class-&gt;SUPER::BUILDARGS(@_)</span>;
 }
 
@@ -962,6 +974,8 @@ sub BUILD {
 
   <ul>
     <li><code>extends</code> is sugar for declaring parent classes</li>
+    <li>Also ensures metaclass compatibility between parent and child</li>
+    <li>Do not <code>use base</code></li>
   </ul>
 
   <pre><code>package Employee;
@@ -1029,7 +1043,8 @@ use Moose;
 <span class="incremental">override</span> work =&gt; sub {
     my $self = shift;
 
-    die "Pay me first" unless $self-&gt;got_paid;
+    die "Pay me first"
+        unless $self-&gt;got_paid;
     <span class="incremental">super();</span>
 }<span class="incremental">;</span></code></pre>
 </div>
@@ -1127,7 +1142,7 @@ Person->can('extends');</code></pre>
 
   <ul>
     <li><code>no Moose</code> at the end of a package is a best practice</li>
-    <li>Or <code>namespace::clean</code> at the top</li>
+    <li>Or <code>use namespace::autoclean</code> at the top</li>
     <li>Just do it</li>
   </ul>
 </div>
@@ -1195,6 +1210,8 @@ use Moose;
 
 # perl bin/prove -lv t/01-classes.t
 
+# edit lib/Person.pm and lib/Employee.pm
+
 Iterate til this passes all its tests</pre>
 </div>
 
@@ -1212,11 +1229,10 @@ Iterate til this passes all its tests</pre>
 </div>
 
 <div class="slide">
-  <h1>Roles Can Have State <strong>and</strong> Behavior</h1>
+  <h1>Roles - State <strong>and</strong> Behavior</h1>
 
   <pre><code>package HasPermissions;
 use Moose::Role;
-
 <span class="current incremental"># state
 has access_level =&gt; ( is =&gt; 'rw' );</span>
 
@@ -1225,7 +1241,8 @@ sub can_access {
     my $self     = shift;
     my $required = shift;
 
-    return $self-&gt;access_level &gt;= $required;
+    return $self-&gt;access_level
+             &gt;= $required;
 }</span></code></pre>
 
 </div>
@@ -1269,8 +1286,8 @@ with 'HasPermissions';</code></pre>
   <h1>Classes Consume Roles</h1>
 
 <pre><code>my $person = Person-&gt;new(
-    first_name => 'Kenichi',
-    last_name => 'Asai',
+    first_name   =&gt; 'Kenichi',
+    last_name    =&gt; 'Asai',
     access_level =&gt; 42,
 );
 
@@ -1326,7 +1343,7 @@ sub print {
 
 # or ...
 
-if ( Person-&gt;meta-&gt;does('Printable') ) { ... }</code></pre>
+Person-&gt;meta-&gt;does('Printable')</code></pre>
 
 </div>
 
@@ -1392,10 +1409,10 @@ use Moose;
 use Moose;
 
 <span class="highlight">with 'IsFragile' =>
-         { alias =>
+         { -alias =>
                { break => 'break_bone' } },
      'CanBreakdance' =>
-         { alias =>
+         { -alias =>
                { break => 'break_it_down' } };</span></code></pre>
 
   <ul>
@@ -1411,13 +1428,13 @@ use Moose;
 use Moose;
 
 <span class="highlight">with 'IsFragile' =>
-         { alias =>
+         { -alias =>
                { break => 'break_bone' },
-           exclude => 'break' },
+           -excludes => 'break' },
      'CanBreakdance' =>
-         { alias =>
-               { break => 'break_dance' },
-           exclude => 'break' };</span></code></pre>
+         { -alias =>
+               { break => 'break_it_down' },
+           -excludes => 'break' };</span></code></pre>
 </div>
 
 <div class="slide">
@@ -1429,7 +1446,7 @@ use Moose;
 sub break {
     my $self = shift;
 
-    $self->break_dance;
+    $self->break_it_down;
     if ( rand(1) &lt; 0.5 ) {
         $self->break_bone;
     }
@@ -1590,7 +1607,8 @@ has [ 'left', 'right' ] => (
   <pre><code>use Moose::Util qw( apply_all_roles );
 
 my $fragile_person = Person->new( ... );
-apply_all_roles( $fragile_person, 'IsFragile' );</code></pre>
+apply_all_roles( $fragile_person,
+                 'IsFragile' );</code></pre>
 
   <ul>
     <li>Does not change the <code>Person</code> class</li>
@@ -1753,7 +1771,7 @@ Person->new(); # kaboom</span></code></pre>
 
   <ul>
     <li>Attributes can have defaults</li>
-    <li>Simple non-referecne scalars (number, string)</li>
+    <li>Simple non-reference scalars (number, string)</li>
     <li>Subroutine reference</li>
     <li>A builder method</li>
   </ul>
@@ -1794,7 +1812,7 @@ has bank =&gt; (
 </div>
 
 <div class="slide">
-  <h1>Default as a Subroutine Reference</h1>
+  <h1>Subroutine Reference Default</h1>
 
   <ul>
     <li>Called as a method on the object</li>
@@ -1867,7 +1885,8 @@ has bank =&gt; (
 
 sub _build_bank {
     my $self = shift;
-    return Bank-&gt;new( name => 'Spire FCU' );
+    return Bank-&gt;new(
+        name => 'Spire FCU' );
 }</code></pre>
 </div>
 
@@ -1906,7 +1925,7 @@ has bank =&gt; (
 </div>
 
 <div class="slide">
-  <h1>Lazy, Good for Nothing Attributes</h1>
+  <h1>Lazy, Good for Nothin' Attributes</h1>
 
   <ul>
     <li>Normally, defaults are generated during object construction</li>
@@ -2190,7 +2209,8 @@ has first_name =&gt; (
   <h1>Exercises</h1>
 
   <pre># cd exercises
-# perl bin/prove -lv t/03-basic-attributes.t
+# perl bin/prove -lv \
+      t/03-basic-attributes.t
 
 Iterate til this passes all its tests</pre>
 </div>
@@ -2259,7 +2279,8 @@ before work =&gt; sub {
     my $self = shift;
     return unless $DEBUG;
 
-    warn "Called work on ", $self->full_name,
+    warn "Called work on ",
+         $self-&gt;full_name,
          "with the arguments: [@_]\n";
 };</code></pre>
 </div>    
@@ -2291,19 +2312,17 @@ after work =&gt; sub {
 </div>
 
 <div class="slide">
-  <h1>Other Uses Example</h1>
+  <h1>More Modifier Examples</h1>
 
   <pre><code>has password =&gt; (
      is      =&gt; 'rw',
      clearer =&gt; 'clear_password',
 );
-
 has hashed_password =&gt; (
      is      =&gt; 'ro',
      builder =&gt; '_build_hashed_password',
      clearer =&gt; '_clear_hashed_password',
 );
-
 after clear_password =&gt; sub {
     my $self = shift;
     $self-&gt;_clear_hashed_password;
@@ -2344,7 +2363,6 @@ after clear_password =&gt; sub {
             $self-&gt;_munge_insert(@_) );
 
     $new_user->_assign_uri;
-
     return $new_user;
 };</code></pre>
 </div>
@@ -2353,7 +2371,7 @@ after clear_password =&gt; sub {
   <h1>Modifier Order</h1>
 
   <ul>
-    <li>Before runs order from last to first</li>
+    <li>Before runs in order from last to first</li>
     <li>After runs in order from first to last</li>
     <li>Around runs in order from last to first</li>
   </ul>
@@ -2422,13 +2440,11 @@ sub xml { '&lt;doc&gt;' . <span class="highlight">inner()</span> . '&lt;/doc&gt;
 
 package Report;
 extends 'Document';
-
 <span class="highlight">augment xml</span> =&gt;
     sub { title() . <span class="highlight">inner()</span> . summary() };
 
 package TPSReport;
 extends 'Report';
-
 <span class="highlight">augment xml</span> =&gt;
     sub { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
 </div>
@@ -2509,7 +2525,8 @@ extends 'Report';
   <h1>Exercises</h1>
 
   <pre># cd exercises
-# perl bin/prove -lv t/04-method-modifiers.t
+# perl bin/prove -lv \
+      t/04-method-modifiers.t
 
 Iterate til this passes all its tests</pre>
 </div>
@@ -2565,6 +2582,7 @@ Item
 <pre>
 (Item)
     (Defined)
+        (Value)
         Ref
             ScalarRef
             ArrayRef[`a]
@@ -2663,8 +2681,9 @@ undef</code></pre>
 <span class="incremental current">subtype 'PositiveInt',</span>
     <span class="incremental">as      'Int',</span>
     <span class="incremental">where   { $_ &gt; 0 },</span>
-    <span class="incremental">message { "The value you provided ($_)"
-              . " was not a positive number." };</span>
+    <span class="incremental">message
+        { "The value you provided ($_)"
+          . " was not a positive int." };</span>
 
 has size =&gt; (
     is  =&gt; 'ro',
@@ -2743,7 +2762,9 @@ subtype 'Car',
   <pre><code>use Moose::Util::TypeConstraints;
 enum Color =&gt; qw( red blue green ) );
 
-my %ok = map { $_ =&gt; 1 } qw( red blue green );
+my %ok = map { $_ =&gt; 1 }
+             qw( red blue green );
+
 subtype 'Color'
     as      'Str',
     where   { $ok{$_} },
@@ -2775,9 +2796,13 @@ has size =&gt; (
 
 subtype 'UCStr',
     as    'Str',
-    where { ! /[a-z]/ };
+    where { ! /[a-z]/ };</code></pre>
+</div>
 
-<span class="incremental current">coerce 'UCStr',</span>
+<div class="slide">
+  <h1>Coercions</h1>
+
+  <pre><code><span class="incremental current">coerce 'UCStr',</span>
     <span class="incremental">from 'Str',</span>
     <span class="incremental">via  { uc };</span>
 
@@ -2816,7 +2841,7 @@ coerce 'My::DateTime',
     via  { [ $_ ] };</code></pre>
 
   <ul>
-    <li>Coerce instead of a union like <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
+    <li>Instead of union - <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
   </ul>
 </div>
 
@@ -2825,8 +2850,6 @@ coerce 'My::DateTime',
 
   <pre><code>package Person;
 
-use Moose::Util::TypeConstraints;
-
 has height =&gt; (
     is  =&gt; 'rw',
     <span class="highlight">isa =&gt; 'Num',</span>
@@ -2843,7 +2866,7 @@ has favorite_numbers =&gt; (
   <h1>More Droppings</h1>
 
   <ul>
-    <li><code>Moose::Util::TypeConstraint</code> also needs cleanup</li>
+    <li><code>Moose::Util::TypeConstraints</code> also needs cleanup</li>
   </ul>
 
   <pre><code>package Person;
@@ -2870,12 +2893,11 @@ sub work {
             \@_,
             tasks    =&gt;
                 { isa    =&gt; 'ArrayRef[Task]',
-                  coerce =&gt;1 },
+                  coerce =&gt; 1 },
             can_rest =&gt;
                 { isa     =&gt; 'Bool',
                   default =&gt; 0 },
         );</span>
-
     ...
 }</code></pre>
 </div>
@@ -2899,7 +2921,7 @@ sub work {
   <h1>Digression: The Type Registry</h1>
 
   <ul>
-    <li>Types are actually <code>Moose::Meta::TypeConstraint</code> <em>objects</em></li>
+    <li>Types are actually <code>Moose::Meta::TypeConstraints</code> <em>objects</em></li>
     <li>Stored in an interpreter-global registry mapping names to objects</li>
   </ul>
 </div>
@@ -2928,7 +2950,6 @@ sub work {
   <h1>Namespace Fix</h1>
 
   <pre><code>use Moose::Util::TypeConstraints;
-
 subtype <span class="highlight">'MyApp::Type::DateTime',</span>
     as 'DateTime';
 
@@ -3108,7 +3129,8 @@ $alice-&gt;friend($bob);</code></pre>
 use Moose;
 
 has name   =&gt; ( is =&gt; 'ro' );
-has friend =&gt; ( is =&gt; 'rw', <span class="highlight">weak_ref =&gt; 1</span> );
+has friend =&gt; ( is       =&gt; 'rw',
+                <span class="highlight">weak_ref =&gt; 1</span> );
 
 my $alice = Person-&gt;new( name =&gt; 'Alice' );
 my $bob   = Person-&gt;new( name =&gt; 'Bob' );
@@ -3227,7 +3249,6 @@ has lungs =&gt; (
 
   <pre><code>package Person;
 use Moose;
-
 has account =&gt; (
     is      =&gt; 'ro',
     isa     =&gt; 'BankAccount',
@@ -3275,7 +3296,6 @@ has name =&gt; (
 
   <pre><code>package Auditor;
 use Moose::Role;
-
 sub record_change  { ... }
 sub change_history { ... }
 
@@ -3303,6 +3323,112 @@ has history =&gt; (
 </div>
 
 <div class="slide">
+  <h1>Native Delegation</h1>
+
+  <ul>
+    <li>Delegate to <em>unblessed</em> Perl types</li>
+    <li>Scalar, array or hash ref, etc</li>
+    <li>Treat Perl types as objects</li>
+    <li>Still uses <code>handles</code></li>
+    <li>Pretend that native Perl types have methods</li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>Native Delegation - Array(Ref)</h1>
+
+  <ul>
+    <li>Methods include:
+      <ul>
+        <li><code>push</code></li>
+        <li><code>shift</code></li>
+        <li><code>elements</code> - returns all elements</li>
+        <li><code>count</code></li>
+        <li><code>is_empty</code></li>
+        <li>quite a few more</li>
+      </ul>
+    </li>
+  </ul>
+<div>
+
+<div class="slide">
+  <h1>Native Delegation - Array(Ref)</h1>
+
+  <pre><code>package Person;
+use Moose;
+has _favorite_numbers =&gt; (
+    traits   =&gt; [ 'Array' ],
+    is       =&gt; 'ro',
+    isa      =&gt; 'ArrayRef[Int]',
+    default  =&gt; sub { [] },
+    init_arg =&gt; undef,
+    <span class="highlight">handles  =&gt;
+      { favorite_numbers    =&gt; 'elements',
+        add_favorite_number =&gt; 'push',
+      },</span>
+);</code></pre>
+</div>
+
+<div class="slide">
+  <h1>Native Delegation - Array(Ref)</h1>
+
+  <pre><code>my $person = Person-&gt;new();
+
+$person-&gt;add_favorite_number(7);
+$person-&gt;add_favorite_number(42);
+
+print "$_\n"
+    for $person-&gt;favorite_numbers;
+
+# 7
+# 42</code></pre>
+</div>
+
+<div class="slide">
+  <h1>Native Delegation</h1>
+
+  <ul>
+    <li>Native types are ...
+      <ul>
+        <li>Number - <code>add</code>, <code>mul</code>, ...</li>
+        <li>String - <code>append</code>, <code>chop</code>, ...</li>
+        <li>Counter - <code>inc</code>, <code>dec</code>, ...</li>
+        <li>Bool - <code>set</code>, <code>toggle</code>, ...</li>
+        <li>Hash - <code>get</code>, <code>set</code>, ...</li>
+        <li>Array - already saw it</li>
+        <li>Code - <code>execute</code>, that's it</li>
+      </ul>
+    </li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>Curried Delegation</h1>
+
+  <ul>
+    <li>A delegation with some preset arguments</li>
+    <li>Works with object or Native delegation</li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>Curried Delegation</h1>
+
+  <pre><code>package Person;
+use Moose;
+has account =&gt; (
+    is      =&gt; 'ro',
+    isa     =&gt; 'BankAccount',
+    handles =&gt; {
+        receive_100 =&gt;
+            <span class="highlight">[ 'deposit', 100 ]</span>
+        give_100    =&gt;
+            <span class="highlight">[ 'withdraw', 100 ]</span>
+    },</span>
+);</code></pre>
+</div>
+
+<div class="slide">
   <h1>Traits and Metaclasses</h1>
 
   <ul>
@@ -3345,7 +3471,6 @@ has ssn =&gt; (
     isa    =&gt; 'Str',
     <span class="highlight">label  =&gt; 'Social Security Number',</span>
 );
-
 print <span class="highlight">Person-&gt;meta
             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
 </div>
@@ -3364,7 +3489,6 @@ has ssn =&gt; (
     isa       =&gt; 'Str',
     <span class="highlight">label     =&gt; 'Social Security Number',</span>
 );
-
 print <span class="highlight">Person-&gt;meta
             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
 </div>
@@ -3399,7 +3523,8 @@ print <span class="highlight">Person-&gt;meta
   <h1>Exercises</h1>
 
   <pre># cd exercises
-# perl bin/prove -lv t/06-advanced-attributes.t
+# perl bin/prove -lv \
+      t/06-advanced-attributes.t
 
 Iterate til this passes all its tests</pre>
 </div>