| line |
stmt |
bran |
cond |
sub |
code |
|
1
|
|
|
|
|
package List::Objects::WithUtils::Role::Array::Typed; |
|
2
|
14
|
|
|
14
|
use strictures 1; |
|
|
14
|
|
|
|
|
|
|
14
|
|
|
|
|
|
3
|
|
|
|
|
|
|
4
|
14
|
|
|
14
|
use Carp (); |
|
|
14
|
|
|
|
|
|
|
14
|
|
|
|
|
|
5
|
14
|
|
|
14
|
use Scalar::Util (); |
|
|
14
|
|
|
|
|
|
|
14
|
|
|
|
|
|
6
|
14
|
|
|
14
|
use Type::Tie (); |
|
|
14
|
|
|
|
|
|
|
14
|
|
|
|
|
|
7
|
|
|
|
|
|
|
8
|
14
|
|
|
14
|
use Role::Tiny; |
|
|
14
|
|
|
|
|
|
|
14
|
|
|
|
|
|
9
|
|
|
|
|
requires 'type', 'new'; |
|
10
|
|
|
|
|
|
|
11
|
|
|
|
|
around type => sub { tied(@{$_[1]})->type }; |
|
12
|
|
|
|
|
|
|
13
|
|
|
|
|
around new => sub { |
|
14
|
|
|
|
|
my (undef, $class, $type) = splice @_, 0, 2; |
|
15
|
|
|
|
|
|
|
16
|
|
|
|
|
if (my $blessed = Scalar::Util::blessed $class) { |
|
17
|
|
|
|
|
$type = $class->type; |
|
18
|
|
|
|
|
$class = $blessed; |
|
19
|
|
|
|
|
} else { |
|
20
|
|
|
|
|
$type = shift; |
|
21
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
23
|
|
|
|
|
Carp::confess "Expected a Type::Tiny type but got '$type'" |
|
24
|
|
|
|
|
unless Scalar::Util::blessed($type) |
|
25
|
|
|
|
|
&& $type->isa('Type::Tiny'); |
|
26
|
|
|
|
|
|
|
27
|
|
|
|
|
my $self = []; |
|
28
|
|
|
|
|
tie(@$self, 'Type::Tie::ARRAY', $type); |
|
29
|
|
|
|
|
push @$self, @_; |
|
30
|
|
|
|
|
bless $self, $class; |
|
31
|
|
|
|
|
}; |
|
32
|
|
|
|
|
|
|
33
|
|
|
|
|
print |
|
34
|
|
|
|
|
qq[<Su-Shee> there are those days when I'm too stupid to loop over a], |
|
35
|
|
|
|
|
qq[ simple list of things... I should close my editor now.\n], |
|
36
|
|
|
|
|
qq[<dngor> Su-Shee: Hire an iterator. \n], |
|
37
|
|
|
|
|
unless caller; |
|
38
|
|
|
|
|
1; |
|
39
|
|
|
|
|
|
|
40
|
|
|
|
|
=pod |
|
41
|
|
|
|
|
|
|
42
|
|
|
|
|
=for Pod::Coverage new array_of |
|
43
|
|
|
|
|
|
|
44
|
|
|
|
|
=head1 NAME |
|
45
|
|
|
|
|
|
|
46
|
|
|
|
|
List::Objects::WithUtils::Role::Array::Typed - Type-checking array behavior |
|
47
|
|
|
|
|
|
|
48
|
|
|
|
|
=head1 SYNOPSIS |
|
49
|
|
|
|
|
|
|
50
|
|
|
|
|
# Via List::Objects::WithUtils::Array::Typed -> |
|
51
|
|
|
|
|
use List::Objects::WithUtils 'array_of'; |
|
52
|
|
|
|
|
use Types::Standard -all; |
|
53
|
|
|
|
|
use List::Objects::Types -all; |
|
54
|
|
|
|
|
|
|
55
|
|
|
|
|
my $arr_of_arrs = array_of( ArrayObj => [], [] ); |
|
56
|
|
|
|
|
|
|
57
|
|
|
|
|
=head1 DESCRIPTION |
|
58
|
|
|
|
|
|
|
59
|
|
|
|
|
This role makes use of L<Type::Tie> to add type-checking behavior to |
|
60
|
|
|
|
|
L<List::Objects::WithUtils::Role::Array> consumers. |
|
61
|
|
|
|
|
|
|
62
|
|
|
|
|
The first argument passed to the constructor should be a L<Type::Tiny> type: |
|
63
|
|
|
|
|
|
|
64
|
|
|
|
|
use Types::Standard -all; |
|
65
|
|
|
|
|
my $arr = array_of Str() => qw/foo bar baz/; |
|
66
|
|
|
|
|
|
|
67
|
|
|
|
|
Elements are checked against the specified type when the object is constructed |
|
68
|
|
|
|
|
or new elements are added. |
|
69
|
|
|
|
|
|
|
70
|
|
|
|
|
If the initial type-check fails, a coercion is attempted. |
|
71
|
|
|
|
|
|
|
72
|
|
|
|
|
Values that cannot be coerced will throw an exception. |
|
73
|
|
|
|
|
|
|
74
|
|
|
|
|
Also see L<Types::Standard>, L<List::Objects::Types> |
|
75
|
|
|
|
|
|
|
76
|
|
|
|
|
=head2 type |
|
77
|
|
|
|
|
|
|
78
|
|
|
|
|
Returns the L<Type::Tiny> type the object was created with. |
|
79
|
|
|
|
|
|
|
80
|
|
|
|
|
=head1 AUTHOR |
|
81
|
|
|
|
|
|
|
82
|
|
|
|
|
Jon Portnoy <avenj@cobaltirc.org> with significant contributions from Toby |
|
83
|
|
|
|
|
Inkster (CPAN: TOBYINK) |
|
84
|
|
|
|
|
|
|
85
|
|
|
|
|
=cut |
|
86
|
|
|
|
|
|