<ul>
<li><strong>Declarative</strong> OO sugar</li>
<li>Introspectable</li>
- <li>Extensible (177 MooseX::* on CPAN)</li>
- <li>Community approved (1222 downstream dependents on CPAN)</li>
+ <li>Extensible (188 MooseX::* on CPAN)</li>
+ <li>Community approved (1200+ downstream dependents on CPAN)</li>
</ul>
</div>
<pre><code>package Person;
use Moose;
-<span class="highlight">has first_name => ( is => 'rw' );</span></code></pre>
+<span class="highlight">has first_name => ( is => 'ro' );</span></code></pre>
</div>
<li>Accepts a hash or hashref; errors otherwise</li>
<li>Provide your own for other cases</li>
<li><strong>Always</strong> call <code>$class->SUPER::BUILDARGS(@_)</code> as a fallback!</li>
- <li>Make sure to pass the <strong>original value of <code>@_</code></strong> to the parent</li>
</ul>
</div>
<span class="current incremental">extends 'Person';</span>
-<span class="incremental">override</span> <span class="incremental">work</span> => sub {
+<span class="incremental">override</span> <span class="incremental">work</span> <span class="incremental">=> sub {
my $self = shift;
die "Pay me first"
unless $self->got_paid;
- <span class="incremental">super();</span>
-}<span class="incremental">;</span></code></pre>
+ super();
+}</span><span class="incremental">;</span></code></pre>
</div>
<div class="slide">
<ul>
<li>Mostly like <code>$self->SUPER::work(@_)</code></li>
<li><strong>But</strong> cannot change <code>@_</code>!</li>
- <li>Binds the parent's method at compile time</li>
+ <li>Binds the parent's method <strong>correctly</strong> at compile time</li>
<li>Parent determined by checking <code>Child->meta()->superclasses()</code></li>
</ul>
</div>
# perl install-moose (if needed)
+## Read the instructions in t/01-classes.t
+
# perl bin/prove -lv t/01-classes.t
# edit lib/Person.pm and lib/Employee.pm
<pre><code>package Person;
use Moose;
-with 'HasPermissions';</code></pre>
+with 'Printable';</code></pre>
</div>
<div class="slide">
<h1>Classes Consume Roles</h1>
-<pre><code>my $person = Person->new(
+<pre><code>package Person;
+
+sub as_string { $_[0]->first_name() }
+
+...
+
+my $person = Person->new(
first_name => 'Kenichi',
last_name => 'Asai',
access_level => 42,
);
-print $person->full_name
- . ' has '
- . $person->can_access(42)
- ? 'great power'
- : 'little power';</code></pre>
+$person->print();</code></pre>
</div>
<div class="slide">
<h1>Roles in Practice</h1>
<ul>
- <li>Consuming a role ≈ inlining the role</li>
+ <li>Consuming a role =~ inlining the role</li>
</ul>
</div>
<li>Human <em>@ISA</em> Animal</li>
<li>Human <em>does</em> Toolmaker (as <em>does</em> Chimpanzee)</li>
<li>Car <em>@ISA</em> Vehicle</li>
- <li>Car <em>does</em> Destroyable</li>
+ <li>Car <em>does</em> HasEngine</li>
</ul>
</div>
</div>
<div class="slide">
+ <h1>What if I Want to Share?</h1>
+
+ <pre><code>package Person;
+use Moose;
+
+my $highlander_bank =
+ Bank->new(
+ name => 'Clan MacLeod Trust' );
+
+has bank => (
+ is => 'rw',
+ default => sub { $highlander_bank },
+);</code></pre>
+</div>
+
+<div class="slide">
<h1>Builder</h1>
<ul>
<ul>
<li>By default, subclasses inherit attribute as-is</li>
- <li>Can change most attribute parameters in subclasses</li>
+ <li>Can change some attribute parameters in subclasses
+ <ul>
+ <li>default</li>
+ <li>builder</li>
+ <li>required</li>
+ <li>lazy</li>
+ <li>others we've not yet covered</li>
+ </ul>
+ </li>
</ul>
</div>
<ul>
<li>An attribute's constructor name can be changed with <code>init_arg</code></li>
- <li>Can set <code>init_arg</code> to <code>undef</code></li>
<li>A subclass can alter its parents' attributes</li>
- <li>Accessor naming scheme can be changed</li>
+ <li>Attribute accessor names can be changed</li>
</ul>
</div>
</p>
<p>
- Otherwise, jump to slide 268 ...
+ Otherwise, jump to slide 269 ...
</p>
</div>
<li>mailing list - <a href="mailto:moose@perl.org">moose@perl.org</a></li>
<li>Slides and exercises are in Moose's git repo:
<br />
- <span style="font-size:80%; white-space: nowrap">git://git.moose.perl.org/moose-presentations/moose.git</span></li>
+ <span style="font-size:80%; white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
</ul>
</div>