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