« List/Objects/WithUtils/Hash/Inflated/RW.pm
↑ Summary
List/Objects/WithUtils/Role/Array/Immutable.pm »
| 1 | package List::Objects::WithUtils::Role::Hash::Immutable; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 98 | $List::Objects::WithUtils::Role::Hash::Immutable::VERSION = '2.028002'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 98 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 98 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 98 | ▶ | use Tie::Hash (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | sub _make_unimp { | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 392 | ▶ | my ($method) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_unimp: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | local $Carp::CarpLevel = 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | Carp::croak "Method '$method' not implemented on immutable hashes" | |||||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 392 | } | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 98 | our @ImmutableMethods = qw/ | |||||||||||||||||||||||||||||||||||||||||
| 16 | clear | ||||||||||||||||||||||||||||||||||||||||||
| 17 | set | ||||||||||||||||||||||||||||||||||||||||||
| 18 | maybe_set | ||||||||||||||||||||||||||||||||||||||||||
| 19 | delete | ||||||||||||||||||||||||||||||||||||||||||
| 20 | /; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 98 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 98 | requires 'new', @ImmutableMethods; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | around is_mutable => sub { () }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | around new => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my ($orig, $class) = splice @_, 0, 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $self = $class->$orig(@_); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # This behavior changed in c. 45f59a73 -- | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # we can revert back if Hash::Util gains the flexibility discussed on p5p | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # (lock_keys without an exception on unknown key retrieval) | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # For now, take the tie performance hit :( | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | tie %$self, 'Tie::StdHash' and %$self = @_ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
tied %$self || tie(%$self, 'Tie::StdHash') && (%$self = @_)
tie %$self, "Tie::StdHash" and %$self = @_
MC/DC0%0/3
tied %$self || tie(%$self, 'Tie::StdHash') && (%$self = @_)
tied %$self
tie %$self, "Tie::StdHash"
%$self = @_
Truth table0%0/4
A || B and C
A= tied %$selfB= tie %$self, "Tie::StdHash"C= %$self = @_
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | unless tied %$self; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | Role::Tiny->apply_roles_to_object( tied(%$self), | |||||||||||||||||||||||||||||||||||||||||
| 39 | 'List::Objects::WithUtils::Role::Hash::TiedRO' | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $self | |||||||||||||||||||||||||||||||||||||||||
| 43 | 98 | }; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 98 | around $_ => _make_unimp($_) for @ImmutableMethods; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 98 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | List::Objects::WithUtils::Role::Hash::Immutable - Immutable hash behavior | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Via List::Objects::WithUtils::Hash::Immutable -> | ||||||||||||||||||||||||||||||||||||||||||
| 58 | use List::Objects::WithUtils 'immhash'; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | my $hash = immhash( foo => 1, bar => 2 ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | $hash->set(foo => 3); # dies | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | This role adds immutable behavior to L<List::Objects::WithUtils::Role::Hash> | ||||||||||||||||||||||||||||||||||||||||||
| 65 | consumers. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | The following methods are not available and will throw an exception: | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | clear | ||||||||||||||||||||||||||||||||||||||||||
| 70 | set | ||||||||||||||||||||||||||||||||||||||||||
| 71 | maybe_set | ||||||||||||||||||||||||||||||||||||||||||
| 72 | delete | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | (The backing hash is also marked read-only.) | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | See L<List::Objects::WithUtils::Hash::Immutable> for a consumer | ||||||||||||||||||||||||||||||||||||||||||
| 77 | implementation. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||||||||