File Coverage

blib/lib/Data/Undump/PPI.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition 5 5 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             #!perl
2             package Data::Undump::PPI;
3 3     3   26340 use warnings;
  3         4  
  3         98  
4 3     3   17 use strict;
  3         4  
  3         113  
5              
6             our $VERSION = '0.04';
7              
8             =head1 Name
9              
10             Data::Undump::PPI - Perl extension for limited undumping of data structures
11             (via PPI, not eval)
12              
13             =head1 Synopsis
14              
15             =for comment
16             Remember to test this by copy/pasting to/from 91_author_pod.t
17              
18             use Data::Dumper;
19             use Data::Undump::PPI; # exports the "Undump()" function
20             $Data::Dumper::Purity=1; # should always be turned on for Undump
21            
22             my @input = ( {foo=>"bar"}, ["Hello","World"], "undumping!" );
23             my $str = Dumper(@input); # dump the data structure to a string
24             my @parsed = Undump($str); # parse the data structure back out
25            
26             # @parsed now looks identical to @input (is a deep copy)
27              
28             =head1 Description
29              
30             This module allows for I undumping and round-tripping of data
31             structures from strings generated by L,
32             with some support for L and possibly others.
33             It is a thin wrapper around L, so please
34             see L for more details, including the limitations.
35              
36             B<< This module exports a single function, C, >> which accepts a
37             string and attempts to return the data as it would have been passed to
38             L's C, or L's C functions.
39             This means that for example, the C<$VAR1> variable names generated by
40             C will be removed and the list passed to C is returned.
41             If the string doesn't look like the output of one of the dumper modules,
42             the output of L's C will be passed through.
43             C will C if it encounters problems.
44              
45             B<< When using L, >> make sure to always turn on its
46             C option and turn off its C option,
47             as otherwise L may produce code that may not evaluate
48             back to the same data structure, sometimes even though it's valid, parseable Perl!
49              
50             This module aims to support most of L's features
51             - except, notably, code references.
52             If you find a L data structure that this module
53             does not yet support, please feel free to send in your data structure, as
54             it can help extend L's features and help fix bugs.
55             Currently, using modules other than L may not work,
56             for example, L sometimes generates code with the C<..>
57             range operator, which is currently not supported by L.
58             In the future, this module's features may be extended to more fully support
59             dumper modules like L as well.
60              
61             Although L now supports self-referential data
62             structures, you can also use L's C
63             option to get rid of references within data structures,
64             if the loss of references and copying of data is acceptable for your application.
65              
66             This module is part of the L distribution,
67             but was named separately in an attempt to make its purpose more clear
68             and its name a little easier to remember.
69              
70             This document describes version 0.04 of the module.
71             Although this module has a fair number of tests, it still lacks some
72             features (see L) and there may be bugs lurking.
73             Contributions are welcome!
74              
75             =head1 Author, Copyright, and License
76              
77             Copyright (c) 2015 Hauke Daempfling (haukex@zero-g.net).
78              
79             This library is free software; you can redistribute it and/or modify
80             it under the same terms as Perl 5 itself.
81              
82             For more information see the L,
83             which should have been distributed with your copy of Perl.
84             Try the command "C" or see
85             L.
86              
87             =cut
88              
89 3     3   9 use Carp;
  3         3  
  3         136  
90 3     3   9 use Exporter 'import';
  3         3  
  3         704  
91              
92             our @EXPORT = qw(Undump); ## no critic (ProhibitAutomaticExportation)
93              
94 3     3   371 use Config::Perl;
  3         4  
  3         512  
95              
96             sub Undump {
97 39     39 0 24597 my ($in) = shift;
98 39 100       207 warnings::warnif('Config::Perl',"ignoring extra arguments to Undump") if @_;
99            
100 39         113 my $parsed = Config::Perl->new->parse_or_die(\$in);
101 39         123 my @keys = keys %$parsed;
102            
103             # does this look like Data::Dumper output?
104 39         7443 my $data_dumper=1;
105 39   100     329 /^\$VAR\d+$/ or $data_dumper=0 for @keys;
106             # if yes, sort the $VAR\d+ variables correctly
107             $data_dumper and return
108 168         281 map { $$parsed{ $$_[0] } }
109 389         296 sort { $$a[1] <=> $$b[1] }
110 39 100       76 map { [$_, /^\$VAR(\d+)$/] }
  168         385  
111             @keys;
112            
113             # is the output a single value?
114             # then it's likely Data::Dump, or Data::Dumper with Terse option
115 8 100 100     30 if (@keys==1 && $keys[0] eq '_') {
116 5         4 return @{ $$parsed{_} };
  5         19  
117             }
118            
119             # none of the above, just pass through output
120 3         7 return $parsed;
121             }
122              
123              
124             1;
125