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