File Coverage

blib/lib/List/Objects/WithUtils/Role/Hash/Immutable.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 22 100.0


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.027002';
3 98     98   65700 use strictures 2;
  98         620  
  98         4197  
4 98     98   20311 use Carp ();
  98         193  
  98         1540  
5 98     98   14524 use Tie::Hash ();
  98         18001  
  98         10769  
6              
7             sub _make_unimp {
8 392     392   2768 my ($method) = @_;
9             sub {
10 8     8   3176 local $Carp::CarpLevel = 1;
11 8         1034 Carp::croak "Method '$method' not implemented on immutable hashes"
12             }
13 392         2088 }
14              
15             our @ImmutableMethods = qw/
16             clear
17             set
18             maybe_set
19             delete
20             /;
21              
22 98     98   536 use Role::Tiny;
  98         211  
  98         617  
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