File Coverage

blib/lib/Stringify/Deep.pm
Criterion Covered Total %
statement 33 33 100.0
branch 9 10 90.0
condition 13 15 86.6
subroutine 6 6 100.0
pod 1 1 100.0
total 62 65 95.3


line stmt bran cond sub pod time code
1             package Stringify::Deep;
2              
3 1     1   60681 use strict;
  1         3  
  1         25  
4 1     1   4 use warnings;
  1         2  
  1         38  
5              
6             require Exporter;
7 1     1   6 use base 'Exporter';
  1         1  
  1         154  
8              
9             our @EXPORT = qw();
10             our @EXPORT_OK = qw(deep_stringify);
11              
12 1     1   423 use Data::Structure::Util qw(unbless);
  1         2731  
  1         51  
13 1     1   440 use Ref::Util qw(is_blessed_ref);
  1         1341  
  1         277  
14              
15             our $VERSION = '0.03';
16              
17             =head1 NAME
18              
19             Stringify::Deep - Stringifies elements in data structures for easy serialization
20              
21             =head1 SYNOPSIS
22              
23             my $struct = {
24             foo => 1,
25             bar => [ 1, 2, 7, {
26             blah => $some_obj, # Object that's overloaded so it stringifies to "1234"
27             foo => [ 1, 2, 3, 4, 5 ],
28             } ],
29             };
30              
31             $struct = deep_stringify($struct);
32              
33             # $struct is now:
34             # {
35             # foo => 1,
36             # bar => [ 1, 2, 7, {
37             # blah => "1234",
38             # foo => [ 1, 2, 3, 4, 5 ],
39             # } ],
40             # }
41              
42             =head1 DESCRIPTION
43              
44             Let's say that you have a complex data structure that you need to serialize using one of the dozens of tools available on the CPAN, but the structure contains objects, code references, or other things that don't serialize so nicely.
45              
46             Given a data structure, this module will return the same data structure, but with all contained objects/references that aren't ARRAY or HASH references evaluated as a string.
47              
48             =head1 FUNCTIONS
49              
50             =head2 deep_stringify( $struct, $params )
51              
52             Given a data structure, returns the same structure, but with all contained objects/references other than ARRAY and HASH references evaluated as a string.
53              
54             Takes an optional hash reference of parameters:
55              
56             =over 4
57              
58             =item * B
59              
60             If this parameter is passed, Stringify::Deep will unbless and stringify objects that overload stringification, but will leave the data structure intact for objects that don't overload stringification.
61              
62             =back
63              
64             =cut
65              
66             sub deep_stringify {
67 37     37 1 4286 my $struct = shift;
68 37 100       63 return unless defined $struct;
69              
70 36   100     53 my $params = shift || {};
71 36   100     72 my $reftype = ref $struct || '';
72              
73 36 100       50 if ($reftype eq 'HASH') {
74 8         19 for my $key (keys %$struct) {
75 20         52 $struct->{$key} = deep_stringify($struct->{$key}, $params);
76             }
77 8         19 return $struct;
78             }
79              
80 28 100       57 if ($reftype eq 'ARRAY') {
81 7         15 for my $i (0..scalar(@$struct) - 1) {
82 12         20 $struct->[$i] = deep_stringify($struct->[$i], $params);
83             }
84 7         28 return $struct;
85             }
86              
87 21 100 100     62 if (
      66        
      100        
88             $reftype &&
89             $params->{leave_unoverloaded_objects_intact} &&
90             is_blessed_ref($struct) &&
91             ! overload::Method($struct, q{""})
92             ) {
93 1         62 unbless $struct;
94 1   50     12 $reftype = ref $struct || '';
95 1 50       7 if ($reftype =~ /^(ARRAY|HASH)$/) {
96 1         5 return $struct;
97             }
98             }
99              
100 20         422 return "$struct";
101             }
102              
103             =head1 DEPENDENCIES
104              
105             Data::Structure::Util, Scalar::Util
106              
107             =head1 AUTHORS
108              
109             Michael Aquilina
110              
111             Thanks to LARRYL (Larry Leszczynski) for his patch contributing performance improvements.
112              
113             =head1 COPYRIGHT AND LICENSE
114              
115             Copyright (C) 2012-2018 Michael Aquilina.
116              
117             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
118              
119             =cut
120              
121             1;
122              
123