| 1 | package Stringify::Deep; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT_OK = qw(deep_stringify); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Data::Structure::Util qw(unbless); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Ref::Util qw(is_blessed_ref); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | 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<leave_unoverloaded_objects_intact> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 37 | ▶ | sub deep_stringify { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deep_stringify: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 37 | my $struct = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 37 | ▶ | return unless defined $struct; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 36 | ▶ | my $params = shift || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 36 | ▶ | my $reftype = ref $struct || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
ref $struct || ''
MC/DC100%1/1
ref $struct || ''
ref $struct
Truth table100%2/2
A
A= ref $struct
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 36 | ▶ | if ($reftype eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | for my $key (keys %$struct) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 20 | $struct->{$key} = deep_stringify($struct->{$key}, $params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | return $struct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 28 | ▶ | if ($reftype eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 7 | for my $i (0..scalar(@$struct) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 12 | $struct->[$i] = deep_stringify($struct->[$i], $params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | return $struct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 21 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$reftype and $params->{"leave_unoverloaded_objects_intact"} and is_blessed_ref($struct) and not overload::Method($struct, "\"\"")
$reftype and $params->{"leave_unoverloaded_objects_intact"} and is_blessed_ref($struct)
$reftype and $params->{"leave_unoverloaded_objects_intact"}
MC/DC75%3/4
$reftype and $params->{"leave_unoverloaded_objects_intact"} and is_blessed_ref($struct) and not overload::Method($struct, "\"\"")
$reftype
$params->{"leave_unoverloaded_objects_intact"}
is_blessed_ref($struct)
not overload::Method($struct, "\"\"")
Truth table80%4/5
A and B and C and not(D)
A= $reftypeB= $params->{"leave_unoverloaded_objects_intact"}C= is_blessed_ref($struct)D= not overload::Method($struct, "\"\"")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | $reftype && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | $params->{leave_unoverloaded_objects_intact} && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | is_blessed_ref($struct) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ! overload::Method($struct, q{""}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | unbless $struct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $reftype = ref $struct || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
ref $struct || ''
MC/DC0%0/1
ref $struct || ''
ref $struct
Truth table50%1/2
A
A= ref $struct
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ($reftype =~ /^(ARRAY|HASH)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | return $struct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 20 | return "$struct"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | Data::Structure::Util, Scalar::Util | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | Michael Aquilina <aquilina@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||