File Coverage

blib/lib/Stringify/Deep.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Stringify::Deep;
2              
3 1     1   50470 use strict;
  1         3  
  1         49  
4 1     1   6 use warnings;
  1         1  
  1         47  
5              
6             require Exporter;
7 1     1   6 use base 'Exporter';
  1         6  
  1         466  
8              
9             our @EXPORT = qw();
10             our @EXPORT_OK = qw(deep_stringify);
11              
12 1     1   3303 use Data::Structure::Util qw(get_refs unbless);
  0            
  0            
13             use Scalar::Util qw(blessed);
14              
15             our $VERSION = '0.02';
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             my $struct = shift;
68             my $params = shift || {};
69             my $reftype = ref $struct || '';
70              
71             if ($reftype eq 'ARRAY') {
72             for my $i (0..scalar(@$struct) - 1) {
73             $struct->[$i] = deep_stringify($struct->[$i], $params);
74             }
75             } elsif ($reftype eq 'HASH') {
76             for my $key (keys %$struct) {
77             $struct->{$key} = deep_stringify($struct->{$key}, $params);
78             }
79             } else {
80             if (blessed $struct) {
81             my $overloaded = overload::Method( $struct, q{""} );
82             if (!$overloaded and $params->{leave_unoverloaded_objects_intact}) {
83             unbless $struct;
84             $reftype = ref $struct || '';
85             }
86             }
87              
88             if ($reftype !~ /^(ARRAY|HASH)$/) {
89             $struct = "$struct"
90             if defined $struct;
91             }
92             }
93              
94             return $struct;
95             }
96              
97             =head1 DEPENDENCIES
98              
99             Data::Structure::Util, Scalar::Util
100              
101             =head1 AUTHORS
102              
103             Michael Aquilina
104              
105             =head1 COPYRIGHT AND LICENSE
106              
107             Copyright (C) 2012 Michael Aquilina.
108              
109             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
110              
111             =cut
112              
113             1;
114