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