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