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