| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package List::Objects::WithUtils::Role::Hash::Immutable; | 
| 2 |  |  |  |  |  |  | $List::Objects::WithUtils::Role::Hash::Immutable::VERSION = '2.028003'; | 
| 3 | 98 |  |  | 98 |  | 39041 | use strictures 2; | 
|  | 98 |  |  |  |  | 447 |  | 
|  | 98 |  |  |  |  | 2883 |  | 
| 4 | 98 |  |  | 98 |  | 12266 | use Carp (); | 
|  | 98 |  |  |  |  | 111 |  | 
|  | 98 |  |  |  |  | 992 |  | 
| 5 | 98 |  |  | 98 |  | 11508 | use Tie::Hash (); | 
|  | 98 |  |  |  |  | 17571 |  | 
|  | 98 |  |  |  |  | 7719 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | sub _make_unimp { | 
| 8 | 392 |  |  | 392 |  | 1548 | my ($method) = @_; | 
| 9 |  |  |  |  |  |  | sub { | 
| 10 | 8 |  |  | 8 |  | 2998 | local $Carp::CarpLevel = 1; | 
| 11 | 8 |  |  |  |  | 713 | Carp::croak "Method '$method' not implemented on immutable hashes" | 
| 12 |  |  |  |  |  |  | } | 
| 13 | 392 |  |  |  |  | 1101 | } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our @ImmutableMethods = qw/ | 
| 16 |  |  |  |  |  |  | clear | 
| 17 |  |  |  |  |  |  | set | 
| 18 |  |  |  |  |  |  | maybe_set | 
| 19 |  |  |  |  |  |  | delete | 
| 20 |  |  |  |  |  |  | /; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 98 |  |  | 98 |  | 357 | use Role::Tiny; | 
|  | 98 |  |  |  |  | 104 |  | 
|  | 98 |  |  |  |  | 450 |  | 
| 23 |  |  |  |  |  |  | requires 'new', @ImmutableMethods; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | around is_mutable => sub { () }; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | around new => sub { | 
| 28 |  |  |  |  |  |  | my ($orig, $class) = splice @_, 0, 2; | 
| 29 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | tie %$self, 'Tie::StdHash' and %$self = @_ | 
| 36 |  |  |  |  |  |  | unless tied %$self; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | Role::Tiny->apply_roles_to_object( tied(%$self), | 
| 39 |  |  |  |  |  |  | 'List::Objects::WithUtils::Role::Hash::TiedRO' | 
| 40 |  |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $self | 
| 43 |  |  |  |  |  |  | }; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | around $_ => _make_unimp($_) for @ImmutableMethods; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | 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 | 
| 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 for a consumer | 
| 77 |  |  |  |  |  |  | implementation. | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head1 AUTHOR | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Jon Portnoy | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =cut |