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