blib/lib/Data/Dumper/UnDumper.pm

filter: Statementstmt 89.7% 35 / 39 Branchbran 75.0% 9 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 85.4% 53 / 62 CC 10 SCAR 19.1
CC
10
Cov
85%
_recurse_resolve
9.7
undumper
2.0
BEGIN
1.0
CRAP 6.7
↑ Summary
1 package Data::Dumper::UnDumper;
2 2 $Data::Dumper::UnDumper::VERSION = '0.02';
3 # ABSTRACT: load Data::Dumper output, including self-references
4  
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Data::Dumper::UnDumper - load Dumper output including $VAR1 refs
12  
13  
14 =head1 SYNOPSIS
15  
16 Load in a L<Data::Dumper> output via eval, including supporting C<$VAR1>
17 style references etc as emitted if you don't set the C<Purity> option:
18  
19 use Data::Dumper::UnDumper;
20
21 my $complex_ref = { ... };
22 my $dumped = Data::Dumper::Dumper($complex_ref);
23  
24 my $undumped = Data::Dumper::UnDumper::undumper($dumped);
25  
26 =head1 DESCRIPTION
27  
28 Firstly, a safety warning: loading L<Data::Dumper> output, which is designed
29 to be C<eval>ed, is a big safety risk if the data comes from an untrusted
30 source. It's evaled as Perl code, so it can do anything you could write a
31 Perl program to. Future versions of this module may use L<Safe> to mitigate
32 that risk somewhat, but it's still there - to support object references,
33 C<bless> would have to be allowed.
34  
35 So, given the choice, what should you use instead? Any of the many serialisation
36 options that don't serialise as code - for e.g. JSON, YAML, etc.
37  
38 I wrote this module, though, because I didn't have a choice - I was receiving
39 L<Data::Dumper> output which had been written to a log in the past by some code,
40 without using the C<<$Data::Dumper::PURITY>> setting, so it included C<$VAR1>
41 references, including re-used L<JSON::PP> objects.
42  
43 This has been lightly tested with the default output from C<Data::Dumper::Dump()>.
44 It's quite likely that you could have L<Data::Dumper> generate output this will
45 not handle by setting some of the dumping options.
46  
47 =head1 SUBROUTINES
48  
49 =head2 undumper
50  
51 Given the output of L<Data::Dumper>'s C<Dumper> / C<Dump> method, "undump"
52 it, deserialising it back in to a Perl scalar/object, handling `$VAR1`
53 references.
54  
55 =cut
56  
57 1 ▶sub undumper {
Subroutine undumper: called CC=2 SCAR=7.0
Pod documented
58 1 my $dumper_in = shift;
59 # First, remove the leading $VAR1 assignment, we're going to assign to
60 # our own var.
61 1 $dumper_in =~ s{^\$VAR1 = }{};
62  
63 # Next, for all the VAR1 refs, turn them into a string we can eval later
64 # They'll turn into a quoted form of e.g. "DUMPERREF:$_->{'foo'}" or whatever
65 1 $dumper_in =~ s{\$VAR1->(.+)(,|$)}{
66 3 my $cap = $1;
67 3 my $end = $2;
68 3 $cap =~ s/\{/\\{/g;
69 3 $cap =~ s/\}/\\}/g;
70 3 "q{DUMPERREF:\$obj->$cap}".$end
71 }xge;
72  
73 # Right, now we can eval it (FIXME: do this as safely as an eval can be done,
74 # e.g. using Safe)
75 1 my $obj = eval $dumper_in;
76  
77 # Firstly, if the Data::Dumper-ed thing was just e.g. a plain scalar, we
78 # have no more work to do
79 1 ▶ if (!ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $obj) 0 1 1
80 0 return $obj;
81 }
82  
83 # Start recursing (passing the ref as both args, this first call will
84 # then start walking and recursing
85 1 _recurse_resolve($obj, $obj);
86  
87 1 return $obj;
88  
89 }
90  
91 # Given a reference to the object we undumpered walk through its values
92 # (array / hash values), recursing whenever another level is encountered.
93 11 ▶sub _recurse_resolve {
Subroutine _recurse_resolve: called CC=9 SCAR=22.7
94 11 my ($value, $obj, $depth) = @_;
95  
96 11 ▶ if ($depth++ > 50) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth++ > 50) 0 11 11
97 0 die "Too many levels of recursion resolving this dumper input "
98 . " - stopping at depth $depth on value $value";
99 }
100  
101 11 ▶ if (ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 11 11
elsif (ref $value eq 'HASH') { } 4 7 11
102 0 for (@$value) { _recurse_resolve($_, $obj, $depth); }
103 } elsif (ref $value eq 'HASH') {
104 4 for (values %$value) { _recurse_resolve($_, $obj, $depth); }
105 } else {
106 # A plain value, resolve it if it's a DUMPERREF
107 7 ▶ if ($value && $value =~ /^DUMPERREF:(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value =~ /^DUMPERREF:(.+)$/) 4 3 7
Condition66%2/3
$value and $value =~ /^DUMPERREF:(.+)$/
!ll&&!rl&&r
034
MC/DC50%1/2
$value and $value =~ /^DUMPERREF:(.+)$/
$value $value =~ /^DUMPERREF:(.+)$/
Truth table66%2/3
A and B
A= $valueB= $value =~ /^DUMPERREF:(.+)$/
ABresult
0X0
100
111
108 # We need to unescape the escaped braces first, then what we're
109 # left with should be safe to eval (FIXME prob use Safe here?)
110 4 my $ref = $1;
111 4 $ref =~ s/\\\{/\{/g;
112 4 $ref =~ s/\\\}/\}/g;
113 4 $value = eval $ref;
114  
115 # If the value we get is a token, then this was a ref to another
116 # ref, and we need to resolve that too
117 4 ▶ if ($value =~ /^DUMPERREF:(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^DUMPERREF:(.+)$/) 1 3 4
118 1 _recurse_resolve($value, $obj, $depth);
119 }
120 4 $_[0] = $value;
121 }
122 }
123 }
124  
125  
126 =head1 SEE ALSO
127  
128 =over
129  
130 =item L<Data::Undump>
131  
132 Doesn't support cyclical references, blessed objects.
133  
134 =item L<Data::Undump::PPI>
135  
136 Safer as it uses PPI not C<eval>, but doesn't support blessed objects
137 or refs.
138  
139 =item plain old eval
140  
141 For simple Data::Dumper output you can of course just C<eval> it, but that
142 falls down when the output includes references to other parts of the object
143 e.g. C<< 'foo' => $VAR1->{'bar'} >>
144  
145 =back
146  
147  
148 =head1 AUTHOR
149  
150 David Precious (BIGPRESH), C<< <davidp@preshweb.co.uk> >>
151  
152 =head1 COPYRIGHT AND LICENCE
153  
154 Copyright (C) 2023-2024 by David Precious
155  
156 This library is free software; you can redistribute it and/or modify
157 it under the same terms as Perl itself.
158  
159 =head1 ACKNOWLEDGEMENTS
160  
161 =cut
162  
163  
164 2 1; # End of Data::Dumper::UnDumper
↑ Summary