Role/HashDumper.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 50.0% 9 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 82.0% 55 / 67 CC 11 SCAR 17.7
CC
11
Cov
79%
extract_keys_as_hash
9.7
hash_as_string
4.4
BEGIN
1.0
CRAP 5.9
« Plugin/RecommendedPrereqs.pm ↑ Summary Plugin/MakeMaker/Custom.pm »
1 #---------------------------------------------------------------------
2 package Dist::Zilla::Role::HashDumper;
3 #
4 # Copyright 2011 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 4 Nov 2011
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Dump selected hash keys as a string
18 #---------------------------------------------------------------------
19  
20 3 our $VERSION = '4.13';
21 # This file is part of Dist-Zilla-Plugins-CJM 6.000 (December 17, 2017)
22  
23 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 sub hash_as_string
31 7 ▶{
Subroutine hash_as_string: called CC=4 SCAR=14.8
Pod documented
32 7 my ($self, $hash) = @_;
33  
34 # Format the hash as a string:
35 7 require Data::Dumper;
36  
37 7 my $data = Data::Dumper->new([ $hash ])
38 ->Indent(1)->Sortkeys(1)->Terse(1)->Dump;
39  
40 7 ▶ if ($data eq "{}\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "{}\n") { } 0 7 7
41 0 $data = '';
42 } else {
43 7 ▶ $data =~ s/^\{\n// or die "Dump prefix! $data";
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s/^\{\n// or die "Dump prefix! $data" 7 0 7
44 7 ▶ $data =~ s/\n\}\n\z/,/ or die "Dump postfix! $data";
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s/\n\}\n\z/,/ or die "Dump postfix! $data" 7 0 7
45 }
46  
47 7 return $data;
48 } # end hash_as_string
49  
50  
51 sub extract_keys
52 4 ▶{
Subroutine extract_keys: called CC=1 SCAR=0.0
Pod documented
53 4 my $self = shift;
54  
55 4 return $self->hash_as_string( $self->extract_keys_as_hash(@_) );
56 } # end extract_keys
57  
58  
59 sub extract_keys_as_hash
60 7 ▶{
Subroutine extract_keys_as_hash: called CC=8 SCAR=22.7
Pod documented
61 7 my $self = shift;
62 7 my $type = shift;
63 7 my $hash = shift;
64  
65 # Extract the wanted keys from the hash:
66 7 my %want;
67  
68 7 foreach my $key (@_) {
69 29 $self->log_debug("Fetching $type key $key");
70 29 ▶ next unless defined $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{$key} 7 22 29
71  
72 # Skip keys with empty value:
73 22 my $reftype = reftype($hash->{$key});
74 22 ▶ if (not $reftype) {}
Branch: 3 branches
BranchTrueFalseTotal
if (not $reftype) { } 1 21 22
elsif ($reftype eq 'HASH') { } 21 0 21
elsif ($reftype eq 'ARRAY') { } 0 0 0
75 21 ▶ elsif ($reftype eq 'HASH') { next unless %{ $hash->{$key} } }
Branch: 1 branch
BranchTrueFalseTotal
unless %{$hash->{$key};} 0 21 21
76 0 ▶ elsif ($reftype eq 'ARRAY') { next unless @{ $hash->{$key} } }
Branch: 1 branch
BranchTrueFalseTotal
unless @{$hash->{$key};} 0 0 0
77  
78 22 $want{$key} = $hash->{$key};
79 } # end foreach $key
80  
81 7 return \%want;
82 } # end extract_keys_as_hash
83  
84 3 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 3 1;
86  
87 __END__
« Plugin/RecommendedPrereqs.pm ↑ Summary Plugin/MakeMaker/Custom.pm »