File Coverage

blib/lib/Encode/Deep.pm
Criterion Covered Total %
statement 47 48 97.9
branch 9 10 90.0
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 69 71 97.1


line stmt bran cond sub pod time code
1             package Encode::Deep;
2              
3 3     3   27776 use 5.006;
  3         10  
  3         117  
4 3     3   16 use strict;
  3         6  
  3         88  
5 3     3   14 use warnings;
  3         10  
  3         120  
6              
7             =head1 NAME
8              
9             Encode::Deep - Encode or decode each element of a reference and it's sub-references.
10              
11             =head1 VERSION
12              
13             Version 0.01
14              
15             =cut
16              
17 3     3   14 use Carp qw(croak);
  3         4  
  3         220  
18 3     3   12995 use Encode ();
  3         38317  
  3         61  
19 3     3   24 use base 'Exporter';
  3         6  
  3         1637  
20              
21             our $VERSION = '0.01';
22             our %EXPORT_TAGS = (
23             all => [qw(encode decode encode_inplace decode_inplace)],
24             );
25             our @EXPORT_OK = (map {@$_} values %EXPORT_TAGS);
26              
27             =pod
28              
29             =head1 SYNOPSIS
30              
31             Apply any encoding on a reference and all references within the parent.
32              
33             Supports hash, array and scalar reference but no blessed references (objects),
34             croaks on unknown refrences.
35              
36             Perhaps a little code snippet.
37              
38             use Encode::Deep;
39              
40             Encode::Deep::encode($encoding, $reference);
41              
42             Encode::Deep::decode($encoding, $reference);
43              
44             =head1 OTHER CHOICES
45              
46             L is also on CPAN but can't handle circular references while this module
47             recreates them as copies to new circular references. L does it's changes
48             in-place modifying the original reference which might be what you want or not.
49              
50             =head1 EXPORT
51              
52             A list of functions that can be exported. You can delete this section
53             if you don't export anything, such as for a purely object-oriented module.
54              
55             =head1 FUNCTIONS
56              
57             =head2 encode
58              
59             $copy_ref = Encode::Deep::encode($encoding, $ref);
60              
61             Walks through the given $ref and runs Encode::encode($encoding, $value) for every
62             non-reference value.
63              
64             See L for more information about the encode call being used for recoding.
65              
66             Returns a deep copy of the original reference meaning that every value and reference
67             will be copied.
68              
69             =cut
70              
71             sub encode {
72 1     1 1 759 my $encoding = shift;
73 1         2 my $ref = shift;
74              
75             return _walk(
76             $ref,
77 1     1   4 sub { return Encode::encode($encoding, shift); },
78             {},
79 1         6 );
80             }
81              
82             =pod
83              
84             =head2 decode
85              
86             $copy_ref = Encode::Deep::decode($encoding, $ref);
87              
88             Walks through the given $ref and runs Encode::decode($encoding, $value) for every
89             non-reference value.
90              
91             See L for more information about the decode call being used for recoding.
92              
93             Returns a deep copy of the original reference meaning that every value and reference
94             will be copied.
95              
96             =cut
97              
98             sub decode {
99 1     1 1 466 my $encoding = shift;
100 1         2 my $ref = shift;
101              
102             return _walk(
103             $ref,
104 1     1   4 sub { return Encode::decode($encoding, shift); },
105             {},
106 1         4 );
107             }
108              
109             ### INTERNAL FUNCTIONS
110              
111             sub _walk {
112 60     60   5071 my $ref = shift;
113 60         71 my ($sub, $ref_map) = @_;
114              
115             # Convert values
116 60 100       164 return &$sub($ref) unless ref($ref);
117              
118             # Handle circular references
119 30 100       85 return $ref_map->{$ref} if $ref_map->{$ref};
120              
121 27 100       57 if (ref($ref) eq 'SCALAR') {
122 4         4 my $new_value; # create new SCALAR
123 4         8 $ref_map->{$ref} = \$new_value; # Add to list of known references
124 4         13 $new_value = _walk($$ref, @_);
125 4         23 return \$new_value;
126             }
127              
128 23 100       43 if (ref($ref) eq 'ARRAY') {
129 8         7 my @new_array; # create new ARRAY
130 8         18 $ref_map->{$ref} = \@new_array; # Add to list of known references
131 8         13 @new_array = map { _walk($_, @_); } @$ref;
  21         52  
132 8         53 return \@new_array;
133             }
134              
135 15 50       35 if (ref($ref) eq 'HASH') {
136 15         14 my %new_hash; # create new HASH
137 15         35 $ref_map->{$ref} = \%new_hash; # Add to list of known references
138             # Convert hash keys directly because hash keys can't contain working references
139 15         35 %new_hash = map { &$sub($_) => _walk($ref->{$_}, @_); } keys %$ref;
  20         47  
140 15         98 return \%new_hash;
141             }
142              
143 0           croak('Unknown refernce '.$ref.' of type '.ref($ref));
144             }
145              
146             =pod
147              
148             =head1 AUTHOR
149              
150             Sebastian Willing, C<< >>
151              
152             =head1 BUGS
153              
154             Please report any bugs or feature requests to C, or through
155             the web interface at L. I will be notified, and then you'll
156             automatically be notified of progress on your bug as I make changes.
157              
158              
159             =head1 SUPPORT
160              
161             You can find documentation for this module with the perldoc command.
162              
163             perldoc Encode::Deep
164              
165              
166             You can also look for information at:
167              
168             =over 4
169              
170             =item * RT: CPAN's request tracker (report bugs here)
171              
172             L
173              
174             =back
175              
176              
177             =head1 ACKNOWLEDGEMENTS
178              
179              
180             =head1 LICENSE AND COPYRIGHT
181              
182             Copyright 2012 Sebastian Willing, eGENTIC Systems L
183              
184             This program is free software; you can redistribute it and/or modify it
185             under the terms of either: the GNU General Public License as published
186             by the Free Software Foundation; or the Artistic License.
187              
188             See http://dev.perl.org/licenses/ for more information.
189              
190             =cut
191              
192             1; # End of Encode::Deep