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