File Coverage

lib/App/Tel/Merge.pm
Criterion Covered Total %
statement 18 19 94.7
branch 4 8 50.0
condition 2 3 66.6
subroutine 3 3 100.0
pod 1 1 100.0
total 28 34 82.3


line stmt bran cond sub pod time code
1             package App::Tel::Merge;
2              
3 7     7   20 use strict;
  7         7  
  7         153  
4 7     7   20 use warnings;
  7         7  
  7         1275  
5              
6             require Exporter;
7             our @ISA = qw/ Exporter /;
8             our @EXPORT_OK = qw/ merge /;
9              
10             =head1 NAME
11              
12             App::Tel::Merge - simple hash merge
13              
14             =cut
15              
16              
17             # This was stoled from Hash::Merge::Simple, which stoled from Catalyst::Utils
18             # I stole it because I wanted to get rid of the dependancy on Clone which
19             # needs to be compiled by cpanm when we weren't using it.
20              
21             =head2 merge
22              
23             my $merged = merge($left, @right);
24              
25             Used to merge two hash references together. Returns a hashref.
26              
27             =cut
28              
29             sub merge (@); ## no critic qw(Subroutines::ProhibitSubroutinePrototypes)
30             sub merge (@) { ## no critic qw(Subroutines::ProhibitSubroutinePrototypes)
31 8 50   8 1 21 shift unless ref $_[0]; # Take care of the case we're called like Hash::Merge::Simple->merge(...)
32 8         12 my ($left, @right) = @_;
33              
34 8 50       18 return $left unless @right;
35              
36 8 50       18 return merge($left, merge(@right)) if @right > 1;
37              
38 8         11 my ($right) = @right;
39              
40 8         24 my %merge = %$left;
41              
42 8         17 for my $key (keys %$right) {
43              
44 22         19 my ($hr, $hl) = map { ref $_->{$key} eq 'HASH' } $right, $left;
  44         70  
45              
46 22 50 66     71 if ($hr and $hl){
47 0         0 $merge{$key} = merge($left->{$key}, $right->{$key});
48             }
49             else {
50 22         34 $merge{$key} = $right->{$key};
51             }
52             }
53 8         25 return \%merge;
54             }
55