X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fslides%2Findex.html;h=46ac36ab9225822fa1e8e023c01e5960fceb5602;hb=66b11069294324c31214650bd2e466fb75acd1f5;hp=b3f57f681376337f550f7a3624631f0a305fcc1c;hpb=7f7e655a677ae4e66c2bf208a9898c796a12e779;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index b3f57f6..46ac36a 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -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
  • @@ -3174,8 +3201,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 +3294,7 @@ has history => (
package Person;
 use Moose;
 has _favorite_numbers => (
-    traits   => [ 'Array' ],
+    traits   => [ 'Array' ],
     isa      => 'ArrayRef[Int]',
     default  => sub { [] },
     init_arg => undef,
@@ -3342,82 +3373,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 +3380,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 +3612,13 @@ with HasCollection => { type => 'Int' };

@@ -3672,7 +3627,7 @@ with HasCollection => { type => 'Int' };