blib/lib/Hash/Mogrify.pm

filter: Statementstmt 92.8% 104 / 112 Branchbran 51.9% 27 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 79.2% 141 / 178 CC 31 SCAR 19.5
CC
31
Cov
80%
_double
13.3
ktrans
7.3
hmap
6.9
CRAP 7.0
↑ Summary
1 package Hash::Mogrify;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Hash::Mogrify - Perl extension for modifying hashes
10  
11 =head1 SYNOPSIS
12  
13 use Hash::Mogrify qw(kmap vmap hmap);
14 # or :all
15 or
16 use Hash::Mogrify qw(kmap vmap hmap :force :nowarning :dieonerror);
17 # to set global bitmaps
18 or
19 use Hash::Mogrify qw(:all :const);
20 # also get constants for setting local bitmaps.
21  
22 my %hash = ( foo => 'bar',
23 quuz => 'quux',
24 bla => 'bulb',);
25  
26 my %newhash = kmap { $_ =~ s/foo/food/ } %hash;
27 my $newhashref = vmap { $_ =~ s/bulb/burp/ } %hash;
28 my $samehashref = hmap { $_[0] =~ s/foo/food/; $_[1] =~ s/bulb/burp/ } \%hash;
29  
30 ## setting local bitmaps
31 my %newhash = kmap { $_ =~ s/foo/food/ } %hash, NOWARNING | FORCE;
32 # to enable nowarning and force for this action.
33  
34 kmap { $_ =~ s/foo/food/ } \%hash, DIEONERR
35 # to let kmap die on error.
36  
37 ktrans { foo => 'food' }, \%hash;
38 # Change key foo into key food.
39
40 =head1 DESCRIPTION
41  
42 Hash::Mogrify contains functions for changes parts of hashes, change/mogrify it's keys or it's values.
43  
44 The functions are flexible in design.
45 The functions kmap, vmap and hmap return a hash/list in list context and a hash-reference in scalar context.
46 The first argument to these functions is a code block to mogrify the hash, the second either a hash or a hashref.
47  
48 If a hash(list) is provided as an argument a new hash is created. When a hash-reference (e.a \%hash) is provided the original hash is changed.
49  
50 The function ktrans works similar to kmap, except that it takes a hashref as translation table instead of a codeblock.
51  
52 By default no function overwrites existing keys and warns about this when trying.
53 this can be changed by setting the global or local bitmap.
54 The global bitmap can be set on load by the following keys:
55 :nowarning # do not warn about errors
56 :dieonerror # die incase you're trying to override an existing key
57 :force # override existing keys (overrrides :dieonerror).
58 The local bitmap can be set by adding to the end of the function, there are the following constants:
59 NOWARNING
60 FORCE
61 DIEONERR
62 The local bitmap will completely override the global bitmap.
63  
64 More options might be provided in later versions.
65  
66 =head2 EXPORT
67  
68 None by default.
69  
70 =cut
71  
72 1 ▶use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $GLOBALMAP);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 require Exporter;
74  
75 1 @ISA = qw(Exporter);
76  
77 1 %EXPORT_TAGS = (
78 'all' => [ qw(
79 hmap
80 kmap
81 vmap
82 ktrans
83 ) ],
84 'const' => [ qw(
85 FORCE
86 NOWARNING
87 DIEONERR
88 ) ],
89 nowarning => [],
90 force => [],
91 dieonerror => [],);
92  
93 1 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} }, @{ $EXPORT_TAGS{'const'} });
94  
95 1 @EXPORT = ();
96  
97 1 $VERSION = '0.03';
98  
99 sub FORCE() { 1; }
100 sub NOWARNING() { 2; }
101 sub DIEONERR() { 4; }
102  
103 1 $GLOBALMAP = 0;
104  
105 2 ▶sub import {
Subroutine import: called CC=4 SCAR=14.2
106 2 ▶ $GLOBALMAP |= FORCE if(grep /:force/, @_);
Branch: 1 branch
BranchTrueFalseTotal
if grep /:force/, @_ 0 2 2
107 2 ▶ $GLOBALMAP |= NOWARNING if(grep /:nowarning/, @_);
Branch: 1 branch
BranchTrueFalseTotal
if grep /:nowarning/, @_ 1 1 2
108 2 ▶ $GLOBALMAP |= DIEONERR if(grep /:dieonerror/, @_);
Branch: 1 branch
BranchTrueFalseTotal
if grep /:dieonerror/, @_ 0 2 2
109 2 Hash::Mogrify->export_to_level(1, @_);
110 }
111  
112 3 ▶sub kmap(&@) {
Subroutine kmap: called CC=6 SCAR=18.0
Pod undocumented
113 3 my $code = shift;
114 3 my $hash = $_[0];
115 3 my $bitmap;
116 3 ▶ if(!ref $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash) 1 2 3
117 1 ▶ $bitmap = shift if((scalar @_) % 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 1 1
118 1 $hash = { @_ };
119 }
120  
121 3 my $temp;
122 3 for (keys %{$hash}) {
123 9 my $value = $hash->{$_};
124 9 $code->($_, $value);
125 9 ▶ _double($temp, $_, $bitmap) or return;
Branch: 1 branch
BranchTrueFalseTotal
_double($temp, $_, $bitmap) or return 7 2 9
126 7 $temp->{$_} = $value;
127 }
128 1 %{$hash} = %{$temp};
129  
130 0 ▶ return %{$hash} if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
131 1 return $hash;
132 }
133  
134 1 ▶sub vmap(&@) {
Subroutine vmap: called CC=5 SCAR=16.3
Pod undocumented
135 1 my $code = shift;
136 1 my $hash = $_[0];
137 1 my $bitmap; # we don't use this, but maybe once :)
138 1 ▶ if(!ref $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash) 1 0 1
139 1 ▶ $bitmap = shift if((scalar @_) % 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 1 1
140 1 $hash = { @_ };
141 }
142  
143 1 my $temp;
144 1 for my $key (keys(%{ $hash })) {
145 4 $_ = $hash->{$key};
146 4 $code->($key, $_);
147 4 $temp->{$key} = $_;
148 }
149 1 %{$hash} = %{$temp};
150  
151 1 ▶ return %{$hash} if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 0 1
152 0 return $hash;
153 }
154  
155 1 ▶sub hmap(&@) {
Subroutine hmap: called CC=6 SCAR=19.4
Pod undocumented
156 1 my $code = shift;
157 1 my $hash = $_[0];
158 1 my $bitmap;
159 1 ▶ if(!ref $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash) 0 1 1
160 0 ▶ $bitmap = shift if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
161 0 $hash = { @_ };
162 }
163  
164 1 my $temp;
165 1 for my $key (keys(%{ $hash })) {
166 4 my $value = $hash->{$key};
167 4 $code->($key, $value);
168 4 ▶ _double($temp, $key, $bitmap) or return;
Branch: 1 branch
BranchTrueFalseTotal
_double($temp, $key, $bitmap) or return 4 0 4
169 4 $temp->{$key} = $value;
170 }
171 1 %{$hash} = %{$temp};
172  
173 0 ▶ return %{$hash} if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
174 1 return $hash;
175 }
176  
177 1 ▶sub ktrans($@) {
Subroutine ktrans: called CC=7 SCAR=19.9
Pod undocumented
178 1 my $table = shift;
179 1 my $hash = $_[0];
180  
181 1 my $bitmap;
182 1 ▶ if(!ref $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash) 1 0 1
183 1 ▶ $bitmap = shift if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
184 1 $hash = { @_ };
185 }
186  
187 1 my $temp = { %{$hash} };
188 1 for my $old (keys(%{ $table })) {
189 2 ▶ next if(!exists $temp->{$old});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $temp->{$old} 0 2 2
190 2 my $new = $table->{$old};
191  
192 2 ▶ _double($temp, $new, $bitmap) or return;
Branch: 1 branch
BranchTrueFalseTotal
_double($temp, $new, $bitmap) or return 2 0 2
193  
194 2 my $value = $temp->{$old};
195 2 delete $temp->{$old};
196 2 $temp->{$new} = $value;
197 }
198 1 %{$hash} = %{$temp};
199  
200 1 ▶ return %{$hash} if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 0 1
201 0 return $hash;
202 }
203  
204 # check if a hashkey exists, and act depending on global&local settings
205 15 ▶sub _double {
Subroutine _double: called CC=8 SCAR=25.8
206 15 my ($hash, $key, $bitmap) = @_;
207 15 ▶ my $map = defined($bitmap) ? $bitmap : $GLOBALMAP;
Branch: 1 branch
BranchTrueFalseTotal
defined $bitmap ? : 0 15 15
208  
209 15 ▶ return 1 if(!exists $hash->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 13 2 15
210 2 ▶ if(!($map & NOWARNING)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($map & 2) 0 2 2
211 0 ▶ warn('Attempting to override existing key, failing.') if(!$map & FORCE);
Branch: 1 branch
BranchTrueFalseTotal
if !$map & 1 0 0 0
212 0 ▶ warn('Attempting to override existing key, forcing.') if($map & FORCE);
Branch: 1 branch
BranchTrueFalseTotal
if $map & 1 0 0 0
213 }
214 2 ▶ return 1 if($map & FORCE);
Branch: 1 branch
BranchTrueFalseTotal
if $map & 1 0 2 2
215 2 ▶ die 'Died, trying to override existing key' if($map & DIEONERR);
Branch: 1 branch
BranchTrueFalseTotal
if $map & 4 0 2 2
216 2 return;
217 }
218 1 1;
219 __END__
↑ Summary