X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=e8302e4e73cbe636f765ed888232e74e28c1cd6d;hb=a88cc08084397dc7667e26dc315fb5492d8bf558;hp=802425fb21fefba3897223b36d1ed399f0ab8308;hpb=34b8c7482de09355b22545268b81d53654f66b2a;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index 802425f..e8302e4 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -950,10 +950,10 @@ sub BUILD {

Object Construction a la Moose

-
Person->new(@_)
+
Person->new(@args)
    -
  1. Calls Person->BUILDARGS(@_) to turn @_ into a hashref
  2. +
  3. Calls Person->BUILDARGS(@args) to turn @args into a hashref
  4. Blesses a reference
  5. Populates attributes based on the hashref from #1
  6. Calls $new_object->BUILDALL($constructor_args) @@ -1032,7 +1032,7 @@ extends 'LWP';
  7. No DEMOLISH()
  8. -
  9. But see MooseX::NonMoose for a workaround
  10. +
  11. But MooseX::NonMoose fixes all of this
@@ -1234,8 +1234,6 @@ use Moose; # 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 @@ -2017,7 +2015,7 @@ Person->new( shoes => Shoes->new );Attribute Inheritance @@ -2602,11 +2600,15 @@ has start_date => (

Subtype Shortcuts - class_type

use Moose::Util::TypeConstraints;
-class_type 'DateTime';
+ +class_type 'DateTime'; + +
-
subtype     'DateTime',
+

+subtype     'DateTime',
     as      'Object',
     where   { $_->isa('DateTime') },
     message { ... };
@@ -2616,11 +2618,15 @@ class_type 'DateTime';

Subtype Shortcuts - role_type

use Moose::Util::TypeConstraints;
-role_type 'Printable';
+ +role_type 'Printable'; + +
-
subtype 'Printable',
+

+subtype 'Printable',
     as  'Object',
     where
         { Moose::Util::does_role(
@@ -2632,11 +2638,15 @@ role_type 'Printable';

Subtype Shortcuts - duck_type

use Moose::Util::TypeConstraints;
-duck_type Car => qw( run break_down );
+ +duck_type Car => qw( run break_down ); + +

-
subtype 'Car',
+

+subtype 'Car',
     as      'Object',
     where   { all { $_->can($_) }
               qw( run break_down ) },
@@ -2647,11 +2657,15 @@ 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 }
+

+my %ok = map { $_ => 1 }
              qw( red blue green );
 
 subtype     'Color'
@@ -2666,7 +2680,9 @@ subtype     'Color'
   
package Person;
 
 my $posint =
-    subtype as 'Int', where { $_ > 0 };
+    subtype
+        as 'Int',
+        where { $_ > 0 };
 
 has size => (
     is  => 'ro',
@@ -2946,10 +2962,21 @@ has transaction_history => (
 
 
 
+

Specio

+ +
    +
  • My attempt to replace MooseX::Types and built-in types
  • +
  • Third-system effect?
  • +
  • Still alpha - needs some work
  • +
+
+ +

Recommendation

    -
  • Use MooseX::Types
  • +
  • Use MooseX::Types for now
  • +
  • Switch to Specio when it's ready?
  • Compile time error catching and automatic namespacing are huge wins
  • Docs from Moose::Util::TypeConstraints are 98% compatible with MooseX::Types anyway
  • A function exported by a type library works wherever a type name would
  • @@ -3111,6 +3138,25 @@ has lungs => (
+

Delegation Explained

+ +
package Person;
+
+has lungs => (
+    is      => 'ro',
+    isa     => 'Lungs',
+    handles => [ 'inhale', 'exhale' ],
+);
+
+sub inhale {
+    my $self = shift;
+    $self->lungs()->inhale();
+}
+
+sub exhale { ... }
+
+ +

Why Delegation?

    @@ -3174,8 +3220,12 @@ has account => ( },
    -
  • $person->receive_money = $person->account->deposit
  • -
  • $person->give_money = $person->account->withdraw
  • +
  • $person->receive_money +
    +     = $person->account->deposit
  • +
  • $person->give_money +
    +     = $person->account->withdraw
@@ -3263,7 +3313,7 @@ has history => (
package Person;
 use Moose;
 has _favorite_numbers => (
-    traits   => [ 'Array' ],
+    traits   => [ 'Array' ],
     isa      => 'ArrayRef[Int]',
     default  => sub { [] },
     init_arg => undef,
@@ -3342,82 +3392,6 @@ $person->account->deposit(100);
-

Traits and Metaclasses

- -
    -
  • The ultimate in customization
  • -
  • Per attribute metaclasses
  • -
  • Per attribute roles applied to the attribute metaclass
  • -
  • Change the meta-level behavior
  • -
-
- -
-

Traits and Metaclasses

- -
    -
  • The default metaclass is Moose::Meta::Attribute
  • -
  • Controls accessor generation, defaults, delegation, etc.
  • -
  • Adding a role to this metaclass (or replacing it) allows for infinite customization
  • -
-
- -
-

Traits and Metaclasses

- -
    -
  • Can add/alter/remove an attribute parameter (from has)
  • -
  • Can change behavior of created attribute
  • -
-
- -
-

Simple Trait Example

- -
package Person;
-use Moose;
-use MooseX::LabeledAttributes;
-
-has ssn => (
-    traits => [ 'Labeled' ],
-    is     => 'ro',
-    isa    => 'Str',
-    label  => 'Social Security Number',
-);
-print Person->meta
-            ->get_attribute('ssn')->label;
-
- -
-

Simple Metaclass Example

- -
package Person;
-use Moose;
-use MooseX::LabeledAttributes;
-
-has ssn => (
-    metaclass =>
-        'MooseX::Meta::Attribute::Labeled',
-    is        => 'ro',
-    isa       => 'Str',
-    label     => 'Social Security Number',
-);
-print Person->meta
-            ->get_attribute('ssn')->label;
-
- -
-

Traits vs Metaclass

- -
    -
  • Can apply any mix of traits to an attribute
  • -
  • But just one metaclass
  • -
  • Traits (aka roles) can cooperate
  • -
  • Metaclasses require you to pick just one
  • -
-
- -

Advanced Attributes Summary

    @@ -3425,7 +3399,6 @@ print Person->meta
  • 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
@@ -3658,12 +3631,13 @@ with HasCollection => { type => 'Int' };

@@ -3672,12 +3646,12 @@ with HasCollection => { type => 'Int' };
@@ -3691,7 +3665,7 @@ with HasCollection => { type => 'Int' };