Removed as_xml bits from 06 answers
[gitmo/moose-presentations.git] / moose-class / slides / index.html
index 4f2fcd3..94178fe 100644 (file)
@@ -341,7 +341,7 @@ has blog_uri => (
   <h1>Why Moose?</h1>
 
   <ul>
-    <li>A quick bit of propoganda ...</li>
+    <li>A quick bit of propaganda ...</li>
   </ul>
 </div>
 
@@ -866,6 +866,98 @@ use Moose;</code></pre>
 </div>
 
 <div class="slide">
+  <h1>BUILDARGS</h1>
+
+  <ul>
+    <li>Takes <code>@_</code>, returns a hash reference of attribute names/value</li>
+    <li>Accepts a hash or hashref; throws otherwise</li>
+    <li>Provide your own for other cases</li>
+    <li><strong>Always</strong> call <code>$class-&gt;SUPER::BUILDARGS(@_)</code> as a fallback!</li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>BUILDARGS Example</h1>
+
+  <pre><code>package Person;
+use Moose;
+
+sub BUILDARGS {
+    my $class = shift;
+
+    if ( @_ == 1 &amp;&amp; ! ref $_[0] ) {
+        <span class="highlight">return { ssn =&gt; $_[0] };</span>
+    }
+
+    <span class="highlight">return $class-&gt;SUPER::BUILDARGS(@_)</span>;
+}
+
+<span class="highlight">Person-&gt;new('123-45-6789')</span></code></pre>
+</div>
+
+<div class="slide">
+  <h1>BUILD</h1>
+
+  <ul>
+    <li>Called after object is created, before <code>new</code> returns</li>
+    <li>Chance to do more complex validation, set complex attributes</li>
+    <li>Called in reverse inheritance order, parents to children</li>
+    <li>Return value is ignored</li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>BUILD Example</h1>
+
+  <pre><code>package Person;
+use Moose;
+
+sub BUILD {
+    my $self = shift;
+
+    if ( $self-&gt;country_of_residence
+         eq 'USA' ) {
+        die 'All US residents'
+            . ' must have an SSN'
+            unless $self-&gt;has_ssn;
+    }
+}</code></pre>
+</div>
+
+<div class="slide">
+  <h1>Object Construction a la Moose</h1>
+
+  <pre><code>Person-&gt;new(@_)</code></pre>
+
+  <ol>
+    <li>Calls <code>Person-&gt;BUILDARGS(@_)</code> to turn <code>@_</code> into a hashref</li>
+    <li>Blesses a reference</li>
+    <li>Populates attributes based on the hashref from #1</li>
+    <li>Calls <code>$new_object->BUILDALL($constructor_args)</code>
+        <br />... which calls all <code>BUILD</code> methods</li>    
+    <li>Returns the object</li>
+  </ol>
+</div>
+
+<div class="slide">
+  <h1>The Object is Oqaque</h1>
+
+  <ul>
+    <li>Technically it's a hash reference</li>
+    <li><span class="wrong">If you <em>ever</em> treat it as one <strong>you are doing it wrong!</strong></span></li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>DEMOLISH</h1>
+
+  <ul>
+    <li>Like <code>DESTROY</code>, but Moose makes sure all <code>DEMOLISH</code> methods in a hierarchy are called</li>
+    <li>Called in normal inheritance order, children to parents</li>
+  </ul>
+</div>
+
+<div class="slide">
   <h1>extends</h1>
 
   <ul>
@@ -918,23 +1010,23 @@ extends 'LWP';</code></pre>
 </div>  
 
 <div class="slide">
-  <h1><code>overrides</code> and <code>super</code></h1>
+  <h1><code>override</code> and <code>super</code></h1>
 
   <ul>
-    <li><code>overrides</code> is another method modifier</li>
+    <li><code>override</code> is another method modifier</li>
     <li>An alternative to Perl's <code>SUPER::</code></li>
   </ul>
 </div>
 
 <div class="slide">
-  <h1><code>overrides</code> and <code>super</code></h1>
+  <h1><code>override</code> and <code>super</code></h1>
 
   <pre><code>package Employee;
 use Moose;
 
 <span class="current incremental">extends 'Person';</span>
 
-<span class="incremental">overrides</span> work =&gt; sub {
+<span class="incremental">override</span> work =&gt; sub {
     my $self = shift;
 
     die "Pay me first" unless $self-&gt;got_paid;
@@ -1080,7 +1172,7 @@ use Moose;
     <li><code>use Moose</code></li>
     <li><code>Class-&gt;meta</code></li>
     <li><code>Moose::Object</code> base class</li>
-    <li><code>extends</code>, <code>overrides</code>, and <code>super</code></li>
+    <li><code>extends</code>, <code>override</code>, and <code>super</code></li>
     <li>Simple attributes: <code>has</code>, <code>is&nbsp;=&gt;&nbsp;'ro'</code>, &amp; <code>is&nbsp;=&gt;&nbsp;'rw'</code></li>
     <li><code>no Moose</code></li>
     <li><code>__PACKAGE__-&gt;meta-&gt;make_immutable</code></li>
@@ -1286,7 +1378,7 @@ use Moose;
   <h1>Conflict Resolution</h1>
 
   <ul>
-    <li>The consuming class must resolve the conflict by implementing th emethod</li>
+    <li>The consuming class must resolve the conflict by implementing the method</li>
     <li>Can use some combination of method exclusion and aliasing</li>
   </ul>
 </div>
@@ -1846,7 +1938,7 @@ sub _build_shoes {
     my $self = shift;
 
     return Shoes-&gt;new(
-        size =&gt; <span class="highlight">$_[0]-&gt;shoe_size</span> );
+        size =&gt; <span class="highlight">$self-&gt;shoe_size</span> );
 }</code></pre>
 </div>
 
@@ -1996,8 +2088,7 @@ has '<span class="highlight">+first_name</span>' =&gt; (
 use Moose;
 
 has first_name =&gt; (
-    <span class="highlight">reader</span> =&gt; 'first_name',
-    <span class="highlight">writer</span> =&gt; 'first_name',
+    <span class="highlight">accessor</span> =&gt; 'first_name',
 );</code></pre>
 
   <ul>
@@ -2030,7 +2121,7 @@ use Moose;
 
 has first_name =&gt; (
     <span class="highlight">reader</span> =&gt; 'get_first_name',
-    <span class="highlight">writer</span> =&gt; 'set_first_name,
+    <span class="highlight">writer</span> =&gt; 'set_first_name',
 );</code></pre>
 </div>
 
@@ -2046,7 +2137,7 @@ has first_name =&gt; (
 );</code></pre>
 
   <ul>
-    <li>Can also mix-and-match</li>
+    <li>Can also mix-and-match <code>is</code> and explicit names</li>
   </ul>
 </div>
 
@@ -2330,12 +2421,14 @@ sub xml { '&lt;doc&gt;' . <span class="highlight">inner()</span> . '&lt;/doc&gt;
 package Report;
 extends 'Document';
 
-<span class="highlight">augment xml</span> =&gt; { title() . <span class="highlight">inner()</span> . summary() };
+<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; { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
+<span class="highlight">augment xml</span> =&gt;
+    sub { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
 </div>
 
 <div class="slide">
@@ -2794,6 +2887,24 @@ Iterate til this passes all its tests</pre>
   <h1>Part 9: Writing Moose Extensions</h1>
 </div>
 
+<div class="slide fake-slide0">
+  <h1>The End</h1>
+</div>
+
+<div class="slide">
+  <h1>More Information</h1>
+
+  <ul>
+    <li><a href="http://moose.perl.org/">http://moose.perl.org/</a></li>
+    <li><a href="http://search.cpan.org/dist/Moose/lib/Moose/Manual.pod">Moose::Manual</a> and <a href="http://search.cpan.org/dist/Moose/lib/Moose/Cookbook.pod">Moose::Cookbook</a></li>
+    <li><a href="irc://irc.perl.org/#moose">irc://irc.perl.org/#moose</a></li>
+    <li>mailing list - <a href="mailto:moose@perl.org">moose@perl.org</a></li>
+    <li>Slides and exercises are in Moose's git repo:
+        <br />
+        <span style="white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
+  </ul>
+</div>
+
 </div> 
 </body>
 </html>