File Coverage

blib/lib/Dist/Zilla/Role/HashDumper.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
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             our $VERSION = '4.13';
21             # This file is part of Dist-Zilla-Plugins-CJM 4.26 (December 13, 2014)
22              
23 1     1   1660 use Moose::Role;
  0            
  0            
24              
25             use namespace::autoclean;
26              
27             use Scalar::Util 'reftype';
28              
29              
30             sub hash_as_string
31             {
32             my ($self, $hash) = @_;
33              
34             # Format the hash as a string:
35             require Data::Dumper;
36              
37             my $data = Data::Dumper->new([ $hash ])
38             ->Indent(1)->Sortkeys(1)->Terse(1)->Dump;
39              
40             if ($data eq "{}\n") {
41             $data = '';
42             } else {
43             $data =~ s/^\{\n// or die "Dump prefix! $data";
44             $data =~ s/\n\}\n\z/,/ or die "Dump postfix! $data";
45             }
46              
47             return $data;
48             } # end hash_as_string
49              
50              
51             sub extract_keys
52             {
53             my $self = shift;
54              
55             return $self->hash_as_string( $self->extract_keys_as_hash(@_) );
56             } # end extract_keys
57              
58              
59             sub extract_keys_as_hash
60             {
61             my $self = shift;
62             my $type = shift;
63             my $hash = shift;
64              
65             # Extract the wanted keys from the hash:
66             my %want;
67              
68             foreach my $key (@_) {
69             $self->log_debug("Fetching $type key $key");
70             next unless defined $hash->{$key};
71              
72             # Skip keys with empty value:
73             my $reftype = reftype($hash->{$key});
74             if (not $reftype) {}
75             elsif ($reftype eq 'HASH') { next unless %{ $hash->{$key} } }
76             elsif ($reftype eq 'ARRAY') { next unless @{ $hash->{$key} } }
77              
78             $want{$key} = $hash->{$key};
79             } # end foreach $key
80              
81             return \%want;
82             } # end extract_keys_as_hash
83              
84             no Moose::Role;
85             1;
86              
87             __END__
88              
89             =head1 NAME
90              
91             Dist::Zilla::Role::HashDumper - Dump selected hash keys as a string
92              
93             =head1 VERSION
94              
95             This document describes version 4.13 of
96             Dist::Zilla::Role::HashDumper, released December 13, 2014
97             as part of Dist-Zilla-Plugins-CJM version 4.26.
98              
99             =head1 DESCRIPTION
100              
101             Plugins implementing HashDumper may call their own C<extract_keys>
102             method to extract selected keys from a hash and return a string
103             suitable for injecting into Perl code. They may also call the
104             C<hash_as_string> method to do the same for an entire hash.
105              
106             =head1 METHODS
107              
108             =head2 extract_keys
109              
110             my $string = $plugin->extract_keys($name, \%hash, @keys);
111             eval "%new_hash = ($string);";
112              
113             This combines C<extract_keys_as_hash> and C<hash_as_string>.
114             It constructs a string of properly quoted keys and values from
115             selected keys in a hash. (Note that C<\%hash> is a reference, but
116             C<@keys> is not.) The C<$name> is used only in a log_debug message.
117              
118             If any key has no value (or its value is an empty hash or array ref)
119             it will be omitted from the list. If all keys are omitted, the empty
120             string is returned. Otherwise, the result always ends with a comma.
121              
122              
123             =head2 extract_keys_as_hash
124              
125             my $hashref = $plugin->extract_keys_as_hash($name, \%hash, @keys);
126              
127             This constructs a hashref from from selected keys in a hash. (Note
128             that C<\%hash> is a reference, but C<@keys> is not.) The C<$name> is
129             used only in a log_debug message.
130              
131             If any key has no value (or its value is an empty hash or array ref)
132             it will be omitted from the new hashref. If all keys are omitted,
133             an empty hashref is returned.
134              
135              
136             =head2 hash_as_string
137              
138             my $string = $plugin->hash_as_string(\%hash);
139             eval "%new_hash = ($string);";
140              
141             This constructs a string of properly quoted keys and values from a
142             hash. If the hash is empty, the empty string will be returned.
143             Otherwise, the result always ends with a comma.
144              
145             =head1 CONFIGURATION AND ENVIRONMENT
146              
147             Dist::Zilla::Role::HashDumper requires no configuration files or environment variables.
148              
149             =head1 DEPENDENCIES
150              
151             L<Data::Dumper>.
152              
153             =head1 INCOMPATIBILITIES
154              
155             None reported.
156              
157             =head1 BUGS AND LIMITATIONS
158              
159             No bugs have been reported.
160              
161             =head1 AUTHOR
162              
163             Christopher J. Madsen S<C<< <perl AT cjmweb.net> >>>
164              
165             Please report any bugs or feature requests
166             to S<C<< <bug-Dist-Zilla-Plugins-CJM AT rt.cpan.org> >>>
167             or through the web interface at
168             L<< http://rt.cpan.org/Public/Bug/Report.html?Queue=Dist-Zilla-Plugins-CJM >>.
169              
170             You can follow or contribute to Dist-Zilla-Plugins-CJM's development at
171             L<< https://github.com/madsen/dist-zilla-plugins-cjm >>.
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2014 by Christopher J. Madsen.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =head1 DISCLAIMER OF WARRANTY
181              
182             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
183             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
184             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
185             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
186             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
187             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
188             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
189             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
190             NECESSARY SERVICING, REPAIR, OR CORRECTION.
191              
192             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
193             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
194             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
195             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
196             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
197             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
198             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
199             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
200             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
201             SUCH DAMAGES.
202              
203             =cut