« List/Objects/WithUtils/Role/Array/Typed.pm
↑ Summary
List/Objects/WithUtils/Array/Immutable/Typed.pm »
| 1 | package List::Objects::WithUtils::Role::Hash::Typed; | ||||||||||
| 2 | 99 | $List::Objects::WithUtils::Role::Hash::Typed::VERSION = '2.028002'; | |||||||||
| 3 | 99 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 99 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 99 | ▶ | use Scalar::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 99 | ▶ | use Type::Tie (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 99 | ▶ | use Role::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 99 | requires 'type', 'untyped', 'new'; | |||||||||
| 11 | |||||||||||
| 12 | 4 | ▶ | 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::Hash; | |||||||||
| 17 | 1 | List::Objects::WithUtils::Hash->new(%$self) | |||||||||
| 18 | 99 | }; | |||||||||
| 19 | |||||||||||
| 20 | around new => sub { | ||||||||||
| 21 | 7 | ▶ | my (undef, $class, $type) = splice @_, 0, 2; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 22 | |||||||||||
| 23 | 7 | ▶ | if (my $blessed = Scalar::Util::blessed $class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | $type = $class->type; | |||||||||
| 25 | 1 | $class = $blessed; | |||||||||
| 26 | } else { | ||||||||||
| 27 | 6 | $type = shift; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 7 | my $self = +{}; | |||||||||
| 31 | 7 | tie %$self, 'Type::Tie::HASH', $type; | |||||||||
| 32 | 7 | %$self = @_; | |||||||||
| 33 | 5 | bless $self, $class; | |||||||||
| 34 | 99 | }; | |||||||||
| 35 | |||||||||||
| 36 | 99 | 1; | |||||||||
| 37 | |||||||||||
| 38 | =pod | ||||||||||
| 39 | |||||||||||
| 40 | =for Pod::Coverage new hash_of | ||||||||||
| 41 | |||||||||||
| 42 | =head1 NAME | ||||||||||
| 43 | |||||||||||
| 44 | List::Objects::WithUtils::Role::Hash::Typed - Type-checking hash behavior | ||||||||||
| 45 | |||||||||||
| 46 | =head1 SYNOPSIS | ||||||||||
| 47 | |||||||||||
| 48 | # Via List::Objects::WithUtils::Hash::Typed -> | ||||||||||
| 49 | use List::Objects::WithUtils 'hash_of'; | ||||||||||
| 50 | use Types::Standard -all; | ||||||||||
| 51 | |||||||||||
| 52 | my $arr = hash_of(Int, foo => 1, bar => 2); | ||||||||||
| 53 | $arr->set(baz => 3.14159); # dies, failed type check | ||||||||||
| 54 | |||||||||||
| 55 | =head1 DESCRIPTION | ||||||||||
| 56 | |||||||||||
| 57 | This role makes use of L<Type::Tie> to add type-checking behavior to | ||||||||||
| 58 | L<List::Objects::WithUtils::Role::Hash> consumers. | ||||||||||
| 59 | |||||||||||
| 60 | The first argument passed to the constructor should be a L<Type::Tiny> type | ||||||||||
| 61 | (or other object conforming to L<Type::API>, as of C<v2.25>): | ||||||||||
| 62 | |||||||||||
| 63 | use Types::Standard -all; | ||||||||||
| 64 | my $arr = hash_of ArrayRef() => (foo => [], bar => []); | ||||||||||
| 65 | |||||||||||
| 66 | Values are checked against the specified type when the object is constructed | ||||||||||
| 67 | or new elements are added. | ||||||||||
| 68 | |||||||||||
| 69 | If the initial type-check fails, a coercion is attempted. | ||||||||||
| 70 | |||||||||||
| 71 | Values that cannot be coerced will throw an exception. | ||||||||||
| 72 | |||||||||||
| 73 | Also see L<Types::Standard>, L<List::Objects::Types> | ||||||||||
| 74 | |||||||||||
| 75 | =head2 type | ||||||||||
| 76 | |||||||||||
| 77 | Returns the L<Type::Tiny> type the object was created with. | ||||||||||
| 78 | |||||||||||
| 79 | =head2 untyped | ||||||||||
| 80 | |||||||||||
| 81 | Returns a (shallow) clone that is a plain L<List::Objects::WithUtils::Hash>. | ||||||||||
| 82 | |||||||||||
| 83 | =head1 AUTHOR | ||||||||||
| 84 | |||||||||||
| 85 | Jon Portnoy <avenj@cobaltirc.org>; typed hashes implemented by Toby Inkster | ||||||||||
| 86 | (CPAN: TOBYINK) | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||