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