File Coverage

blib/lib/List/Objects/WithUtils/Role/Array/Typed.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package List::Objects::WithUtils::Role::Array::Typed;
2             $List::Objects::WithUtils::Role::Array::Typed::VERSION = '2.028002';
3 99     99   41465 use strictures 2;
  99         466  
  99         3154  
4              
5 99     99   12783 use Carp ();
  99         119  
  99         1051  
6 99     99   274 use Scalar::Util ();
  99         223  
  99         1071  
7 99     99   19566 use Type::Tie ();
  99         366352  
  99         1646  
8              
9 99     99   397 use Role::Tiny;
  99         114  
  99         502  
10             requires 'type', 'untyped', 'new';
11              
12             around type => sub { tied(@{$_[1]})->type };
13              
14             around untyped => sub {
15             my (undef, $self) = @_;
16             require List::Objects::WithUtils::Array;
17             List::Objects::WithUtils::Array->new(@$self)
18             };
19              
20             around new => sub {
21             # yes, this splice is correct:
22             my (undef, $class, $type) = splice @_, 0, 2;
23              
24             if (my $blessed = Scalar::Util::blessed $class) {
25             $type = $class->type;
26             $class = $blessed;
27             } else {
28             $type = shift;
29             }
30              
31             my $self = [];
32             tie @$self, 'Type::Tie::ARRAY', $type;
33             push @$self, @_;
34             bless $self, $class;
35             };
36              
37             print
38             qq[ you seem to be ignoring mst\n],
39             qq[ would you like to talk to me instead?\n],
40             qq[ mauke++ # talking paperclip\n],
41             qq[ I can't help you but I'm in a pretty good mood\n]
42             unless caller;
43             1;
44              
45             =pod
46              
47             =for Pod::Coverage new array_of
48              
49             =head1 NAME
50              
51             List::Objects::WithUtils::Role::Array::Typed - Type-checking array behavior
52              
53             =head1 SYNOPSIS
54              
55             # Via List::Objects::WithUtils::Array::Typed ->
56             use List::Objects::WithUtils 'array_of';
57             use Types::Standard -all;
58             use List::Objects::Types -all;
59              
60             # Array of Ints:
61             my $arr = array_of Int() => (1,2,3);
62              
63             # Array of array objects of Ints (coerced from ARRAYs):
64             my $arr = array_of TypedArray[Int] => [1,2,3], [4,5,6];
65              
66             =head1 DESCRIPTION
67              
68             This role makes use of L to add type-checking behavior to
69             L consumers.
70              
71             The first argument passed to the constructor should be a L type
72             (or other object conforming to L, as of C):
73              
74             use Types::Standard -all;
75             my $arr = array_of Str() => qw/foo bar baz/;
76              
77             Elements are checked against the specified type when the object is constructed
78             or new elements are added.
79              
80             If the initial type-check fails, a coercion is attempted.
81              
82             Values that cannot be coerced will throw an exception.
83              
84             Also see L, L
85              
86             =head2 type
87              
88             Returns the L type the object was created with.
89              
90             =head2 untyped
91              
92             Returns a (shallow) clone that is a plain L.
93              
94             Since most methods that return a new list will (attempt to) return a list
95             object of the same type as their parent, this can be useful to avoid type
96             check failures in a method chain that creates intermediate lists.
97              
98             =head1 AUTHOR
99              
100             Jon Portnoy with significant contributions from Toby
101             Inkster (CPAN: TOBYINK)
102              
103             =cut