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