line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LEOCHARRE::DataConverter; |
2
|
2
|
|
|
2
|
|
159707
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
73
|
|
3
|
2
|
|
|
2
|
|
11
|
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
124
|
|
4
|
2
|
|
|
2
|
|
10
|
use Exporter; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
70
|
|
5
|
2
|
|
|
2
|
|
11
|
use Smart::Comments '###'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
5267
|
use YAML; |
|
2
|
|
|
|
|
15206
|
|
|
2
|
|
|
|
|
127
|
|
10
|
2
|
|
|
2
|
|
17
|
use Data::Dumper; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
97
|
|
11
|
2
|
|
|
2
|
|
1652
|
use Text::CSV::Slurp; |
|
2
|
|
|
|
|
35264
|
|
|
2
|
|
|
|
|
73
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
17
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
2129
|
|
14
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)/g; |
15
|
|
|
|
|
|
|
@ISA = qw/Exporter/; |
16
|
|
|
|
|
|
|
@EXPORT_OK = qw(data_type data2yaml data2csv data2dumper dumper2data yaml2data csv2data string2data); |
17
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub data_type { |
21
|
11
|
|
|
11
|
1
|
82340
|
my $string = $_[0]; |
22
|
|
|
|
|
|
|
|
23
|
11
|
100
|
|
|
|
283
|
if ($string=~/^---\n/){ |
24
|
3
|
|
|
|
|
50
|
return 'yaml'; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
8
|
100
|
|
|
|
131
|
if ($string=~/^\$VAR\d = /){ |
28
|
3
|
|
|
|
|
27
|
return 'dumper'; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# is it a csv |
32
|
5
|
|
|
|
|
41
|
my $is_csv; |
33
|
5
|
50
|
|
|
|
117
|
if( my @lines = split( /\n/,$string) ){ |
34
|
5
|
|
|
|
|
32
|
my $delimited_lines; |
35
|
|
|
|
|
|
|
my $not_delimited_lines; |
36
|
5
|
|
|
|
|
34
|
my $delimiter =','; |
37
|
5
|
|
|
|
|
45
|
for (@lines){ |
38
|
28
|
50
|
|
|
|
97
|
if ($_=~/^#/){ next } |
|
0
|
|
|
|
|
0
|
|
39
|
|
|
|
|
|
|
|
40
|
28
|
100
|
|
|
|
213
|
if ($_=~/\w/){ |
41
|
25
|
100
|
|
|
|
202
|
if ($_=~/\Q$delimiter\E/){ |
42
|
17
|
|
|
|
|
49
|
$delimited_lines++; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
else { |
45
|
8
|
|
|
|
|
48
|
$not_delimited_lines++; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
5
|
100
|
66
|
|
|
83
|
if ($delimited_lines and !$not_delimited_lines){ |
50
|
3
|
|
|
|
|
43
|
return 'csv'; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
2
|
|
|
|
|
23
|
return; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# figure it out ! :-) |
60
|
|
|
|
|
|
|
sub string2data { |
61
|
3
|
|
|
3
|
1
|
5751
|
my $string = $_[0]; |
62
|
3
|
50
|
|
|
|
28
|
ref $string and die('data must be stringified, not ref'); |
63
|
|
|
|
|
|
|
|
64
|
3
|
50
|
|
|
|
17
|
if ( my $type = data_type($string) ){ |
65
|
3
|
|
|
|
|
22
|
### type is: $type |
|
3
|
|
|
|
|
737
|
|
66
|
3
|
100
|
|
|
|
23
|
($type eq 'csv') |
67
|
|
|
|
|
|
|
and return csv2data($string); |
68
|
2
|
100
|
|
|
|
105
|
($type eq 'yaml') |
69
|
|
|
|
|
|
|
and return yaml2data($string); |
70
|
1
|
50
|
|
|
|
10
|
($type eq 'dumper') |
71
|
|
|
|
|
|
|
and return dumper2data($string); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
### dunno what to do with : $type |
|
0
|
|
|
|
|
0
|
|
74
|
0
|
|
|
|
|
0
|
return; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _allkeys { # get all keys present in all hashes in the array ref |
86
|
2
|
|
|
2
|
|
6
|
my $aref_of_hashes=$_[0]; |
87
|
2
|
|
|
|
|
4
|
my %keys; |
88
|
2
|
|
|
|
|
11
|
for my $href (@$aref_of_hashes){ |
89
|
8
|
|
|
|
|
18
|
map { $keys{$_}++ } keys %$href; |
|
14
|
|
|
|
|
31
|
|
90
|
|
|
|
|
|
|
} |
91
|
2
|
|
|
|
|
18
|
sort keys %keys; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub data2csv { |
96
|
2
|
|
|
2
|
1
|
1884
|
my $ref = $_[0]; |
97
|
2
|
50
|
|
|
|
22
|
ref $ref or die; |
98
|
|
|
|
|
|
|
#my $opts = $_[1]; |
99
|
|
|
|
|
|
|
# |
100
|
2
|
|
|
|
|
10
|
my @fields = _allkeys($ref); |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
47
|
my $string = Text::CSV::Slurp->create( input => $ref, field_order => [@fields] ); |
103
|
2
|
|
|
|
|
1390
|
return $string; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub data2yaml { |
107
|
2
|
|
|
2
|
1
|
1042
|
my $ref = $_[0]; |
108
|
2
|
50
|
|
|
|
21
|
ref $ref or die; |
109
|
2
|
|
|
|
|
40
|
my $string = YAML::Dump($ref); |
110
|
2
|
|
|
|
|
54672
|
return $string; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub data2dumper { |
114
|
2
|
|
|
2
|
1
|
15159
|
my $ref = $_[0]; |
115
|
2
|
50
|
|
|
|
44
|
ref $ref or die; |
116
|
|
|
|
|
|
|
|
117
|
2
|
|
|
|
|
45
|
my $string = Data::Dumper::Dumper($ref); |
118
|
|
|
|
|
|
|
#my $string = Data::Dumper->Dump($ref); |
119
|
2
|
|
|
|
|
630
|
return $string; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub csv2data { |
123
|
1
|
|
|
1
|
1
|
9
|
my $string = $_[0]; |
124
|
1
|
50
|
|
|
|
38
|
ref $string and die("Cannot be ref, must be stringified"); |
125
|
1
|
|
|
|
|
35
|
my $data = Text::CSV::Slurp->load( string => $string ); |
126
|
1
|
|
|
|
|
1684
|
return $data; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub dumper2data { # TODO use heuristcs to see if it's a series of vars or just one |
130
|
1
|
|
|
1
|
1
|
3
|
my $string = $_[0]; |
131
|
1
|
50
|
|
|
|
7
|
ref $string and die("Cannot be ref, must be stringified"); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# may be $VAR1, $VAR2 etc or just $VAR1 |
134
|
1
|
|
|
|
|
2
|
my $VAR1; |
135
|
|
|
|
|
|
|
my $VAR2; |
136
|
1
|
|
|
|
|
168
|
eval $string; |
137
|
1
|
50
|
|
|
|
10
|
defined $VAR2 and die("will not parse more than one var in dumper"); |
138
|
1
|
|
|
|
|
4
|
return $VAR1; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub yaml2data { |
142
|
1
|
|
|
1
|
1
|
9
|
my $string = $_[0]; |
143
|
1
|
50
|
|
|
|
13
|
ref $string and die("Cannot be ref, must be stringified"); |
144
|
1
|
|
|
|
|
20
|
my @data = YAML::Load($string); |
145
|
1
|
|
|
|
|
24117
|
[@data]; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
__END__ |