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-2018 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   109134 use vars '$VERSION';
  2         17  
  2         100  
11             $VERSION = '1.03';
12              
13 2     2   9 use base 'Hash::Case';
  2         3  
  2         702  
14              
15 2     2   14 use strict;
  2         3  
  2         32  
16 2     2   7 use warnings;
  2         3  
  2         44  
17              
18 2     2   10 use Log::Report 'hash-case';
  2         4  
  2         7  
19              
20              
21             sub init($)
22 6     6 0 11 { my ($self, $args) = @_;
23              
24 6         21 $self->{HCP_data} = {};
25 6         11 $self->{HCP_keys} = {};
26              
27 6   50     16 my $keep = $args->{keep} || 'LAST';
28 6 100       18 if($keep eq 'LAST') { $self->{HCP_update} = 1 }
  3 50       6  
29 3         5 elsif($keep eq 'FIRST') { $self->{HCP_update} = 0 }
30             else
31 0         0 { error "use 'FIRST' or 'LAST' with the option keep";
32             }
33              
34 6         21 $self->SUPER::native_init($args);
35             }
36              
37             # Maintain two hashes within this object: one to store the values, and
38             # one to preserve the casing. The main object also stores the options.
39             # The data is kept under lower cased keys.
40              
41 30     30   5689 sub FETCH($) { $_[0]->{HCP_data}{lc $_[1]} }
42              
43             sub STORE($$)
44 16     16   2941 { my ($self, $key, $value) = @_;
45 16         26 my $lckey = lc $key;
46              
47             $self->{HCP_keys}{$lckey} = $key
48 16 100 100     61 if $self->{HCP_update} || !exists $self->{HCP_keys}{$lckey};
49              
50 16         42 $self->{HCP_data}{$lckey} = $value;
51             }
52              
53             sub FIRSTKEY
54 26     26   5834 { my $self = shift;
55 26         30 my $a = scalar keys %{$self->{HCP_keys}};
  26         43  
56 26         46 $self->NEXTKEY;
57             }
58              
59             sub NEXTKEY($)
60 56     56   81 { my $self = shift;
61 56 100       53 if(my ($k, $v) = each %{$self->{HCP_keys}})
  56         129  
62 30 50       99 { return wantarray ? ($v, $self->{HCP_data}{$k}) : $v;
63             }
64 26         77 else { return () }
65             }
66              
67 2     2   4718 sub EXISTS($) { exists $_[0]->{HCP_data}{lc $_[1]} }
68              
69             sub DELETE($)
70 2     2   7 { my $lckey = lc $_[1];
71 2         5 delete $_[0]->{HCP_keys}{$lckey};
72 2         10 delete $_[0]->{HCP_data}{$lckey};
73             }
74              
75             sub CLEAR()
76 2     2   5 { %{$_[0]->{HCP_data}} = ();
  2         6  
77 2         5 %{$_[0]->{HCP_keys}} = ();
  2         7  
78             }
79              
80             1;