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 2     2   31442 use warnings;
  2         4  
  2         63  
4 2     2   8 use strict;
  2         2  
  2         111  
5              
6             our $VERSION = '0.02';
7              
8             =head1 Name
9              
10             Data::Undump::PPI - Perl extension for limited undumping of data structures via PPI
11              
12             =head1 Synopsis
13              
14             =for comment
15             Remember to test this by copy/pasting to/from 91_author_pod.t
16              
17             use Data::Dumper;
18             use Data::Undump::PPI; # exports the "Undump()" function
19            
20             my @input = ( {foo=>"bar"}, ["Hello","World"], "undumping!" );
21             my $str = Dumper(@input); # dump the data structure to a string
22             my @parsed = Undump($str); # parse the data structure back out
23            
24             # @parsed now looks like this:
25             # ( { 'foo' => 'bar' },
26             # [ 'Hello', 'World' ],
27             # 'undumping!' )
28              
29             =head1 Description
30              
31             This module allows for I undumping and round-tripping of data
32             structures from strings generated by modules such as
33             L and L.
34             It is a thin wrapper around L, so please
35             see L for more details, including the limitations.
36              
37             This module exports a single function, C, which attempts to
38             return the data as it would have been passed to
39             L's C or L's C functions.
40             This means that for example, the C<$VAR1> variables generated by C will be stripped.
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             Because at the moment L has only very limited support
46             for references, self-referential data structures will most likely not work
47             (support may be added in a later release of L).
48             For now, a possible workaround may be L's C option,
49             if the loss of references and copying of data is acceptable for your application.
50              
51             If you're using L, note that some of the code it generates
52             is currently unsupported by L, such as the range operator C<..>.
53             Because of this, you may be better off using L for now.
54              
55             If you're using L, note that its C
56             option may cause C to generate invalid Perl strings if you pass it
57             more than one value.
58              
59             This module is part of the L distribution,
60             but was named seperately in an attempt to make its purpose more clear
61             and its name a little easier to remember.
62              
63             This document describes version 0.02 of the module.
64             Although this module is well-tested and working, it still lacks some
65             features to make it I useful (see L).
66             Contributions are welcome!
67              
68             =head1 Author, Copyright, and License
69              
70             Copyright (c) 2015 Hauke Daempfling (haukex@zero-g.net).
71              
72             This library is free software; you can redistribute it and/or modify
73             it under the same terms as Perl 5 itself.
74              
75             For more information see the L,
76             which should have been distributed with your copy of Perl.
77             Try the command "C" or see
78             L.
79              
80             =cut
81              
82 2     2   8 use Carp;
  2         2  
  2         104  
83 2     2   7 use Exporter 'import';
  2         2  
  2         80  
84              
85             our @EXPORT = qw(Undump); ## no critic (ProhibitAutomaticExportation)
86              
87 2     2   399 use Config::Perl;
  2         3  
  2         371  
88              
89             sub Undump {
90 41     41 0 30184 my ($in) = shift;
91 41 100       321 warnings::warnif('Config::Perl',"ignoring extra arguments to Undump") if @_;
92            
93 41         153 my $parsed = Config::Perl->new->parse_or_die(\$in);
94 41         121 my @keys = keys %$parsed;
95            
96             # does this look like Data::Dumper output?
97 41         8401 my $data_dumper=1;
98 41   100     374 /^\$VAR\d+$/ or $data_dumper=0 for @keys;
99             # if yes, sort the $VAR\d+ variables correctly
100 131         269 $data_dumper and return
101 296         295 map { $$parsed{ $$_[0] } }
102 131         362 sort { $$a[1] <=> $$b[1] }
103 41 100       100 map { [$_, /^\$VAR(\d+)$/] }
104             @keys;
105            
106             # is the output a single value?
107             # then it's likely Data::Dump, or Data::Dumper with Terse option
108 8 100 100     38 if (@keys==1 && $keys[0] eq '_') {
109 5         6 return @{ $$parsed{_} };
  5         56  
110             }
111            
112             # none of the above, just pass through output
113 3         8 return $parsed;
114             }
115              
116              
117             1;
118