<div id="header"></div>
<div id="footer">
<div id="license">
- <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/us/88x31.png" /></a>
+ <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>
<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>.
</div>
</div>
<div class="slide">
+ <h1>Introduce Yourselves</h1>
+
+ <ul>
+ <li>Your name</li>
+ <li>What you do with Perl</li>
+ <li>Why you're here today (optional)</li>
+ </ul>
+</div>
+
+<div class="slide">
<h1>Moose Summed Up</h1>
<ul>
my @return =
$self->$real_foo( @_, bar => 42 );
- return ( @return, 'modify return values' );
+ return (
+ @return,
+ 'modify return values'
+ );
};</code></pre>
</div>
);
# kaboom
-Person->new( weight => 'fat' );</code></pre>
+Person->new( weight => 'heavy' );</code></pre>
</div>
<div class="slide">
has blog_uri => (
is => 'rw',
isa => 'URI',
- <span class="highlight">handles => { 'blog_hostname' => 'host' },</span>
+ <span class="highlight">handles => { 'blog_host' => 'host' },</span>
);
-<span class="highlight">$person->blog_hostname;</span>
+<span class="highlight">$person->blog_host;</span>
# really calls $person->blog_uri->host</code></pre>
</div>
);</code></pre>
</div>
+<div class="slide">
+ <h1>More Why Moose?</h1>
+
+ <ul>
+ <li>Less code == fewer bugs</li>
+ <li>Moose is well-tested, test your own code, not Moose</li>
+ <li>Focus on <strong>what</strong>, not <strong>how</strong></li>
+ </ul>
+</div>
+
<div class="slide fake-slide0">
<h1>Part 1: Moose Classes</h1>
</div>
if ( @_ == 1 && ! ref $_[0] ) {
<span class="highlight">return { ssn => $_[0] };</span>
}
-
<span class="highlight">return $class->SUPER::BUILDARGS(@_)</span>;
}
<ul>
<li><code>extends</code> is sugar for declaring parent classes</li>
+ <li>Also ensures metaclass compatibility between parent and child</li>
+ <li>Do not <code>use base</code></li>
</ul>
<pre><code>package Employee;
<span class="incremental">override</span> work => sub {
my $self = shift;
- die "Pay me first" unless $self->got_paid;
+ die "Pay me first"
+ unless $self->got_paid;
<span class="incremental">super();</span>
}<span class="incremental">;</span></code></pre>
</div>
<ul>
<li><code>no Moose</code> at the end of a package is a best practice</li>
- <li>Or <code>namespace::clean</code> at the top</li>
+ <li>Or <code>use namespace::autoclean</code> at the top</li>
<li>Just do it</li>
</ul>
</div>
# perl bin/prove -lv t/01-classes.t
+# edit lib/Person.pm and lib/Employee.pm
+
Iterate til this passes all its tests</pre>
</div>
</div>
<div class="slide">
- <h1>Roles Can Have State <strong>and</strong> Behavior</h1>
+ <h1>Roles - State <strong>and</strong> Behavior</h1>
<pre><code>package HasPermissions;
use Moose::Role;
-
<span class="current incremental"># state
has access_level => ( is => 'rw' );</span>
my $self = shift;
my $required = shift;
- return $self->access_level >= $required;
+ return $self->access_level
+ >= $required;
}</span></code></pre>
</div>
<h1>Classes Consume Roles</h1>
<pre><code>my $person = Person->new(
- first_name => 'Kenichi',
- last_name => 'Asai',
+ first_name => 'Kenichi',
+ last_name => 'Asai',
access_level => 42,
);
# or ...
-if ( Person->meta->does('Printable') ) { ... }</code></pre>
+Person->meta->does('Printable')</code></pre>
</div>
use Moose;
<span class="highlight">with 'IsFragile' =>
- { alias =>
+ { -alias =>
{ break => 'break_bone' } },
'CanBreakdance' =>
- { alias =>
+ { -alias =>
{ break => 'break_it_down' } };</span></code></pre>
<ul>
use Moose;
<span class="highlight">with 'IsFragile' =>
- { alias =>
+ { -alias =>
{ break => 'break_bone' },
- exclude => 'break' },
+ -excludes => 'break' },
'CanBreakdance' =>
- { alias =>
- { break => 'break_dance' },
- exclude => 'break' };</span></code></pre>
+ { -alias =>
+ { break => 'break_it_down' },
+ -excludes => 'break' };</span></code></pre>
</div>
<div class="slide">
sub break {
my $self = shift;
- $self->break_dance;
+ $self->break_it_down;
if ( rand(1) < 0.5 ) {
$self->break_bone;
}
<pre><code>use Moose::Util qw( apply_all_roles );
my $fragile_person = Person->new( ... );
-apply_all_roles( $fragile_person, 'IsFragile' );</code></pre>
+apply_all_roles( $fragile_person,
+ 'IsFragile' );</code></pre>
<ul>
<li>Does not change the <code>Person</code> class</li>
<ul>
<li>Attributes can have defaults</li>
- <li>Simple non-referecne scalars (number, string)</li>
+ <li>Simple non-reference scalars (number, string)</li>
<li>Subroutine reference</li>
<li>A builder method</li>
</ul>
</div>
<div class="slide">
- <h1>Default as a Subroutine Reference</h1>
+ <h1>Subroutine Reference Default</h1>
<ul>
<li>Called as a method on the object</li>
sub _build_bank {
my $self = shift;
- return Bank->new( name => 'Spire FCU' );
+ return Bank->new(
+ name => 'Spire FCU' );
}</code></pre>
</div>
</div>
<div class="slide">
- <h1>Lazy, Good for Nothing Attributes</h1>
+ <h1>Lazy, Good for Nothin' Attributes</h1>
<ul>
<li>Normally, defaults are generated during object construction</li>
<h1>Exercises</h1>
<pre># cd exercises
-# perl bin/prove -lv t/03-basic-attributes.t
+# perl bin/prove -lv \
+ t/03-basic-attributes.t
Iterate til this passes all its tests</pre>
</div>
my $self = shift;
return unless $DEBUG;
- warn "Called work on ", $self->full_name,
+ warn "Called work on ",
+ $self->full_name,
"with the arguments: [@_]\n";
};</code></pre>
</div>
</div>
<div class="slide">
- <h1>Other Uses Example</h1>
+ <h1>More Modifier Examples</h1>
<pre><code>has password => (
is => 'rw',
clearer => 'clear_password',
);
-
has hashed_password => (
is => 'ro',
builder => '_build_hashed_password',
clearer => '_clear_hashed_password',
);
-
after clear_password => sub {
my $self = shift;
$self->_clear_hashed_password;
$self->_munge_insert(@_) );
$new_user->_assign_uri;
-
return $new_user;
};</code></pre>
</div>
<h1>Modifier Order</h1>
<ul>
- <li>Before runs order from last to first</li>
+ <li>Before runs in order from last to first</li>
<li>After runs in order from first to last</li>
<li>Around runs in order from last to first</li>
</ul>
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>
<h1>Exercises</h1>
<pre># cd exercises
-# perl bin/prove -lv t/04-method-modifiers.t
+# perl bin/prove -lv \
+ t/04-method-modifiers.t
Iterate til this passes all its tests</pre>
</div>
<pre>
(Item)
(Defined)
+ (Value)
Ref
ScalarRef
ArrayRef[`a]
<span class="incremental current">subtype 'PositiveInt',</span>
<span class="incremental">as 'Int',</span>
<span class="incremental">where { $_ > 0 },</span>
- <span class="incremental">message { "The value you provided ($_)"
- . " was not a positive number." };</span>
+ <span class="incremental">message
+ { "The value you provided ($_)"
+ . " was not a positive int." };</span>
has size => (
is => 'ro',
<pre><code>use Moose::Util::TypeConstraints;
enum Color => qw( red blue green ) );
-my %ok = map { $_ => 1 } qw( red blue green );
+my %ok = map { $_ => 1 }
+ qw( red blue green );
+
subtype 'Color'
as 'Str',
where { $ok{$_} },
subtype 'UCStr',
as 'Str',
- where { ! /[a-z]/ };
+ where { ! /[a-z]/ };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Coercions</h1>
-<span class="incremental current">coerce 'UCStr',</span>
+ <pre><code><span class="incremental current">coerce 'UCStr',</span>
<span class="incremental">from 'Str',</span>
<span class="incremental">via { uc };</span>
via { [ $_ ] };</code></pre>
<ul>
- <li>Coerce instead of a union like <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
+ <li>Instead of union - <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
</ul>
</div>
<pre><code>package Person;
-use Moose::Util::TypeConstraints;
-
has height => (
is => 'rw',
<span class="highlight">isa => 'Num',</span>
<h1>More Droppings</h1>
<ul>
- <li><code>Moose::Util::TypeConstraint</code> also needs cleanup</li>
+ <li><code>Moose::Util::TypeConstraints</code> also needs cleanup</li>
</ul>
<pre><code>package Person;
\@_,
tasks =>
{ isa => 'ArrayRef[Task]',
- coerce =>1 },
+ coerce => 1 },
can_rest =>
{ isa => 'Bool',
default => 0 },
);</span>
-
...
}</code></pre>
</div>
<h1>Digression: The Type Registry</h1>
<ul>
- <li>Types are actually <code>Moose::Meta::TypeConstraint</code> <em>objects</em></li>
+ <li>Types are actually <code>Moose::Meta::TypeConstraints</code> <em>objects</em></li>
<li>Stored in an interpreter-global registry mapping names to objects</li>
</ul>
</div>
<h1>Namespace Fix</h1>
<pre><code>use Moose::Util::TypeConstraints;
-
subtype <span class="highlight">'MyApp::Type::DateTime',</span>
as 'DateTime';
use Moose;
has name => ( is => 'ro' );
-has friend => ( is => 'rw', <span class="highlight">weak_ref => 1</span> );
+has friend => ( is => 'rw',
+ <span class="highlight">weak_ref => 1</span> );
my $alice = Person->new( name => 'Alice' );
my $bob = Person->new( name => 'Bob' );
<pre><code>package Person;
use Moose;
-
has account => (
is => 'ro',
isa => 'BankAccount',
<pre><code>package Auditor;
use Moose::Role;
-
sub record_change { ... }
sub change_history { ... }
<ul>
<li>Delegate to <em>unblessed</em> Perl types</li>
<li>Scalar, array or hash ref, etc</li>
+ <li>Treat Perl types as objects</li>
+ <li>Still uses <code>handles</code></li>
+ <li>Pretend that native Perl types have methods</li>
</ul>
</div>
<div class="slide">
- <h1>Native Delegation - Array</h1>
+ <h1>Native Delegation - Array(Ref)</h1>
+
+ <ul>
+ <li>Methods include:
+ <ul>
+ <li><code>push</code></li>
+ <li><code>shift</code></li>
+ <li><code>elements</code> - returns all elements</li>
+ <li><code>count</code></li>
+ <li><code>is_empty</code></li>
+ <li>quite a few more</li>
+ </ul>
+ </li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Native Delegation - Array(Ref)</h1>
<pre><code>package Person;
use Moose;
-
has _favorite_numbers => (
traits => [ 'Array' ],
is => 'ro',
add_favorite_number => 'push',
},</span>
);</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Native Delegation - Array(Ref)</h1>
+
+ <pre><code>my $person = Person->new();
+
+$person->add_favorite_number(7);
+$person->add_favorite_number(42);
+
+print "$_\n"
+ for $person->favorite_numbers;
+
+# 7
+# 42</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Native Delegation</h1>
<ul>
- <li>Automatically defaults to <code>[]</code></li>
+ <li>Native types are ...
+ <ul>
+ <li>Number - <code>add</code>, <code>mul</code>, ...</li>
+ <li>String - <code>append</code>, <code>chop</code>, ...</li>
+ <li>Counter - <code>inc</code>, <code>dec</code>, ...</li>
+ <li>Bool - <code>set</code>, <code>toggle</code>, ...</li>
+ <li>Hash - <code>get</code>, <code>set</code>, ...</li>
+ <li>Array - already saw it</li>
+ <li>Code - <code>execute</code>, that's it</li>
+ </ul>
+ </li>
</ul>
</div>
<div class="slide">
- <h1>Native Delegation - Counter</h1>
+ <h1>Curried Delegation</h1>
- <pre><code>package Stack;
-use Moose;
+ <ul>
+ <li>A delegation with some preset arguments</li>
+ <li>Works with object or Native delegation</li>
+ </ul>
+</div>
-has depth => (
- traits => [ 'Counter' ],
- is => 'ro',
- isa => 'Int',
- default => 0,
- init_arg => undef,
- <span class="highlight">handles =>
- { _inc_depth => 'inc',
- _dec_depth => 'dec',
- },</span>
+<div class="slide">
+ <h1>Curried Delegation</h1>
+
+ <pre><code>package Person;
+use Moose;
+has account => (
+ is => 'ro',
+ isa => 'BankAccount',
+ handles => {
+ receive_100 =>
+ <span class="highlight">[ 'deposit', 100 ]</span>
+ give_100 =>
+ <span class="highlight">[ 'withdraw', 100 ]</span>
+ },
);</code></pre>
</div>
+<div class="slide">
+ <h1>Curried Delegation</h1>
+
+ <pre><code>$person->receive_100;
+# really is
+$person->account->deposit(100);</code></pre>
+</div>
<div class="slide">
<h1>Traits and Metaclasses</h1>
isa => 'Str',
<span class="highlight">label => 'Social Security Number',</span>
);
-
print <span class="highlight">Person->meta
->get_attribute('ssn')->label;</span></code></pre>
</div>
isa => 'Str',
<span class="highlight">label => 'Social Security Number',</span>
);
-
print <span class="highlight">Person->meta
->get_attribute('ssn')->label;</span></code></pre>
</div>
<h1>Exercises</h1>
<pre># cd exercises
-# perl bin/prove -lv t/06-advanced-attributes.t
+# perl bin/prove -lv \
+ t/06-advanced-attributes.t
Iterate til this passes all its tests</pre>
</div>
</div>
<div class="slide fake-slide0">
- <h1>Part 8: A Tour of MooseX</h1>
+ <h1>Part 8: A Brief Tour of MooseX</h1>
</div>
+<div class="slide">
+ <h1>Notable MX Modules on CPAN</h1>
+
+ <ul>
+ <li><strong>Not comprehensive</strong></li>
+ <li>128 MooseX distributions on CPAN as of 09/24/2009</li>
+ <li>Some of them are crap</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Already Mentioned Several</h1>
+
+ <ul>
+ <li>MooseX::NonMoose - best solution for subclassing non-Moose parents</li>
+ <li>MooseX::Declare - <em>real</em> Perl 5 OO</li>
+ <li>MooseX::FollowPBP and MooseX::SemiAffordanceAccessor</li>
+ <li>MooseX::Params::Validate and MooseX::Method::Signatures</li>
+ <li>MooseX::Types</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Declare</h1>
+
+<pre><code>use MooseX::Declare;
+use 5.10.0; # for say
+
+class Person {
+ has greeting
+ => ( is => 'ro', isa => 'Str' );
+
+ method speak {
+ say $self->greeting;
+ }
+}</code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Declare</h1>
+
+ <ul>
+ <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>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::StrictConstructor</h1>
+
+ <ul>
+ <li>By default, unknown constructor arguments are ignore</li>
+ <li>MX::StrictConstructor turns these into an error</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::StrictConstructor</h1>
+
+ <pre><code>package Person;
+
+use Moose;
+<span class="highlight">use MooseX::StrictConstructor;</span>
+
+has name => ( is => 'ro' );
+
+Person->new
+ ( na<span class="wrong">n</span>e => 'Ringo Shiina' ); # kaboom</code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Traits</h1>
+
+ <ul>
+ <li>Combines object construction and role application</li>
+ <li>Makes it easy to create one-off customized objects</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Traits</h1>
+
+ <pre><code>package MyApp::Thingy;
+use Moose;
+
+<span class="highlight">with 'MooseX::Traits';</span>
+
+my $thing =
+ MyApp::Thingy-><span class="highlight">new_with_traits</span>
+ ( <span class="highlight">traits => [ 'Foo', 'Bar' ],</span>
+ size => 42 );</code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Getopt</h1>
+
+ <ul>
+ <li>Makes command-line interface programs easy!</li>
+ <li>Construct an object from CLI arguments</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Getopt</h1>
+
+ <pre><code>package App::CLI;
+use Moose;
+
+<span class="highlight">with 'MooseX::Getopt';</span>
+
+has file =>
+ ( is => 'ro', required => 1 );
+has filters =>
+ ( is => 'ro', isa => 'Str' );
+
+sub run { ... }</code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Getopt</h1>
+
+ <ul>
+ <li>Then call it like this:</li>
+ </ul>
+
+<pre><code>#!/usr/bin/perl
+
+use App::CLI;
+
+<span class="highlight">App::CLI->new_with_options()</span>->run();</code></pre>
+
+<pre>$ myapp-cli \
+ --file foo \
+ --filters compress \
+ --filters sanitize</pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Clone</h1>
+
+ <pre><code>package Person;
+
+use Moose;
+<span class="highlight">with 'MooseX::Clone';</span>
+
+my $person = Person->new;
+my $clone = <span class="highlight">$person->clone;</span></code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::NonMoose</h1>
+
+ <ul>
+ <li>Highly recommended for subclassing non-Moose parents</li>
+ <li>Gets all the little annoying details right</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Role::Parameterized</h1>
+
+ <pre><code>package HasCollection;
+<span class="current incremental">use MooseX::Role::Parameterized;</span>
+<span class="incremental">parameter type => ( isa => 'Str',
+ default => 'Item' );</span>
+<span class="incremental">role {
+ my $p = shift;
+
+ my $type =
+ 'ArrayRef[' . $p->type() . ']';
+ has collection =>
+ ( is => 'ro',
+ isa => $type );
+};</span></code></pre>
+</div>
+
+<div class="slide">
+ <h1>MooseX::Role::Parameterized</h1>
+
+ <pre><code>package Person;
+
+use Moose;
+with HasCollection => { type => 'Int' };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Questions?</h1>
+</div>
+
<div class="slide fake-slide0">
<h1>Part 9: Writing Moose Extensions</h1>
</div>