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