X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=e2e889a1a72d032d6527ae0358e029615f2400dc;hb=d7deb8a91eda73a9252643bc0b221dd6e5d34482;hp=6bcad03bc3d927838f4923f7a1808e3a37427682;hpb=78c5b76a4c64e9693ad841efc562d7ab4257ac3c;p=gitmo%2Fmoose-presentations.git
diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html
index 6bcad03..e2e889a 100644
--- a/moose-class/slides/index.html
+++ b/moose-class/slides/index.html
@@ -52,7 +52,7 @@ img#me05 {top: 43px;left: 36px;}
Introduction to Moose
-
+
Dave Rolsky
@@ -71,7 +71,8 @@ img#me05 {top: 43px;left: 36px;}
- Declarative OO sugar
- Introspectable
- - Extensible (MooseX::* on CPAN)
+ - Extensible (177 MooseX::* on CPAN)
+ - Community approved (1222 downstream dependents on CPAN)
@@ -218,7 +219,7 @@ use Moose;
- AKA advice
- "Before foo(), do this first"
- - "Do this after foo()
+ - "Do this after foo()"
- "Put this code around foo()"
@@ -300,8 +301,8 @@ has blog_uri => (
handles => { 'blog_host' => 'host' },
);
-$person->blog_host;
-# really calls $person->blog_uri->host
+$person->blog_host;
+# really calls $person->blog_uri->host
@@ -955,7 +956,7 @@ sub BUILD {
Calls Person->BUILDARGS(@_)
to turn @_
into a hashref
Blesses a reference
Populates attributes based on the hashref from #1
-
Calls $new_object->BUILDALL($constructor_args)
+ Calls $new_object->BUILDALL($constructor_args)
... which calls all BUILD
methods
Returns the object
@@ -976,6 +977,7 @@ sub BUILD {
- Like
DESTROY
, but Moose makes sure all DEMOLISH
methods in a hierarchy are called
- Called in normal inheritance order, children to parents
+ - Never called by you, only by Perl itself
@@ -1039,6 +1041,7 @@ extends 'LWP';
override
is another method modifier
- An alternative to Perl's
SUPER::
+ - Declares your intent to override a method
@@ -1107,8 +1110,7 @@ has first_name => ( is => 'ro' );
my $person =
Person->new( first_name => 'Dave' );
-$person->first_name('Stevan');
-print $person->first_name; # Dave
+$person->first_name('Stevan'); # dies
@@ -1127,7 +1129,7 @@ print $person->first_name; # Dave
use Moose;
# true
-Person->can('extends');
+Person->can('extends');
- Not very hygienic
@@ -1142,10 +1144,23 @@ use Moose;
...
-no Moose;
+no Moose;
+
+# false
+Person->can('extends');
+
+
+
+
Cleaning Up Moose Droppings
+
+
package Person;
+use namespace::autoclean;
+use Moose;
+
+...
# false
-Person->can('extends');
+Person->can('extends');
@@ -1163,13 +1178,13 @@ Person->can('extends');
Immutability
- - Stevan's Incantation of Fleet-Footedness
+ - Stevan's Incantation of Fleet-Footedness
package Person;
use Moose;
-__PACKAGE__->meta->make_immutable;
+
__PACKAGE__->meta->make_immutable;
@@ -1355,7 +1370,7 @@ sub print {
# or ...
-Person->meta->does('Printable')
+Person->meta->does_role('Printable')
@@ -1415,69 +1430,17 @@ use Moose;
-
Method Aliasing
-
-
package FragileDancer;
-use Moose;
-
-with 'IsFragile' =>
- { -alias =>
- { break => 'break_bone' } },
- 'CanBreakdance' =>
- { -alias =>
- { break => 'break_it_down' } };
-
-
- - Renames the roles' methods
- - Still conflicts, need to
exclude
as well
-
-
-
-
-
Method Exclusion
-
-
package FragileDancer;
-use Moose;
-
-with 'IsFragile' =>
- { -alias =>
- { break => 'break_bone' },
- -excludes => 'break' },
- 'CanBreakdance' =>
- { -alias =>
- { break => 'break_it_down' },
- -excludes => 'break' };
-
-
-
-
And then ...
-
-
package FragileDancer;
-use Moose;
-
-sub break {
- my $self = shift;
-
- $self->break_it_down;
- if ( rand(1) < 0.5 ) {
- $self->break_bone;
- }
-}
-
-
-
-
Still Full of Fail
+
Conflicts Are a Smell
- - Roles are also about semantics!
- - We've fulfilled the letter and lost the spirit
+ - Roles are about semantics!
- Roles have a meaning
- - Think twice before blindly aliasing and excluding methods!
+ - Method name conflicts smell like bad design
-
Hot Role-on-Role Action
+
Roles With Roles
package Comparable;
use Moose::Role;
@@ -1486,7 +1449,7 @@ requires 'compare';
-
Hot Role-on-Role Action
+
Roles With Roles
package TestsEquality;
use Moose::Role;
@@ -1495,7 +1458,7 @@ with 'Comparable';
sub is_equal {
my $self = shift;
- return $self->compare(@_) == 0;
+ return $self->compare(@_) == 0;
}
@@ -1510,8 +1473,8 @@ with 'TestsEquality';
# Satisfies the Comparable role
sub compare { ... }
-Integer->does('TestsEquality'); # true
-Integer->does('Comparable'); # also true!
+Integer->does('TestsEquality'); # true
+Integer->does('Comparable'); # also true!
@@ -1533,11 +1496,11 @@ with 'HasSubProcess';
Delayed Conflict
-
package StateOfTexas;
+ package SysadminAssassin;
with 'Killer';
- StateOfTexas
must implement its own execute
+ SysadminAssassin
must implement its own execute
- But loading the
Killer
role by itself does not cause an error
@@ -1565,7 +1528,7 @@ use Moose;
with 'HasSize';
-has size => ( is => 'ro' );
+has size => ( is => 'ro' );
@@ -1579,7 +1542,7 @@ requires 'size';
package Shirt;
use Moose;
-has size => ( is => 'ro' );
+has size => ( is => 'ro' );
with 'HasSize';
@@ -1601,9 +1564,9 @@ with 'HasSize';
package Comparison;
use Moose;
-has [ 'left', 'right' ] => (
- is => 'ro',
- does => 'Comparable',
+has [ 'left', 'right' ] => (
+ is => 'ro',
+ does => 'Comparable',
);
@@ -1618,7 +1581,7 @@ has [ 'left', 'right' ] => (
use Moose::Util qw( apply_all_roles );
-my $fragile_person = Person->new( ... );
+my $fragile_person = Person->new( ... );
apply_all_roles( $fragile_person,
'IsFragile' );
@@ -1774,8 +1737,8 @@ has first_name => (
required => 1,
);
-Person->new( first_name => undef ); # ok
-Person->new(); # kaboom
+Person->new( first_name => undef ); # ok
+Person->new(); # kaboom
@@ -1882,7 +1845,7 @@ has bank => (
- A method name
- - When called, this methods returns the default value
+ - When called, this method returns the default value
@@ -1900,7 +1863,7 @@ has bank => (
sub _build_bank {
my $self = shift;
return Bank->new(
- name => 'Spire FCU' );
+ name => 'Spire FCU' );
}
@@ -1956,7 +1919,8 @@ has bank => (
use Moose;
has shoe_size => (
- is => 'ro',
+ is => 'ro',
+ required => 1,
);
@@ -1966,7 +1930,7 @@ has shoe_size => (
has shoes => (
is => 'ro',
lazy => 1,
- builder => '_build_shoes',
+ builder => '_build_shoes',
);
sub _build_shoes {
@@ -2039,16 +2003,16 @@ has account => (
package Person;
use Moose;
-has shoe_size => (
+has shoe_size => (
is => 'ro',
init_arg => 'foot_size',
);
-Person->new( shoe_size => 13 );
+Person->new( shoe_size => 13 );
my $person =
- Person->new( foot_size => 13 );
-print $person->shoe_size;
+ Person->new( foot_size => 13 );
+print $person->shoe_size;
@@ -2057,12 +2021,12 @@ print $person->shoe_size;
package Person;
use Moose;
-has shoes => (
+has shoes => (
is => 'ro',
init_arg => undef,
);
-Person->new( shoes => Shoes->new );
+Person->new(
shoes => Shoes->new );
@@ -2383,7 +2347,7 @@ after clear_password => sub {
$self->$orig(
$self->_munge_insert(@_) );
- $new_user->_assign_uri;
+ $new_user->_assign_uri;
return $new_user;
};
@@ -2456,19 +2420,21 @@ around run => sub {
Augment and Inner
-
package Document;
+ package Document;
sub xml { '<doc>' . inner() . '</doc>' }
package Report;
extends 'Document';
augment xml =>
- sub { title() . inner() . summary() };
+ sub { my $self = shift;
+ $self->title() . inner() . $self->summary() };
package TPSReport;
extends 'Report';
augment xml =>
- sub { tps_xml() . inner() };
+ sub { my $self = shift;
+ $self->tps_xml() . inner() };
@@ -2594,6 +2560,7 @@ Item
Num
Int
ClassName
+ RoleName
@@ -2747,7 +2714,7 @@ class_type 'DateTime';
-subtype 'DateTime',
+subtype 'DateTime',
as 'Object',
where { $_->isa('DateTime') },
message { ... };
@@ -2762,7 +2729,7 @@ role_type 'Printable';
subtype 'Printable',
- as 'Object',
+ as 'Object',
where
{ Moose::Util::does_role(
$_, 'Printable' ) },
@@ -2788,14 +2755,14 @@ duck_type Car => qw( run break_down );
Subtype Shortcuts - enum
use Moose::Util::TypeConstraints;
-enum Color => qw( red blue green ) );
+enum Color => qw( red blue green );
my %ok = map { $_ => 1 }
qw( red blue green );
-subtype 'Color'
+subtype 'Color'
as 'Str',
where { $ok{$_} },
message { ... };
@@ -2911,6 +2878,19 @@ no Moose;
+
Questions So Far?
+
+
+
+
Exercises
+
+
# cd exercises
+# perl bin/prove -lv t/05-types.t
+
+Iterate til this passes all its tests
+
+
+
Typed Methods (Low-tech)
package Person;
@@ -3041,9 +3021,9 @@ coerce ArrayOfInt
use MyApp::Types qw( ArrayOfInt );
-has transaction_history => (
- is => 'rw',
- isa => ArrayOfInt,
+has transaction_history => (
+ is => 'rw',
+ isa => ArrayOfInt,
);
@@ -3087,15 +3067,6 @@ has transaction_history => (
Questions?
-
-
Exercises
-
-
# cd exercises
-# perl bin/prove -lv t/05-types.t
-
-Iterate til this passes all its tests
-
-
Part 6: Advanced Attributes
@@ -3211,6 +3182,16 @@ $alice->friend($bob);
+
Trigger Arguments
+
+
+ $self
+ $new_value
+ $old_value
- if one exists
+
+
+
+
Delegation
@@ -3226,7 +3207,7 @@ $alice->friend($bob);
has lungs => (
is => 'ro',
- isa => 'Lungs',
+ isa => 'Lungs',
handles => [ 'inhale', 'exhale' ],
);
@@ -3390,7 +3371,7 @@ has history => (
use Moose;
has _favorite_numbers => (
traits => [ 'Array' ],
- is => 'ro',
+ is => 'bare',
isa => 'ArrayRef[Int]',
default => sub { [] },
init_arg => undef,
@@ -3551,15 +3532,12 @@ print Person->meta
- Use
weak_ref
to avoid circular references
- Use trigger to do an action post-attribute write
- Use delegations to hide "internal" objects
+ - Use native delegations to treat Perl types as objects
- Traits and metaclasses let you extend Moose's core attribute features
-
Questions?
-
-
-
Exercises
# cd exercises
@@ -3569,12 +3547,20 @@ print Person->meta
Iterate til this passes all its tests
-
-
Part 7: Introspection
+
+
CYOA
+
+
+ If there is time, keep going ...
+
+
+
+ Otherwise, jump to slide 269 ...
+
-
Part 8: A Brief Tour of MooseX
+ Bonus: A Brief Tour of MooseX
@@ -3708,7 +3694,7 @@ sub run { ... }
use App::CLI;
-
App::CLI->new_with_options()->run();
+
App::CLI->new_with_options()->run();
$ myapp-cli \
--file foo \
@@ -3768,12 +3754,21 @@ with HasCollection => { type => 'Int' };
Questions?
-
-
Part 9: Writing Moose Extensions
-
+
+
Moose-using Modules
-
-
The End
+
+ For further reading, a few modules which use Moose ...
+
+
+
@@ -3790,6 +3785,10 @@ with HasCollection => { type => 'Int' };
+
+
The End
+
+