<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 class="slide">
<h1>Introduction to Moose</h1>
- <h2>YAPC 2009</h2>
+ <h2>Dave Rolsky</a>
+</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">
<ul>
<li><strong>Declarative</strong> OO sugar</li>
<li>Introspectable</li>
- <li>Extensible (MooseX::* on CPAN)</li>
+ <li>Extensible (188 MooseX::* on CPAN)</li>
+ <li>Community approved (1200+ downstream dependents on CPAN)</li>
</ul>
</div>
<li>
Attributes have ...
<ul>
- <li>Access-control (read-only vs read-write)</li>
+ <li>Mutability (read-only vs read-write)</li>
<li>An optional type</li>
<li>Accessor methods</li>
<li>Delegation methods</li>
<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>
<ul>
<li>AKA advice</li>
<li>"<strong>Before</strong> foo(), do this first"</li>
- <li>"Do this <strong>after</strong> foo()</li>
+ <li>"Do this <strong>after</strong> foo()"</li>
<li>"Put this code <strong>around</strong> foo()"</li>
</ul>
</div>
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>
-# really calls $person->blog_uri->host</code></pre>
+<span class="highlight">$person->blog_host;</span>
+# really calls $person->blog_uri->host</code></pre>
</div>
<div class="slide">
);</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>
<h1>BUILDARGS</h1>
<ul>
- <li>Takes <code>@_</code>, returns a hash reference of attribute names/value</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>
if ( @_ == 1 && ! ref $_[0] ) {
<span class="highlight">return { ssn => $_[0] };</span>
}
-
<span class="highlight">return $class->SUPER::BUILDARGS(@_)</span>;
}
<pre><code>Person->new(@_)</code></pre>
- <ol>
+ <ol style="margin-top: 0">
<li>Calls <code>Person->BUILDARGS(@_)</code> to turn <code>@_</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>
+ <li>Calls <code>$new_object->BUILDALL($constructor_args)</code>
<br />... which calls all <code>BUILD</code> methods</li>
<li>Returns the object</li>
</ol>
</div>
<div class="slide">
- <h1>The Object is Oqaque</h1>
+ <h1>The Object is Opaque</h1>
<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>
<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;
<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>
+ die "Pay me first"
+ unless $self->got_paid;
+ 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>
use Moose;
# true
-Person->can('extends');</code></pre>
+Person->can('extends');</code></pre>
<ul>
<li>Not very hygienic</li>
...
-no Moose;
+<span class="highlight">no Moose;</span>
+
+# false
+Person->can('extends');</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Cleaning Up Moose Droppings</h1>
+
+ <pre><code>package Person;
+<span class="highlight">use namespace::autoclean;</span>
+use Moose;
+
+...
# false
-Person->can('extends');</code></pre>
+Person->can('extends');</code></pre>
</div>
<div class="slide">
<h1>No Moose</h1>
<ul>
- <li><code>no Moose</code> at the end of a package is a best practice</li>
+ <li>Cleaning up is a best practice</li>
+ <li>Say <code>no Moose</code> at the end of a package</li>
+ <li>Or <code>use namespace::autoclean</code> at the top</li>
<li>Just do it</li>
</ul>
</div>
<h1>Immutability</h1>
<ul>
- <li><span style="font-family: URW Chancery L; font-size: 140%">Stevan's Incantation of Fleet-Footedness</span></li>
+ <li><span style="font-family: URW Chancery L; font-size: 120%">Stevan's Incantation of Fleet-Footedness</span></li>
</ul>
<pre><code>package Person;
use Moose;
-<span class="highlight">__PACKAGE__->meta->make_immutable;</span></code></pre>
+<span class="highlight">__PACKAGE__->meta->make_immutable;</span></code></pre>
</div>
<div class="slide">
<h1>Exercises</h1>
<pre># cd exercises
-$ perl bin/prove -lv t/00-prereq.t
-Missing anything? Install it. (see tarballs/)
+# perl bin/prove -lv t/00-prereq.t
+
+# 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
+
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>
<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(
- first_name => 'Kenichi',
- last_name => 'Asai',
+<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>
+sub as_string { $_[0]->first_name() }
+
<span class="highlight">has has_been_printed => ( is => 'rw' );
sub print {
# or ...
-if ( 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>
-
- <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' },
- exclude => 'break' },
- 'CanBreakdance' =>
- { alias =>
- { break => 'break_dance' },
- exclude => '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_dance;
- if ( rand(1) < 0.5 ) {
- $self->break_bone;
- }
-}</code></pre>
-</div>
-
-<div class="slide">
- <h1>Still Full of Fail</h1>
+ <h1>Conflicts Are a Smell</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;
sub is_equal {
my $self = shift;
- return $self->compare(@_) == 0;
+ return $self->compare(@_) == 0;
}</code></pre>
</div>
# Satisfies the Comparable role
sub compare { ... }
-Integer->does('TestsEquality'); # true
-Integer->does('Comparable'); # also true!</code></pre>
+Integer->does('TestsEquality'); # true
+Integer->does('Comparable'); # also true!</code></pre>
</div>
<div class="slide">
<div class="slide">
<h1>Delayed Conflict</h1>
- <pre><code>package StateOfTexas;
+ <pre><code>package SysadminAssassin;
with 'Killer';</code></pre>
<ul>
- <li><code>StateOfTexas</code> must implement its own <code>execute</code></li>
+ <li><code>SysadminAssassin</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>
<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>
<pre><code>use Moose::Util qw( apply_all_roles );
-my $fragile_person = Person->new( ... );
-apply_all_roles( $fragile_person, 'IsFragile' );</code></pre>
+my $fragile_person = Person->new( ... );
+apply_all_roles( $fragile_person,
+ 'IsFragile' );</code></pre>
<ul>
<li>Does not change the <code>Person</code> class</li>
<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>
<span class="current incremental">required => 1,</span>
);
-<span class="incremental">Person->new( first_name => undef ); # ok
-Person->new(); # kaboom</span></code></pre>
+<span class="incremental">Person->new( first_name => undef ); # ok
+Person->new(); # kaboom</span></code></pre>
</div>
<div class="slide">
<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>
use Moose;
my $highlander_bank =
- Bank->new( name => 'Spire FCU' );
+ Bank->new(
+ name => 'Clan MacLeod Trust' );
has bank => (
is => 'rw',
<h1>Builder</h1>
<ul>
- <li>A method <em>name</em> which returns the default</li>
+ <li>A method <em>name</em></li>
+ <li>When called, this method returns the default value</li>
</ul>
</div>
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>
use Moose;
has shoe_size => (
- is => 'ro',
+ is => '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>
);
-Person->new( <span class="wrong">shoe_size => 13</span> );
+Person->new( <span class="wrong">shoe_size => 13</span> );
my $person =
- Person->new( <span class="right">foot_size => 13</span> );
-print $person->shoe_size;</code></pre>
+ Person->new( <span class="right">foot_size => 13</span> );
+print $person->shoe_size;</code></pre>
</div>
<div class="slide">
<pre><code>package Person;
use Moose;
-has shoes => (
+has shoes => (
is => 'ro',
<span class="highlight">init_arg => undef,</span>
);
-Person->new( <span class="wrong">shoes => Shoes->new</span> );</code></pre>
+Person->new( <span class="wrong">shoes => Shoes->new</span> );</code></pre>
</div>
<div class="slide">
<li>Attributes can have a <code>default</code> or <code>builder</code></li>
<li>Attributes with a default or builder can be <code>lazy</code></li>
<li>Attributes can have a <code>clearer</code> and/or <code>predicate</code></li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Basic Attributes Summary</h1>
+
+ <ul>
<li>An attribute's constructor name can be changed with <code>init_arg</code></li>
<li>A subclass can alter its parents' attributes</li>
<li>Attribute accessor names can be changed</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>
<ul>
<li>Apply to an existing method</li>
- <li>... from a parent class, the current class, or a role</li>
+ <li>... that comes from a parent class, the current class, or a role</li>
<li>Roles can provide modifiers that are applied at composition time</li>
</ul>
</div>
<div class="slide">
- <h1>What is a Method Modifier</h1>
+ <h1>What Are Method Modifiers For?</h1>
<ul>
<li>"Inject" behavior</li>
<li>Add behavior to generated methods (accessors, delegations)</li>
- <li>Provide roles which modify existing behavior</li>
+ <li>Added from a role, can modify existing behavior</li>
</ul>
</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->$orig(
$self->_munge_insert(@_) );
- $new_user->_assign_uri;
-
+ $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>
<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 class="slide">
<h1>Augment and Inner</h1>
- <pre><code>package Document;
+ <pre class="medium"><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() };
+ sub { my $self = shift;
+ $self->title() . <span class="highlight">inner()</span> . $self->summary() };
package TPSReport;
extends 'Report';
-
<span class="highlight">augment xml</span> =>
- sub { tps_xml() . <span class="highlight">inner()</span> };</code></pre>
+ sub { my $self = shift;
+ $self->tps_xml() . <span class="highlight">inner()</span> };</code></pre>
</div>
<div class="slide">
<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>
Undef
Defined
Value
- Num
- Int
- Str
- ClassName
- RoleName
+ Str
+ Num
+ Int
+ ClassName
+ RoleName
</pre>
</div>
<pre>
(Item)
(Defined)
+ (Value)
Ref
ScalarRef
ArrayRef[`a]
CodeRef
RegexpRef
GlobRef
- FileHandle
+ FileHandle
Object
</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>
<ul>
<li><code>Value</code> is true when <code>! ref $thing</code></li>
<li><code>Value</code> and <code>Str</code> are effectively the same, but <code>Str</code> is more expressive</li>
+ <li><code>Num</code> is true when a <code>$scalar</code> looks like a number</li>
<li>An overloaded object which numifies does not pass the <code>Num</code> constraint!</li>
<li>Perl 5's overloading is hopelessly broken</li>
</ul>
<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',
<h1>Subtype Shortcuts - <code>class_type</code></h1>
<pre><code>use Moose::Util::TypeConstraints;
-class_type 'DateTime';
+class_type 'DateTime';</code></pre>
-subtype 'DateTime',
+<hr />
+
+<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';
+role_type 'Printable';</coe></pre>
-subtype 'Printable',
- as 'Object',
+<hr />
+
+<pre><code>subtype 'Printable',
+ as 'Object',
where
{ Moose::Util::does_role(
$_, 'Printable' ) },
<h1>Subtype Shortcuts - <code>duck_type</code></h1>
<pre><code>use Moose::Util::TypeConstraints;
-duck_type Car => qw( run break_down );
+duck_type Car => qw( run break_down );</code></pre>
-subtype 'Car',
+<hr />
+
+<pre><code>subtype 'Car',
as 'Object',
where { all { $_->can($_) }
qw( run break_down ) },
<h1>Subtype Shortcuts - <code>enum</code></h1>
<pre><code>use Moose::Util::TypeConstraints;
-enum Color => qw( red blue green ) );
+enum Color => qw( red blue green );</code></pre>
+
+<hr />
-my %ok = map { $_ => 1 } qw( red blue green );
-subtype 'Color'
+<pre><code>my %ok = map { $_ => 1 }
+ qw( red blue green );
+
+subtype 'Color'
as 'Str',
where { $ok{$_} },
message { ... };</code></pre>
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>
-
<div class="slide">
<h1>Using Types with Attributes</h1>
<pre><code>package Person;
-use Moose::Util::TypeConstraints;
-
has height => (
is => 'rw',
<span class="highlight">isa => 'Num',</span>
</div>
<div class="slide">
+ <h1>More Droppings</h1>
+
+ <ul>
+ <li><code>Moose::Util::TypeConstraints</code> also needs cleanup</li>
+ </ul>
+
+ <pre><code>package Person;
+
+use Moose;
+use Moose::Util::TypeConstraints;
+
+subtype ...;
+
+no Moose;
+<span class="highlight">no Moose::Util::TypeConstraints;</span></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;
\@_,
tasks =>
{ isa => 'ArrayRef[Task]',
- coerce =>1 },
+ coerce => 1 },
can_rest =>
{ isa => 'Bool',
default => 0 },
);</span>
-
...
}</code></pre>
+</div>
<div class="slide">
<h1>Typed Methods (High-tech)</h1>
...
}</code></pre>
+</div>
<div class="slide">
<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 MyApp::Types qw( ArrayOfInt );
-has transaction_history => (
- is => 'rw',
- isa => ArrayOfInt,
+has transaction_history => (
+ is => 'rw',
+ isa => ArrayOfInt,
);</code></pre>
+</div>
<div class="slide">
<h1>MooseX::Types</h1>
<ul>
<li>Type names are exported functions, catches typos early</li>
<li>Types must be pre-declared</li>
- <li>Types are stored with namespaces internally, but externally are short</li>
+ <li>Types are stored with namespaces internally, but you use short names</li>
<li>Import existing Moose types as functions from <code>MooseX::Types::Moose</code></li>
<li>Still need string names for things like <code>ArrayRef['Email::Address']</code></li>
</ul>
<li><span class="right">Catches typos at compile time</span></li>
<li><span class="right">Automatic namespacing</span></li>
<li><span class="wrong">One more thing to install and learn</span></li>
- <li><span class="wrong">Every name gets types twice (declared and then defined)</span></li>
+ <li><span class="wrong">Every name is typed twice (declared and then defined)</span></li>
<li><span class="wrong">Still stuck with strings when referring to class or role names</span></li>
- <li><span class="wrong">Coercion gotcha from earlier still applies to types exported from <code>MooseX::Types::Moose<?code></span></li>
+ <li><span class="wrong">Coercion gotcha from earlier still applies to types exported from <code>MooseX::Types::Moose</code></span></li>
</ul>
</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>
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>after salary_level => {
my $self = shift;
- return unless @_;
+ <span class="highlight">return unless @_;</span>
$self->clear_salary;
};</code></pre>
</div>
<pre><code>has salary_level => (
is => 'rw',
- trigger => sub { $_[0]->clear_salary },
+ trigger =>
+ sub { $_[0]->clear_salary },
);</code></pre>
</div>
<div class="slide">
+ <h1>Trigger Arguments</h1>
+
+ <ul>
+ <li><code>$self</code></li>
+ <li><code>$new_value</code></li>
+ <li><code>$old_value</code> - if one exists</li>
+ </ul>
+</div>
+
+<div class="slide">
<h1>Delegation</h1>
<ul>
has lungs => (
is => 'ro',
- isa => 'Lungs',
+ isa => 'Lungs',
<span class="highlight">handles => [ 'inhale', 'exhale' ],</span>
);</code></pre>
<h1>Array Reference</h1>
<ul>
+ <li>1-to-1 mapping</li>
<li>Takes each method name and creates a simple delegation from the delegating class to the delegatee attribute</li>
</ul>
</div>
<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 { ... }
</div>
<div class="slide">
+ <h1>Native Delegation</h1>
+
+ <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(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 => 'bare',
+ isa => 'ArrayRef[Int]',
+ default => sub { [] },
+ init_arg => undef,
+ <span class="highlight">handles =>
+ { favorite_numbers => 'elements',
+ 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>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> and <code>execute_method</code></li>
+ </ul>
+ </li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Curried Delegation</h1>
+
+ <ul>
+ <li>A delegation with some preset arguments</li>
+ <li>Works with object or Native delegation</li>
+ </ul>
+</div>
+
+<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>
<ul>
<h1>Traits and Metaclasses</h1>
<ul>
- <li>Can add/alter/remove attribute parameter (from <code>has</code>)</li>
+ <li>Can add/alter/remove an attribute parameter (from <code>has</code>)</li>
<li>Can change behavior of created attribute</li>
</ul>
</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>
isa => 'Str',
<span class="highlight">label => 'Social Security Number',</span>
);
-
print <span class="highlight">Person->meta
->get_attribute('ssn')->label;</span></code></pre>
</div>
<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>
<div class="slide">
- <h1>Questions?</h1>
-</div>
-
-<div class="slide">
<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 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 Tour of MooseX</h1>
+ <h1>Bonus: A Brief Tour of MooseX</h1>
</div>
-<div class="slide fake-slide0">
- <h1>Part 9: Writing Moose Extensions</h1>
+<div class="slide">
+ <h1>Notable MX Modules on CPAN</h1>
+
+ <ul>
+ <li><strong>Not comprehensive</strong></li>
+ <li>177 MooseX distributions on CPAN as of 09/21/2010</li>
+ <li>Some of them are crap</li>
+ </ul>
</div>
-<div class="slide fake-slide0">
- <h1>The End</h1>
+<div class="slide">
+ <h1>Already Mentioned Several</h1>
+
+ <ul>
+ <li><code>MooseX::NonMoose</code> - best solution for subclassing non-Moose parents</li>
+ <li><code>MooseX::Declare</code> - <em>real</em> Perl 5 OO</li>
+ <li><code>MooseX::FollowPBP</code> and <code>MooseX::SemiAffordanceAccessor</code></li>
+ <li><code>MooseX::Params::Validate</code> and <code>MooseX::Method::Signatures</code></li>
+ <li><code>MooseX::Types</code></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>
+ <li>But not supported by <code>PPI</code>, <code>perltidy</code>, etc.</li> (yet?)
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>MooseX::StrictConstructor</h1>
+
+ <ul>
+ <li>By default, unknown constructor arguments are ignored</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 => 'ArrayRef[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">
+ <h1>Moose-using Modules</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="white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
+ <span style="font-size:80%; white-space: nowrap">git://jules.scsys.co.uk/gitmo/moose-presentations</span></li>
</ul>
</div>
+<div class="slide fake-slide0">
+ <h1>The End</h1>
+</div>
+
</div>
</body>
</html>