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