| 1 | package List::Objects::WithUtils::Role::Array::Typed; | ||||||||||
| 2 | 93 | $List::Objects::WithUtils::Role::Array::Typed::VERSION = '2.028002'; | |||||||||
| 3 | 93 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 93 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 93 | ▶ | use Scalar::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 93 | ▶ | use Type::Tie (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 93 | ▶ | use Role::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 93 | requires 'type', 'untyped', 'new'; | |||||||||
| 11 | |||||||||||
| 12 | 12 | ▶ | around type => sub { tied(@{$_[1]})->type }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | around untyped => sub { | ||||||||||
| 15 | 1 | ▶ | my (undef, $self) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | require List::Objects::WithUtils::Array; | |||||||||
| 17 | 1 | List::Objects::WithUtils::Array->new(@$self) | |||||||||
| 18 | 93 | }; | |||||||||
| 19 | |||||||||||
| 20 | around new => sub { | ||||||||||
| 21 | # yes, this splice is correct: | ||||||||||
| 22 | 12 | ▶ | my (undef, $class, $type) = splice @_, 0, 2; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 23 | |||||||||||
| 24 | 12 | ▶ | if (my $blessed = Scalar::Util::blessed $class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 7 | $type = $class->type; | |||||||||
| 26 | 7 | $class = $blessed; | |||||||||
| 27 | } else { | ||||||||||
| 28 | 5 | $type = shift; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 12 | my $self = []; | |||||||||
| 32 | 12 | tie @$self, 'Type::Tie::ARRAY', $type; | |||||||||
| 33 | 12 | push @$self, @_; | |||||||||
| 34 | 9 | bless $self, $class; | |||||||||
| 35 | 93 | }; | |||||||||
| 36 | |||||||||||
| 37 | 93 | ▶ | |||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | qq[<mauke> you seem to be ignoring mst\n], | ||||||||||
| 39 | qq[<mauke> would you like to talk to me instead?\n], | ||||||||||
| 40 | qq[<joel> mauke++ # talking paperclip\n], | ||||||||||
| 41 | qq[<mauke> I can't help you but I'm in a pretty good mood\n] | ||||||||||
| 42 | unless caller; | ||||||||||
| 43 | 93 | 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<Type::Tie> to add type-checking behavior to | ||||||||||
| 69 | L<List::Objects::WithUtils::Role::Array> consumers. | ||||||||||
| 70 | |||||||||||
| 71 | The first argument passed to the constructor should be a L<Type::Tiny> type | ||||||||||
| 72 | (or other object conforming to L<Type::API>, as of C<v2.25>): | ||||||||||
| 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<Types::Standard>, L<List::Objects::Types> | ||||||||||
| 85 | |||||||||||
| 86 | =head2 type | ||||||||||
| 87 | |||||||||||
| 88 | Returns the L<Type::Tiny> type the object was created with. | ||||||||||
| 89 | |||||||||||
| 90 | =head2 untyped | ||||||||||
| 91 | |||||||||||
| 92 | Returns a (shallow) clone that is a plain L<List::Objects::WithUtils::Array>. | ||||||||||
| 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 <avenj@cobaltirc.org> with significant contributions from Toby | ||||||||||
| 101 | Inkster (CPAN: TOBYINK) | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||