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