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