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