| 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
| |||||||||||||||||||
| 41 | 0 | $data = ''; | |||||||||||||||||
| 42 | } else { | ||||||||||||||||||
| 43 | 7 | ▶ | $data =~ s/^\{\n// or die "Dump prefix! $data"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 7 | ▶ | $data =~ s/\n\}\n\z/,/ or die "Dump postfix! $data"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | # Skip keys with empty value: | ||||||||||||||||||
| 73 | 22 | my $reftype = reftype($hash->{$key}); | |||||||||||||||||
| 74 | 22 | ▶ | if (not $reftype) {} | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 75 | 21 | ▶ | elsif ($reftype eq 'HASH') { next unless %{ $hash->{$key} } } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 76 | 0 | ▶ | elsif ($reftype eq 'ARRAY') { next unless @{ $hash->{$key} } } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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__ | ||||||||||||||||||