override declares your intent to override a superclass method
[gitmo/moose-presentations.git] / moose-class / slides / index.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
2
3 <html xmlns="http://www.w3.org/1999/xhtml">
4
5 <head>
6 <title>Introduction to Moose</title>
7 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
8 <!-- metadata -->
9 <meta name="generator" content="S5" />
10 <meta name="version" content="S5 1.2a2" />
11 <meta name="author" content="Eric A. Meyer" />
12 <meta name="company" content="Complex Spiral Consulting" />
13 <!-- configuration parameters -->
14 <meta name="defaultView" content="slideshow" />
15 <meta name="controlVis" content="hidden" />
16 <!-- style sheet links -->
17 <link rel="stylesheet" href="ui/default/slides.css" type="text/css" media="projection" id="slideProj" />
18 <link rel="stylesheet" href="ui/default/outline.css" type="text/css" media="screen" id="outlineStyle" />
19 <link rel="stylesheet" href="ui/default/print.css" type="text/css" media="print" id="slidePrint" />
20 <link rel="stylesheet" href="ui/default/opera.css" type="text/css" media="projection" id="operaFix" />
21 <!-- embedded styles -->
22 <style type="text/css" media="all">
23 .imgcon {width: 525px; margin: 0 auto; padding: 0; text-align: center;}
24 #anim {width: 270px; height: 320px; position: relative; margin-top: 0.5em;}
25 #anim img {position: absolute; top: 42px; left: 24px;}
26 img#me01 {top: 0; left: 0;}
27 img#me02 {left: 23px;}
28 img#me04 {top: 44px;}
29 img#me05 {top: 43px;left: 36px;}
30 </style>
31 <!-- S5 JS -->
32 <script src="ui/default/slides.js" type="text/javascript"></script>
33 <link rel="stylesheet" href="ui/custom.css" type="text/css" />
34 </head>
35 <body>
36
37 <div class="layout">
38 <div id="controls"><!-- DO NOT EDIT --></div>
39 <div id="currentSlide"><!-- DO NOT EDIT --></div>
40 <div id="header"></div>
41 <div id="footer">
42   <div id="license">
43     <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>
44     <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>.
45   </div>
46
47   <h2>Introduction to Moose</h2>
48 </div>
49 </div>
50
51 <div class="presentation">
52
53 <div class="slide">
54   <h1>Introduction to Moose</h1>
55   <h2>Dave Rolsky</a>
56 </div>
57
58 <div class="slide">
59   <h1>Introduce Yourselves</h1>
60
61   <ul>
62     <li>Your name</li>
63     <li>What you do with Perl</li>
64     <li>Why you're here today (optional)</li>
65   </ul>
66 </div>
67
68 <div class="slide">
69   <h1>Moose Summed Up</h1>
70
71   <ul>
72     <li><strong>Declarative</strong> OO sugar</li>
73     <li>Introspectable</li>
74     <li>Extensible (MooseX::* on CPAN)</li>
75   </ul>
76 </div>
77
78 <div class="slide">
79   <h1>Moose Background</h1>
80
81   <ul>
82     <li>Created by Stevan Little, first released in 2006</li>
83     <li>Moose builds on Perl 5's native OO</li>
84     <li>Borrows ideas from other languages, notably Perl 6</li>
85     <li>Provides semantics for common operations</li>
86   </ul>
87 </div>
88
89 <div class="slide fake-slide0">
90   <h1>Part 0: Moose Concepts</h1>
91 </div>
92
93 <div class="slide">
94   <h1>Classes</h1>
95
96   <ul>
97     <li>
98       Classes have ...
99       <ul>
100         <li>Attributes</li>
101         <li>Methods</li>
102         <li>Superclasses</li>
103         <li>Method modifiers</li>
104         <li>Constructor and destructor</li>
105         <li>One metaclass object</li>
106       </ul>
107     </li>
108     <li>Classes do roles</li>
109   </ul>
110 </div>
111
112 <div class="slide">
113   <h1>Class Example</h1>
114
115   <pre><code>package Person;
116 <span class="highlight">use Moose;</span></code></pre>
117
118   <ul>
119     <li>Poof, a Moose-based class!</li>
120   </ul>
121 </div>
122
123 <div class="slide">
124   <h1>Attributes</h1>
125
126   <ul>
127     <li>Aka property, slot, field, member variable</li>
128     <li>A piece of data owned by an object</li>
129   </ul>
130 </div>
131
132 <div class="slide">
133   <h1>Attributes</h1>
134
135   <ul>
136     <li>
137       Attributes have ...
138       <ul>
139         <li>Mutability (read-only vs read-write)</li>
140         <li>An optional type</li>
141         <li>Accessor methods</li>
142         <li>Delegation methods</li>
143         <li>Optional default value</li>
144         <li>Many more features</li>
145       </ul>
146     </li>
147     <li>Stored in the object, but don't worry about that</li>
148   </ul>
149 </div>
150
151 <div class="slide">
152   <h1>Attribute Example</h1>
153
154   <pre><code>package Person;
155 use Moose;
156
157 <span class="highlight">has first_name =&gt; ( is =&gt; 'rw' );</span></code></pre>
158
159 </div>
160
161 <div class="slide">
162   <h1>Methods</h1>
163
164   <ul>
165     <li>Nothing fancy here, just Perl subroutines</li>
166   </ul>
167
168   <pre><code>package Person;
169 use Moose;
170
171 <span class="highlight">sub greet { ... }</span></code></pre>
172 </div>
173
174 <div class="slide">
175   <h1>Roles</h1>
176
177   <ul>
178     <li>Classes <strong>do</strong> (or consume) roles</li>
179     <li>Similar to mixins and Java interfaces</li>
180   </ul>
181 </div>
182
183 <div class="slide">
184   <h1>Roles</h1>
185
186   <ul>
187     <li>Like classes, can have attributes, methods, do roles</li>
188     <li>Roles can require methods</li>
189     <li>Roles are composed (flattened) into classes</li>
190   </ul>
191 </div>
192
193 <div class="slide">
194   <h1>Role Example</h1>
195
196 <pre><code>package HasPermissions;
197 <span class="highlight">use Moose::Role;</span>
198
199 has is_admin =&gt; ( is =&gt; 'rw' );</code></pre>
200 </div>
201
202 <div class="slide">
203   <h1>Role Example</h1>
204
205   <p>
206     And then ...
207   </p>
208   
209 <pre><code>package Person;
210 use Moose;
211
212 <span class="highlight">with 'HasPermissions';</span></code></pre>
213 </div>
214
215 <div class="slide">
216   <h1>Method Modifiers</h1>
217
218   <ul>
219     <li>AKA advice</li>
220     <li>&quot;<strong>Before</strong> foo(), do this first&quot;</li>
221     <li>&quot;Do this <strong>after</strong> foo()&quot;</li>
222     <li>&quot;Put this code <strong>around</strong> foo()&quot;</li>
223   </ul>
224 </div>
225
226 <div class="slide">
227   <h1>Before and After</h1>
228
229 <pre><code>before 'foo'
230     =&gt; sub { warn 'About to call foo()' };
231
232 after  'foo'
233     =&gt; sub { warn 'Leaving foo()' };</code></pre>
234
235 </div>
236
237 <div class="slide">
238   <h1>Around</h1>
239
240 <pre><code>around 'foo' =&gt; sub {
241     my $real_foo = shift;
242     my $self     = shift;
243
244     warn 'Just before foo()';
245     my @return =
246         $self-&gt;$real_foo( @_, bar =&gt; 42 );
247
248     return (
249         @return,
250         'modify return values'
251     );
252 };</code></pre>
253 </div>
254
255 <div class="slide">
256   <h1>Type Constraints</h1>
257
258   <ul>
259     <li>NOT A FULL-BLOWN TYPE SYSTEM!</li>
260     <li>But still darn useful</li>
261     <li>Constrain attribute values</li>
262     <li>Coerce from other types</li>
263   </ul>
264 </div>
265
266 <div class="slide">
267   <h1>Type Constraint Example</h1>
268
269 <pre><code>package Person;
270 use Moose;
271
272 has weight =&gt; (
273     is  =&gt; 'ro',
274     <span class="highlight">isa =&gt; 'Int'</span>,
275 );
276
277 # kaboom
278 Person-&gt;new( weight =&gt; 'heavy' );</code></pre>
279 </div>
280
281 <div class="slide">
282   <h1>Delegation</h1>
283
284   <ul>
285     <li>Attributes can define delegations</li>
286     <li>Lets you hide some implementation details</li>
287     <li>Fewer objects to chase around</li>
288   </ul>
289 </div>
290
291 <div class="slide">
292   <h1>Delegation</h1>
293
294 <pre><code>package Person;
295 use Moose;
296
297 has blog_uri =&gt; (
298     is      =&gt; 'rw',
299     isa     =&gt; 'URI',
300     <span class="highlight">handles =&gt; { 'blog_host' =&gt; 'host' },</span>
301 );
302
303 <span class="highlight">$person-&gt;blog_host;</span>
304 # really calls $person-&gt;blog_uri-&gt;host</code></pre>
305 </div>
306
307 <div class="slide">
308   <h1>Constructors</h1>
309
310   <ul>
311     <li>Moose creates <code>new()</code> for you</li>
312     <li>Provide an optional <code>BUILDARGS()</code> and <code>BUILD()</code></li>
313   </ul>
314 </div>
315
316 <div class="slide">
317   <h1>Destructors</h1>
318
319   <ul>
320     <li>Provide an optional <code>DEMOLISH()</code></li>
321   </ul>
322 </div>
323
324 <div class="slide">
325   <h1>Moose Meta-API</h1>
326
327   <ul>
328     <li>Answers questions like ...
329       <ul>
330         <li>What methods does this class have?</li>
331         <li>What are its parents?</li>
332         <li>What attributes does it have (including inherited attributes)?</li>
333         <li>What roles does it do?</li>
334         <li>Much, much, more</li>
335       </ul>
336     </li>
337   </ul>
338 </div>
339
340 <div class="slide">
341   <h1>Moose Meta-API</h1>
342
343   <ul>
344     <li>Not just for introspection ...
345       <ul>
346         <li>Add methods, attributes, roles, etc</li>
347         <li>Extend and alter core features</li>
348       </ul>
349     </li>
350   </ul>
351 </div>
352
353 <div class="slide">
354   <h1>Why Moose?</h1>
355
356   <ul>
357     <li>A quick bit of propaganda ...</li>
358   </ul>
359 </div>
360
361 <div class="slide">
362   <h1>With Moose</h1>
363
364   <pre><code>package Person;
365 use Moose;
366
367 has last_name =&gt; (
368     is  =&gt; 'rw',
369     isa =&gt; 'Str',
370 );</code></pre>
371 </div>
372
373 <div class="slide">
374     <h1>Without Moose</h1>
375
376     <pre class="small"><code>package Person;
377 use strict;
378 use warnings;
379 use Carp 'confess';
380
381 sub new {
382     my $class = shift;
383     my %args  = @_;
384     my $self  = {};
385
386     if (exists $args{last_name}) {
387         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
388                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
389             if ref($args{last_name});
390         $self-&gt;{last_nane} = $args{last_name};
391     }
392
393     return bless $self, $class;
394 }
395
396 sub last_name {
397     my $self = shift;
398
399     if (@_) {
400         my $value = shift;
401         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
402                 . &quot;Validation failed for 'Str' with value $value&quot;
403             if ref($value);
404         $self-&gt;{last_name} = $value;
405     }
406
407     return $self-&gt;{last_name};
408 }</code></pre>
409
410 </div>
411
412 <div class="slide">
413     <h1>Side by side</h1>
414
415     <table class="side-by-side">
416         <tr>
417           <td>
418             <pre><code>package Person;
419 use Moose;
420
421 has last_name =&gt; (
422     is  =&gt; 'rw',
423     isa =&gt; 'Str',
424 );</code></pre>
425             </td>
426             <td>
427                 <pre class="small"><code>package Person;
428 use strict;
429 use warnings;
430 use Carp 'confess';
431
432 sub new {
433     my $class = shift;
434     my %args  = @_;
435     my $self  = {};
436
437     if (exists $args{last_name}) {
438         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
439                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
440             if ref($args{last_name});
441         $self-&gt;{last_nane} = $args{last_name};
442     }
443
444     return bless $self, $class;
445 }
446
447 sub last_name {
448     my $self = shift;
449
450     if (@_) {
451         my $value = shift;
452         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
453                 . &quot;Validation failed for 'Str' with value $value&quot;
454             if ref($value);
455         $self-&gt;{last_name} = $value;
456     }
457
458     return $self-&gt;{last_name};
459 }</code></pre>
460             </td>
461         </tr>
462     </table>
463
464 </div>
465
466 <div class="slide">
467     <h1>Side by side</h1>
468
469     <table class="side-by-side">
470         <tr>
471             <td>
472                 <pre><code><span class="match-moose">package Person;</span>
473 use Moose;
474
475 has last_name =&gt; (
476     is  =&gt; 'rw',
477     isa =&gt; 'Str',
478 );</code></pre>
479             </td>
480             <td>
481                 <pre class="small"><code><span class="match-unsweet">package Person;</span>
482 use strict;
483 use warnings;
484 use Carp 'confess';
485
486 sub new {
487     my $class = shift;
488     my %args  = @_;
489     my $self  = {};
490
491     if (exists $args{last_name}) {
492         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
493                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
494             if ref($args{last_name});
495         $self-&gt;{last_nane} = $args{last_name};
496     }
497
498     return bless $self, $class;
499 }
500
501 sub last_name {
502     my $self = shift;
503
504     if (@_) {
505         my $value = shift;
506         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
507                 . &quot;Validation failed for 'Str' with value $value&quot;
508             if ref($value);
509         $self-&gt;{last_name} = $value;
510     }
511
512     return $self-&gt;{last_name};
513 }</code></pre>
514             </td>
515         </tr>
516     </table>
517 </div>
518
519 <div class="slide">
520     <h1>Side by side</h1>
521
522     <table class="side-by-side">
523         <tr>
524             <td>
525                 <pre><code>package Person;
526 <span class="match-moose">use Moose;</span>
527
528 has last_name =&gt; (
529     is  =&gt; 'rw',
530     isa =&gt; 'Str',
531 );</code></pre>
532             </td>
533             <td>
534                 <pre class="small"><code>package Person;
535 <span class="match-unsweet">use strict;
536 use warnings;
537 use Carp 'confess';
538
539 sub new {
540     my $class = shift;
541     my %args  = @_;
542     my $self  = {};</span>
543
544     if (exists $args{last_name}) {
545         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
546                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
547             if ref($args{last_name});
548         $self-&gt;{last_nane} = $args{last_name};
549     }
550
551     <span class="match-unsweet">return bless $self, $class;
552 }</span>
553
554 sub last_name {
555     my $self = shift;
556
557     if (@_) {
558         my $value = shift;
559         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
560                 . &quot;Validation failed for 'Str' with value $value&quot;
561             if ref($value);
562         $self-&gt;{last_name} = $value;
563     }
564
565     return $self-&gt;{last_name};
566 }</code></pre>
567             </td>
568         </tr>
569     </table>
570 </div>
571
572 <div class="slide">
573     <h1>Side by side</h1>
574
575     <table class="side-by-side">
576         <tr>
577             <td>
578                 <pre><code>package Person;
579 use Moose;
580
581 <span class="match-moose">has last_name =&gt; (</span>
582     is  =&gt; 'rw',
583     isa =&gt; 'Str',
584 <span class="match-moose">);</span></code></pre>
585             </td>
586             <td>
587                 <pre class="small"><code>package Person;
588 use strict;
589 use warnings;
590 use Carp 'confess';
591
592 sub new {
593     my $class = shift;
594     my %args  = @_;
595     my $self  = {};
596
597     <span class="match-unsweet">if (exists $args{last_name}) {</span>
598         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
599                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
600             if ref($args{last_name});
601         <span class="match-unsweet">$self-&gt;{last_nane} = $args{last_name};
602     }</span>
603
604     return bless $self, $class;
605 }
606
607 sub last_name {
608     my $self = shift;
609
610     if (@_) {
611         my $value = shift;
612         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
613                 . &quot;Validation failed for 'Str' with value $value&quot;
614             if ref($value);
615         $self-&gt;{last_name} = $value;
616     }
617
618     return $self-&gt;{last_name};
619 }</code></pre>
620             </td>
621         </tr>
622     </table>
623 </div>
624
625 <div class="slide">
626     <h1>Side by side</h1>
627
628     <table class="side-by-side">
629         <tr>
630             <td>
631                 <pre><code>package Person;
632 use Moose;
633
634 has last_name =&gt; (
635     <span class="match-moose">is  =&gt; 'rw',</span>
636     isa =&gt; 'Str',
637 );</code></pre>
638             </td>
639             <td>
640                 <pre class="small"><code>package Person;
641 use strict;
642 use warnings;
643 use Carp 'confess';
644
645 sub new {
646     my $class = shift;
647     my %args  = @_;
648     my $self  = {};
649
650     if (exists $args{last_name}) {
651         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
652                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
653             if ref($args{last_name});
654         $self-&gt;{last_nane} = $args{last_name};
655     }
656
657     return bless $self, $class;
658 }
659
660 <span class="match-unsweet">sub last_name {
661     my $self = shift;
662
663     if (@_) {
664         my $value = shift;</span>
665         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
666                 . &quot;Validation failed for 'Str' with value $value&quot;
667             if ref($value);
668         <span class="match-unsweet">$self-&gt;{last_name} = $value;
669     }
670
671     return $self-&gt;{last_name};
672 }</span></code></pre>
673             </td>
674         </tr>
675     </table>
676 </div>
677
678 <div class="slide">
679     <h1>Side by side</h1>
680
681     <table class="side-by-side">
682         <tr>
683             <td>
684                 <pre><code>package Person;
685 use Moose;
686
687 has last_name =&gt; (
688     is  =&gt; 'rw',
689     <span class="match-moose">isa =&gt; 'Str',</span>
690 );</code></pre>
691             </td>
692             <td>
693                 <pre class="small"><code>package Person;
694 use strict;
695 use warnings;
696 use Carp 'confess';
697
698 sub new {
699     my $class = shift;
700     my %args  = @_;
701     my $self  = {};
702
703     if (exists $args{last_name}) {
704         <span class="match-unsweet">confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
705                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
706             if ref($args{last_name});</span>
707         $self-&gt;{last_nane} = $args{last_name};
708     }
709
710     return bless $self, $class;
711 }
712
713 sub last_name {
714     my $self = shift;
715
716     if (@_) {
717         my $value = shift;
718         <span class="match-unsweet">confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
719                 . &quot;Validation failed for 'Str' with value $value&quot;
720             if ref($value);</span>
721         $self-&gt;{last_name} = $value;
722     }
723
724     return $self-&gt;{last_name};
725 }</code></pre>
726             </td>
727         </tr>
728     </table>
729 </div>
730
731 <div class="slide">
732     <h1>Side by side</h1>
733
734     <table class="side-by-side">
735         <tr class="incremental">
736             <td>5 lines</td>
737             <td>21 lines</td>
738         </tr>
739         <tr class="incremental">
740             <td>92 characters</td>
741             <td>741 characters</td>
742         </tr>
743         <tr>
744             <td>
745                 <pre><code>package Person;
746 use Moose;
747
748 has last_name =&gt; (
749     is  =&gt; 'rw',
750     isa =&gt; 'Str',
751 );</code></pre>
752             </td>
753             <td>
754                 <pre class="small"><code>package Person;
755 use strict;
756 use warnings;
757 use Carp 'confess';
758
759 sub new {
760     my $class = shift;
761     my %args  = @_;
762     my $self  = {};
763
764     if (exists $args{last_name}) {
765         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
766                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
767             if ref($args{last_name});
768         $self-&gt;{last_nane} = $args{last_name};
769     }
770
771     return bless $self, $class;
772 }
773
774 sub last_name {
775     my $self = shift;
776
777     if (@_) {
778         my $value = shift;
779         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
780                 . &quot;Validation failed for 'Str' with value $value&quot;
781             if ref($value);
782         $self-&gt;{last_name} = $value;
783     }
784
785     return $self-&gt;{last_name};
786 }</code></pre>
787             </td>
788         </tr>
789     </table>
790 </div>
791
792 <div class="slide">
793     <h1>Typo?</h1>
794
795     <pre class="small"><code>sub new {
796     my $class = shift;
797     my %args  = @_;
798     my $self  = {};
799
800     if (exists $args{last_name}) {
801         confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
802                 . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
803             if ref($args{last_name});
804         $self-&gt;{last_nane} = $args{last_name};
805     }
806
807     return bless $self, $class;
808 }</code></pre>
809 </div>
810
811 <div class="slide">
812     <h1>Typo?</h1>
813
814     <pre class="small"><code>if (exists $args{last_name}) {
815     confess &quot;Attribute (last_name) does not pass the type constraint because: &quot;
816             . &quot;Validation failed for 'Str' with value $args{last_name}&quot;
817         if ref($args{last_name});
818     $self-&gt;{last_nane} = $args{last_name};
819 }</code></pre>
820 </div>
821
822 <div class="slide">
823     <h1>Typo?</h1>
824
825     <code>$self-&gt;{last_nane} = $args{last_name};</code>
826 </div>
827
828 <div class="slide">
829     <h1>Typo?</h1>
830     <code>$self-&gt;{last_na<span class="wrong">n</span>e}</code>
831 </div>
832
833 <div class="slide">
834     <h1>Why Moose?</h1>
835
836     <pre><code>package Person;
837 use Moose;
838
839 has last_name =&gt; (
840     is  =&gt; 'rw',
841     isa =&gt; 'Str',
842 );</code></pre>
843 </div>
844
845 <div class="slide">
846     <h1>More Why Moose?</h1>
847
848     <ul>
849       <li>Less code == fewer bugs</li>
850       <li>Moose is well-tested, test your own code, not Moose</li>
851       <li>Focus on <strong>what</strong>, not <strong>how</strong></li>
852     </ul>
853 </div>
854
855 <div class="slide fake-slide0">
856   <h1>Part 1: Moose Classes</h1>
857 </div>
858
859 <div class="slide">
860   <h1>Moose Classes</h1>
861
862   <ul>
863     <li>Moose classes are Perl packages which <code>use Moose</code></li>
864   </ul>
865 </div>
866
867 <div class="slide">
868   <h1>Moose.pm and Your Class</h1>
869
870   <pre><code>package Person;
871 use Moose;</code></pre>
872
873   <ul>
874     <li><code>Moose.pm</code> provides declarative sugar</li>
875     <li>Turns on <code>strict</code> and <code>warnings</code></li>
876     <li>Creates metaclasses for your class: <code>Person-&gt;meta</code></li>
877     <li>Moose classes automatically inherit from <code>Moose::Object</code></li>
878   </ul>
879 </div>
880
881 <div class="slide">
882   <h1>What <code>Moose::Object</code> Provides</h1>
883
884   <ul>
885     <li>Constructor - <code>new()</code></li>
886     <li>Calls your <code>BUILDARGS()</code> and/or <code>BUILD()</code></li>
887     <li>Calls your <code>DEMOLISH</code> during object destruction</li>
888   </ul>
889 </div>
890
891 <div class="slide">
892   <h1>BUILDARGS</h1>
893
894   <ul>
895     <li>Takes <code>@_</code>, returns a hash reference of attribute name/value pairs</li>
896     <li>Accepts a hash or hashref; throws otherwise</li>
897     <li>Provide your own for other cases</li>
898     <li><strong>Always</strong> call <code>$class-&gt;SUPER::BUILDARGS(@_)</code> as a fallback!</li>
899   </ul>
900 </div>
901
902 <div class="slide">
903   <h1>BUILDARGS Example</h1>
904
905   <pre><code>package Person;
906 use Moose;
907
908 sub BUILDARGS {
909     my $class = shift;
910
911     if ( @_ == 1 &amp;&amp; ! ref $_[0] ) {
912         <span class="highlight">return { ssn =&gt; $_[0] };</span>
913     }
914     <span class="highlight">return $class-&gt;SUPER::BUILDARGS(@_)</span>;
915 }
916
917 <span class="highlight">Person-&gt;new('123-45-6789')</span></code></pre>
918 </div>
919
920 <div class="slide">
921   <h1>BUILD</h1>
922
923   <ul>
924     <li>Called after object is created, before <code>new</code> returns</li>
925     <li>Chance to do more complex validation, set complex attributes</li>
926     <li>Called in reverse inheritance order, parents to children</li>
927     <li>Return value is ignored</li>
928   </ul>
929 </div>
930
931 <div class="slide">
932   <h1>BUILD Example</h1>
933
934   <pre><code>package Person;
935 use Moose;
936
937 sub BUILD {
938     my $self = shift;
939
940     if ( $self-&gt;country_of_residence
941          eq 'USA' ) {
942         die 'All US residents'
943             . ' must have an SSN'
944             unless $self-&gt;has_ssn;
945     }
946 }</code></pre>
947 </div>
948
949 <div class="slide">
950   <h1>Object Construction a la Moose</h1>
951
952   <pre><code>Person-&gt;new(@_)</code></pre>
953
954   <ol style="margin-top: 0">
955     <li>Calls <code>Person-&gt;BUILDARGS(@_)</code> to turn <code>@_</code> into a hashref</li>
956     <li>Blesses a reference</li>
957     <li>Populates attributes based on the hashref from #1</li>
958     <li>Calls <code>$new_object-&gt;BUILDALL($constructor_args)</code>
959         <br />... which calls all <code>BUILD</code> methods</li>    
960     <li>Returns the object</li>
961   </ol>
962 </div>
963
964 <div class="slide">
965   <h1>The Object is Opaque</h1>
966
967   <ul>
968     <li>Technically it's a hash reference</li>
969     <li><span class="wrong">If you <em>ever</em> treat it as one <strong>you are doing it wrong!</strong></span></li>
970   </ul>
971 </div>
972
973 <div class="slide">
974   <h1>DEMOLISH</h1>
975
976   <ul>
977     <li>Like <code>DESTROY</code>, but Moose makes sure all <code>DEMOLISH</code> methods in a hierarchy are called</li>
978     <li>Called in normal inheritance order, children to parents</li>
979   </ul>
980 </div>
981
982 <div class="slide">
983   <h1>extends</h1>
984
985   <ul>
986     <li><code>extends</code> is sugar for declaring parent classes</li>
987     <li>Also ensures metaclass compatibility between parent and child</li>
988     <li>Do not <code>use base</code></li>
989   </ul>
990
991   <pre><code>package Employee;
992 use Moose;
993 <span class="highlight">extends 'Person';</span></code></pre>
994 </div>
995
996 <div class="slide">
997   <h1>extends</h1>
998
999   <ul>
1000     <li>Each call to <code>extends</code> <strong>resets</strong> your parents</li>
1001   </ul>
1002
1003   <h2 class="wrong">Wrong</h2>
1004
1005   <pre><code>package EvilEmployee;
1006 use Moose;
1007 extends 'Person';
1008 extends 'Thief';</code></pre>
1009
1010   <h2 class="right">Right</h2>
1011
1012   <pre><code>package EvilEmployee;
1013 use Moose;
1014 extends 'Person', 'Thief';</code></pre>
1015 </div>
1016
1017 <div class="slide">
1018   <h1>Extending un-Moose-y Parents</h1>
1019
1020   <pre><code>package My::LWP;
1021 use Moose;
1022 extends 'LWP';</code></pre>
1023
1024   <ul>
1025     <li>No <code>Moose::Object</code>, so ...
1026       <ul>
1027         <li>No attribute-handling <code>new()</code></li>
1028         <li>No <code>BUILDARGS()</code> or <code>BUILD()</code></li>
1029         <li>No <code>DEMOLISH()</code></li>
1030       </ul>
1031     </li>
1032     <li>But see <code>MooseX::NonMoose</code> for a workaround</li>
1033   </ul>
1034 </div>  
1035
1036 <div class="slide">
1037   <h1><code>override</code> and <code>super</code></h1>
1038
1039   <ul>
1040     <li><code>override</code> is another method modifier</li>
1041     <li>An alternative to Perl's <code>SUPER::</code></li>
1042     <li><em>Declares</em> your intent to override a method</li>
1043   </ul>
1044 </div>
1045
1046 <div class="slide">
1047   <h1><code>override</code> and <code>super</code></h1>
1048
1049   <pre><code>package Employee;
1050 use Moose;
1051
1052 <span class="current incremental">extends 'Person';</span>
1053
1054 <span class="incremental">override</span> work =&gt; sub {
1055     my $self = shift;
1056
1057     die "Pay me first"
1058         unless $self-&gt;got_paid;
1059     <span class="incremental">super();</span>
1060 }<span class="incremental">;</span></code></pre>
1061 </div>
1062
1063 <div class="slide">
1064   <h1>Caveat <code>super</code></h1>
1065
1066   <ul>
1067     <li>Mostly like <code>$self-&gt;SUPER::work(@_)</code></li>
1068     <li><strong>But</strong> cannot change <code>@_</code>!</li>
1069     <li>Binds the parent's method at compile time</li>
1070     <li>Parent determined by checking <code>Child-&gt;meta()-&gt;superclasses()</code></li>
1071   </ul>
1072 </div>
1073
1074 <div class="slide">
1075   <h1>Minimal Attributes</h1>
1076
1077   <ul>
1078     <li><code>has 'foo'</code></li>
1079     <li>Use <code>is =&gt; 'ro'</code> or <code>is =&gt; 'rw'</code></li>
1080     <li>Attributes without "is" have no accessors</li>
1081   </ul>
1082 </div>
1083
1084 <div class="slide">
1085   <h1>Read-write attributes</h1>
1086
1087   <pre><code>package Person;
1088 use Moose;
1089
1090 has first_name =&gt; ( <span class="highlight">is =&gt; 'rw'</span> );
1091
1092 my $person =
1093     Person-&gt;new( first_name =&gt; 'Dave' );
1094
1095 $person-&gt;first_name('Stevan');
1096 print $person-&gt;first_name; # Stevan</code></pre>
1097
1098 </div>
1099
1100 <div class="slide">
1101   <h1>Read-only attributes</h1>
1102
1103   <pre><code>package Person;
1104 use Moose;
1105
1106 has first_name =&gt; ( <span class="highlight">is =&gt; 'ro'</span> );
1107
1108 my $person =
1109     Person-&gt;new( first_name =&gt; 'Dave' );
1110
1111 $person-&gt;first_name('Stevan'); # dies</code></pre>
1112
1113 </div>
1114
1115 <div class="slide">
1116   <h1>There is More to Come</h1>
1117
1118   <ul>
1119     <li>Attributes have a <em>lot</em> of features</li>
1120   </ul>
1121 </div>
1122
1123 <div class="slide">
1124   <h1>Cleaning Up Moose Droppings</h1>
1125
1126   <pre><code>package Person;
1127 use Moose;
1128
1129 # true
1130 Person-&gt;can('extends');</code></pre>
1131
1132   <ul>
1133     <li>Not very hygienic</li>
1134   </ul>
1135 </div>
1136
1137 <div class="slide">
1138   <h1>Cleaning Up Moose Droppings</h1>
1139
1140   <pre><code>package Person;
1141 use Moose;
1142
1143 ...
1144
1145 <span class="highlight">no Moose;</span>
1146
1147 # false
1148 Person-&gt;can('extends');</code></pre>
1149 </div>
1150
1151 <div class="slide">
1152   <h1>Cleaning Up Moose Droppings</h1>
1153
1154   <pre><code>package Person;
1155 <span class="highlight">use namespace::autoclean;</span>
1156 use Moose;
1157
1158 ...
1159
1160 # false
1161 Person-&gt;can('extends');</code></pre>
1162 </div>
1163
1164 <div class="slide">
1165   <h1>No Moose</h1>
1166
1167   <ul>
1168     <li>Cleaning up is a best practice</li>
1169     <li>Say <code>no Moose</code> at the end of a package</li>
1170     <li>Or <code>use namespace::autoclean</code> at the top</li>
1171     <li>Just do it</li>
1172   </ul>
1173 </div>
1174
1175 <div class="slide">
1176   <h1>Immutability</h1>
1177
1178   <ul>
1179     <li><span style="font-family: URW Chancery L; font-size: 120%">Stevan's Incantation of Fleet-Footedness</span></li>
1180   </ul>
1181
1182   <pre><code>package Person;
1183 use Moose;
1184
1185 <span class="highlight">__PACKAGE__-&gt;meta-&gt;make_immutable;</span></code></pre>
1186 </div>
1187
1188 <div class="slide">
1189   <h1>What <code>make_immutable</code> does</h1>
1190
1191   <ul>
1192     <li>Magic</li>
1193     <li>Uses <code>eval</code> to "inline" a constructor</li>
1194     <li>Memoizes a lot of meta-information</li>
1195     <li>Makes loading your class slower</li>
1196     <li>Makes object creation <em>much</em> faster</li>
1197   </ul>
1198 </div>
1199
1200 <div class="slide">
1201   <h1>When to Immutabilize?</h1>
1202
1203   <ul>
1204     <li><em>Almost</em> always</li>
1205     <li>Startup time vs execution time</li>
1206   </ul>
1207 </div>
1208
1209 <div class="slide">
1210   <h1>Classes Summary</h1>
1211
1212   <ul>
1213     <li><code>use Moose</code></li>
1214     <li><code>Class-&gt;meta</code></li>
1215     <li><code>Moose::Object</code> base class</li>
1216     <li><code>extends</code>, <code>override</code>, and <code>super</code></li>
1217     <li>Simple attributes: <code>has</code>, <code>is&nbsp;=&gt;&nbsp;'ro'</code>, &amp; <code>is&nbsp;=&gt;&nbsp;'rw'</code></li>
1218     <li><code>no Moose</code></li>
1219     <li><code>__PACKAGE__-&gt;meta-&gt;make_immutable</code></li>
1220   </ul>
1221 </div>
1222
1223 <div class="slide">
1224   <h1>Questions?</h1>
1225 </div>  
1226
1227 <div class="slide">
1228   <h1>Exercises</h1>
1229
1230   <pre># cd exercises
1231
1232 # perl bin/prove -lv t/00-prereq.t
1233
1234 # perl install-moose (if needed)
1235
1236 # perl bin/prove -lv t/01-classes.t
1237
1238 # edit lib/Person.pm and lib/Employee.pm
1239
1240 Iterate til this passes all its tests</pre>
1241 </div>
1242
1243 <div class="slide fake-slide0">
1244   <h1>Part 2: Roles</h1>
1245 </div>
1246
1247 <div class="slide">
1248   <h1>Just What Is a Role?</h1>
1249
1250   <ul>
1251     <li>Mixin? Interface? Trait?</li>
1252     <li>Yes ... and more!</li>
1253   </ul>
1254 </div>
1255
1256 <div class="slide">
1257   <h1>Roles - State <strong>and</strong> Behavior</h1>
1258
1259   <pre><code>package HasPermissions;
1260 use Moose::Role;
1261 <span class="current incremental"># state
1262 has access_level =&gt; ( is =&gt; 'rw' );</span>
1263
1264 <span class="incremental"># behavior
1265 sub can_access {
1266     my $self     = shift;
1267     my $required = shift;
1268
1269     return $self-&gt;access_level
1270              &gt;= $required;
1271 }</span></code></pre>
1272
1273 </div>
1274
1275 <div class="slide">
1276   <h1>Roles Can Define Interfaces</h1>
1277
1278   <pre><code>package Printable;
1279 use Moose::Role;
1280
1281 requires 'as_string';</code></pre>
1282 </div>
1283
1284 <div class="slide">
1285   <h1>Roles Can Do All Three</h1>
1286
1287   <pre><code>package Printable;
1288 use Moose::Role;
1289
1290 requires 'as_string';
1291
1292 has has_been_printed =&gt; ( is =&gt; 'rw'  );
1293
1294 sub print {
1295     my $self = shift;
1296     print $self-&gt;as_string;
1297     $self-&gt;has_been_printed(1);
1298 }</code></pre>
1299 </div>
1300
1301 <div class="slide">
1302   <h1>Classes Consume Roles</h1>
1303
1304   <pre><code>package Person;
1305 use Moose;
1306
1307 with 'HasPermissions';</code></pre>
1308 </div>
1309
1310 <div class="slide">
1311   <h1>Classes Consume Roles</h1>
1312
1313 <pre><code>my $person = Person-&gt;new(
1314     first_name   =&gt; 'Kenichi',
1315     last_name    =&gt; 'Asai',
1316     access_level =&gt; 42,
1317 );
1318
1319 print $person-&gt;full_name
1320     . ' has '
1321     . $person-&gt;can_access(42)
1322         ? 'great power'
1323         : 'little power';</code></pre>
1324 </div>
1325
1326 <div class="slide">
1327   <h1>Roles in Practice</h1>
1328
1329   <ul>
1330     <li>Consuming a role =~ inlining the role</li>
1331   </ul>
1332 </div>
1333
1334 <div class="slide">
1335   <h1>In Other Words ...</h1>
1336
1337 <pre><code>package Person;
1338 use Moose;
1339
1340 <span class="highlight">with 'Printable';</span></code></pre>
1341 </div>
1342
1343 <div class="slide">
1344   <h1>In Other Words ...</h1>
1345
1346 <pre><code>package Person;
1347 use Moose;
1348
1349 <span class="delete">with 'Printable';</span>
1350
1351 <span class="highlight">has has_been_printed =&gt; ( is =&gt; 'rw'  );
1352
1353 sub print {
1354     my $self = shift;
1355     print $self-&gt;as_string;
1356     $self-&gt;has_been_printed(1);
1357 }</span></code></pre>
1358 </div>
1359
1360 <div class="slide">
1361   <h1>Except</h1>
1362
1363   <ul>
1364     <li>Role consumption is introspectable</li>
1365   </ul>
1366
1367   <pre><code>if ( Person-&gt;does('Printable') ) { ... }
1368
1369 # or ...
1370
1371 Person-&gt;meta-&gt;does_role('Printable')</code></pre>
1372
1373 </div>
1374
1375 <div class="slide">
1376   <h1>These Names Are the Same</h1>
1377
1378   <ul>
1379     <li>What if a role and class define the same method?</li>
1380     <li>A class's <em>local</em> methods win over the role's</li>
1381     <li>The role's methods win over the class's <em>inherited</em> methods</li>
1382   </ul>
1383 </div>
1384
1385 <div class="slide">
1386   <h1>Conflicts Between Roles</h1>
1387
1388   <ul>
1389     <li>Two roles with a method of the same name</li>
1390     <li>Generates a compile-time error when consumed by a class</li>
1391   </ul>
1392 </div>
1393
1394 <div class="slide">
1395   <h1>Conflict Example</h1>
1396
1397   <pre><code>package IsFragile;
1398 use Moose::Role;
1399
1400 sub break { ... }
1401
1402 package CanBreakdance;
1403 use Moose::Role;
1404
1405 sub break { ... }</code></pre>
1406 </div>
1407
1408 <div class="slide">
1409   <h1>Conflict Example</h1>
1410
1411   <pre><code>package FragileDancer;
1412 use Moose;
1413
1414 <span class="highlight">with 'IsFragile', 'CanBreakdance';</span></code></pre>
1415
1416   <ul>
1417     <li>Only one <code>with</code>!</li>
1418   </ul>
1419 </div>
1420
1421 <div class="slide">
1422   <h1>Conflict Resolution</h1>
1423
1424   <ul>
1425     <li>The consuming class must resolve the conflict by implementing the method</li>
1426     <li>Can use some combination of method exclusion and aliasing</li>
1427   </ul>
1428 </div>
1429
1430 <div class="slide">
1431   <h1>Conflicts Are a Smell</h1>
1432
1433   <ul>
1434     <li>Roles are about semantics!</li>
1435     <li>Roles have a <em>meaning</em></li>
1436     <li>Method name conflicts smell like bad design</li>
1437   </ul>
1438 </div>
1439
1440 <div class="slide">
1441   <h1>Roles With Roles</h1>
1442
1443   <pre><code>package Comparable;
1444 use Moose::Role;
1445
1446 requires 'compare';</code></pre>
1447 </div>
1448
1449 <div class="slide">
1450   <h1>Roles With Roles</h1>
1451
1452   <pre><code>package TestsEquality;
1453 use Moose::Role;
1454
1455 with 'Comparable';
1456
1457 sub is_equal {
1458     my $self = shift;
1459     return $self-&gt;compare(@_) == 0;
1460 }</code></pre>
1461 </div>
1462
1463 <div class="slide">
1464   <h1>And then ...</h1>
1465
1466   <pre><code>package Integer;
1467 use Moose;
1468
1469 with 'TestsEquality';
1470
1471 # Satisfies the Comparable role
1472 sub compare { ... }
1473
1474 Integer-&gt;does('TestsEquality'); # true
1475 Integer-&gt;does('Comparable'); # also true!</code></pre>
1476 </div>
1477
1478 <div class="slide">
1479   <h1>Name Conflicts Between Roles</h1>
1480
1481   <pre><code>package HasSubProcess;
1482 use Moose::Role;
1483
1484 <span class="highlight">sub execute { ... }</span>
1485
1486 package Killer;
1487 use Moose::Role;
1488
1489 with 'HasSubProcess';
1490
1491 <span class="highlight">sub execute { ... }</span></code></pre>
1492 </div>
1493
1494 <div class="slide">
1495   <h1>Delayed Conflict</h1>
1496
1497   <pre><code>package SysadminAssassin;
1498 with 'Killer';</code></pre>
1499
1500   <ul>
1501     <li><code>SysadminAssassin</code> must implement its own <code>execute</code></li>
1502     <li>But loading the <code>Killer</code> role by itself does not cause an error</li>
1503   </ul>
1504 </div>
1505
1506 <div class="slide">
1507   <h1>Roles as Interfaces</h1>
1508
1509   <ul>
1510     <li>Roles can <code>require</code> methods of their consumers</li>
1511     <li>Compile-time checks</li>
1512     <li>Method must exist when the role is consumed</li>
1513   </ul>
1514 </div>
1515
1516 <div class="slide">
1517   <h1>The Attribute Gotcha</h1>
1518
1519 <pre><code>package HasSize;
1520 use Moose::Role;
1521
1522 <span class="current incremental">requires 'size';</span>
1523
1524 package Shirt;
1525 use Moose;
1526
1527 <span class="incremental">with 'HasSize';
1528
1529 has size => ( is => 'ro' );</span></code></pre>
1530 </div>
1531
1532 <div class="slide">
1533   <h1>The Attribute Gotcha Workaround</h1>
1534
1535   <pre><code>package HasSize;
1536 use Moose::Role;
1537
1538 requires 'size';
1539
1540 package Shirt;
1541 use Moose;
1542
1543 has size => ( is => 'ro' );
1544
1545 with 'HasSize';</code></pre>
1546 </div>
1547
1548 <div class="slide">
1549   <h1>Compile-time Is a Lie</h1>
1550
1551   <ul>
1552     <li>Really, it's <em>package load</em> time</li>
1553     <li>That's run-time, but before the "real" run-time</li>
1554     <li>Moose does not rewire Perl, it's just sugar!</li>
1555     <li>(but <code>MooseX::Declare</code> <em>does</em> rewire Perl)</li>
1556   </ul>
1557 </div>
1558
1559 <div class="slide">
1560   <h1>Enforcing Roles</h1>
1561
1562   <pre><code>package Comparison;
1563 use Moose;
1564
1565 has [ 'left', 'right' ] => (
1566     is   => 'ro',
1567     <span class="highlight">does => 'Comparable',</span>
1568 );
1569 </code></pre>
1570
1571   <ul>
1572     <li>A sneak peek at type constraints</li>
1573   </ul>
1574 </div>
1575
1576
1577 <div class="slide">
1578   <h1>Roles Can Be Applied to Objects</h1>
1579
1580   <pre><code>use Moose::Util qw( apply_all_roles );
1581
1582 my $fragile_person = Person-&gt;new( ... );
1583 apply_all_roles( $fragile_person,
1584                  'IsFragile' );</code></pre>
1585
1586   <ul>
1587     <li>Does not change the <code>Person</code> class</li>
1588     <li>Works with non-Moose classes, great for monkey-patching!</li>
1589   </ul>
1590 </div>
1591
1592 <div class="slide">
1593   <h1>Roles Are Dirty Too</h1>
1594
1595   <ul>
1596     <li>Once again, clean up those Moose droppings</li>
1597   </ul>
1598
1599   <pre><code>package Comparable;
1600 use Moose::Role;
1601
1602 requires 'compare';
1603
1604 <span class="highlight">no Moose::Role;</span></code></pre>
1605
1606   <ul>
1607     <li>But roles cannot be made immutable</li>
1608   </ul>
1609 </div>
1610
1611 <div class="slide">
1612   <h1>The Zen of Roles</h1>
1613
1614   <ul>
1615     <li>Roles represent discrete units of ...
1616       <ul>
1617         <li>state</li>
1618         <li>behavior</li>
1619         <li>interface</li>
1620       </ul>
1621     </li>
1622     <li>Roles are shareable between unrelated classes</li>
1623     <li>Roles are what a class <em>does</em>, not what it <em>is</em></li>
1624     <li>Roles <em>add</em> functionality, inheritance <em>specializes</em></li>
1625   </ul>
1626 </div>
1627
1628 <div class="slide">
1629   <h1>Abstract Examples</h1>
1630
1631   <ul>
1632     <li>Human <em>@ISA</em> Animal</li>
1633     <li>Human <em>does</em> Toolmaker (as <em>does</em> Chimpanzee)</li>
1634     <li>Car <em>@ISA</em> Vehicle</li>
1635     <li>Car <em>does</em> HasEngine</li>
1636   </ul>
1637 </div>
1638
1639 <div class="slide">
1640   <h1>Real Examples</h1>
1641
1642   <ul>
1643     <li>Objects representing SQL database components and queries
1644       <ul>
1645         <li>Schema, Table, Column, ColumnAlias</li>
1646         <li>Select, Insert, Update, Delete</li>
1647       </ul>
1648     </li>
1649   </ul>
1650 </div>
1651
1652 <div class="slide">
1653   <h1>Real Examples</h1>
1654
1655   <ul>
1656     <li>Column and ColumnAlias both <em>do</em> ColumnLike</li>
1657     <li>ColumnLike things can be used in certain parts of queries</li>
1658     <li>All queries <em>do</em> HasWhereClause</li>
1659     <li>Select <em>does</em> Comparable and Selectable (for subselects)</li>
1660     <li>A where clause requires its components to <em>do</em> Comparable</li>
1661   </ul>
1662 </div>
1663
1664 <div class="slide">
1665   <h1>Roles Summary</h1>
1666
1667   <ul>
1668     <li>Roles can define an interface with <code>requires</code></li>
1669     <li>Roles can have state (attributes) and behavior (methods)</li>
1670     <li>Roles can mix interface, state, &amp; behavior</li>
1671     <li>Roles are composed (flattened) into classes</li>
1672     <li>Roles can do other roles</li>
1673     <li>Roles can be used as a type in APIs (must do Comparable)</li>
1674   </ul>
1675 </div>
1676
1677 <div class="slide">
1678   <h1>Questions?</h1>
1679 </div>  
1680
1681 <div class="slide">
1682   <h1>Exercises</h1>
1683
1684   <pre># cd exercises
1685 # perl bin/prove -lv t/02-roles.t
1686
1687 Iterate til this passes all its tests</pre>
1688 </div>
1689
1690 <div class="slide fake-slide0">
1691   <h1>Part 3: Basic Attributes</h1>
1692 </div>
1693
1694 <div class="slide">
1695   <h1>Attributes Are Huge</h1>
1696
1697   <ul>
1698     <li>Moose's biggest feature</li>
1699     <li>The target of <em>many</em> MooseX modules</li>
1700   </ul>
1701 </div>
1702
1703 <div class="slide">
1704   <h1>Quick Review</h1>
1705
1706   <ul>
1707     <li>Declared with <code>has</code></li>
1708     <li>Read-only or read-write</li>
1709   </ul>
1710
1711   <pre><code>package Shirt;
1712 use Moose;
1713
1714 has 'color'     =&gt; ( is =&gt; 'ro' );
1715 has 'is_ripped' =&gt; ( is =&gt; 'rw' );</code></pre>
1716 </div>
1717
1718 <div class="slide">
1719   <h1>Required-ness</h1>
1720
1721   <ul>
1722     <li>Required means "must be passed to the constructor"</li>
1723     <li>But can be <code>undef</code></li>
1724   </ul>
1725 </div>
1726
1727 <div class="slide">
1728   <h1>Required-ness</h1>
1729
1730   <pre><code>package Person;
1731 use Moose;
1732
1733 has first_name =&gt; (
1734     is       =&gt; 'ro',
1735     <span class="current incremental">required =&gt; 1,</span>
1736 );
1737
1738 <span class="incremental">Person-&gt;new( first_name =&gt; undef ); # ok
1739 Person-&gt;new(); # kaboom</span></code></pre>
1740 </div>
1741
1742 <div class="slide">
1743   <h1>Default and Builder</h1>
1744
1745   <ul>
1746     <li>Attributes can have defaults</li>
1747     <li>Simple non-reference scalars (number, string)</li>
1748     <li>Subroutine reference</li>
1749     <li>A builder method</li>
1750   </ul>
1751 </div>
1752
1753 <div class="slide">
1754   <h1>Default</h1>
1755
1756   <ul>
1757     <li>Can be a non-reference scalar (including <code>undef</code>)</li>
1758   </ul>
1759
1760   <pre><code>package Person;
1761 use Moose;
1762
1763 has bank =&gt; (
1764     is      =&gt; 'rw',
1765     default =&gt; 'Spire FCU',
1766 );</code></pre>
1767 </div>
1768
1769 <div class="slide">
1770   <h1>Default</h1>
1771
1772   <ul>
1773     <li>Can be a subroutine reference</li>
1774   </ul>
1775
1776   <pre><code>package Person;
1777 use Moose;
1778
1779 has bank =&gt; (
1780     is      =&gt; 'rw',
1781     default =&gt;
1782         sub { Bank-&gt;new(
1783                   name =&gt; 'Spire FCU' ) },
1784 );</code></pre>
1785 </div>
1786
1787 <div class="slide">
1788   <h1>Subroutine Reference Default</h1>
1789
1790   <ul>
1791     <li>Called as a method on the object</li>
1792     <li>Called anew for each object</li>
1793   </ul>
1794 </div>
1795
1796 <div class="slide">
1797   <h1>Why No Other Reference Types?</h1>
1798
1799   <pre><code>package Person;
1800 use Moose;
1801
1802 has bank =&gt; (
1803     is      =&gt; 'rw',
1804     <span class="wrong">default =&gt; Bank-&gt;new(
1805                    name =&gt; 'Spire FCU' ),</span>
1806 );</code></pre>
1807
1808   <ul>
1809     <li>Now <strong>every</strong> person shares the <strong>same</strong> Bank object!</li>
1810   </ul>
1811 </div>
1812
1813 <div class="slide">
1814      <h1>Defaulting to an Empty Reference</h1>
1815
1816   <pre><code>package Person;
1817 use Moose;
1818
1819 has packages =&gt; (
1820     is      =&gt; 'rw',
1821     default =&gt; <span class="highlight">sub { [] }</span>,
1822 );</code></pre>
1823 </div>
1824
1825 <div class="slide">
1826   <h1>What if I Want to Share?</h1>
1827
1828   <pre><code>package Person;
1829 use Moose;
1830
1831 my $highlander_bank =
1832     Bank-&gt;new(
1833         name =&gt; 'Clan MacLeod Trust' );
1834
1835 has bank =&gt; (
1836     is      =&gt; 'rw',
1837     default =&gt; sub { $highlander_bank },
1838 );</code></pre>
1839 </div>
1840
1841 <div class="slide">
1842   <h1>Builder</h1>
1843
1844   <ul>
1845     <li>A method <em>name</em></li>
1846     <li>When called, this method returns the default value</li>
1847   </ul>
1848 </div>
1849
1850 <div class="slide">
1851   <h1>Builder</h1>
1852
1853   <pre><code>package Person;
1854 use Moose;
1855
1856 has bank =&gt; (
1857     is      =&gt; 'rw',
1858     builder =&gt; '_build_bank',
1859 );
1860
1861 sub _build_bank {
1862     my $self = shift;
1863     return Bank-&gt;new(
1864         name => 'Spire FCU' );
1865 }</code></pre>
1866 </div>
1867
1868 <div class="slide">
1869   <h1>Default vs Builder</h1>
1870
1871   <ul>
1872     <li>Use default for simple scalars</li>
1873     <li>Use default to return empty references</li>
1874     <li>Use default for <em>very</em> trivial subroutine references</li>
1875     <li>Use builder for everything else</li>
1876   </ul>
1877 </div>
1878
1879 <div class="slide">
1880   <h1>Builder Bonuses</h1>
1881
1882   <ul>
1883     <li>Can be overridden and method modified, because it's called by <em>name</em></li>
1884     <li>Roles can require a builder</li>
1885   </ul>
1886 </div>
1887       
1888 <div class="slide">
1889   <h1>Role Requires Builder</h1>
1890
1891   <pre><code>package HasBank;
1892 use Moose::Role;
1893
1894 requires '_build_bank';
1895
1896 has bank =&gt; (
1897     is      =&gt; 'ro',
1898     builder =&gt; '_build_bank',
1899 );</code></pre>
1900 </div>
1901
1902 <div class="slide">
1903   <h1>Lazy, Good for Nothin' Attributes</h1>
1904
1905   <ul>
1906     <li>Normally, defaults are generated during object construction</li>
1907     <li>This can be expensive</li>
1908     <li>We want to default to <code>$self-&gt;size * 2</code>, but attribute initialization order is unpredictable</li>
1909     <li>Use lazy attributes!</li>
1910   </ul>
1911 </div>
1912
1913 <div class="slide">
1914   <h1>The Power of Dynamic Defaults</h1>
1915
1916   <pre><code>package Person;
1917 use Moose;
1918
1919 has shoe_size =&gt; (
1920     is       =&gt; 'ro',
1921     required =&gt; 1,
1922 );</code></pre>
1923 </div>
1924
1925 <div class="slide">
1926   <h1>The Power of Dynamic Defaults</h1>
1927
1928   <pre><code>has shoes =&gt; (
1929     is      =&gt; 'ro',
1930     <span class="highlight">lazy    =&gt; 1,</span>
1931     builder => '_build_shoes',
1932 );
1933
1934 sub _build_shoes {
1935     my $self = shift;
1936
1937     return Shoes-&gt;new(
1938         size =&gt; <span class="highlight">$self-&gt;shoe_size</span> );
1939 }</code></pre>
1940 </div>
1941
1942 <div class="slide">
1943   <h1>Lazy is Good</h1>
1944
1945   <ul>
1946     <li>Lazy defaults are executed when the attribute is read</li>
1947     <li>Can see other object attributes</li>
1948     <li>Still need to watch out for circular laziness</li>
1949   </ul>
1950 </div>    
1951
1952 <div class="slide">
1953   <h1>Clearer and Predicate</h1>
1954
1955   <ul>
1956     <li>Attributes can have a value, including <code>undef</code>, or not</li>
1957     <li>Can clear the value with a clearer method</li>
1958     <li>Can check for the existence of a value with a predicate method</li>
1959     <li>By default, these methods are not created</li>
1960   </ul>
1961 </div>
1962
1963 <div class="slide">
1964   <h1>Clearer and Predicate</h1>
1965
1966   <pre><code>package Person;
1967 use Moose;
1968
1969 has account =&gt; (
1970     is        =&gt; 'ro',
1971     lazy      =&gt; 1,
1972     builder   =&gt; '_build_account',
1973     <span class="highlight">clearer   =&gt; '_clear_account',
1974     predicate =&gt; 'has_account',</span>
1975 );</code></pre>
1976 </div>
1977
1978 <div class="slide">
1979   <h1>Clearer and Lazy Defaults</h1>
1980
1981   <ul>
1982     <li>Lazy defaults are good for computed attributes</li>
1983     <li>Clear the attribute when the source data changes</li>
1984     <li>Recalculated at next access</li>
1985   </ul>
1986 </div>
1987
1988 <div class="slide">
1989   <h1>Renaming constructor arguments</h1>
1990
1991   <ul>
1992     <li>By default, constructor names = attribute names</li>
1993     <li>Use <code>init_arg</code> to change this</li>
1994     <li>Set <code>init_arg =&gt; undef</code> to make it unconstructable</li>
1995   </ul>
1996 </div>
1997
1998 <div class="slide">
1999   <h1>Some <code>init_arg</code> examples</h1>
2000
2001   <pre><code>package Person;
2002 use Moose;
2003
2004 has shoe_size => (
2005     is       =&gt; 'ro',
2006     <span class="highlight">init_arg =&gt; 'foot_size',</span>
2007 );
2008
2009 Person-&gt;new( <span class="wrong">shoe_size =&gt; 13</span> );
2010
2011 my $person =
2012     Person-&gt;new( <span class="right">foot_size =&gt; 13</span> );
2013 print $person-&gt;shoe_size;</code></pre>
2014 </div>
2015
2016 <div class="slide">
2017   <h1>Some <code>init_arg</code> examples</h1>
2018
2019 <pre><code>package Person;
2020 use Moose;
2021
2022 has shoes => (
2023     is       =&gt; 'ro',
2024     <span class="highlight">init_arg =&gt; undef,</span>
2025 );
2026
2027 Person-&gt;new( <span class="wrong">shoes =&gt; Shoes-&gt;new</span> );</code></pre>
2028 </div>
2029
2030 <div class="slide">
2031   <h1>Why Set <code>init_arg =&gt; undef</code>?</h1>
2032
2033   <ul>
2034     <li>Use this with a lazy default for attributes-as-cache</li>
2035     <li>Compute the value as needed</li>
2036     <li>Ensure that it is always generated correctly (not set by constructor)</li>
2037     <li>Use triggers or method modifiers (coming soon) to clear the value</li>
2038   </ul>
2039 </div>
2040
2041 <div class="slide">
2042   <h1>Attribute Inheritance</h1>
2043
2044   <ul>
2045     <li>By default, subclasses inherit attribute as-is</li>
2046     <li>Can change some attribute parameters in subclasses
2047       <ul>
2048         <li>default</li>
2049         <li>builder</li>
2050         <li>required</li>
2051         <li>lazy</li>
2052         <li>others we've not yet covered</li>
2053       </ul>
2054     </li>
2055   </ul>
2056 </div>   
2057
2058 <div class="slide">
2059   <h1>Attribute Inheritance Example</h1>
2060
2061   <pre><code>package Employee;
2062 use Moose;
2063
2064 extends 'Person';
2065
2066 has '<span class="highlight">+first_name</span>' =&gt; (
2067     default =&gt; 'Joe',
2068 );</code></pre>
2069 </div>
2070
2071 <div class="slide">
2072   <h1>Attribute Inheritance Warning</h1>
2073
2074   <ul>
2075     <li>An attribute is a contract about a class's API</li>
2076     <li>Don't break that contract in a subclass</li>
2077     <li>Especially important in the context of types</li>
2078   </ul>
2079 </div>
2080
2081 <div class="slide">
2082   <h1>Changing Accessor Names</h1>
2083
2084   <pre><code>package Person;
2085 use Moose;
2086
2087 has first_name =&gt; (
2088     <span class="highlight">accessor</span> =&gt; 'first_name',
2089 );</code></pre>
2090
2091   <ul>
2092     <li>The long-hand version of <code>is =&gt; 'rw'</code></li>
2093   </ul>
2094 </div>
2095
2096 <div class="slide">
2097   <h1>Changing Accessor Names</h1>
2098
2099   <pre><code>package Person;
2100 use Moose;
2101
2102 has first_name =&gt; (
2103     <span class="highlight">reader</span> =&gt; 'first_name',
2104     <span class="highlight">writer</span> =&gt; undef,
2105 );</code></pre>
2106
2107   <ul>
2108     <li>The long-hand version of <code>is =&gt; 'ro'</code></li>
2109   </ul>
2110 </div>
2111
2112
2113 <div class="slide">
2114   <h1>Changing Accessor Names</h1>
2115
2116   <pre><code>package Person;
2117 use Moose;
2118
2119 has first_name =&gt; (
2120     <span class="highlight">reader</span> =&gt; 'get_first_name',
2121     <span class="highlight">writer</span> =&gt; 'set_first_name',
2122 );</code></pre>
2123 </div>
2124
2125 <div class="slide">
2126   <h1>Changing Accessor Names</h1>
2127
2128   <pre><code>package Person;
2129 use Moose;
2130
2131 has first_name =&gt; (
2132     <span class="highlight">is</span>     =&gt; 'rw',
2133     <span class="highlight">writer</span> =&gt; '_first_name',
2134 );</code></pre>
2135
2136   <ul>
2137     <li>Can also mix-and-match <code>is</code> and explicit names</li>
2138   </ul>
2139 </div>
2140
2141 <div class="slide">
2142   <h1>ETOOMUCHTYPING</h1>
2143
2144   <ul>
2145     <li><code>MooseX::FollowPBP</code><br /><code>get_foo</code> and <code>set_foo</code></li>
2146     <li><code>MooseX::SemiAffordanceAccessor</code><br /><code>foo</code> and <code>set_foo</code></li>
2147   </ul>
2148 </div>
2149
2150 <div class="slide">
2151   <h1>ETOOMUCHTYPING</h1>
2152
2153   <pre><code>package Person;
2154 use Moose;
2155 <span class="highlight">use MooseX::SemiAffordanceAccessor;</span>
2156
2157 has first_name =&gt; (
2158     is =&gt; 'rw',
2159 );</code></pre>
2160
2161   <ul>
2162     <li>Creates <code>first_name</code> and <code>set_first_name</code></li>
2163   </ul>
2164 </div>
2165
2166 <div class="slide">
2167   <h1>Basic Attributes Summary</h1>
2168
2169   <ul>
2170     <li>Attributes can be <code>required</code></li>
2171     <li>Attributes can have a <code>default</code> or <code>builder</code></li>
2172     <li>Attributes with a default or builder can be <code>lazy</code></li>
2173     <li>Attributes can have a <code>clearer</code> and/or <code>predicate</code></li>
2174   </ul>
2175 </div>
2176
2177 <div class="slide">
2178   <h1>Basic Attributes Summary</h1>
2179
2180   <ul>
2181     <li>An attribute's constructor name can be changed with <code>init_arg</code></li>
2182     <li>A subclass can alter its parents' attributes</li>
2183     <li>Attribute accessor names can be changed</li>
2184   </ul>
2185 </div>
2186
2187 <div class="slide">
2188   <h1>Questions?</h1>
2189 </div>  
2190
2191 <div class="slide">
2192   <h1>Exercises</h1>
2193
2194   <pre># cd exercises
2195 # perl bin/prove -lv \
2196       t/03-basic-attributes.t
2197
2198 Iterate til this passes all its tests</pre>
2199 </div>
2200
2201 <div class="slide fake-slide0">
2202   <h1>Part 4: Method Modifiers</h1>
2203 </div>
2204
2205 <div class="slide">
2206   <h1>What is a Method Modifier</h1>
2207
2208   <ul>
2209     <li>Apply to an existing method</li>
2210     <li>... that comes from a parent class, the current class, or a role</li>
2211     <li>Roles can provide modifiers that are applied at composition time</li>
2212   </ul>
2213 </div>
2214
2215 <div class="slide">
2216   <h1>What Are Method Modifiers For?</h1>
2217
2218   <ul>
2219     <li>"Inject" behavior</li>
2220     <li>Add behavior to generated methods (accessors, delegations)</li>
2221     <li>Added from a role, can modify existing behavior</li>
2222   </ul>
2223 </div>
2224
2225 <div class="slide">
2226   <h1>Before and After</h1>
2227
2228   <ul>
2229     <li>Simplest modifiers - <code>before</code> and <code>after</code></li>
2230     <li>Guess when they run!</li>
2231   </ul>
2232 </div>
2233
2234 <div class="slide">
2235   <h1>Uses for <code>before</code></h1>
2236
2237   <ul>
2238     <li>As a pre-call check</li>
2239   </ul>
2240
2241   <pre><code>package Person;
2242 use Moose;
2243
2244 before work =&gt; sub {
2245     my $self = shift;
2246     die 'I have no job!'
2247         unless $self-&gt;has_title;
2248 };</code></pre>
2249 </div>    
2250
2251 <div class="slide">
2252   <h1>Uses for <code>before</code></h1>
2253
2254   <ul>
2255     <li>Logging/Debugging</li>
2256   </ul>
2257
2258   <pre><code>package Person;
2259 use Moose;
2260
2261 before work =&gt; sub {
2262     my $self = shift;
2263     return unless $DEBUG;
2264
2265     warn "Called work on ",
2266          $self-&gt;full_name,
2267          "with the arguments: [@_]\n";
2268 };</code></pre>
2269 </div>    
2270
2271 <div class="slide">
2272   <h1>Uses for <code>after</code></h1>
2273
2274   <ul>
2275     <li>Also works for logging/debugging</li>
2276     <li>Post-X side-effects (recording audit info)</li>
2277   </ul>
2278
2279   <pre><code>package Person;
2280 use Moose;
2281
2282 after work =&gt; sub {
2283     my $self = shift;
2284     $self-&gt;work_count(
2285         $self-&gt;work_count + 1 );
2286 };</code></pre>
2287 </div>
2288
2289 <div class="slide">
2290   <h1>Other Uses</h1>
2291
2292   <ul>
2293     <li>Modifiers are useful for adding behavior to generated methods</li>
2294   </ul>
2295 </div>
2296
2297 <div class="slide">
2298   <h1>More Modifier Examples</h1>
2299
2300   <pre><code>has password =&gt; (
2301      is      =&gt; 'rw',
2302      clearer =&gt; 'clear_password',
2303 );
2304 has hashed_password =&gt; (
2305      is      =&gt; 'ro',
2306      builder =&gt; '_build_hashed_password',
2307      clearer =&gt; '_clear_hashed_password',
2308 );
2309 after clear_password =&gt; sub {
2310     my $self = shift;
2311     $self-&gt;_clear_hashed_password;
2312 };</code></pre>
2313 </div>
2314
2315 <div class="slide">
2316   <h1><code>before</code> and <code>after</code> Limitations</h1>
2317
2318   <ul>
2319     <li>Cannot alter method parameters</li>
2320     <li>Cannot alter return value</li>
2321     <li>But <strong>can</strong> throw an exception</li>
2322   </ul>
2323 </div>
2324
2325 <div class="slide">
2326   <h1>The <code>around</code> Modifier</h1>
2327
2328   <ul>
2329     <li>The big gun</li>
2330     <li>Can alter parameters <strong>and/or</strong> return values</li>
2331     <li>Can skip calling the wrapped method entirely</li>
2332   </ul>
2333 </div>
2334
2335 <div class="slide">
2336   <h1>The power of <code>around</code></h1>
2337
2338   <pre><code>around insert =&gt; sub {
2339     my $orig = shift;
2340     my $self = shift;
2341
2342     $self-&gt;_validate_insert(@_);
2343
2344     my $new_user =
2345         $self-&gt;$orig(
2346             $self-&gt;_munge_insert(@_) );
2347
2348     $new_user-&gt;_assign_uri;
2349     return $new_user;
2350 };</code></pre>
2351 </div>
2352
2353 <div class="slide">
2354   <h1>Modifier Order</h1>
2355
2356   <ul>
2357     <li>Before runs in order from last to first</li>
2358     <li>After runs in order from first to last</li>
2359     <li>Around runs in order from last to first</li>
2360   </ul>
2361 </div>
2362
2363 <div class="slide">
2364   <h1>Modifier Order Illustrated</h1>
2365
2366 <pre>
2367 <span class="current incremental">before 2
2368  before 1</span>
2369   <span class="incremental">around 2
2370    around 1</span>
2371     <span class="incremental">wrapped method</span>
2372    <span class="incremental">around 1
2373   around 2</span>
2374  <span class="incremental">after 1
2375 after 2</span>
2376 </pre>
2377 </div>
2378
2379 <div class="slide">
2380   <h1>Modifiers in Roles</h1>
2381
2382   <ul>
2383     <li>Roles can use these modifiers</li>
2384     <li>Very powerful!</li>
2385   </ul>
2386 </div>
2387
2388 <div class="slide">
2389   <h1>Modifiers in Roles</h1>
2390
2391   <pre><code>package IsUnreliable;
2392 use Moose::Role;
2393
2394 <span class="highlight">requires 'run';
2395
2396 around run</span> =&gt; sub {
2397     my $orig = shift;
2398     my $self = shift;
2399
2400     return if rand(1) &lt; 0.5;
2401
2402     return $self-&gt;$orig(@_);
2403 };</code></pre>
2404 </div>
2405
2406 <div class="slide">
2407   <h1>Augment and Inner</h1>
2408
2409   <ul>
2410     <li>Inverted <code>super</code></li>
2411     <li>From least- to most-specific</li>
2412     <li>Like Mason's autohandler feature</li>
2413     <li>Grandparent to parent to child</li>
2414     <li>Not allowed in roles</li>
2415   </ul>
2416 </div>
2417
2418 <div class="slide">
2419   <h1>Augment and Inner</h1>
2420
2421   <pre class="medium"><code>package Document;
2422
2423 sub xml { '&lt;doc&gt;' . <span class="highlight">inner()</span> . '&lt;/doc&gt;' }
2424
2425 package Report;
2426 extends 'Document';
2427 <span class="highlight">augment xml</span> =&gt;
2428     sub { my $self = shift;
2429           $self-&gt;title() . <span class="highlight">inner()</span> . $self-&gt;summary() };
2430
2431 package TPSReport;
2432 extends 'Report';
2433 <span class="highlight">augment xml</span> =&gt;
2434     sub { my $self = shift;
2435           $self-&gt;tps_xml() . <span class="highlight">inner()</span> };</code></pre>
2436 </div>
2437
2438 <div class="slide">
2439   <h1>Augment and Inner</h1>
2440
2441   <ul>
2442     <li>When we call <code>$tps-&gt;xml</code> ...
2443       <ul>
2444         <li><code>Document-&gt;xml</code></li>
2445         <li><code>Report-&gt;xml</code></li>
2446         <li><code>TPSReport-&gt;xml</code></li>
2447       </ul>
2448     </li>
2449   </ul>
2450 </div>
2451
2452 <div class="slide">
2453   <h1>Augment and Inner Usage</h1>
2454
2455   <ul>
2456     <li>Call <code>inner()</code> to "fill in the blank"</li>
2457     <li>Requires designing for subclassing</li>
2458     <li>Call <code>inner()</code> in the terminal class, just in case</li>
2459   </ul>
2460 </div>
2461
2462 <div class="slide">
2463   <h1>Method Modifiers Summary</h1>
2464
2465   <ul>
2466     <li>Use <code>before</code> and <code>after</code> for ...
2467       <ul>
2468         <li>logging</li>
2469         <li>pre- or post-validation</li>
2470         <li>to add behavior to generated methods</li>
2471       </ul>
2472     </li>
2473     <li>These two modifiers cannot change parameters or return values</li>
2474   </ul>
2475 </div>
2476
2477 <div class="slide">
2478   <h1>Method Modifiers Summary</h1>
2479
2480   <ul>
2481     <li>Use <code>around</code> to ...
2482       <ul>
2483         <li>alter parameters passed to the original method</li>
2484         <li>alter the return value of the original method</li>
2485         <li>not call the original method at all (or call a <em>different</em> method)</li>
2486       </ul>
2487     </li>
2488   </ul>
2489 </div>
2490
2491 <div class="slide">
2492   <h1>Method Modifiers Summary</h1>
2493
2494   <ul>
2495     <li>When using modifiers in a role, require the modified method</li>
2496     <li>Use <code>augment</code> and <code>inner</code> to invert the normal subclassing flow ...
2497       <ul>
2498         <li>Least- to most-specific (parents to children)</li>
2499         <li>Build in "insertability" (stick more stuff in the "middle")</li>
2500       </ul>
2501     </li>
2502     <li>Always call <code>inner</code> in the most specific subclass to allow for future extension</li>
2503   </ul>
2504 </div>
2505
2506 <div class="slide">
2507   <h1>Questions?</h1>
2508 </div>  
2509
2510 <div class="slide">
2511   <h1>Exercises</h1>
2512
2513   <pre># cd exercises
2514 # perl bin/prove -lv \
2515       t/04-method-modifiers.t
2516
2517 Iterate til this passes all its tests</pre>
2518 </div>
2519
2520 <div class="slide fake-slide0">
2521   <h1>Part 5: Types</h1>
2522 </div>
2523
2524 <div class="slide">
2525   <h1>A Type System for Perl</h1>
2526
2527   <ul>
2528     <li>Sort of ...</li>
2529     <li><em>Variables</em> are not typed</li>
2530     <li>Attributes can have types</li>
2531     <li>MooseX modules let you define method signatures</li>
2532   </ul>
2533 </div>
2534
2535 <div class="slide">
2536   <h1>Components of a Moose Type</h1>
2537
2538   <ul>
2539     <li>A type is a name and a constraint</li>
2540     <li>Types have a hierarchy</li>
2541     <li>Constraints are cumulative from parents</li>
2542     <li>Types can have associated coercions</li>
2543   </ul>
2544 </div>
2545
2546 <div class="slide">
2547   <h1>Built-in Type Hierarchy</h1>
2548
2549   <pre>
2550 Any
2551 Item
2552     Bool
2553     Maybe[`a]
2554     Undef
2555     Defined
2556         Value
2557             Str
2558                 Num
2559                     Int
2560                 ClassName
2561                 RoleName
2562 </pre>
2563 </div>
2564
2565 <div class="slide">
2566   <h1>Built-in Type Hierarchy</h1>
2567
2568 <pre>
2569 (Item)
2570     (Defined)
2571         (Value)
2572         Ref
2573             ScalarRef
2574             ArrayRef[`a]
2575             HashRef[`a]
2576             CodeRef
2577             RegexpRef
2578             GlobRef
2579                 FileHandle
2580             Object
2581 </pre>
2582 </div>
2583
2584 <div class="slide">
2585   <h1>Bool</h1>
2586
2587   <h2>True</h2>
2588   <pre><code>1
2589 924.1
2590 'true'
2591 {}</code></pre>
2592
2593   <h2>False</h2>
2594   <pre><code>0
2595 0.0
2596 '0'
2597 undef</code></pre>
2598
2599   <ul>
2600     <li>Like Perl's <code>if ($foo)</code></li>
2601   </ul>
2602 </div>
2603
2604 <div class="slide">
2605   <h1>Value (and subtypes)</h1>
2606
2607   <ul>
2608     <li><code>Value</code> is true when <code>! ref $thing</code></li>
2609     <li><code>Value</code> and <code>Str</code> are effectively the same, but <code>Str</code> is more expressive</li>
2610     <li><code>Num</code> is true when a <code>$scalar</code> looks like a number</li>
2611     <li>An overloaded object which numifies does not pass the <code>Num</code> constraint!</li>
2612     <li>Perl 5's overloading is hopelessly broken</li>
2613   </ul>
2614 </div>
2615
2616 <div class="slide">
2617   <h1>ClassName and RoleName</h1>
2618
2619   <ul>
2620     <li>A string with a package name</li>
2621     <li>The package <strong>must already be loaded</strong></li>
2622   </ul>
2623 </div>
2624
2625 <div class="slide">
2626   <h1>Parameterizable Types</h1>
2627
2628   <ul>
2629     <li>What does <code>ArrayRef[`a]</code> mean?</li>
2630     <li><code>s/`a/Int/</code> (or <code>Str</code> or ...)</li>
2631     <li>When you use it you can write ...
2632       <ul>
2633         <li><code>ArrayRef</code> (== <code>ArrayRef[Item]</code>)</li>
2634         <li><code>ArrayRef[Str]</code></li>
2635         <li><code>ArrayRef[MyTypeName]</code></li>
2636         <li><code>ArrayRef[HashRef[Maybe[Int]]]</code></li>
2637       </ul>
2638     </li>
2639   </ul>
2640 </div>
2641
2642 <div class="slide">
2643   <h1>Maybe[`a]</h1>
2644
2645   <ul>
2646     <li>Maybe means either the named type or <code>undef</code></li>
2647     <li><code>Maybe[Int]</code> accepts integers or <code>undef</code></li>
2648   </ul>
2649 </div>
2650
2651 <div class="slide">
2652   <h1>Type Union</h1>
2653
2654   <ul>
2655     <li>This or that (or that or ...)</li>
2656     <li><code>Int | ArrayRef[Int]</code></li>
2657     <li>But use a coercion instead when possible</li>
2658     <li>Or use a <code>role_type</code>,  <code>duck_type</code>, or anything not a union</li>
2659     <li>A union is often a code smell</li>
2660   </ul>
2661 </div>
2662
2663 <div class="slide">
2664   <h1>Making Your Own Types</h1>
2665
2666   <pre><code>use Moose::Util::TypeConstraints;
2667
2668 <span class="incremental current">subtype 'PositiveInt',</span>
2669     <span class="incremental">as      'Int',</span>
2670     <span class="incremental">where   { $_ &gt; 0 },</span>
2671     <span class="incremental">message
2672         { "The value you provided ($_)"
2673           . " was not a positive int." };</span>
2674
2675 has size =&gt; (
2676     is  =&gt; 'ro',
2677     <span class="incremental">isa =&gt; 'PositiveInt',</span>
2678 );</code></pre>
2679 </div>
2680
2681 <div class="slide">
2682   <h1>Automatic Types</h1>
2683
2684   <ul>
2685     <li>Moose creates a type for every Moose class and role</li>
2686     <li>Unknown names are assumed to be classes</li>
2687   </ul>
2688 </div>
2689
2690 <div class="slide">
2691   <h1>Automatic Types</h1>
2692
2693   <pre><code>package Employee;
2694 use Moose;
2695
2696 has manager =&gt; (
2697     is  =&gt; 'rw',
2698     <span class="highlight">isa =&gt; 'Employee',</span>
2699 );
2700
2701 has start_date =&gt; (
2702     is  =&gt; 'ro',
2703     <span class="highlight">isa =&gt; 'DateTime',</span>
2704 );</code></pre>
2705 </div>
2706
2707 <div class="slide">
2708   <h1>Subtype Shortcuts - <code>class_type</code></h1>
2709
2710   <pre><code>use Moose::Util::TypeConstraints;
2711 class_type 'DateTime';</code></pre>
2712
2713 <hr />
2714
2715 <pre><code>subtype     'DateTime',
2716     as      'Object',
2717     where   { $_-&gt;isa('DateTime') },
2718     message { ... };</code></pre>
2719 </div>
2720
2721 <div class="slide">
2722   <h1>Subtype Shortcuts - <code>role_type</code></h1>
2723
2724   <pre><code>use Moose::Util::TypeConstraints;
2725 role_type 'Printable';</coe></pre>
2726
2727 <hr />
2728
2729 <pre><code>subtype 'Printable',
2730     as  'Object',
2731     where
2732         { Moose::Util::does_role(
2733               $_, 'Printable' ) },
2734     message { ... };</code></pre>
2735 </div>
2736
2737 <div class="slide">
2738   <h1>Subtype Shortcuts - <code>duck_type</code></h1>
2739
2740   <pre><code>use Moose::Util::TypeConstraints;
2741 duck_type Car =&gt; qw( run break_down );</code></pre>
2742
2743 <hr />
2744
2745 <pre><code>subtype 'Car',
2746     as      'Object',
2747     where   { all { $_-&gt;can($_) }
2748               qw( run break_down ) },
2749     message { ... };</code></pre>
2750 </div>
2751
2752 <div class="slide">
2753   <h1>Subtype Shortcuts - <code>enum</code></h1>
2754
2755   <pre><code>use Moose::Util::TypeConstraints;
2756 enum Color =&gt; qw( red blue green );</code></pre>
2757
2758 <hr />
2759
2760 <pre><code>my %ok = map { $_ =&gt; 1 }
2761              qw( red blue green );
2762
2763 subtype     'Color'
2764     as      'Str',
2765     where   { $ok{$_} },
2766     message { ... };</code></pre>
2767 </div>
2768
2769 <div class="slide">
2770   <h1>Anonymous Subtypes</h1>
2771
2772   <pre><code>package Person;
2773
2774 <span class="highlight">my $posint =
2775     subtype as 'Int', where { $_ &gt; 0 };</span>
2776
2777 has size =&gt; (
2778     is  =&gt; 'ro',
2779     <span class="highlight">isa =&gt; $posint,</span>
2780 );</code></pre>
2781
2782   <ul>
2783     <li>Shortcuts have anonymous forms as well</li>
2784   </ul>
2785 </div>
2786
2787 <div class="slide">
2788   <h1>Coercions</h1>
2789
2790   <pre><code>use Moose::Util::TypeConstraints;
2791
2792 subtype 'UCStr',
2793     as    'Str',
2794     where { ! /[a-z]/ };</code></pre>
2795 </div>
2796
2797 <div class="slide">
2798   <h1>Coercions</h1>
2799
2800   <pre><code><span class="incremental current">coerce 'UCStr',</span>
2801     <span class="incremental">from 'Str',</span>
2802     <span class="incremental">via  { uc };</span>
2803
2804 has shouty_name =&gt; (
2805     is     =&gt; 'ro',
2806     isa    =&gt; 'UCStr',
2807     <span class="incremental">coerce =&gt; 1,</span>
2808 );</code></pre>
2809 </div>
2810
2811 <div class="slide">
2812   <h1>Coercion Examples</h1>
2813
2814   <pre><code>subtype 'My::DateTime',
2815     as class_type 'DateTime';
2816
2817 coerce 'My::DateTime',
2818     from 'HashRef',
2819     via  { DateTime-&gt;new( %{$_} ) };
2820
2821 coerce 'My::DateTime',
2822     from 'Int',
2823     via  { DateTime-&gt;from_epoch(
2824                epoch =&gt; $_ ) };</code></pre>
2825
2826   <ul>
2827     <li>Use coercion to inflate a value</li>
2828   </ul>
2829 </div>
2830
2831 <div class="slide">
2832   <h1>Coercion Examples</h1>
2833
2834   <pre><code>coerce 'ArrayRef[Int]',
2835     from 'Int',
2836     via  { [ $_ ] };</code></pre>
2837
2838   <ul>
2839     <li>Instead of union - <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
2840   </ul>
2841 </div>
2842
2843 <div class="slide">
2844   <h1>Using Types with Attributes</h1>
2845
2846   <pre><code>package Person;
2847
2848 has height =&gt; (
2849     is  =&gt; 'rw',
2850     <span class="highlight">isa =&gt; 'Num',</span>
2851 );
2852
2853 has favorite_numbers =&gt; (
2854     is     =&gt; 'rw',
2855     <span class="highlight">isa    =&gt; 'ArrayRef[Int]',
2856     coerce =&gt; 1,</span>
2857 );</code></pre>
2858 </div>
2859
2860 <div class="slide">
2861   <h1>More Droppings</h1>
2862
2863   <ul>
2864     <li><code>Moose::Util::TypeConstraints</code> also needs cleanup</li>
2865   </ul>
2866
2867   <pre><code>package Person;
2868
2869 use Moose;
2870 use Moose::Util::TypeConstraints;
2871
2872 subtype ...;
2873
2874 no Moose;
2875 <span class="highlight">no Moose::Util::TypeConstraints;</span></code></pre>
2876 </div>
2877
2878 <div class="slide">
2879   <h1>Questions So Far?</h1>
2880 </div>  
2881
2882 <div class="slide">
2883   <h1>Exercises</h1>
2884
2885   <pre># cd exercises
2886 # perl bin/prove -lv t/05-types.t
2887
2888 Iterate til this passes all its tests</pre>
2889 </div>
2890
2891 <div class="slide">
2892   <h1>Typed Methods (Low-tech)</h1>
2893
2894   <pre class="medium"><code>package Person;
2895 <span class="highlight">use MooseX::Params::Validate qw( validated_list );</span>
2896
2897 sub work {
2898     my $self = shift;
2899     <span class="highlight">my ( $tasks, $can_rest ) =
2900         validated_list(
2901             \@_,
2902             tasks    =&gt;
2903                 { isa    =&gt; 'ArrayRef[Task]',
2904                   coerce =&gt; 1 },
2905             can_rest =&gt;
2906                 { isa     =&gt; 'Bool',
2907                   default =&gt; 0 },
2908         );</span>
2909     ...
2910 }</code></pre>
2911 </div>
2912
2913 <div class="slide">
2914   <h1>Typed Methods (High-tech)</h1>
2915
2916   <pre class="medium"><code>package Person;
2917
2918 <span class="highlight">use MooseX::Method::Signatures;</span>
2919
2920 <span class="highlight">method work ( ArrayRef[Task] :$tasks,
2921                         Bool :$can_rest = 0 )</span> {
2922     my $self = shift;
2923
2924     ...
2925 }</code></pre>
2926 </div>
2927
2928 <div class="slide">
2929   <h1>Digression: The Type Registry</h1>
2930
2931   <ul>
2932     <li>Types are actually <code>Moose::Meta::TypeConstraints</code> <em>objects</em></li>
2933     <li>Stored in an interpreter-global registry mapping names to objects</li>
2934   </ul>
2935 </div>
2936
2937 <div class="slide">
2938   <h1>Danger!</h1>
2939
2940   <ul>
2941     <li>Coercions are attached to type objects</li>
2942     <li>Therefore also global</li>
2943     <li>Name conflicts between modules!</li>
2944     <li>Coercion conflicts between modules!</li>
2945   </ul>
2946 </div>
2947
2948 <div class="slide">
2949   <h1>Namespace Fix</h1>
2950
2951   <ul>
2952     <li>Use some sort of pseudo-namespacing scheme</li>
2953     <li>Never coerce directly to a class name, or <em>to</em> built-in types</li>
2954   </ul>
2955 </div>
2956
2957 <div class="slide">
2958   <h1>Namespace Fix</h1>
2959
2960   <pre><code>use Moose::Util::TypeConstraints;
2961 subtype <span class="highlight">'MyApp::Type::DateTime',</span>
2962     as 'DateTime';
2963
2964 <span class="highlight">coerce 'MyApp::Type::DateTime',</span>
2965     from 'HashRef',
2966     via  { DateTime-&gt;new( %{$_} ) }
2967
2968 has creation_date =&gt; (
2969     is     =&gt; 'ro',
2970     <span class="highlight">isa    =&gt; 'MyApp::Type::DateTime',</span>
2971     coerce =&gt; 1,
2972 );</code></pre>
2973 </div>
2974
2975 <div class="slide">
2976   <h1>Namespace Fix</h1>
2977
2978   <pre><code>subtype 'MyApp::Type::ArrayOfInt',
2979     as 'ArrayRef[Int]';
2980
2981 coerce 'MyApp::Type::ArrayOfInt',
2982     from 'Int',
2983     via  { [ $_ ] };</code></pre>
2984 </div>
2985
2986 <div class="slide">
2987   <h1>Namespace Fix Pros and Cons</h1>
2988
2989   <ul>
2990     <li><span class="right">Relatively simple</span></li>
2991     <li><span class="right">Already built into Moose</span></li>
2992     <li><span class="wrong">Conflates type and module namespaces</span></li>
2993     <li><span class="wrong">Type names are strings, so typos are easy to make and may be hard to find</span></li>
2994   </ul>
2995 </div>
2996
2997 <div class="slide">
2998   <h1>MooseX::Types</h1>
2999
3000   <pre><code>package MyApp::Types;
3001
3002 use MooseX::Types
3003     <span class="highlight">-declare =&gt; [ qw( ArrayOfInt ) ]</span>;
3004 use MooseX::Types::Moose
3005     qw( ArrayRef Int );
3006
3007 subtype <span class="highlight">ArrayOfInt</span>,
3008     as ArrayRef[Int];
3009
3010 coerce <span class="highlight">ArrayOfInt</span>
3011     from Int,
3012     via  { [ $_ ] };</code></pre>
3013 </div>
3014
3015 <div class="slide">
3016   <h1>MooseX::Types</h1>
3017
3018   <pre><code>package MyApp::Account;
3019
3020 use MyApp::Types qw( ArrayOfInt );
3021
3022 has transaction_history => (
3023     is  => 'rw',
3024     isa => ArrayOfInt,
3025 );</code></pre>
3026 </div>
3027
3028 <div class="slide">
3029   <h1>MooseX::Types</h1>
3030
3031   <ul>
3032     <li>Type names are exported functions, catches typos early</li>
3033     <li>Types must be pre-declared</li>
3034     <li>Types are stored with namespaces internally, but you use short names</li>
3035     <li>Import existing Moose types as functions from <code>MooseX::Types::Moose</code></li>
3036     <li>Still need string names for things like <code>ArrayRef['Email::Address']</code></li>
3037   </ul>
3038 </div>
3039
3040 <div class="slide">
3041   <h1>MooseX::Types Pros and Cons</h1>
3042
3043   <ul>
3044     <li><span class="right">Catches typos at compile time</span></li>
3045     <li><span class="right">Automatic namespacing</span></li>
3046     <li><span class="wrong">One more thing to install and learn</span></li>
3047     <li><span class="wrong">Every name is typed twice (declared and then defined)</span></li>
3048     <li><span class="wrong">Still stuck with strings when referring to class or role names</span></li>
3049     <li><span class="wrong">Coercion gotcha from earlier still applies to types exported from <code>MooseX::Types::Moose</code></span></li>
3050   </ul>
3051 </div>
3052
3053 <div class="slide">
3054   <h1>Recommendation</h1>
3055
3056   <ul>
3057     <li>Use <code>MooseX::Types</code></li>
3058     <li>Compile time error catching and automatic namespacing are huge wins</li>
3059     <li>Docs from <code>Moose::Util::TypeConstraints</code> are 98% compatible with <code>MooseX::Types</code> anyway</li>
3060     <li>A function exported by a type library works wherever a type name would</li>
3061   </ul>
3062 </div>
3063
3064 <div class="slide">
3065   <h1>Questions?</h1>
3066 </div>  
3067
3068 <div class="slide fake-slide0">
3069   <h1>Part 6: Advanced Attributes</h1>
3070 </div>
3071
3072 <div class="slide">
3073   <h1>Weak References</h1>
3074
3075   <ul>
3076     <li>A weak reference lets you avoid circular references</li>
3077     <li>Weak references do not increase the reference count</li>
3078   </ul>
3079 </div>
3080
3081 <div class="slide">
3082   <h1>Circular Reference Illustrated</h1>
3083
3084   <pre><code>my $foo = {};
3085 my $bar = { foo =&gt; $foo };
3086 $foo-&gt;{bar} = $bar;</code></pre>
3087
3088   <ul>
3089     <li>Neither <code>$foo</code> nor <code>$bar</code> go out of scope<br />
3090         (until the program exits)</li>
3091   </ul>
3092 </div>
3093
3094 <div class="slide">
3095   <h1>Weakening Circular References</h1>
3096
3097   <pre><code>use Scalar::Util qw( weaken );
3098
3099 my $foo = {};
3100 my $bar = { foo =&gt; $foo };
3101 $foo-&gt;{bar} = $bar;
3102 weaken $foo-&gt;{bar}</code></pre>
3103
3104   <ul>
3105     <li>When <code>$bar</code> goes out of scope, <code>$foo-&gt;{bar}</code> becomes <code>undef</code></li>
3106   </ul>
3107 </div>
3108
3109 <div class="slide">
3110   <h1>Circular References in Attributes</h1>
3111
3112   <pre><code>package Person;
3113 use Moose;
3114
3115 has name   =&gt; ( is =&gt; 'ro' );
3116 has friend =&gt; ( is =&gt; 'rw' );
3117
3118 my $alice = Person-&gt;new( name =&gt; 'Alice' );
3119 my $bob   = Person-&gt;new( name =&gt; 'Bob' );
3120 $bob-&gt;friend($alice);
3121 $alice-&gt;friend($bob);</code></pre>
3122 </div>
3123
3124 <div class="slide">
3125   <h1>The Fix</h1>
3126
3127   <pre><code>package Person;
3128 use Moose;
3129
3130 has name   =&gt; ( is =&gt; 'ro' );
3131 has friend =&gt; ( is       =&gt; 'rw',
3132                 <span class="highlight">weak_ref =&gt; 1</span> );
3133
3134 my $alice = Person-&gt;new( name =&gt; 'Alice' );
3135 my $bob   = Person-&gt;new( name =&gt; 'Bob' );
3136 $bob-&gt;friend($alice);
3137 $alice-&gt;friend($bob);</code></pre>
3138 </div>
3139
3140 <div class="slide">
3141   <h1>Under the Hood</h1>
3142
3143   <ul>
3144     <li>A <code>weak_ref</code> attribute calls <code>weaken</code> ...
3145       <ul>
3146         <li>during object construction</li>
3147         <li>when the attribute is set via a writer</li>
3148       </ul>
3149     </li>
3150   </ul>
3151 </div>
3152
3153 <div class="slide">
3154   <h1>Triggers</h1>
3155
3156   <ul>
3157     <li>A code reference run after an attribute is <em>set</em></li>
3158     <li>Like an <code>after</code> modifier, but makes intentions clearer</li>
3159   </ul>
3160
3161   <h2 class="wrong">Gross</h2>
3162
3163   <pre><code>after salary_level =&gt; {
3164     my $self = shift;
3165     <span class="highlight">return unless @_;</span>
3166     $self-&gt;clear_salary;
3167 };</code></pre>
3168 </div>
3169
3170 <div class="slide">
3171   <h1>Use a Trigger Instead</h1>
3172
3173   <h2 class="right">Cleaner</h2>
3174
3175   <pre><code>has salary_level =&gt; (
3176     is      =&gt; 'rw',
3177     trigger =&gt;
3178         sub { $_[0]-&gt;clear_salary },
3179 );</code></pre>
3180 </div>
3181
3182 <div class="slide">
3183   <h1>Trigger Arguments</h1>
3184
3185   <ul>
3186     <li><code>$self</code></li>
3187     <li><code>$new_value</code></li>
3188     <li><code>$old_value</code> - if one exists</li>
3189   </ul>
3190 </div>
3191
3192 <div class="slide">
3193   <h1>Delegation</h1>
3194
3195   <ul>
3196     <li>Attributes can be objects</li>
3197     <li>Delegation transparently calls methods on those objects</li>
3198   </ul>
3199 </div>
3200
3201 <div class="slide">
3202   <h1>Delegation Examples</h1>
3203
3204   <pre><code>package Person;
3205
3206 has lungs =&gt; (
3207     is      =&gt; 'ro',
3208     isa     => 'Lungs',
3209     <span class="highlight">handles =&gt; [ 'inhale', 'exhale' ],</span>
3210 );</code></pre>
3211
3212   <ul>
3213     <li>Creates <code>$person-&gt;inhale</code> and <code>-&gt;exhale</code> methods</li>
3214     <li>Internally calls <code>$person-&gt;lungs-&gt;inhale</code></li>
3215   </ul>
3216 </div>
3217
3218 <div class="slide">
3219   <h1>Why Delegation?</h1>
3220
3221   <ul>
3222     <li>Reduce the number of classes exposed</li>
3223     <li>Re-arrange class internals -<br />
3224         turn a method into an attribute with delegation</li>
3225     <li>Provide convenenience methods</li>
3226   </ul>
3227 </div> 
3228
3229 <div class="slide">
3230   <h1>Moose's <code>handles</code> Parameter</h1>
3231
3232   <ul>
3233     <li>Accepts many arguments ...
3234       <ul>
3235         <li>Array reference - list of methods to delegate as-is</li>
3236         <li>Hash reference - map of method names</li>
3237         <li>Regex - delegates all matching methods</li>
3238         <li>Role name - delegates all methods in the role</li>
3239         <li>Sub reference - does something complicated ;)</li>
3240       </ul>
3241     </li>
3242   </ul>
3243 </div>      
3244
3245 <div class="slide">
3246   <h1>Array Reference</h1>
3247
3248   <ul>
3249     <li>1-to-1 mapping</li>
3250     <li>Takes each method name and creates a simple delegation from the delegating class to the delegatee attribute</li>
3251   </ul>
3252 </div>
3253
3254 <div class="slide">
3255   <h1>Hash Reference</h1>
3256
3257   <ul>
3258     <li>Mapping of names in the delegating class to the delegatee class</li>
3259   </ul>
3260
3261   <pre><code>package Person;
3262 use Moose;
3263 has account =&gt; (
3264     is      =&gt; 'ro',
3265     isa     =&gt; 'BankAccount',
3266     <span class="highlight">handles =&gt; {
3267         receive_money =&gt; 'deposit',
3268         give_money    =&gt; 'withdraw',
3269     },</span>
3270 );</code></pre>
3271 </div>
3272
3273 <div class="slide">
3274   <h1>Hash Reference Detailed</h1>
3275
3276   <pre><code>    handles =&gt; {
3277         receive_money =&gt; 'deposit',
3278         give_money    =&gt; 'withdraw',
3279     },</code></pre>
3280
3281   <ul>
3282     <li><code>$person-&gt;receive_money</code> = <code>$person-&gt;account-&gt;deposit</code></li>
3283     <li><code>$person-&gt;give_money</code> = <code>$person-&gt;account-&gt;withdraw</code></li>
3284   </ul>
3285 </div>
3286
3287 <div class="slide">
3288   <h1>Regex</h1>
3289
3290   <pre><code>package Person;
3291 use Moose;
3292
3293 has name =&gt; (
3294     is      =&gt; 'ro',
3295     isa     =&gt; 'Name',
3296     handles =&gt; qr/.*/,
3297 );</code></pre>
3298
3299   <ul>
3300     <li>Creates a delegation for every method in the Name class</li>
3301     <li>Excludes <code>meta</code> and methods inherited from <code>Moose::Object</code></li>
3302   </ul>
3303 </div>
3304
3305 <div class="slide">
3306   <h1>Role Name</h1>
3307
3308   <pre><code>package Auditor;
3309 use Moose::Role;
3310 sub record_change  { ... }
3311 sub change_history { ... }
3312
3313 package Account;
3314 use Moose;
3315
3316 has history =&gt; (
3317     is      =&gt; 'ro',
3318     does    =&gt; 'Auditor',
3319     <span class="highlight">handles =&gt; 'Auditor',</span>
3320 );</code></pre>
3321 </div>
3322
3323 <div class="slide">
3324   <h1>Role Name Detailed</h1>
3325
3326   <ul>
3327     <li>Account gets delegate methods for each method in the <code>Auditor</code> role
3328       <ul>
3329         <li>record_history</li>
3330         <li>change_history</li>
3331       </ul>
3332     </li>
3333   </ul>
3334 </div>
3335
3336 <div class="slide">
3337   <h1>Native Delegation</h1>
3338
3339   <ul>
3340     <li>Delegate to <em>unblessed</em> Perl types</li>
3341     <li>Scalar, array or hash ref, etc</li>
3342     <li>Treat Perl types as objects</li>
3343     <li>Still uses <code>handles</code></li>
3344     <li>Pretend that native Perl types have methods</li>
3345   </ul>
3346 </div>
3347
3348 <div class="slide">
3349   <h1>Native Delegation - Array(Ref)</h1>
3350
3351   <ul>
3352     <li>Methods include:
3353       <ul>
3354         <li><code>push</code></li>
3355         <li><code>shift</code></li>
3356         <li><code>elements</code> - returns all elements</li>
3357         <li><code>count</code></li>
3358         <li><code>is_empty</code></li>
3359         <li>quite a few more</li>
3360       </ul>
3361     </li>
3362   </ul>
3363 </div>
3364
3365 <div class="slide">
3366   <h1>Native Delegation - Array(Ref)</h1>
3367
3368   <pre><code>package Person;
3369 use Moose;
3370 has _favorite_numbers =&gt; (
3371     traits   =&gt; [ 'Array' ],
3372     is       =&gt; 'bare',
3373     isa      =&gt; 'ArrayRef[Int]',
3374     default  =&gt; sub { [] },
3375     init_arg =&gt; undef,
3376     <span class="highlight">handles  =&gt;
3377       { favorite_numbers    =&gt; 'elements',
3378         add_favorite_number =&gt; 'push',
3379       },</span>
3380 );</code></pre>
3381 </div>
3382
3383 <div class="slide">
3384   <h1>Native Delegation - Array(Ref)</h1>
3385
3386   <pre><code>my $person = Person-&gt;new();
3387
3388 $person-&gt;add_favorite_number(7);
3389 $person-&gt;add_favorite_number(42);
3390
3391 print "$_\n"
3392     for $person-&gt;favorite_numbers;
3393
3394 # 7
3395 # 42</code></pre>
3396 </div>
3397
3398 <div class="slide">
3399   <h1>Native Delegation</h1>
3400
3401   <ul>
3402     <li>Native types are ...
3403       <ul>
3404         <li>Number - <code>add</code>, <code>mul</code>, ...</li>
3405         <li>String - <code>append</code>, <code>chop</code>, ...</li>
3406         <li>Counter - <code>inc</code>, <code>dec</code>, ...</li>
3407         <li>Bool - <code>set</code>, <code>toggle</code>, ...</li>
3408         <li>Hash - <code>get</code>, <code>set</code>, ...</li>
3409         <li>Array - already saw it</li>
3410         <li>Code - <code>execute</code> and <code>execute_method</code></li>
3411       </ul>
3412     </li>
3413   </ul>
3414 </div>
3415
3416 <div class="slide">
3417   <h1>Curried Delegation</h1>
3418
3419   <ul>
3420     <li>A delegation with some preset arguments</li>
3421     <li>Works with object or Native delegation</li>
3422   </ul>
3423 </div>
3424
3425 <div class="slide">
3426   <h1>Curried Delegation</h1>
3427
3428   <pre><code>package Person;
3429 use Moose;
3430 has account =&gt; (
3431     is      =&gt; 'ro',
3432     isa     =&gt; 'BankAccount',
3433     handles =&gt; {
3434         receive_100 =&gt;
3435             <span class="highlight">[ 'deposit', 100 ]</span>
3436         give_100    =&gt;
3437             <span class="highlight">[ 'withdraw', 100 ]</span>
3438     },
3439 );</code></pre>
3440 </div>
3441
3442 <div class="slide">
3443   <h1>Curried Delegation</h1>
3444
3445   <pre><code>$person-&gt;receive_100;
3446 # really is
3447 $person-&gt;account-&gt;deposit(100);</code></pre>
3448 </div>
3449
3450 <div class="slide">
3451   <h1>Traits and Metaclasses</h1>
3452
3453   <ul>
3454     <li>The ultimate in customization</li>
3455     <li>Per attribute metaclasses</li>
3456     <li>Per attribute roles applied to the attribute metaclass</li>
3457     <li>Change the meta-level behavior</li>
3458   </ul>
3459 </div>
3460
3461 <div class="slide">
3462   <h1>Traits and Metaclasses</h1>
3463
3464   <ul>
3465     <li>The default metaclass is <code>Moose::Meta::Attribute</code></li>
3466     <li>Controls accessor generation, defaults, delegation, etc.</li>
3467     <li>Adding a role to this metaclass (or replacing it) allows for infinite customization</li>
3468   </ul>
3469 </div>
3470
3471 <div class="slide">
3472   <h1>Traits and Metaclasses</h1>
3473
3474   <ul>
3475     <li>Can add/alter/remove an attribute parameter (from <code>has</code>)</li>
3476     <li>Can change behavior of created attribute</li>
3477   </ul>
3478 </div>
3479
3480 <div class="slide">
3481   <h1>Simple Trait Example</h1>
3482
3483   <pre><code>package Person;
3484 use Moose;
3485 use MooseX::LabeledAttributes;
3486
3487 has ssn =&gt; (
3488     <span class="highlight">traits =&gt; [ 'Labeled' ],</span>
3489     is     =&gt; 'ro',
3490     isa    =&gt; 'Str',
3491     <span class="highlight">label  =&gt; 'Social Security Number',</span>
3492 );
3493 print <span class="highlight">Person-&gt;meta
3494             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
3495 </div>
3496
3497 <div class="slide">
3498   <h1>Simple Metaclass Example</h1>
3499
3500   <pre><code>package Person;
3501 use Moose;
3502 use MooseX::LabeledAttributes;
3503
3504 has ssn =&gt; (
3505     <span class="highlight">metaclass =&gt;
3506         'MooseX::Meta::Attribute::Labeled',</span>
3507     is        =&gt; 'ro',
3508     isa       =&gt; 'Str',
3509     <span class="highlight">label     =&gt; 'Social Security Number',</span>
3510 );
3511 print <span class="highlight">Person-&gt;meta
3512             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
3513 </div>
3514
3515 <div class="slide">
3516   <h1>Traits vs Metaclass</h1>
3517
3518   <ul>
3519     <li>Can apply any mix of traits to an attribute</li>
3520     <li>But just one metaclass</li>
3521     <li>Traits (aka roles) can cooperate</li>
3522     <li>Metaclasses require you to pick just one</li>
3523   </ul>
3524 </div>
3525
3526 <div class="slide">
3527   <h1>Advanced Attributes Summary</h1>
3528
3529   <ul>
3530     <li>Use <code>weak_ref</code> to avoid circular references</li>
3531     <li>Use trigger to do an action post-attribute write</li>
3532     <li>Use delegations to hide "internal" objects</li>
3533     <li>Use native delegations to treat Perl types as objects</li>
3534     <li>Traits and metaclasses let you extend Moose's core attribute features</li>
3535   </ul>
3536 </div>
3537
3538 <div class="slide">
3539   <h1>Exercises</h1>
3540
3541   <pre># cd exercises
3542 # perl bin/prove -lv \
3543       t/06-advanced-attributes.t
3544
3545 Iterate til this passes all its tests</pre>
3546 </div>
3547
3548 <div class="slide">
3549   <h1>CYOA</h1>
3550
3551   <p>
3552     If there is time, keep going ...
3553   </p>
3554
3555   <p>
3556     Otherwise, jump to slide 269 ...
3557   </p>
3558 </div>
3559
3560 <div class="slide fake-slide0">
3561   <h1>Bonus: A Brief Tour of MooseX</h1>
3562 </div>
3563
3564 <div class="slide">
3565   <h1>Notable MX Modules on CPAN</h1>
3566
3567   <ul>
3568     <li><strong>Not comprehensive</strong></li>
3569     <li>152 MooseX distributions on CPAN as of 02/02/2010</li>
3570     <li>Some of them are crap</li>
3571   </ul>
3572 </div>
3573
3574 <div class="slide">
3575   <h1>Already Mentioned Several</h1>
3576
3577   <ul>
3578     <li><code>MooseX::NonMoose</code> - best solution for subclassing non-Moose parents</li>
3579     <li><code>MooseX::Declare</code> - <em>real</em> Perl 5 OO</li>
3580     <li><code>MooseX::FollowPBP</code> and <code>MooseX::SemiAffordanceAccessor</code></li>
3581     <li><code>MooseX::Params::Validate</code> and <code>MooseX::Method::Signatures</code></li>
3582     <li><code>MooseX::Types</code></li>
3583   </ul>
3584 </div>    
3585
3586 <div class="slide">
3587   <h1>MooseX::Declare</h1>
3588
3589 <pre><code>use MooseX::Declare;
3590 use 5.10.0; # for say
3591
3592 class Person {
3593     has greeting =&gt;
3594         ( is =&gt; 'ro', isa =&gt; 'Str' );
3595
3596     method speak {
3597         say $self-&gt;greeting;
3598     }
3599 }</code></pre>
3600 </div>
3601
3602 <div class="slide">
3603   <h1>MooseX::Declare</h1>
3604
3605   <ul>
3606     <li>Still experimental-ish, but seeing more and more use</li>
3607     <li><strong>Not</strong> a source filter!</li>
3608     <li>Hooks into the Perl parser rather than filtering all your code</li>
3609     <li>But not supported by <code>PPI</code>, <code>perltidy</code>, etc.</li> (yet?)
3610   </ul>
3611 </div>
3612
3613 <div class="slide">
3614   <h1>MooseX::StrictConstructor</h1>
3615
3616   <ul>
3617     <li>By default, unknown constructor arguments are ignored</li>
3618     <li>MX::StrictConstructor turns these into an error</li>
3619   </ul>
3620 </div>
3621
3622 <div class="slide">
3623   <h1>MooseX::StrictConstructor</h1>
3624
3625   <pre><code>package Person;
3626
3627 use Moose;
3628 <span class="highlight">use MooseX::StrictConstructor;</span>
3629
3630 has name =&gt; ( is =&gt; 'ro' );
3631
3632 Person-&gt;new
3633     ( na<span class="wrong">n</span>e =&gt; 'Ringo Shiina' ); # kaboom</code></pre>
3634 </div>
3635
3636 <div class="slide">
3637   <h1>MooseX::Traits</h1>
3638
3639   <ul>
3640     <li>Combines object construction and role application</li>
3641     <li>Makes it easy to create one-off customized objects</li>
3642   </ul>
3643 </div>
3644
3645 <div class="slide">
3646   <h1>MooseX::Traits</h1>
3647
3648   <pre><code>package MyApp::Thingy;
3649 use Moose;
3650
3651 <span class="highlight">with 'MooseX::Traits';</span>
3652
3653 my $thing =
3654     MyApp::Thingy-&gt;<span class="highlight">new_with_traits</span>
3655         ( <span class="highlight">traits =&gt; [ 'Foo', 'Bar' ],</span>
3656           size   =&gt; 42 );</code></pre>
3657 </div>
3658
3659 <div class="slide">
3660   <h1>MooseX::Getopt</h1>
3661
3662   <ul>
3663     <li>Makes command-line interface programs easy!</li>
3664     <li>Construct an object from CLI arguments</li>
3665   </ul>
3666 </div>
3667
3668 <div class="slide">
3669   <h1>MooseX::Getopt</h1>
3670
3671   <pre><code>package App::CLI;
3672 use Moose;
3673
3674 <span class="highlight">with 'MooseX::Getopt';</span>
3675
3676 has file    =&gt;
3677     ( is =&gt; 'ro', required =&gt; 1 );
3678 has filters =&gt;
3679     ( is =&gt; 'ro', isa =&gt; 'ArrayRef[Str]' );
3680
3681 sub run { ... }</code></pre>
3682 </div>
3683
3684 <div class="slide">
3685   <h1>MooseX::Getopt</h1>
3686
3687   <ul>
3688     <li>Then call it like this:</li>
3689   </ul>
3690
3691 <pre><code>#!/usr/bin/perl
3692
3693 use App::CLI;
3694
3695 <span class="highlight">App::CLI-&gt;new_with_options()</span>-&gt;run();</code></pre>
3696
3697 <pre>$ myapp-cli \
3698    --file foo \
3699    --filters compress \
3700    --filters sanitize</pre>
3701 </div>
3702
3703 <div class="slide">
3704   <h1>MooseX::Clone</h1>
3705
3706   <pre><code>package Person;
3707
3708 use Moose;
3709 <span class="highlight">with 'MooseX::Clone';</span>
3710
3711 my $person = Person-&gt;new;
3712 my $clone  = <span class="highlight">$person-&gt;clone;</span></code></pre>
3713 </div>
3714
3715 <div class="slide">
3716   <h1>MooseX::NonMoose</h1>
3717
3718   <ul>
3719     <li>Highly recommended for subclassing non-Moose parents</li>
3720     <li>Gets all the little annoying details right</li>
3721   </ul>
3722 </div>
3723
3724 <div class="slide">
3725   <h1>MooseX::Role::Parameterized</h1>
3726
3727   <pre><code>package HasCollection;
3728 <span class="current incremental">use MooseX::Role::Parameterized;</span>
3729 <span class="incremental">parameter type =&gt; ( isa     =&gt; 'Str',
3730                     default =&gt; 'Item' );</span>
3731 <span class="incremental">role {
3732     my $p = shift;
3733
3734     my $type =
3735         'ArrayRef[' . $p-&gt;type() . ']';
3736     has collection =&gt;
3737         ( is  =&gt; 'ro',
3738           isa =&gt; $type );
3739 };</span></code></pre>
3740 </div>
3741
3742 <div class="slide">
3743   <h1>MooseX::Role::Parameterized</h1>
3744
3745   <pre><code>package Person;
3746
3747 use Moose;
3748 with HasCollection =&gt; { type =&gt; 'Int' };</code></pre>
3749 </div>
3750
3751 <div class="slide">
3752   <h1>Questions?</h1>
3753 </div>  
3754
3755 <div class="slide">
3756   <h1>Moose-using Modules</h1>
3757
3758   <p>
3759     For further reading, a few modules which use Moose ...
3760   </p>
3761
3762   <ul>
3763     <li><a href="http://search.cpan.org/dist/Catalyst-Runtime">Catalyst</a></li>
3764     <li><a href="http://search.cpan.org/dist/CHI">CHI</a></li>
3765     <li><a href="http://search.cpan.org/dist/Devel-REPL">Devel::REPL</a></li>
3766     <li><a href="http://search.cpan.org/dist/Email-Sender">Email::Sender</a></li>
3767     <li><a href="http://search.cpan.org/dist/Fey">Fey</a></li>
3768     <li><a href="http://search.cpan.org/dist/Net-Twitter">Net::Twitter</a></li>
3769   </ul>
3770 </div>
3771
3772 <div class="slide">
3773   <h1>More Information</h1>
3774
3775   <ul>
3776     <li><a href="http://moose.perl.org/">http://moose.perl.org/</a></li>
3777     <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>
3778     <li><a href="irc://irc.perl.org/#moose">irc://irc.perl.org/#moose</a></li>
3779     <li>mailing list - <a href="mailto:moose@perl.org">moose@perl.org</a></li>
3780     <li>Slides and exercises are in Moose's git repo:
3781         <br />
3782         <span style="font-size:80%; white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
3783   </ul>
3784 </div>
3785
3786 <div class="slide fake-slide0">
3787   <h1>The End</h1>
3788 </div>
3789
3790 </div> 
3791 </body>
3792 </html>
3793
3794 <!--
3795
3796 Copyright 2009 David Rolsky. All Rights Reserved.
3797
3798 This work is licensed under a Creative Commons Attribution-Share Alike
3799 3.0 United States License See
3800 http://creativecommons.org/licenses/by-sa/3.0/us/ for details.
3801
3802 -->