<div class="slide">
<h1>Introduction to Moose</h1>
- <h2>Dave Rolsky</a>
+ <h2>Dave Rolsky</h2>
</div>
<div class="slide">
<ul>
<li><strong>Declarative</strong> OO sugar</li>
<li>Introspectable</li>
- <li>Extensible (MooseX::* on CPAN)</li>
+ <li>Extensible (202 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>
<h1>BUILDARGS</h1>
<ul>
- <li>Takes <code>@_</code>, returns a hash reference of attribute name/value pairs</li>
- <li>Accepts a hash or hashref; throws otherwise</li>
+ <li>Processes <code>new</code>'s <code>@_</code>, returns a hash reference of attribute name/value pairs</li>
+ <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>
</ul>
<div class="slide">
<h1>Object Construction a la Moose</h1>
- <pre><code>Person->new(@_)</code></pre>
+ <pre><code>Person->new(@args)</code></pre>
<ol style="margin-top: 0">
- <li>Calls <code>Person->BUILDARGS(@_)</code> to turn <code>@_</code> into a hashref</li>
+ <li>Calls <code>Person->BUILDARGS(@args)</code> to turn <code>@args</code> into a hashref</li>
<li>Blesses a reference</li>
<li>Populates attributes based on the hashref from #1</li>
<li>Calls <code>$new_object->BUILDALL($constructor_args)</code>
<ul>
<li>Technically it's a hash reference</li>
<li><span class="wrong">If you <em>ever</em> treat it as one <strong>you are doing it wrong!</strong></span></li>
+ <li>Moose probably provides a feature to do what you need</li>
</ul>
</div>
<ul>
<li>Like <code>DESTROY</code>, but Moose makes sure all <code>DEMOLISH</code> methods in a hierarchy are called</li>
<li>Called in normal inheritance order, children to parents</li>
+ <li><em>Never</em> called by you, only by Perl itself</li>
</ul>
</div>
<li>No <code>DEMOLISH()</code></li>
</ul>
</li>
- <li>But see <code>MooseX::NonMoose</code> for a workaround</li>
+ <li>But <code>MooseX::NonMoose</code> fixes all of this</li>
</ul>
</div>
<ul>
<li><code>override</code> is another method modifier</li>
<li>An alternative to Perl's <code>SUPER::</code></li>
+ <li><em>Declares</em> your intent to override a method</li>
</ul>
</div>
<span class="current incremental">extends 'Person';</span>
-<span class="incremental">override</span> work => 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>
my $person =
Person->new( first_name => 'Dave' );
-$person->first_name('Stevan');
-print $person->first_name; # Dave</code></pre>
+$person->first_name('Stevan'); # dies</code></pre>
</div>
<h1>Cleaning Up Moose Droppings</h1>
<pre><code>package Person;
-<span class="highlight">use namespace::autoclean;</span>
use Moose;
+<span class="highlight">use namespace::autoclean;</span>
...
# 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">
<pre><code>package Person;
use Moose;
-<span class="highlight">with 'Printable';</span></code></pre>
+<span class="highlight">with 'Printable';</span>
+
+sub as_string { $_[0]->first_name() }</code></pre>
</div>
<div class="slide">
<span class="delete">with 'Printable';</span>
-<span class="highlight">die '...' unless __PACKAGE__->can('as_string');
+sub as_string { $_[0]->first_name() }
-has has_been_printed => ( is => 'rw' );
+<span class="highlight">has has_been_printed => ( is => 'rw' );
sub print {
my $self = shift;
# or ...
-Person->meta->does('Printable')</code></pre>
+Person->meta->does_role('Printable')</code></pre>
</div>
</div>
<div class="slide">
- <h1>Method Aliasing</h1>
-
- <pre><code>package FragileDancer;
-use Moose;
-
-<span class="highlight">with 'IsFragile' =>
- { -alias =>
- { break => 'break_bone' } },
- 'CanBreakdance' =>
- { -alias =>
- { break => 'break_it_down' } };</span></code></pre>
+ <h1>Conflicts Are a Smell</h1>
<ul>
- <li>Renames the roles' methods</li>
- <li>Still conflicts, need to <code>exclude</code> as well</li>
- </ul>
-</div>
-
-<div class="slide">
- <h1>Method Exclusion</h1>
-
- <pre><code>package FragileDancer;
-use Moose;
-
-<span class="highlight">with 'IsFragile' =>
- { -alias =>
- { break => 'break_bone' },
- -excludes => 'break' },
- 'CanBreakdance' =>
- { -alias =>
- { break => 'break_it_down' },
- -excludes => 'break' };</span></code></pre>
-</div>
-
-<div class="slide">
- <h1>And then ...</h1>
-
- <pre><code>package FragileDancer;
-use Moose;
-
-sub break {
- my $self = shift;
-
- $self->break_it_down;
- if ( rand(1) < 0.5 ) {
- $self->break_bone;
- }
-}</code></pre>
-</div>
-
-<div class="slide">
- <h1>Still Full of Fail</h1>
-
- <ul>
- <li>Roles are also about semantics!</li>
- <li>We've fulfilled the letter and lost the spirit</li>
+ <li>Roles are about semantics!</li>
<li>Roles have a <em>meaning</em></li>
- <li>Think twice before blindly aliasing and excluding methods!</li>
+ <li>Method name conflicts smell like bad design</li>
</ul>
</div>
<div class="slide">
- <h1>Hot Role-on-Role Action</h1>
+ <h1>Roles With Roles</h1>
<pre><code>package Comparable;
use Moose::Role;
</div>
<div class="slide">
- <h1>Hot Role-on-Role Action</h1>
+ <h1>Roles With Roles</h1>
<pre><code>package TestsEquality;
use Moose::Role;
</div>
<div class="slide">
- <h1>Name Conflicts Between Roles</h1>
-
- <pre><code>package HasSubProcess;
-use Moose::Role;
-
-<span class="highlight">sub execute { ... }</span>
-
-package Killer;
-use Moose::Role;
-
-with 'HasSubProcess';
-
-<span class="highlight">sub execute { ... }</span></code></pre>
-</div>
-
-<div class="slide">
- <h1>Delayed Conflict</h1>
-
- <pre><code>package StateOfTexas;
-with 'Killer';</code></pre>
-
- <ul>
- <li><code>StateOfTexas</code> must implement its own <code>execute</code></li>
- <li>But loading the <code>Killer</code> role by itself does not cause an error</li>
- </ul>
-</div>
-
-<div class="slide">
<h1>Roles as Interfaces</h1>
<ul>
<span class="incremental">with 'HasSize';
-has size => ( is => 'ro' );</span></code></pre>
+has size => ( is => 'ro' );</span></code></pre>
</div>
<div class="slide">
package Shirt;
use Moose;
-has size => ( is => 'ro' );
+has size => ( is => 'ro' );
with 'HasSize';</code></pre>
</div>
<pre><code>package Comparison;
use Moose;
-has [ 'left', 'right' ] => (
- is => 'ro',
- <span class="highlight">does => 'Comparable',</span>
+has [ 'left', 'right' ] => (
+ is => 'ro',
+ <span class="highlight">does => 'Comparable',</span>
);
</code></pre>
<h1>Real Examples</h1>
<ul>
- <li>Column and ColumnAlias both <em>do</em> ColumnLike</li>
- <li>ColumnLike things can be used in certain parts of queries</li>
<li>All queries <em>do</em> HasWhereClause</li>
<li>Select <em>does</em> Comparable and Selectable (for subselects)</li>
<li>A where clause requires its components to <em>do</em> Comparable</li>
</div>
<div class="slide">
+ <h1>Read-only vs Read-write</h1>
+
+ <ul>
+ <li>Read-only is preferred</li>
+ <li>Minimize state in your application</li>
+ </ul>
+</div>
+
+<div class="slide">
<h1>Required-ness</h1>
<ul>
has bank => (
is => 'rw',
+ # THIS WILL NOT WORK
<span class="wrong">default => Bank->new(
name => 'Spire FCU' ),</span>
);</code></pre>
</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>
sub _build_bank {
my $self = shift;
return Bank->new(
- name => 'Spire FCU' );
+ name => 'Spire FCU' );
}</code></pre>
</div>
has shoe_size => (
is => 'ro',
- required => 'ro',
+ required => 1,
);</code></pre>
</div>
<pre><code>has shoes => (
is => 'ro',
<span class="highlight">lazy => 1,</span>
- builder => '_build_shoes',
+ builder => '_build_shoes',
);
sub _build_shoes {
<pre><code>package Person;
use Moose;
-has shoe_size => (
+has shoe_size => (
is => 'ro',
<span class="highlight">init_arg => 'foot_size',</span>
);
<pre><code>package Person;
use Moose;
-has shoes => (
+has shoes => (
is => 'ro',
<span class="highlight">init_arg => undef,</span>
);
<ul>
<li>By default, subclasses inherit attribute as-is</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>
+ <li>Can change attribute parameters in subclasses</li>
</ul>
</div>
</div>
<div class="slide">
- <h1>Augment and Inner</h1>
-
- <ul>
- <li>Inverted <code>super</code></li>
- <li>From least- to most-specific</li>
- <li>Like Mason's autohandler feature</li>
- <li>Grandparent to parent to child</li>
- <li>Not allowed in roles</li>
- </ul>
-</div>
-
-<div class="slide">
- <h1>Augment and Inner</h1>
-
- <pre><code>package Document;
-
-sub xml { '<doc>' . <span class="highlight">inner()</span> . '</doc>' }
-
-package Report;
-extends 'Document';
-<span class="highlight">augment xml</span> =>
- sub { title() . <span class="highlight">inner()</span> . summary() };
-
-package TPSReport;
-extends 'Report';
-<span class="highlight">augment xml</span> =>
- sub { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
-</div>
-
-<div class="slide">
- <h1>Augment and Inner</h1>
-
- <ul>
- <li>When we call <code>$tps->xml</code> ...
- <ul>
- <li><code>Document->xml</code></li>
- <li><code>Report->xml</code></li>
- <li><code>TPSReport->xml</code></li>
- </ul>
- </li>
- </ul>
-</div>
-
-<div class="slide">
- <h1>Augment and Inner Usage</h1>
-
- <ul>
- <li>Call <code>inner()</code> to "fill in the blank"</li>
- <li>Requires designing for subclassing</li>
- <li>Call <code>inner()</code> in the terminal class, just in case</li>
- </ul>
-</div>
-
-<div class="slide">
<h1>Method Modifiers Summary</h1>
<ul>
<li>not call the original method at all (or call a <em>different</em> method)</li>
</ul>
</li>
- </ul>
-</div>
-
-<div class="slide">
- <h1>Method Modifiers Summary</h1>
-
- <ul>
<li>When using modifiers in a role, require the modified method</li>
- <li>Use <code>augment</code> and <code>inner</code> to invert the normal subclassing flow ...
- <ul>
- <li>Least- to most-specific (parents to children)</li>
- <li>Build in "insertability" (stick more stuff in the "middle")</li>
- </ul>
- </li>
- <li>Always call <code>inner</code> in the most specific subclass to allow for future extension</li>
</ul>
</div>
Num
Int
ClassName
+ RoleName
</pre>
</div>
<h1>Bool</h1>
<h2>True</h2>
- <pre><code>1
-924.1
-'true'
-{}</code></pre>
+ <pre><code>1</code></pre>
<h2>False</h2>
<pre><code>0
-0.0
'0'
+''
undef</code></pre>
<ul>
<hr />
-<pre><code>subtype 'DateTime',
+<pre><code>subtype 'DateTime',
as 'Object',
where { $_->isa('DateTime') },
message { ... };</code></pre>
<h1>Subtype Shortcuts - <code>role_type</code></h1>
<pre><code>use Moose::Util::TypeConstraints;
-role_type 'Printable';</coe></pre>
+role_type 'Printable';</code></pre>
<hr />
<pre><code>subtype 'Printable',
- as 'Object',
+ as 'Object',
where
{ Moose::Util::does_role(
$_, 'Printable' ) },
<h1>Subtype Shortcuts - <code>enum</code></h1>
<pre><code>use Moose::Util::TypeConstraints;
-enum Color => qw( red blue green ) );</code></pre>
+enum Color => qw( red blue green );</code></pre>
<hr />
<pre><code>my %ok = map { $_ => 1 }
qw( red blue green );
-subtype 'Color'
+subtype 'Color'
as 'Str',
where { $ok{$_} },
message { ... };</code></pre>
<div class="slide">
<h1>Coercion Examples</h1>
- <pre><code>coerce 'ArrayRef[Int]',
+ <pre><code># BAD CODE - DO NOT COPY
+coerce 'ArrayRef[Int]',
from 'Int',
via { [ $_ ] };</code></pre>
</div>
<div class="slide">
+ <h1>Questions So Far?</h1>
+</div>
+
+<div class="slide">
+ <h1>Exercises</h1>
+
+ <pre># cd exercises
+# perl bin/prove -lv t/05-types.t
+
+Iterate til this passes all its tests</pre>
+</div>
+
+<div class="slide">
<h1>Typed Methods (Low-tech)</h1>
<pre class="medium"><code>package Person;
<h1>Digression: The Type Registry</h1>
<ul>
- <li>Types are actually <code>Moose::Meta::TypeConstraints</code> <em>objects</em></li>
+ <li>Types are actually <code>Moose::Meta::TypeConstraint</code> <em>objects</em></li>
<li>Stored in an interpreter-global registry mapping names to objects</li>
</ul>
</div>
use MyApp::Types qw( ArrayOfInt );
-has transaction_history => (
- is => 'rw',
- isa => ArrayOfInt,
+has transaction_history => (
+ is => 'rw',
+ isa => ArrayOfInt,
);</code></pre>
</div>
<h1>Questions?</h1>
</div>
-<div class="slide">
- <h1>Exercises</h1>
-
- <pre># cd exercises
-# perl bin/prove -lv t/05-types.t
-
-Iterate til this passes all its tests</pre>
-</div>
-
<div class="slide fake-slide0">
<h1>Part 6: Advanced Attributes</h1>
</div>
has lungs => (
is => 'ro',
- isa => 'Lungs',
+ isa => 'Lungs',
<span class="highlight">handles => [ 'inhale', 'exhale' ],</span>
);</code></pre>
use Moose;
has _favorite_numbers => (
traits => [ 'Array' ],
- is => 'ro',
isa => 'ArrayRef[Int]',
default => sub { [] },
init_arg => undef,
isa => 'BankAccount',
handles => {
receive_100 =>
- <span class="highlight">[ 'deposit', 100 ]</span>
+ <span class="highlight">[ 'deposit', 100 ]</span>,
give_100 =>
<span class="highlight">[ 'withdraw', 100 ]</span>
},
<li>Use <code>weak_ref</code> to avoid circular references</li>
<li>Use trigger to do an action post-attribute write</li>
<li>Use delegations to hide "internal" objects</li>
+ <li>Use native delegations to treat Perl types as objects</li>
<li>Traits and metaclasses let you extend Moose's core attribute features</li>
</ul>
</div>
Iterate til this passes all its tests</pre>
</div>
-<div class="slide fake-slide0">
- <h1>Part 7: Introspection</h1>
+<div class="slide">
+ <h1>CYOA</h1>
+
+ <p>
+ If there is time, keep going ...
+ </p>
+
+ <p>
+ Otherwise, jump to slide 269 ...
+ </p>
</div>
<div class="slide fake-slide0">
- <h1>Part 8: A Brief Tour of MooseX</h1>
+ <h1>Bonus: A Brief Tour of MooseX</h1>
</div>
<div class="slide">
<ul>
<li><strong>Not comprehensive</strong></li>
- <li>152 MooseX distributions on CPAN as of 02/02/2010</li>
+ <li>188 MooseX distributions on CPAN as of 02/03/2011</li>
<li>Some of them are crap</li>
</ul>
</div>
<h1>MooseX::Declare</h1>
<pre><code>use MooseX::Declare;
-use 5.10.0; # for say
+use 5.12.0; # for say
class Person {
has greeting =>
<li>Still experimental-ish, but seeing more and more use</li>
<li><strong>Not</strong> a source filter!</li>
<li>Hooks into the Perl parser rather than filtering all your code</li>
- <li>But not supported by <code>PPI</code>, <code>perltidy</code>, etc.</li> (yet?)
+ <li>But not supported by <code>PPI</code>, <code>perltidy</code>, etc. (yet?)</li>
</ul>
</div>
<h1>Questions?</h1>
</div>
-<div class="slide fake-slide0">
- <h1>Part 9: Writing Moose Extensions</h1>
-</div>
+<div class="slide">
+ <h1>Moose-using Modules</h1>
-<div class="slide fake-slide0">
- <h1>The End</h1>
+ <p>
+ For further reading, a few modules which use Moose ...
+ </p>
+
+ <ul>
+ <li><a href="http://search.cpan.org/dist/Catalyst-Runtime">Catalyst</a></li>
+ <li><a href="http://search.cpan.org/dist/CHI">CHI</a></li>
+ <li><a href="http://search.cpan.org/dist/Devel-REPL">Devel::REPL</a></li>
+ <li><a href="http://search.cpan.org/dist/Email-Sender">Email::Sender</a></li>
+ <li><a href="http://search.cpan.org/dist/Fey">Fey</a></li>
+ <li><a href="http://search.cpan.org/dist/Net-Twitter">Net::Twitter</a></li>
+ </ul>
</div>
<div class="slide">
<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://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
+ <span style="font-size:80%; white-space: nowrap">git://git.moose.perl.org/moose-presentations.git</span></li>
</ul>
</div>
+<div class="slide fake-slide0">
+ <h1>The End</h1>
+</div>
+
</div>
</body>
</html>