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