No room to show required method in inlined role example
[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   </ul>
1043 </div>
1044
1045 <div class="slide">
1046   <h1><code>override</code> and <code>super</code></h1>
1047
1048   <pre><code>package Employee;
1049 use Moose;
1050
1051 <span class="current incremental">extends 'Person';</span>
1052
1053 <span class="incremental">override</span> work =&gt; sub {
1054     my $self = shift;
1055
1056     die "Pay me first"
1057         unless $self-&gt;got_paid;
1058     <span class="incremental">super();</span>
1059 }<span class="incremental">;</span></code></pre>
1060 </div>
1061
1062 <div class="slide">
1063   <h1>Caveat <code>super</code></h1>
1064
1065   <ul>
1066     <li>Mostly like <code>$self-&gt;SUPER::work(@_)</code></li>
1067     <li><strong>But</strong> cannot change <code>@_</code>!</li>
1068     <li>Binds the parent's method at compile time</li>
1069     <li>Parent determined by checking <code>Child-&gt;meta()-&gt;superclasses()</code></li>
1070   </ul>
1071 </div>
1072
1073 <div class="slide">
1074   <h1>Minimal Attributes</h1>
1075
1076   <ul>
1077     <li><code>has 'foo'</code></li>
1078     <li>Use <code>is =&gt; 'ro'</code> or <code>is =&gt; 'rw'</code></li>
1079     <li>Attributes without "is" have no accessors</li>
1080   </ul>
1081 </div>
1082
1083 <div class="slide">
1084   <h1>Read-write attributes</h1>
1085
1086   <pre><code>package Person;
1087 use Moose;
1088
1089 has first_name =&gt; ( <span class="highlight">is =&gt; 'rw'</span> );
1090
1091 my $person =
1092     Person-&gt;new( first_name =&gt; 'Dave' );
1093
1094 $person-&gt;first_name('Stevan');
1095 print $person-&gt;first_name; # Stevan</code></pre>
1096
1097 </div>
1098
1099 <div class="slide">
1100   <h1>Read-only attributes</h1>
1101
1102   <pre><code>package Person;
1103 use Moose;
1104
1105 has first_name =&gt; ( <span class="highlight">is =&gt; 'ro'</span> );
1106
1107 my $person =
1108     Person-&gt;new( first_name =&gt; 'Dave' );
1109
1110 $person-&gt;first_name('Stevan');
1111 print $person-&gt;first_name; # Dave</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>Method Aliasing</h1>
1432
1433   <pre><code>package FragileDancer;
1434 use Moose;
1435
1436 <span class="highlight">with 'IsFragile' =>
1437          { -alias =>
1438                { break => 'break_bone' } },
1439      'CanBreakdance' =>
1440          { -alias =>
1441                { break => 'break_it_down' } };</span></code></pre>
1442
1443   <ul>
1444     <li>Renames the roles' methods</li>
1445     <li>Still conflicts, need to <code>exclude</code> as well</li>
1446   </ul>
1447 </div>
1448
1449 <div class="slide">
1450   <h1>Method Exclusion</h1>
1451
1452   <pre><code>package FragileDancer;
1453 use Moose;
1454
1455 <span class="highlight">with 'IsFragile' =>
1456          { -alias =>
1457                { break => 'break_bone' },
1458            -excludes => 'break' },
1459      'CanBreakdance' =>
1460          { -alias =>
1461                { break => 'break_it_down' },
1462            -excludes => 'break' };</span></code></pre>
1463 </div>
1464
1465 <div class="slide">
1466   <h1>And then ...</h1>
1467
1468   <pre><code>package FragileDancer;
1469 use Moose;
1470
1471 sub break {
1472     my $self = shift;
1473
1474     $self-&gt;break_it_down;
1475     if ( rand(1) &lt; 0.5 ) {
1476         $self-&gt;break_bone;
1477     }
1478 }</code></pre>
1479 </div>
1480
1481 <div class="slide">
1482   <h1>Still Full of Fail</h1>
1483
1484   <ul>
1485     <li>Roles are also about semantics!</li>
1486     <li>We've fulfilled the letter and lost the spirit</li>
1487     <li>Roles have a <em>meaning</em></li>
1488     <li>Think twice before blindly aliasing and excluding methods!</li>
1489   </ul>
1490 </div>
1491
1492 <div class="slide">
1493   <h1>Hot Role-on-Role Action</h1>
1494
1495   <pre><code>package Comparable;
1496 use Moose::Role;
1497
1498 requires 'compare';</code></pre>
1499 </div>
1500
1501 <div class="slide">
1502   <h1>Hot Role-on-Role Action</h1>
1503
1504   <pre><code>package TestsEquality;
1505 use Moose::Role;
1506
1507 with 'Comparable';
1508
1509 sub is_equal {
1510     my $self = shift;
1511     return $self-&gt;compare(@_) == 0;
1512 }</code></pre>
1513 </div>
1514
1515 <div class="slide">
1516   <h1>And then ...</h1>
1517
1518   <pre><code>package Integer;
1519 use Moose;
1520
1521 with 'TestsEquality';
1522
1523 # Satisfies the Comparable role
1524 sub compare { ... }
1525
1526 Integer-&gt;does('TestsEquality'); # true
1527 Integer-&gt;does('Comparable'); # also true!</code></pre>
1528 </div>
1529
1530 <div class="slide">
1531   <h1>Name Conflicts Between Roles</h1>
1532
1533   <pre><code>package HasSubProcess;
1534 use Moose::Role;
1535
1536 <span class="highlight">sub execute { ... }</span>
1537
1538 package Killer;
1539 use Moose::Role;
1540
1541 with 'HasSubProcess';
1542
1543 <span class="highlight">sub execute { ... }</span></code></pre>
1544 </div>
1545
1546 <div class="slide">
1547   <h1>Delayed Conflict</h1>
1548
1549   <pre><code>package StateOfTexas;
1550 with 'Killer';</code></pre>
1551
1552   <ul>
1553     <li><code>StateOfTexas</code> must implement its own <code>execute</code></li>
1554     <li>But loading the <code>Killer</code> role by itself does not cause an error</li>
1555   </ul>
1556 </div>
1557
1558 <div class="slide">
1559   <h1>Roles as Interfaces</h1>
1560
1561   <ul>
1562     <li>Roles can <code>require</code> methods of their consumers</li>
1563     <li>Compile-time checks</li>
1564     <li>Method must exist when the role is consumed</li>
1565   </ul>
1566 </div>
1567
1568 <div class="slide">
1569   <h1>The Attribute Gotcha</h1>
1570
1571 <pre><code>package HasSize;
1572 use Moose::Role;
1573
1574 <span class="current incremental">requires 'size';</span>
1575
1576 package Shirt;
1577 use Moose;
1578
1579 <span class="incremental">with 'HasSize';
1580
1581 has size => ( is => 'ro' );</span></code></pre>
1582 </div>
1583
1584 <div class="slide">
1585   <h1>The Attribute Gotcha Workaround</h1>
1586
1587   <pre><code>package HasSize;
1588 use Moose::Role;
1589
1590 requires 'size';
1591
1592 package Shirt;
1593 use Moose;
1594
1595 has size => ( is => 'ro' );
1596
1597 with 'HasSize';</code></pre>
1598 </div>
1599
1600 <div class="slide">
1601   <h1>Compile-time Is a Lie</h1>
1602
1603   <ul>
1604     <li>Really, it's <em>package load</em> time</li>
1605     <li>That's run-time, but before the "real" run-time</li>
1606     <li>Moose does not rewire Perl, it's just sugar!</li>
1607     <li>(but <code>MooseX::Declare</code> <em>does</em> rewire Perl)</li>
1608   </ul>
1609 </div>
1610
1611 <div class="slide">
1612   <h1>Enforcing Roles</h1>
1613
1614   <pre><code>package Comparison;
1615 use Moose;
1616
1617 has [ 'left', 'right' ] => (
1618     is   => 'ro',
1619     <span class="highlight">does => 'Comparable',</span>
1620 );
1621 </code></pre>
1622
1623   <ul>
1624     <li>A sneak peek at type constraints</li>
1625   </ul>
1626 </div>
1627
1628
1629 <div class="slide">
1630   <h1>Roles Can Be Applied to Objects</h1>
1631
1632   <pre><code>use Moose::Util qw( apply_all_roles );
1633
1634 my $fragile_person = Person-&gt;new( ... );
1635 apply_all_roles( $fragile_person,
1636                  'IsFragile' );</code></pre>
1637
1638   <ul>
1639     <li>Does not change the <code>Person</code> class</li>
1640     <li>Works with non-Moose classes, great for monkey-patching!</li>
1641   </ul>
1642 </div>
1643
1644 <div class="slide">
1645   <h1>Roles Are Dirty Too</h1>
1646
1647   <ul>
1648     <li>Once again, clean up those Moose droppings</li>
1649   </ul>
1650
1651   <pre><code>package Comparable;
1652 use Moose::Role;
1653
1654 requires 'compare';
1655
1656 <span class="highlight">no Moose::Role;</span></code></pre>
1657
1658   <ul>
1659     <li>But roles cannot be made immutable</li>
1660   </ul>
1661 </div>
1662
1663 <div class="slide">
1664   <h1>The Zen of Roles</h1>
1665
1666   <ul>
1667     <li>Roles represent discrete units of ...
1668       <ul>
1669         <li>state</li>
1670         <li>behavior</li>
1671         <li>interface</li>
1672       </ul>
1673     </li>
1674     <li>Roles are shareable between unrelated classes</li>
1675     <li>Roles are what a class <em>does</em>, not what it <em>is</em></li>
1676     <li>Roles <em>add</em> functionality, inheritance <em>specializes</em></li>
1677   </ul>
1678 </div>
1679
1680 <div class="slide">
1681   <h1>Abstract Examples</h1>
1682
1683   <ul>
1684     <li>Human <em>@ISA</em> Animal</li>
1685     <li>Human <em>does</em> Toolmaker (as <em>does</em> Chimpanzee)</li>
1686     <li>Car <em>@ISA</em> Vehicle</li>
1687     <li>Car <em>does</em> HasEngine</li>
1688   </ul>
1689 </div>
1690
1691 <div class="slide">
1692   <h1>Real Examples</h1>
1693
1694   <ul>
1695     <li>Objects representing SQL database components and queries
1696       <ul>
1697         <li>Schema, Table, Column, ColumnAlias</li>
1698         <li>Select, Insert, Update, Delete</li>
1699       </ul>
1700     </li>
1701   </ul>
1702 </div>
1703
1704 <div class="slide">
1705   <h1>Real Examples</h1>
1706
1707   <ul>
1708     <li>Column and ColumnAlias both <em>do</em> ColumnLike</li>
1709     <li>ColumnLike things can be used in certain parts of queries</li>
1710     <li>All queries <em>do</em> HasWhereClause</li>
1711     <li>Select <em>does</em> Comparable and Selectable (for subselects)</li>
1712     <li>A where clause requires its components to <em>do</em> Comparable</li>
1713   </ul>
1714 </div>
1715
1716 <div class="slide">
1717   <h1>Roles Summary</h1>
1718
1719   <ul>
1720     <li>Roles can define an interface with <code>requires</code></li>
1721     <li>Roles can have state (attributes) and behavior (methods)</li>
1722     <li>Roles can mix interface, state, &amp; behavior</li>
1723     <li>Roles are composed (flattened) into classes</li>
1724     <li>Roles can do other roles</li>
1725     <li>Roles can be used as a type in APIs (must do Comparable)</li>
1726   </ul>
1727 </div>
1728
1729 <div class="slide">
1730   <h1>Questions?</h1>
1731 </div>  
1732
1733 <div class="slide">
1734   <h1>Exercises</h1>
1735
1736   <pre># cd exercises
1737 # perl bin/prove -lv t/02-roles.t
1738
1739 Iterate til this passes all its tests</pre>
1740 </div>
1741
1742 <div class="slide fake-slide0">
1743   <h1>Part 3: Basic Attributes</h1>
1744 </div>
1745
1746 <div class="slide">
1747   <h1>Attributes Are Huge</h1>
1748
1749   <ul>
1750     <li>Moose's biggest feature</li>
1751     <li>The target of <em>many</em> MooseX modules</li>
1752   </ul>
1753 </div>
1754
1755 <div class="slide">
1756   <h1>Quick Review</h1>
1757
1758   <ul>
1759     <li>Declared with <code>has</code></li>
1760     <li>Read-only or read-write</li>
1761   </ul>
1762
1763   <pre><code>package Shirt;
1764 use Moose;
1765
1766 has 'color'     =&gt; ( is =&gt; 'ro' );
1767 has 'is_ripped' =&gt; ( is =&gt; 'rw' );</code></pre>
1768 </div>
1769
1770 <div class="slide">
1771   <h1>Required-ness</h1>
1772
1773   <ul>
1774     <li>Required means "must be passed to the constructor"</li>
1775     <li>But can be <code>undef</code></li>
1776   </ul>
1777 </div>
1778
1779 <div class="slide">
1780   <h1>Required-ness</h1>
1781
1782   <pre><code>package Person;
1783 use Moose;
1784
1785 has first_name =&gt; (
1786     is       =&gt; 'ro',
1787     <span class="current incremental">required =&gt; 1,</span>
1788 );
1789
1790 <span class="incremental">Person-&gt;new( first_name =&gt; undef ); # ok
1791 Person-&gt;new(); # kaboom</span></code></pre>
1792 </div>
1793
1794 <div class="slide">
1795   <h1>Default and Builder</h1>
1796
1797   <ul>
1798     <li>Attributes can have defaults</li>
1799     <li>Simple non-reference scalars (number, string)</li>
1800     <li>Subroutine reference</li>
1801     <li>A builder method</li>
1802   </ul>
1803 </div>
1804
1805 <div class="slide">
1806   <h1>Default</h1>
1807
1808   <ul>
1809     <li>Can be a non-reference scalar (including <code>undef</code>)</li>
1810   </ul>
1811
1812   <pre><code>package Person;
1813 use Moose;
1814
1815 has bank =&gt; (
1816     is      =&gt; 'rw',
1817     default =&gt; 'Spire FCU',
1818 );</code></pre>
1819 </div>
1820
1821 <div class="slide">
1822   <h1>Default</h1>
1823
1824   <ul>
1825     <li>Can be a subroutine reference</li>
1826   </ul>
1827
1828   <pre><code>package Person;
1829 use Moose;
1830
1831 has bank =&gt; (
1832     is      =&gt; 'rw',
1833     default =&gt;
1834         sub { Bank-&gt;new(
1835                   name =&gt; 'Spire FCU' ) },
1836 );</code></pre>
1837 </div>
1838
1839 <div class="slide">
1840   <h1>Subroutine Reference Default</h1>
1841
1842   <ul>
1843     <li>Called as a method on the object</li>
1844     <li>Called anew for each object</li>
1845   </ul>
1846 </div>
1847
1848 <div class="slide">
1849   <h1>Why No Other Reference Types?</h1>
1850
1851   <pre><code>package Person;
1852 use Moose;
1853
1854 has bank =&gt; (
1855     is      =&gt; 'rw',
1856     <span class="wrong">default =&gt; Bank-&gt;new(
1857                    name =&gt; 'Spire FCU' ),</span>
1858 );</code></pre>
1859
1860   <ul>
1861     <li>Now <strong>every</strong> person shares the <strong>same</strong> Bank object!</li>
1862   </ul>
1863 </div>
1864
1865 <div class="slide">
1866      <h1>Defaulting to an Empty Reference</h1>
1867
1868   <pre><code>package Person;
1869 use Moose;
1870
1871 has packages =&gt; (
1872     is      =&gt; 'rw',
1873     default =&gt; <span class="highlight">sub { [] }</span>,
1874 );</code></pre>
1875 </div>
1876
1877 <div class="slide">
1878   <h1>What if I Want to Share?</h1>
1879
1880   <pre><code>package Person;
1881 use Moose;
1882
1883 my $highlander_bank =
1884     Bank-&gt;new(
1885         name =&gt; 'Clan MacLeod Trust' );
1886
1887 has bank =&gt; (
1888     is      =&gt; 'rw',
1889     default =&gt; sub { $highlander_bank },
1890 );</code></pre>
1891 </div>
1892
1893 <div class="slide">
1894   <h1>Builder</h1>
1895
1896   <ul>
1897     <li>A method <em>name</em></li>
1898     <li>When called, this method returns the default value</li>
1899   </ul>
1900 </div>
1901
1902 <div class="slide">
1903   <h1>Builder</h1>
1904
1905   <pre><code>package Person;
1906 use Moose;
1907
1908 has bank =&gt; (
1909     is      =&gt; 'rw',
1910     builder =&gt; '_build_bank',
1911 );
1912
1913 sub _build_bank {
1914     my $self = shift;
1915     return Bank-&gt;new(
1916         name => 'Spire FCU' );
1917 }</code></pre>
1918 </div>
1919
1920 <div class="slide">
1921   <h1>Default vs Builder</h1>
1922
1923   <ul>
1924     <li>Use default for simple scalars</li>
1925     <li>Use default to return empty references</li>
1926     <li>Use default for <em>very</em> trivial subroutine references</li>
1927     <li>Use builder for everything else</li>
1928   </ul>
1929 </div>
1930
1931 <div class="slide">
1932   <h1>Builder Bonuses</h1>
1933
1934   <ul>
1935     <li>Can be overridden and method modified, because it's called by <em>name</em></li>
1936     <li>Roles can require a builder</li>
1937   </ul>
1938 </div>
1939       
1940 <div class="slide">
1941   <h1>Role Requires Builder</h1>
1942
1943   <pre><code>package HasBank;
1944 use Moose::Role;
1945
1946 requires '_build_bank';
1947
1948 has bank =&gt; (
1949     is      =&gt; 'ro',
1950     builder =&gt; '_build_bank',
1951 );</code></pre>
1952 </div>
1953
1954 <div class="slide">
1955   <h1>Lazy, Good for Nothin' Attributes</h1>
1956
1957   <ul>
1958     <li>Normally, defaults are generated during object construction</li>
1959     <li>This can be expensive</li>
1960     <li>We want to default to <code>$self-&gt;size * 2</code>, but attribute initialization order is unpredictable</li>
1961     <li>Use lazy attributes!</li>
1962   </ul>
1963 </div>
1964
1965 <div class="slide">
1966   <h1>The Power of Dynamic Defaults</h1>
1967
1968   <pre><code>package Person;
1969 use Moose;
1970
1971 has shoe_size =&gt; (
1972     is       =&gt; 'ro',
1973     required =&gt; 'ro',
1974 );</code></pre>
1975 </div>
1976
1977 <div class="slide">
1978   <h1>The Power of Dynamic Defaults</h1>
1979
1980   <pre><code>has shoes =&gt; (
1981     is      =&gt; 'ro',
1982     <span class="highlight">lazy    =&gt; 1,</span>
1983     builder => '_build_shoes',
1984 );
1985
1986 sub _build_shoes {
1987     my $self = shift;
1988
1989     return Shoes-&gt;new(
1990         size =&gt; <span class="highlight">$self-&gt;shoe_size</span> );
1991 }</code></pre>
1992 </div>
1993
1994 <div class="slide">
1995   <h1>Lazy is Good</h1>
1996
1997   <ul>
1998     <li>Lazy defaults are executed when the attribute is read</li>
1999     <li>Can see other object attributes</li>
2000     <li>Still need to watch out for circular laziness</li>
2001   </ul>
2002 </div>    
2003
2004 <div class="slide">
2005   <h1>Clearer and Predicate</h1>
2006
2007   <ul>
2008     <li>Attributes can have a value, including <code>undef</code>, or not</li>
2009     <li>Can clear the value with a clearer method</li>
2010     <li>Can check for the existence of a value with a predicate method</li>
2011     <li>By default, these methods are not created</li>
2012   </ul>
2013 </div>
2014
2015 <div class="slide">
2016   <h1>Clearer and Predicate</h1>
2017
2018   <pre><code>package Person;
2019 use Moose;
2020
2021 has account =&gt; (
2022     is        =&gt; 'ro',
2023     lazy      =&gt; 1,
2024     builder   =&gt; '_build_account',
2025     <span class="highlight">clearer   =&gt; '_clear_account',
2026     predicate =&gt; 'has_account',</span>
2027 );</code></pre>
2028 </div>
2029
2030 <div class="slide">
2031   <h1>Clearer and Lazy Defaults</h1>
2032
2033   <ul>
2034     <li>Lazy defaults are good for computed attributes</li>
2035     <li>Clear the attribute when the source data changes</li>
2036     <li>Recalculated at next access</li>
2037   </ul>
2038 </div>
2039
2040 <div class="slide">
2041   <h1>Renaming constructor arguments</h1>
2042
2043   <ul>
2044     <li>By default, constructor names = attribute names</li>
2045     <li>Use <code>init_arg</code> to change this</li>
2046     <li>Set <code>init_arg =&gt; undef</code> to make it unconstructable</li>
2047   </ul>
2048 </div>
2049
2050 <div class="slide">
2051   <h1>Some <code>init_arg</code> examples</h1>
2052
2053   <pre><code>package Person;
2054 use Moose;
2055
2056 has shoe_size => (
2057     is       =&gt; 'ro',
2058     <span class="highlight">init_arg =&gt; 'foot_size',</span>
2059 );
2060
2061 Person-&gt;new( <span class="wrong">shoe_size =&gt; 13</span> );
2062
2063 my $person =
2064     Person-&gt;new( <span class="right">foot_size =&gt; 13</span> );
2065 print $person-&gt;shoe_size;</code></pre>
2066 </div>
2067
2068 <div class="slide">
2069   <h1>Some <code>init_arg</code> examples</h1>
2070
2071 <pre><code>package Person;
2072 use Moose;
2073
2074 has shoes => (
2075     is       =&gt; 'ro',
2076     <span class="highlight">init_arg =&gt; undef,</span>
2077 );
2078
2079 Person-&gt;new( <span class="wrong">shoes =&gt; Shoes-&gt;new</span> );</code></pre>
2080 </div>
2081
2082 <div class="slide">
2083   <h1>Why Set <code>init_arg =&gt; undef</code>?</h1>
2084
2085   <ul>
2086     <li>Use this with a lazy default for attributes-as-cache</li>
2087     <li>Compute the value as needed</li>
2088     <li>Ensure that it is always generated correctly (not set by constructor)</li>
2089     <li>Use triggers or method modifiers (coming soon) to clear the value</li>
2090   </ul>
2091 </div>
2092
2093 <div class="slide">
2094   <h1>Attribute Inheritance</h1>
2095
2096   <ul>
2097     <li>By default, subclasses inherit attribute as-is</li>
2098     <li>Can change some attribute parameters in subclasses
2099       <ul>
2100         <li>default</li>
2101         <li>builder</li>
2102         <li>required</li>
2103         <li>lazy</li>
2104         <li>others we've not yet covered</li>
2105       </ul>
2106     </li>
2107   </ul>
2108 </div>   
2109
2110 <div class="slide">
2111   <h1>Attribute Inheritance Example</h1>
2112
2113   <pre><code>package Employee;
2114 use Moose;
2115
2116 extends 'Person';
2117
2118 has '<span class="highlight">+first_name</span>' =&gt; (
2119     default =&gt; 'Joe',
2120 );</code></pre>
2121 </div>
2122
2123 <div class="slide">
2124   <h1>Attribute Inheritance Warning</h1>
2125
2126   <ul>
2127     <li>An attribute is a contract about a class's API</li>
2128     <li>Don't break that contract in a subclass</li>
2129     <li>Especially important in the context of types</li>
2130   </ul>
2131 </div>
2132
2133 <div class="slide">
2134   <h1>Changing Accessor Names</h1>
2135
2136   <pre><code>package Person;
2137 use Moose;
2138
2139 has first_name =&gt; (
2140     <span class="highlight">accessor</span> =&gt; 'first_name',
2141 );</code></pre>
2142
2143   <ul>
2144     <li>The long-hand version of <code>is =&gt; 'rw'</code></li>
2145   </ul>
2146 </div>
2147
2148 <div class="slide">
2149   <h1>Changing Accessor Names</h1>
2150
2151   <pre><code>package Person;
2152 use Moose;
2153
2154 has first_name =&gt; (
2155     <span class="highlight">reader</span> =&gt; 'first_name',
2156     <span class="highlight">writer</span> =&gt; undef,
2157 );</code></pre>
2158
2159   <ul>
2160     <li>The long-hand version of <code>is =&gt; 'ro'</code></li>
2161   </ul>
2162 </div>
2163
2164
2165 <div class="slide">
2166   <h1>Changing Accessor Names</h1>
2167
2168   <pre><code>package Person;
2169 use Moose;
2170
2171 has first_name =&gt; (
2172     <span class="highlight">reader</span> =&gt; 'get_first_name',
2173     <span class="highlight">writer</span> =&gt; 'set_first_name',
2174 );</code></pre>
2175 </div>
2176
2177 <div class="slide">
2178   <h1>Changing Accessor Names</h1>
2179
2180   <pre><code>package Person;
2181 use Moose;
2182
2183 has first_name =&gt; (
2184     <span class="highlight">is</span>     =&gt; 'rw',
2185     <span class="highlight">writer</span> =&gt; '_first_name',
2186 );</code></pre>
2187
2188   <ul>
2189     <li>Can also mix-and-match <code>is</code> and explicit names</li>
2190   </ul>
2191 </div>
2192
2193 <div class="slide">
2194   <h1>ETOOMUCHTYPING</h1>
2195
2196   <ul>
2197     <li><code>MooseX::FollowPBP</code><br /><code>get_foo</code> and <code>set_foo</code></li>
2198     <li><code>MooseX::SemiAffordanceAccessor</code><br /><code>foo</code> and <code>set_foo</code></li>
2199   </ul>
2200 </div>
2201
2202 <div class="slide">
2203   <h1>ETOOMUCHTYPING</h1>
2204
2205   <pre><code>package Person;
2206 use Moose;
2207 <span class="highlight">use MooseX::SemiAffordanceAccessor;</span>
2208
2209 has first_name =&gt; (
2210     is =&gt; 'rw',
2211 );</code></pre>
2212
2213   <ul>
2214     <li>Creates <code>first_name</code> and <code>set_first_name</code></li>
2215   </ul>
2216 </div>
2217
2218 <div class="slide">
2219   <h1>Basic Attributes Summary</h1>
2220
2221   <ul>
2222     <li>Attributes can be <code>required</code></li>
2223     <li>Attributes can have a <code>default</code> or <code>builder</code></li>
2224     <li>Attributes with a default or builder can be <code>lazy</code></li>
2225     <li>Attributes can have a <code>clearer</code> and/or <code>predicate</code></li>
2226   </ul>
2227 </div>
2228
2229 <div class="slide">
2230   <h1>Basic Attributes Summary</h1>
2231
2232   <ul>
2233     <li>An attribute's constructor name can be changed with <code>init_arg</code></li>
2234     <li>A subclass can alter its parents' attributes</li>
2235     <li>Attribute accessor names can be changed</li>
2236   </ul>
2237 </div>
2238
2239 <div class="slide">
2240   <h1>Questions?</h1>
2241 </div>  
2242
2243 <div class="slide">
2244   <h1>Exercises</h1>
2245
2246   <pre># cd exercises
2247 # perl bin/prove -lv \
2248       t/03-basic-attributes.t
2249
2250 Iterate til this passes all its tests</pre>
2251 </div>
2252
2253 <div class="slide fake-slide0">
2254   <h1>Part 4: Method Modifiers</h1>
2255 </div>
2256
2257 <div class="slide">
2258   <h1>What is a Method Modifier</h1>
2259
2260   <ul>
2261     <li>Apply to an existing method</li>
2262     <li>... that comes from a parent class, the current class, or a role</li>
2263     <li>Roles can provide modifiers that are applied at composition time</li>
2264   </ul>
2265 </div>
2266
2267 <div class="slide">
2268   <h1>What Are Method Modifiers For?</h1>
2269
2270   <ul>
2271     <li>"Inject" behavior</li>
2272     <li>Add behavior to generated methods (accessors, delegations)</li>
2273     <li>Added from a role, can modify existing behavior</li>
2274   </ul>
2275 </div>
2276
2277 <div class="slide">
2278   <h1>Before and After</h1>
2279
2280   <ul>
2281     <li>Simplest modifiers - <code>before</code> and <code>after</code></li>
2282     <li>Guess when they run!</li>
2283   </ul>
2284 </div>
2285
2286 <div class="slide">
2287   <h1>Uses for <code>before</code></h1>
2288
2289   <ul>
2290     <li>As a pre-call check</li>
2291   </ul>
2292
2293   <pre><code>package Person;
2294 use Moose;
2295
2296 before work =&gt; sub {
2297     my $self = shift;
2298     die 'I have no job!'
2299         unless $self-&gt;has_title;
2300 };</code></pre>
2301 </div>    
2302
2303 <div class="slide">
2304   <h1>Uses for <code>before</code></h1>
2305
2306   <ul>
2307     <li>Logging/Debugging</li>
2308   </ul>
2309
2310   <pre><code>package Person;
2311 use Moose;
2312
2313 before work =&gt; sub {
2314     my $self = shift;
2315     return unless $DEBUG;
2316
2317     warn "Called work on ",
2318          $self-&gt;full_name,
2319          "with the arguments: [@_]\n";
2320 };</code></pre>
2321 </div>    
2322
2323 <div class="slide">
2324   <h1>Uses for <code>after</code></h1>
2325
2326   <ul>
2327     <li>Also works for logging/debugging</li>
2328     <li>Post-X side-effects (recording audit info)</li>
2329   </ul>
2330
2331   <pre><code>package Person;
2332 use Moose;
2333
2334 after work =&gt; sub {
2335     my $self = shift;
2336     $self-&gt;work_count(
2337         $self-&gt;work_count + 1 );
2338 };</code></pre>
2339 </div>
2340
2341 <div class="slide">
2342   <h1>Other Uses</h1>
2343
2344   <ul>
2345     <li>Modifiers are useful for adding behavior to generated methods</li>
2346   </ul>
2347 </div>
2348
2349 <div class="slide">
2350   <h1>More Modifier Examples</h1>
2351
2352   <pre><code>has password =&gt; (
2353      is      =&gt; 'rw',
2354      clearer =&gt; 'clear_password',
2355 );
2356 has hashed_password =&gt; (
2357      is      =&gt; 'ro',
2358      builder =&gt; '_build_hashed_password',
2359      clearer =&gt; '_clear_hashed_password',
2360 );
2361 after clear_password =&gt; sub {
2362     my $self = shift;
2363     $self-&gt;_clear_hashed_password;
2364 };</code></pre>
2365 </div>
2366
2367 <div class="slide">
2368   <h1><code>before</code> and <code>after</code> Limitations</h1>
2369
2370   <ul>
2371     <li>Cannot alter method parameters</li>
2372     <li>Cannot alter return value</li>
2373     <li>But <strong>can</strong> throw an exception</li>
2374   </ul>
2375 </div>
2376
2377 <div class="slide">
2378   <h1>The <code>around</code> Modifier</h1>
2379
2380   <ul>
2381     <li>The big gun</li>
2382     <li>Can alter parameters <strong>and/or</strong> return values</li>
2383     <li>Can skip calling the wrapped method entirely</li>
2384   </ul>
2385 </div>
2386
2387 <div class="slide">
2388   <h1>The power of <code>around</code></h1>
2389
2390   <pre><code>around insert =&gt; sub {
2391     my $orig = shift;
2392     my $self = shift;
2393
2394     $self-&gt;_validate_insert(@_);
2395
2396     my $new_user =
2397         $self-&gt;$orig(
2398             $self-&gt;_munge_insert(@_) );
2399
2400     $new_user-&gt;_assign_uri;
2401     return $new_user;
2402 };</code></pre>
2403 </div>
2404
2405 <div class="slide">
2406   <h1>Modifier Order</h1>
2407
2408   <ul>
2409     <li>Before runs in order from last to first</li>
2410     <li>After runs in order from first to last</li>
2411     <li>Around runs in order from last to first</li>
2412   </ul>
2413 </div>
2414
2415 <div class="slide">
2416   <h1>Modifier Order Illustrated</h1>
2417
2418 <pre>
2419 <span class="current incremental">before 2
2420  before 1</span>
2421   <span class="incremental">around 2
2422    around 1</span>
2423     <span class="incremental">wrapped method</span>
2424    <span class="incremental">around 1
2425   around 2</span>
2426  <span class="incremental">after 1
2427 after 2</span>
2428 </pre>
2429 </div>
2430
2431 <div class="slide">
2432   <h1>Modifiers in Roles</h1>
2433
2434   <ul>
2435     <li>Roles can use these modifiers</li>
2436     <li>Very powerful!</li>
2437   </ul>
2438 </div>
2439
2440 <div class="slide">
2441   <h1>Modifiers in Roles</h1>
2442
2443   <pre><code>package IsUnreliable;
2444 use Moose::Role;
2445
2446 <span class="highlight">requires 'run';
2447
2448 around run</span> =&gt; sub {
2449     my $orig = shift;
2450     my $self = shift;
2451
2452     return if rand(1) &lt; 0.5;
2453
2454     return $self-&gt;$orig(@_);
2455 };</code></pre>
2456 </div>
2457
2458 <div class="slide">
2459   <h1>Augment and Inner</h1>
2460
2461   <ul>
2462     <li>Inverted <code>super</code></li>
2463     <li>From least- to most-specific</li>
2464     <li>Like Mason's autohandler feature</li>
2465     <li>Grandparent to parent to child</li>
2466     <li>Not allowed in roles</li>
2467   </ul>
2468 </div>
2469
2470 <div class="slide">
2471   <h1>Augment and Inner</h1>
2472
2473   <pre><code>package Document;
2474
2475 sub xml { '&lt;doc&gt;' . <span class="highlight">inner()</span> . '&lt;/doc&gt;' }
2476
2477 package Report;
2478 extends 'Document';
2479 <span class="highlight">augment xml</span> =&gt;
2480     sub { title() . <span class="highlight">inner()</span> . summary() };
2481
2482 package TPSReport;
2483 extends 'Report';
2484 <span class="highlight">augment xml</span> =&gt;
2485     sub { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
2486 </div>
2487
2488 <div class="slide">
2489   <h1>Augment and Inner</h1>
2490
2491   <ul>
2492     <li>When we call <code>$tps-&gt;xml</code> ...
2493       <ul>
2494         <li><code>Document-&gt;xml</code></li>
2495         <li><code>Report-&gt;xml</code></li>
2496         <li><code>TPSReport-&gt;xml</code></li>
2497       </ul>
2498     </li>
2499   </ul>
2500 </div>
2501
2502 <div class="slide">
2503   <h1>Augment and Inner Usage</h1>
2504
2505   <ul>
2506     <li>Call <code>inner()</code> to "fill in the blank"</li>
2507     <li>Requires designing for subclassing</li>
2508     <li>Call <code>inner()</code> in the terminal class, just in case</li>
2509   </ul>
2510 </div>
2511
2512 <div class="slide">
2513   <h1>Method Modifiers Summary</h1>
2514
2515   <ul>
2516     <li>Use <code>before</code> and <code>after</code> for ...
2517       <ul>
2518         <li>logging</li>
2519         <li>pre- or post-validation</li>
2520         <li>to add behavior to generated methods</li>
2521       </ul>
2522     </li>
2523     <li>These two modifiers cannot change parameters or return values</li>
2524   </ul>
2525 </div>
2526
2527 <div class="slide">
2528   <h1>Method Modifiers Summary</h1>
2529
2530   <ul>
2531     <li>Use <code>around</code> to ...
2532       <ul>
2533         <li>alter parameters passed to the original method</li>
2534         <li>alter the return value of the original method</li>
2535         <li>not call the original method at all (or call a <em>different</em> method)</li>
2536       </ul>
2537     </li>
2538   </ul>
2539 </div>
2540
2541 <div class="slide">
2542   <h1>Method Modifiers Summary</h1>
2543
2544   <ul>
2545     <li>When using modifiers in a role, require the modified method</li>
2546     <li>Use <code>augment</code> and <code>inner</code> to invert the normal subclassing flow ...
2547       <ul>
2548         <li>Least- to most-specific (parents to children)</li>
2549         <li>Build in "insertability" (stick more stuff in the "middle")</li>
2550       </ul>
2551     </li>
2552     <li>Always call <code>inner</code> in the most specific subclass to allow for future extension</li>
2553   </ul>
2554 </div>
2555
2556 <div class="slide">
2557   <h1>Questions?</h1>
2558 </div>  
2559
2560 <div class="slide">
2561   <h1>Exercises</h1>
2562
2563   <pre># cd exercises
2564 # perl bin/prove -lv \
2565       t/04-method-modifiers.t
2566
2567 Iterate til this passes all its tests</pre>
2568 </div>
2569
2570 <div class="slide fake-slide0">
2571   <h1>Part 5: Types</h1>
2572 </div>
2573
2574 <div class="slide">
2575   <h1>A Type System for Perl</h1>
2576
2577   <ul>
2578     <li>Sort of ...</li>
2579     <li><em>Variables</em> are not typed</li>
2580     <li>Attributes can have types</li>
2581     <li>MooseX modules let you define method signatures</li>
2582   </ul>
2583 </div>
2584
2585 <div class="slide">
2586   <h1>Components of a Moose Type</h1>
2587
2588   <ul>
2589     <li>A type is a name and a constraint</li>
2590     <li>Types have a hierarchy</li>
2591     <li>Constraints are cumulative from parents</li>
2592     <li>Types can have associated coercions</li>
2593   </ul>
2594 </div>
2595
2596 <div class="slide">
2597   <h1>Built-in Type Hierarchy</h1>
2598
2599   <pre>
2600 Any
2601 Item
2602     Bool
2603     Maybe[`a]
2604     Undef
2605     Defined
2606         Value
2607             Str
2608                 Num
2609                     Int
2610                 ClassName
2611 </pre>
2612 </div>
2613
2614 <div class="slide">
2615   <h1>Built-in Type Hierarchy</h1>
2616
2617 <pre>
2618 (Item)
2619     (Defined)
2620         (Value)
2621         Ref
2622             ScalarRef
2623             ArrayRef[`a]
2624             HashRef[`a]
2625             CodeRef
2626             RegexpRef
2627             GlobRef
2628                 FileHandle
2629             Object
2630 </pre>
2631 </div>
2632
2633 <div class="slide">
2634   <h1>Bool</h1>
2635
2636   <h2>True</h2>
2637   <pre><code>1
2638 924.1
2639 'true'
2640 {}</code></pre>
2641
2642   <h2>False</h2>
2643   <pre><code>0
2644 0.0
2645 '0'
2646 undef</code></pre>
2647
2648   <ul>
2649     <li>Like Perl's <code>if ($foo)</code></li>
2650   </ul>
2651 </div>
2652
2653 <div class="slide">
2654   <h1>Value (and subtypes)</h1>
2655
2656   <ul>
2657     <li><code>Value</code> is true when <code>! ref $thing</code></li>
2658     <li><code>Value</code> and <code>Str</code> are effectively the same, but <code>Str</code> is more expressive</li>
2659     <li><code>Num</code> is true when a <code>$scalar</code> looks like a number</li>
2660     <li>An overloaded object which numifies does not pass the <code>Num</code> constraint!</li>
2661     <li>Perl 5's overloading is hopelessly broken</li>
2662   </ul>
2663 </div>
2664
2665 <div class="slide">
2666   <h1>ClassName and RoleName</h1>
2667
2668   <ul>
2669     <li>A string with a package name</li>
2670     <li>The package <strong>must already be loaded</strong></li>
2671   </ul>
2672 </div>
2673
2674 <div class="slide">
2675   <h1>Parameterizable Types</h1>
2676
2677   <ul>
2678     <li>What does <code>ArrayRef[`a]</code> mean?</li>
2679     <li><code>s/`a/Int/</code> (or <code>Str</code> or ...)</li>
2680     <li>When you use it you can write ...
2681       <ul>
2682         <li><code>ArrayRef</code> (== <code>ArrayRef[Item]</code>)</li>
2683         <li><code>ArrayRef[Str]</code></li>
2684         <li><code>ArrayRef[MyTypeName]</code></li>
2685         <li><code>ArrayRef[HashRef[Maybe[Int]]]</code></li>
2686       </ul>
2687     </li>
2688   </ul>
2689 </div>
2690
2691 <div class="slide">
2692   <h1>Maybe[`a]</h1>
2693
2694   <ul>
2695     <li>Maybe means either the named type or <code>undef</code></li>
2696     <li><code>Maybe[Int]</code> accepts integers or <code>undef</code></li>
2697   </ul>
2698 </div>
2699
2700 <div class="slide">
2701   <h1>Type Union</h1>
2702
2703   <ul>
2704     <li>This or that (or that or ...)</li>
2705     <li><code>Int | ArrayRef[Int]</code></li>
2706     <li>But use a coercion instead when possible</li>
2707     <li>Or use a <code>role_type</code>,  <code>duck_type</code>, or anything not a union</li>
2708     <li>A union is often a code smell</li>
2709   </ul>
2710 </div>
2711
2712 <div class="slide">
2713   <h1>Making Your Own Types</h1>
2714
2715   <pre><code>use Moose::Util::TypeConstraints;
2716
2717 <span class="incremental current">subtype 'PositiveInt',</span>
2718     <span class="incremental">as      'Int',</span>
2719     <span class="incremental">where   { $_ &gt; 0 },</span>
2720     <span class="incremental">message
2721         { "The value you provided ($_)"
2722           . " was not a positive int." };</span>
2723
2724 has size =&gt; (
2725     is  =&gt; 'ro',
2726     <span class="incremental">isa =&gt; 'PositiveInt',</span>
2727 );</code></pre>
2728 </div>
2729
2730 <div class="slide">
2731   <h1>Automatic Types</h1>
2732
2733   <ul>
2734     <li>Moose creates a type for every Moose class and role</li>
2735     <li>Unknown names are assumed to be classes</li>
2736   </ul>
2737 </div>
2738
2739 <div class="slide">
2740   <h1>Automatic Types</h1>
2741
2742   <pre><code>package Employee;
2743 use Moose;
2744
2745 has manager =&gt; (
2746     is  =&gt; 'rw',
2747     <span class="highlight">isa =&gt; 'Employee',</span>
2748 );
2749
2750 has start_date =&gt; (
2751     is  =&gt; 'ro',
2752     <span class="highlight">isa =&gt; 'DateTime',</span>
2753 );</code></pre>
2754 </div>
2755
2756 <div class="slide">
2757   <h1>Subtype Shortcuts - <code>class_type</code></h1>
2758
2759   <pre><code>use Moose::Util::TypeConstraints;
2760 class_type 'DateTime';</code></pre>
2761
2762 <hr />
2763
2764 <pre><code>subtype 'DateTime',
2765     as      'Object',
2766     where   { $_-&gt;isa('DateTime') },
2767     message { ... };</code></pre>
2768 </div>
2769
2770 <div class="slide">
2771   <h1>Subtype Shortcuts - <code>role_type</code></h1>
2772
2773   <pre><code>use Moose::Util::TypeConstraints;
2774 role_type 'Printable';</coe></pre>
2775
2776 <hr />
2777
2778 <pre><code>subtype 'Printable',
2779     as      'Object',
2780     where
2781         { Moose::Util::does_role(
2782               $_, 'Printable' ) },
2783     message { ... };</code></pre>
2784 </div>
2785
2786 <div class="slide">
2787   <h1>Subtype Shortcuts - <code>duck_type</code></h1>
2788
2789   <pre><code>use Moose::Util::TypeConstraints;
2790 duck_type Car =&gt; qw( run break_down );</code></pre>
2791
2792 <hr />
2793
2794 <pre><code>subtype 'Car',
2795     as      'Object',
2796     where   { all { $_-&gt;can($_) }
2797               qw( run break_down ) },
2798     message { ... };</code></pre>
2799 </div>
2800
2801 <div class="slide">
2802   <h1>Subtype Shortcuts - <code>enum</code></h1>
2803
2804   <pre><code>use Moose::Util::TypeConstraints;
2805 enum Color =&gt; qw( red blue green ) );</code></pre>
2806
2807 <hr />
2808
2809 <pre><code>my %ok = map { $_ =&gt; 1 }
2810              qw( red blue green );
2811
2812 subtype 'Color'
2813     as      'Str',
2814     where   { $ok{$_} },
2815     message { ... };</code></pre>
2816 </div>
2817
2818 <div class="slide">
2819   <h1>Anonymous Subtypes</h1>
2820
2821   <pre><code>package Person;
2822
2823 <span class="highlight">my $posint =
2824     subtype as 'Int', where { $_ &gt; 0 };</span>
2825
2826 has size =&gt; (
2827     is  =&gt; 'ro',
2828     <span class="highlight">isa =&gt; $posint,</span>
2829 );</code></pre>
2830
2831   <ul>
2832     <li>Shortcuts have anonymous forms as well</li>
2833   </ul>
2834 </div>
2835
2836 <div class="slide">
2837   <h1>Coercions</h1>
2838
2839   <pre><code>use Moose::Util::TypeConstraints;
2840
2841 subtype 'UCStr',
2842     as    'Str',
2843     where { ! /[a-z]/ };</code></pre>
2844 </div>
2845
2846 <div class="slide">
2847   <h1>Coercions</h1>
2848
2849   <pre><code><span class="incremental current">coerce 'UCStr',</span>
2850     <span class="incremental">from 'Str',</span>
2851     <span class="incremental">via  { uc };</span>
2852
2853 has shouty_name =&gt; (
2854     is     =&gt; 'ro',
2855     isa    =&gt; 'UCStr',
2856     <span class="incremental">coerce =&gt; 1,</span>
2857 );</code></pre>
2858 </div>
2859
2860 <div class="slide">
2861   <h1>Coercion Examples</h1>
2862
2863   <pre><code>subtype 'My::DateTime',
2864     as class_type 'DateTime';
2865
2866 coerce 'My::DateTime',
2867     from 'HashRef',
2868     via  { DateTime-&gt;new( %{$_} ) };
2869
2870 coerce 'My::DateTime',
2871     from 'Int',
2872     via  { DateTime-&gt;from_epoch(
2873                epoch =&gt; $_ ) };</code></pre>
2874
2875   <ul>
2876     <li>Use coercion to inflate a value</li>
2877   </ul>
2878 </div>
2879
2880 <div class="slide">
2881   <h1>Coercion Examples</h1>
2882
2883   <pre><code>coerce 'ArrayRef[Int]',
2884     from 'Int',
2885     via  { [ $_ ] };</code></pre>
2886
2887   <ul>
2888     <li>Instead of union - <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
2889   </ul>
2890 </div>
2891
2892 <div class="slide">
2893   <h1>Using Types with Attributes</h1>
2894
2895   <pre><code>package Person;
2896
2897 has height =&gt; (
2898     is  =&gt; 'rw',
2899     <span class="highlight">isa =&gt; 'Num',</span>
2900 );
2901
2902 has favorite_numbers =&gt; (
2903     is     =&gt; 'rw',
2904     <span class="highlight">isa    =&gt; 'ArrayRef[Int]',
2905     coerce =&gt; 1,</span>
2906 );</code></pre>
2907 </div>
2908
2909 <div class="slide">
2910   <h1>More Droppings</h1>
2911
2912   <ul>
2913     <li><code>Moose::Util::TypeConstraints</code> also needs cleanup</li>
2914   </ul>
2915
2916   <pre><code>package Person;
2917
2918 use Moose;
2919 use Moose::Util::TypeConstraints;
2920
2921 subtype ...;
2922
2923 no Moose;
2924 <span class="highlight">no Moose::Util::TypeConstraints;</span></code></pre>
2925 </div>
2926
2927 <div class="slide">
2928   <h1>Typed Methods (Low-tech)</h1>
2929
2930   <pre class="medium"><code>package Person;
2931 <span class="highlight">use MooseX::Params::Validate qw( validated_list );</span>
2932
2933 sub work {
2934     my $self = shift;
2935     <span class="highlight">my ( $tasks, $can_rest ) =
2936         validated_list(
2937             \@_,
2938             tasks    =&gt;
2939                 { isa    =&gt; 'ArrayRef[Task]',
2940                   coerce =&gt; 1 },
2941             can_rest =&gt;
2942                 { isa     =&gt; 'Bool',
2943                   default =&gt; 0 },
2944         );</span>
2945     ...
2946 }</code></pre>
2947 </div>
2948
2949 <div class="slide">
2950   <h1>Typed Methods (High-tech)</h1>
2951
2952   <pre class="medium"><code>package Person;
2953
2954 <span class="highlight">use MooseX::Method::Signatures;</span>
2955
2956 <span class="highlight">method work ( ArrayRef[Task] :$tasks,
2957                         Bool :$can_rest = 0 )</span> {
2958     my $self = shift;
2959
2960     ...
2961 }</code></pre>
2962 </div>
2963
2964 <div class="slide">
2965   <h1>Digression: The Type Registry</h1>
2966
2967   <ul>
2968     <li>Types are actually <code>Moose::Meta::TypeConstraints</code> <em>objects</em></li>
2969     <li>Stored in an interpreter-global registry mapping names to objects</li>
2970   </ul>
2971 </div>
2972
2973 <div class="slide">
2974   <h1>Danger!</h1>
2975
2976   <ul>
2977     <li>Coercions are attached to type objects</li>
2978     <li>Therefore also global</li>
2979     <li>Name conflicts between modules!</li>
2980     <li>Coercion conflicts between modules!</li>
2981   </ul>
2982 </div>
2983
2984 <div class="slide">
2985   <h1>Namespace Fix</h1>
2986
2987   <ul>
2988     <li>Use some sort of pseudo-namespacing scheme</li>
2989     <li>Never coerce directly to a class name, or <em>to</em> built-in types</li>
2990   </ul>
2991 </div>
2992
2993 <div class="slide">
2994   <h1>Namespace Fix</h1>
2995
2996   <pre><code>use Moose::Util::TypeConstraints;
2997 subtype <span class="highlight">'MyApp::Type::DateTime',</span>
2998     as 'DateTime';
2999
3000 <span class="highlight">coerce 'MyApp::Type::DateTime',</span>
3001     from 'HashRef',
3002     via  { DateTime-&gt;new( %{$_} ) }
3003
3004 has creation_date =&gt; (
3005     is     =&gt; 'ro',
3006     <span class="highlight">isa    =&gt; 'MyApp::Type::DateTime',</span>
3007     coerce =&gt; 1,
3008 );</code></pre>
3009 </div>
3010
3011 <div class="slide">
3012   <h1>Namespace Fix</h1>
3013
3014   <pre><code>subtype 'MyApp::Type::ArrayOfInt',
3015     as 'ArrayRef[Int]';
3016
3017 coerce 'MyApp::Type::ArrayOfInt',
3018     from 'Int',
3019     via  { [ $_ ] };</code></pre>
3020 </div>
3021
3022 <div class="slide">
3023   <h1>Namespace Fix Pros and Cons</h1>
3024
3025   <ul>
3026     <li><span class="right">Relatively simple</span></li>
3027     <li><span class="right">Already built into Moose</span></li>
3028     <li><span class="wrong">Conflates type and module namespaces</span></li>
3029     <li><span class="wrong">Type names are strings, so typos are easy to make and may be hard to find</span></li>
3030   </ul>
3031 </div>
3032
3033 <div class="slide">
3034   <h1>MooseX::Types</h1>
3035
3036   <pre><code>package MyApp::Types;
3037
3038 use MooseX::Types
3039     <span class="highlight">-declare =&gt; [ qw( ArrayOfInt ) ]</span>;
3040 use MooseX::Types::Moose
3041     qw( ArrayRef Int );
3042
3043 subtype <span class="highlight">ArrayOfInt</span>,
3044     as ArrayRef[Int];
3045
3046 coerce <span class="highlight">ArrayOfInt</span>
3047     from Int,
3048     via  { [ $_ ] };</code></pre>
3049 </div>
3050
3051 <div class="slide">
3052   <h1>MooseX::Types</h1>
3053
3054   <pre><code>package MyApp::Account;
3055
3056 use MyApp::Types qw( ArrayOfInt );
3057
3058 has transaction_history => (
3059     is  => 'rw',
3060     isa => ArrayOfInt,
3061 );</code></pre>
3062 </div>
3063
3064 <div class="slide">
3065   <h1>MooseX::Types</h1>
3066
3067   <ul>
3068     <li>Type names are exported functions, catches typos early</li>
3069     <li>Types must be pre-declared</li>
3070     <li>Types are stored with namespaces internally, but you use short names</li>
3071     <li>Import existing Moose types as functions from <code>MooseX::Types::Moose</code></li>
3072     <li>Still need string names for things like <code>ArrayRef['Email::Address']</code></li>
3073   </ul>
3074 </div>
3075
3076 <div class="slide">
3077   <h1>MooseX::Types Pros and Cons</h1>
3078
3079   <ul>
3080     <li><span class="right">Catches typos at compile time</span></li>
3081     <li><span class="right">Automatic namespacing</span></li>
3082     <li><span class="wrong">One more thing to install and learn</span></li>
3083     <li><span class="wrong">Every name is typed twice (declared and then defined)</span></li>
3084     <li><span class="wrong">Still stuck with strings when referring to class or role names</span></li>
3085     <li><span class="wrong">Coercion gotcha from earlier still applies to types exported from <code>MooseX::Types::Moose</code></span></li>
3086   </ul>
3087 </div>
3088
3089 <div class="slide">
3090   <h1>Recommendation</h1>
3091
3092   <ul>
3093     <li>Use <code>MooseX::Types</code></li>
3094     <li>Compile time error catching and automatic namespacing are huge wins</li>
3095     <li>Docs from <code>Moose::Util::TypeConstraints</code> are 98% compatible with <code>MooseX::Types</code> anyway</li>
3096     <li>A function exported by a type library works wherever a type name would</li>
3097   </ul>
3098 </div>
3099
3100 <div class="slide">
3101   <h1>Questions?</h1>
3102 </div>  
3103
3104 <div class="slide">
3105   <h1>Exercises</h1>
3106
3107   <pre># cd exercises
3108 # perl bin/prove -lv t/05-types.t
3109
3110 Iterate til this passes all its tests</pre>
3111 </div>
3112
3113 <div class="slide fake-slide0">
3114   <h1>Part 6: Advanced Attributes</h1>
3115 </div>
3116
3117 <div class="slide">
3118   <h1>Weak References</h1>
3119
3120   <ul>
3121     <li>A weak reference lets you avoid circular references</li>
3122     <li>Weak references do not increase the reference count</li>
3123   </ul>
3124 </div>
3125
3126 <div class="slide">
3127   <h1>Circular Reference Illustrated</h1>
3128
3129   <pre><code>my $foo = {};
3130 my $bar = { foo =&gt; $foo };
3131 $foo-&gt;{bar} = $bar;</code></pre>
3132
3133   <ul>
3134     <li>Neither <code>$foo</code> nor <code>$bar</code> go out of scope<br />
3135         (until the program exits)</li>
3136   </ul>
3137 </div>
3138
3139 <div class="slide">
3140   <h1>Weakening Circular References</h1>
3141
3142   <pre><code>use Scalar::Util qw( weaken );
3143
3144 my $foo = {};
3145 my $bar = { foo =&gt; $foo };
3146 $foo-&gt;{bar} = $bar;
3147 weaken $foo-&gt;{bar}</code></pre>
3148
3149   <ul>
3150     <li>When <code>$bar</code> goes out of scope, <code>$foo-&gt;{bar}</code> becomes <code>undef</code></li>
3151   </ul>
3152 </div>
3153
3154 <div class="slide">
3155   <h1>Circular References in Attributes</h1>
3156
3157   <pre><code>package Person;
3158 use Moose;
3159
3160 has name   =&gt; ( is =&gt; 'ro' );
3161 has friend =&gt; ( is =&gt; 'rw' );
3162
3163 my $alice = Person-&gt;new( name =&gt; 'Alice' );
3164 my $bob   = Person-&gt;new( name =&gt; 'Bob' );
3165 $bob-&gt;friend($alice);
3166 $alice-&gt;friend($bob);</code></pre>
3167 </div>
3168
3169 <div class="slide">
3170   <h1>The Fix</h1>
3171
3172   <pre><code>package Person;
3173 use Moose;
3174
3175 has name   =&gt; ( is =&gt; 'ro' );
3176 has friend =&gt; ( is       =&gt; 'rw',
3177                 <span class="highlight">weak_ref =&gt; 1</span> );
3178
3179 my $alice = Person-&gt;new( name =&gt; 'Alice' );
3180 my $bob   = Person-&gt;new( name =&gt; 'Bob' );
3181 $bob-&gt;friend($alice);
3182 $alice-&gt;friend($bob);</code></pre>
3183 </div>
3184
3185 <div class="slide">
3186   <h1>Under the Hood</h1>
3187
3188   <ul>
3189     <li>A <code>weak_ref</code> attribute calls <code>weaken</code> ...
3190       <ul>
3191         <li>during object construction</li>
3192         <li>when the attribute is set via a writer</li>
3193       </ul>
3194     </li>
3195   </ul>
3196 </div>
3197
3198 <div class="slide">
3199   <h1>Triggers</h1>
3200
3201   <ul>
3202     <li>A code reference run after an attribute is <em>set</em></li>
3203     <li>Like an <code>after</code> modifier, but makes intentions clearer</li>
3204   </ul>
3205
3206   <h2 class="wrong">Gross</h2>
3207
3208   <pre><code>after salary_level =&gt; {
3209     my $self = shift;
3210     <span class="highlight">return unless @_;</span>
3211     $self-&gt;clear_salary;
3212 };</code></pre>
3213 </div>
3214
3215 <div class="slide">
3216   <h1>Use a Trigger Instead</h1>
3217
3218   <h2 class="right">Cleaner</h2>
3219
3220   <pre><code>has salary_level =&gt; (
3221     is      =&gt; 'rw',
3222     trigger =&gt;
3223         sub { $_[0]-&gt;clear_salary },
3224 );</code></pre>
3225 </div>
3226
3227 <div class="slide">
3228   <h1>Trigger Arguments</h1>
3229
3230   <ul>
3231     <li><code>$self</code></li>
3232     <li><code>$new_value</code></li>
3233     <li><code>$old_value</code> - if one exists</li>
3234   </ul>
3235 </div>
3236
3237 <div class="slide">
3238   <h1>Delegation</h1>
3239
3240   <ul>
3241     <li>Attributes can be objects</li>
3242     <li>Delegation transparently calls methods on those objects</li>
3243   </ul>
3244 </div>
3245
3246 <div class="slide">
3247   <h1>Delegation Examples</h1>
3248
3249   <pre><code>package Person;
3250
3251 has lungs =&gt; (
3252     is      =&gt; 'ro',
3253     isa     => 'Lungs',
3254     <span class="highlight">handles =&gt; [ 'inhale', 'exhale' ],</span>
3255 );</code></pre>
3256
3257   <ul>
3258     <li>Creates <code>$person-&gt;inhale</code> and <code>-&gt;exhale</code> methods</li>
3259     <li>Internally calls <code>$person-&gt;lungs-&gt;inhale</code></li>
3260   </ul>
3261 </div>
3262
3263 <div class="slide">
3264   <h1>Why Delegation?</h1>
3265
3266   <ul>
3267     <li>Reduce the number of classes exposed</li>
3268     <li>Re-arrange class internals -<br />
3269         turn a method into an attribute with delegation</li>
3270     <li>Provide convenenience methods</li>
3271   </ul>
3272 </div> 
3273
3274 <div class="slide">
3275   <h1>Moose's <code>handles</code> Parameter</h1>
3276
3277   <ul>
3278     <li>Accepts many arguments ...
3279       <ul>
3280         <li>Array reference - list of methods to delegate as-is</li>
3281         <li>Hash reference - map of method names</li>
3282         <li>Regex - delegates all matching methods</li>
3283         <li>Role name - delegates all methods in the role</li>
3284         <li>Sub reference - does something complicated ;)</li>
3285       </ul>
3286     </li>
3287   </ul>
3288 </div>      
3289
3290 <div class="slide">
3291   <h1>Array Reference</h1>
3292
3293   <ul>
3294     <li>1-to-1 mapping</li>
3295     <li>Takes each method name and creates a simple delegation from the delegating class to the delegatee attribute</li>
3296   </ul>
3297 </div>
3298
3299 <div class="slide">
3300   <h1>Hash Reference</h1>
3301
3302   <ul>
3303     <li>Mapping of names in the delegating class to the delegatee class</li>
3304   </ul>
3305
3306   <pre><code>package Person;
3307 use Moose;
3308 has account =&gt; (
3309     is      =&gt; 'ro',
3310     isa     =&gt; 'BankAccount',
3311     <span class="highlight">handles =&gt; {
3312         receive_money =&gt; 'deposit',
3313         give_money    =&gt; 'withdraw',
3314     },</span>
3315 );</code></pre>
3316 </div>
3317
3318 <div class="slide">
3319   <h1>Hash Reference Detailed</h1>
3320
3321   <pre><code>    handles =&gt; {
3322         receive_money =&gt; 'deposit',
3323         give_money    =&gt; 'withdraw',
3324     },</code></pre>
3325
3326   <ul>
3327     <li><code>$person-&gt;receive_money</code> = <code>$person-&gt;account-&gt;deposit</code></li>
3328     <li><code>$person-&gt;give_money</code> = <code>$person-&gt;account-&gt;withdraw</code></li>
3329   </ul>
3330 </div>
3331
3332 <div class="slide">
3333   <h1>Regex</h1>
3334
3335   <pre><code>package Person;
3336 use Moose;
3337
3338 has name =&gt; (
3339     is      =&gt; 'ro',
3340     isa     =&gt; 'Name',
3341     handles =&gt; qr/.*/,
3342 );</code></pre>
3343
3344   <ul>
3345     <li>Creates a delegation for every method in the Name class</li>
3346     <li>Excludes <code>meta</code> and methods inherited from <code>Moose::Object</code></li>
3347   </ul>
3348 </div>
3349
3350 <div class="slide">
3351   <h1>Role Name</h1>
3352
3353   <pre><code>package Auditor;
3354 use Moose::Role;
3355 sub record_change  { ... }
3356 sub change_history { ... }
3357
3358 package Account;
3359 use Moose;
3360
3361 has history =&gt; (
3362     is      =&gt; 'ro',
3363     does    =&gt; 'Auditor',
3364     <span class="highlight">handles =&gt; 'Auditor',</span>
3365 );</code></pre>
3366 </div>
3367
3368 <div class="slide">
3369   <h1>Role Name Detailed</h1>
3370
3371   <ul>
3372     <li>Account gets delegate methods for each method in the <code>Auditor</code> role
3373       <ul>
3374         <li>record_history</li>
3375         <li>change_history</li>
3376       </ul>
3377     </li>
3378   </ul>
3379 </div>
3380
3381 <div class="slide">
3382   <h1>Native Delegation</h1>
3383
3384   <ul>
3385     <li>Delegate to <em>unblessed</em> Perl types</li>
3386     <li>Scalar, array or hash ref, etc</li>
3387     <li>Treat Perl types as objects</li>
3388     <li>Still uses <code>handles</code></li>
3389     <li>Pretend that native Perl types have methods</li>
3390   </ul>
3391 </div>
3392
3393 <div class="slide">
3394   <h1>Native Delegation - Array(Ref)</h1>
3395
3396   <ul>
3397     <li>Methods include:
3398       <ul>
3399         <li><code>push</code></li>
3400         <li><code>shift</code></li>
3401         <li><code>elements</code> - returns all elements</li>
3402         <li><code>count</code></li>
3403         <li><code>is_empty</code></li>
3404         <li>quite a few more</li>
3405       </ul>
3406     </li>
3407   </ul>
3408 </div>
3409
3410 <div class="slide">
3411   <h1>Native Delegation - Array(Ref)</h1>
3412
3413   <pre><code>package Person;
3414 use Moose;
3415 has _favorite_numbers =&gt; (
3416     traits   =&gt; [ 'Array' ],
3417     is       =&gt; 'ro',
3418     isa      =&gt; 'ArrayRef[Int]',
3419     default  =&gt; sub { [] },
3420     init_arg =&gt; undef,
3421     <span class="highlight">handles  =&gt;
3422       { favorite_numbers    =&gt; 'elements',
3423         add_favorite_number =&gt; 'push',
3424       },</span>
3425 );</code></pre>
3426 </div>
3427
3428 <div class="slide">
3429   <h1>Native Delegation - Array(Ref)</h1>
3430
3431   <pre><code>my $person = Person-&gt;new();
3432
3433 $person-&gt;add_favorite_number(7);
3434 $person-&gt;add_favorite_number(42);
3435
3436 print "$_\n"
3437     for $person-&gt;favorite_numbers;
3438
3439 # 7
3440 # 42</code></pre>
3441 </div>
3442
3443 <div class="slide">
3444   <h1>Native Delegation</h1>
3445
3446   <ul>
3447     <li>Native types are ...
3448       <ul>
3449         <li>Number - <code>add</code>, <code>mul</code>, ...</li>
3450         <li>String - <code>append</code>, <code>chop</code>, ...</li>
3451         <li>Counter - <code>inc</code>, <code>dec</code>, ...</li>
3452         <li>Bool - <code>set</code>, <code>toggle</code>, ...</li>
3453         <li>Hash - <code>get</code>, <code>set</code>, ...</li>
3454         <li>Array - already saw it</li>
3455         <li>Code - <code>execute</code> and <code>execute_method</code></li>
3456       </ul>
3457     </li>
3458   </ul>
3459 </div>
3460
3461 <div class="slide">
3462   <h1>Curried Delegation</h1>
3463
3464   <ul>
3465     <li>A delegation with some preset arguments</li>
3466     <li>Works with object or Native delegation</li>
3467   </ul>
3468 </div>
3469
3470 <div class="slide">
3471   <h1>Curried Delegation</h1>
3472
3473   <pre><code>package Person;
3474 use Moose;
3475 has account =&gt; (
3476     is      =&gt; 'ro',
3477     isa     =&gt; 'BankAccount',
3478     handles =&gt; {
3479         receive_100 =&gt;
3480             <span class="highlight">[ 'deposit', 100 ]</span>
3481         give_100    =&gt;
3482             <span class="highlight">[ 'withdraw', 100 ]</span>
3483     },
3484 );</code></pre>
3485 </div>
3486
3487 <div class="slide">
3488   <h1>Curried Delegation</h1>
3489
3490   <pre><code>$person-&gt;receive_100;
3491 # really is
3492 $person-&gt;account-&gt;deposit(100);</code></pre>
3493 </div>
3494
3495 <div class="slide">
3496   <h1>Traits and Metaclasses</h1>
3497
3498   <ul>
3499     <li>The ultimate in customization</li>
3500     <li>Per attribute metaclasses</li>
3501     <li>Per attribute roles applied to the attribute metaclass</li>
3502     <li>Change the meta-level behavior</li>
3503   </ul>
3504 </div>
3505
3506 <div class="slide">
3507   <h1>Traits and Metaclasses</h1>
3508
3509   <ul>
3510     <li>The default metaclass is <code>Moose::Meta::Attribute</code></li>
3511     <li>Controls accessor generation, defaults, delegation, etc.</li>
3512     <li>Adding a role to this metaclass (or replacing it) allows for infinite customization</li>
3513   </ul>
3514 </div>
3515
3516 <div class="slide">
3517   <h1>Traits and Metaclasses</h1>
3518
3519   <ul>
3520     <li>Can add/alter/remove an attribute parameter (from <code>has</code>)</li>
3521     <li>Can change behavior of created attribute</li>
3522   </ul>
3523 </div>
3524
3525 <div class="slide">
3526   <h1>Simple Trait Example</h1>
3527
3528   <pre><code>package Person;
3529 use Moose;
3530 use MooseX::LabeledAttributes;
3531
3532 has ssn =&gt; (
3533     <span class="highlight">traits =&gt; [ 'Labeled' ],</span>
3534     is     =&gt; 'ro',
3535     isa    =&gt; 'Str',
3536     <span class="highlight">label  =&gt; 'Social Security Number',</span>
3537 );
3538 print <span class="highlight">Person-&gt;meta
3539             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
3540 </div>
3541
3542 <div class="slide">
3543   <h1>Simple Metaclass Example</h1>
3544
3545   <pre><code>package Person;
3546 use Moose;
3547 use MooseX::LabeledAttributes;
3548
3549 has ssn =&gt; (
3550     <span class="highlight">metaclass =&gt;
3551         'MooseX::Meta::Attribute::Labeled',</span>
3552     is        =&gt; 'ro',
3553     isa       =&gt; 'Str',
3554     <span class="highlight">label     =&gt; 'Social Security Number',</span>
3555 );
3556 print <span class="highlight">Person-&gt;meta
3557             -&gt;get_attribute('ssn')-&gt;label;</span></code></pre>
3558 </div>
3559
3560 <div class="slide">
3561   <h1>Traits vs Metaclass</h1>
3562
3563   <ul>
3564     <li>Can apply any mix of traits to an attribute</li>
3565     <li>But just one metaclass</li>
3566     <li>Traits (aka roles) can cooperate</li>
3567     <li>Metaclasses require you to pick just one</li>
3568   </ul>
3569 </div>
3570
3571 <div class="slide">
3572   <h1>Advanced Attributes Summary</h1>
3573
3574   <ul>
3575     <li>Use <code>weak_ref</code> to avoid circular references</li>
3576     <li>Use trigger to do an action post-attribute write</li>
3577     <li>Use delegations to hide "internal" objects</li>
3578     <li>Traits and metaclasses let you extend Moose's core attribute features</li>
3579   </ul>
3580 </div>
3581
3582 <div class="slide">
3583   <h1>Questions?</h1>
3584 </div>  
3585
3586 <div class="slide">
3587   <h1>Exercises</h1>
3588
3589   <pre># cd exercises
3590 # perl bin/prove -lv \
3591       t/06-advanced-attributes.t
3592
3593 Iterate til this passes all its tests</pre>
3594 </div>
3595
3596 <div class="slide fake-slide0">
3597   <h1>Part 7: Introspection</h1>
3598 </div>
3599
3600 <div class="slide fake-slide0">
3601   <h1>Part 8: A Brief Tour of MooseX</h1>
3602 </div>
3603
3604 <div class="slide">
3605   <h1>Notable MX Modules on CPAN</h1>
3606
3607   <ul>
3608     <li><strong>Not comprehensive</strong></li>
3609     <li>152 MooseX distributions on CPAN as of 02/02/2010</li>
3610     <li>Some of them are crap</li>
3611   </ul>
3612 </div>
3613
3614 <div class="slide">
3615   <h1>Already Mentioned Several</h1>
3616
3617   <ul>
3618     <li><code>MooseX::NonMoose</code> - best solution for subclassing non-Moose parents</li>
3619     <li><code>MooseX::Declare</code> - <em>real</em> Perl 5 OO</li>
3620     <li><code>MooseX::FollowPBP</code> and <code>MooseX::SemiAffordanceAccessor</code></li>
3621     <li><code>MooseX::Params::Validate</code> and <code>MooseX::Method::Signatures</code></li>
3622     <li><code>MooseX::Types</code></li>
3623   </ul>
3624 </div>    
3625
3626 <div class="slide">
3627   <h1>MooseX::Declare</h1>
3628
3629 <pre><code>use MooseX::Declare;
3630 use 5.10.0; # for say
3631
3632 class Person {
3633     has greeting =&gt;
3634         ( is =&gt; 'ro', isa =&gt; 'Str' );
3635
3636     method speak {
3637         say $self-&gt;greeting;
3638     }
3639 }</code></pre>
3640 </div>
3641
3642 <div class="slide">
3643   <h1>MooseX::Declare</h1>
3644
3645   <ul>
3646     <li>Still experimental-ish, but seeing more and more use</li>
3647     <li><strong>Not</strong> a source filter!</li>
3648     <li>Hooks into the Perl parser rather than filtering all your code</li>
3649     <li>But not supported by <code>PPI</code>, <code>perltidy</code>, etc.</li> (yet?)
3650   </ul>
3651 </div>
3652
3653 <div class="slide">
3654   <h1>MooseX::StrictConstructor</h1>
3655
3656   <ul>
3657     <li>By default, unknown constructor arguments are ignored</li>
3658     <li>MX::StrictConstructor turns these into an error</li>
3659   </ul>
3660 </div>
3661
3662 <div class="slide">
3663   <h1>MooseX::StrictConstructor</h1>
3664
3665   <pre><code>package Person;
3666
3667 use Moose;
3668 <span class="highlight">use MooseX::StrictConstructor;</span>
3669
3670 has name =&gt; ( is =&gt; 'ro' );
3671
3672 Person-&gt;new
3673     ( na<span class="wrong">n</span>e =&gt; 'Ringo Shiina' ); # kaboom</code></pre>
3674 </div>
3675
3676 <div class="slide">
3677   <h1>MooseX::Traits</h1>
3678
3679   <ul>
3680     <li>Combines object construction and role application</li>
3681     <li>Makes it easy to create one-off customized objects</li>
3682   </ul>
3683 </div>
3684
3685 <div class="slide">
3686   <h1>MooseX::Traits</h1>
3687
3688   <pre><code>package MyApp::Thingy;
3689 use Moose;
3690
3691 <span class="highlight">with 'MooseX::Traits';</span>
3692
3693 my $thing =
3694     MyApp::Thingy-&gt;<span class="highlight">new_with_traits</span>
3695         ( <span class="highlight">traits =&gt; [ 'Foo', 'Bar' ],</span>
3696           size   =&gt; 42 );</code></pre>
3697 </div>
3698
3699 <div class="slide">
3700   <h1>MooseX::Getopt</h1>
3701
3702   <ul>
3703     <li>Makes command-line interface programs easy!</li>
3704     <li>Construct an object from CLI arguments</li>
3705   </ul>
3706 </div>
3707
3708 <div class="slide">
3709   <h1>MooseX::Getopt</h1>
3710
3711   <pre><code>package App::CLI;
3712 use Moose;
3713
3714 <span class="highlight">with 'MooseX::Getopt';</span>
3715
3716 has file    =&gt;
3717     ( is =&gt; 'ro', required =&gt; 1 );
3718 has filters =&gt;
3719     ( is =&gt; 'ro', isa =&gt; 'ArrayRef[Str]' );
3720
3721 sub run { ... }</code></pre>
3722 </div>
3723
3724 <div class="slide">
3725   <h1>MooseX::Getopt</h1>
3726
3727   <ul>
3728     <li>Then call it like this:</li>
3729   </ul>
3730
3731 <pre><code>#!/usr/bin/perl
3732
3733 use App::CLI;
3734
3735 <span class="highlight">App::CLI-&gt;new_with_options()</span>-&gt;run();</code></pre>
3736
3737 <pre>$ myapp-cli \
3738    --file foo \
3739    --filters compress \
3740    --filters sanitize</pre>
3741 </div>
3742
3743 <div class="slide">
3744   <h1>MooseX::Clone</h1>
3745
3746   <pre><code>package Person;
3747
3748 use Moose;
3749 <span class="highlight">with 'MooseX::Clone';</span>
3750
3751 my $person = Person-&gt;new;
3752 my $clone  = <span class="highlight">$person-&gt;clone;</span></code></pre>
3753 </div>
3754
3755 <div class="slide">
3756   <h1>MooseX::NonMoose</h1>
3757
3758   <ul>
3759     <li>Highly recommended for subclassing non-Moose parents</li>
3760     <li>Gets all the little annoying details right</li>
3761   </ul>
3762 </div>
3763
3764 <div class="slide">
3765   <h1>MooseX::Role::Parameterized</h1>
3766
3767   <pre><code>package HasCollection;
3768 <span class="current incremental">use MooseX::Role::Parameterized;</span>
3769 <span class="incremental">parameter type =&gt; ( isa     =&gt; 'Str',
3770                     default =&gt; 'Item' );</span>
3771 <span class="incremental">role {
3772     my $p = shift;
3773
3774     my $type =
3775         'ArrayRef[' . $p-&gt;type() . ']';
3776     has collection =&gt;
3777         ( is  =&gt; 'ro',
3778           isa =&gt; $type );
3779 };</span></code></pre>
3780 </div>
3781
3782 <div class="slide">
3783   <h1>MooseX::Role::Parameterized</h1>
3784
3785   <pre><code>package Person;
3786
3787 use Moose;
3788 with HasCollection =&gt; { type =&gt; 'Int' };</code></pre>
3789 </div>
3790
3791 <div class="slide">
3792   <h1>Questions?</h1>
3793 </div>  
3794
3795 <div class="slide fake-slide0">
3796   <h1>Part 9: Writing Moose Extensions</h1>
3797 </div>
3798
3799 <div class="slide fake-slide0">
3800   <h1>The End</h1>
3801 </div>
3802
3803 <div class="slide">
3804   <h1>More Information</h1>
3805
3806   <ul>
3807     <li><a href="http://moose.perl.org/">http://moose.perl.org/</a></li>
3808     <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>
3809     <li><a href="irc://irc.perl.org/#moose">irc://irc.perl.org/#moose</a></li>
3810     <li>mailing list - <a href="mailto:moose@perl.org">moose@perl.org</a></li>
3811     <li>Slides and exercises are in Moose's git repo:
3812         <br />
3813         <span style="font-size:80%; white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
3814   </ul>
3815 </div>
3816
3817 </div> 
3818 </body>
3819 </html>
3820
3821 <!--
3822
3823 Copyright 2009 David Rolsky. All Rights Reserved.
3824
3825 This work is licensed under a Creative Commons Attribution-Share Alike
3826 3.0 United States License See
3827 http://creativecommons.org/licenses/by-sa/3.0/us/ for details.
3828
3829 -->