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