File Coverage

blib/lib/Hash/Case/Preserve.pm
Criterion Covered Total %
statement 45 46 97.8
branch 8 10 80.0
condition 4 5 80.0
subroutine 13 13 100.0
pod 0 1 0.0
total 70 75 93.3


line stmt bran cond sub pod time code
1             # Copyrights 2002-2020 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution Hash::Case. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Hash::Case::Preserve;
10 2     2   135437 use vars '$VERSION';
  2         20  
  2         131  
11             $VERSION = '1.05';
12              
13 2     2   13 use base 'Hash::Case';
  2         3  
  2         918  
14              
15 2     2   16 use strict;
  2         3  
  2         42  
16 2     2   11 use warnings;
  2         2  
  2         50  
17 2     2   10 use Carp 'croak';
  2         3  
  2         990  
18              
19              
20             sub init($)
21 6     6 0 14 { my ($self, $args) = @_;
22              
23 6         21 $self->{HCP_data} = {};
24 6         13 $self->{HCP_keys} = {};
25              
26 6   50     20 my $keep = $args->{keep} || 'LAST';
27 6 100       17 if($keep eq 'LAST') { $self->{HCP_update} = 1 }
  3 50       4  
28 3         5 elsif($keep eq 'FIRST') { $self->{HCP_update} = 0 }
29             else
30 0         0 { croak "use 'FIRST' or 'LAST' with the option keep";
31             }
32              
33 6         21 $self->SUPER::native_init($args);
34             }
35              
36             # Maintain two hashes within this object: one to store the values, and
37             # one to preserve the casing. The main object also stores the options.
38             # The data is kept under lower cased keys.
39              
40 30     30   6465 sub FETCH($) { $_[0]->{HCP_data}{lc $_[1]} }
41              
42             sub STORE($$)
43 16     16   3336 { my ($self, $key, $value) = @_;
44 16         30 my $lckey = lc $key;
45              
46             $self->{HCP_keys}{$lckey} = $key
47 16 100 100     72 if $self->{HCP_update} || !exists $self->{HCP_keys}{$lckey};
48              
49 16         65 $self->{HCP_data}{$lckey} = $value;
50             }
51              
52             sub FIRSTKEY
53 26     26   6672 { my $self = shift;
54 26         36 my $a = scalar keys %{$self->{HCP_keys}};
  26         60  
55 26         56 $self->NEXTKEY;
56             }
57              
58             sub NEXTKEY($)
59 56     56   102 { my $self = shift;
60 56 100       67 if(my ($k, $v) = each %{$self->{HCP_keys}})
  56         152  
61 30 50       125 { return wantarray ? ($v, $self->{HCP_data}{$k}) : $v;
62             }
63 26         98 else { return () }
64             }
65              
66 2     2   5332 sub EXISTS($) { exists $_[0]->{HCP_data}{lc $_[1]} }
67              
68             sub DELETE($)
69 2     2   6 { my $lckey = lc $_[1];
70 2         11 delete $_[0]->{HCP_keys}{$lckey};
71 2         11 delete $_[0]->{HCP_data}{$lckey};
72             }
73              
74             sub CLEAR()
75 2     2   6 { %{$_[0]->{HCP_data}} = ();
  2         17  
76 2         4 %{$_[0]->{HCP_keys}} = ();
  2         9  
77             }
78              
79             1;