line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
1741
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
2
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
3
|
|
|
|
|
|
|
package Config::Any::CSV; |
4
|
|
|
|
|
|
|
#ABSTRACT: Load CSV as config files |
5
|
|
|
|
|
|
|
our $VERSION = '0.05'; #VERSION |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
13
|
use v5.10; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
8
|
1
|
|
|
1
|
|
6
|
use base 'Config::Any::Base'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
948
|
|
9
|
1
|
|
|
1
|
|
1150
|
use Text::CSV; |
|
1
|
|
|
|
|
12181
|
|
|
1
|
|
|
|
|
6
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub load { |
12
|
2
|
|
|
2
|
0
|
535
|
my ($class, $file, $driver) = @_; |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
|
|
4
|
my $with_key = 0; |
15
|
2
|
|
|
|
|
14
|
my $args = { binary => 1, allow_whitespace => 1 }; |
16
|
2
|
50
|
|
|
|
14
|
if ($driver) { |
17
|
2
|
|
|
|
|
7
|
$with_key = delete $driver->{with_key}; |
18
|
2
|
|
|
|
|
16
|
$args->{$_} = $driver->{$_} for keys %$driver; |
19
|
|
|
|
|
|
|
} |
20
|
2
|
|
|
|
|
17
|
my $csv = Text::CSV->new( $args ); |
21
|
2
|
|
|
|
|
228
|
my $config = { }; |
22
|
2
|
50
|
|
|
|
89
|
open my $fh, "<", $file or die $!; |
23
|
|
|
|
|
|
|
|
24
|
2
|
100
|
|
|
|
10
|
my $default = $args->{empty_is_undef} ? undef : ""; |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
|
|
12
|
my $names = $csv->getline($fh); |
27
|
2
|
50
|
|
|
|
10256
|
if ( $names ) { |
28
|
2
|
|
|
|
|
8
|
my $columns = scalar @$names - 1; |
29
|
2
|
|
|
|
|
9
|
while ( my $row = $csv->getline( $fh ) ) { |
30
|
4
|
50
|
33
|
|
|
1174
|
next if @$row == 1 and $row->[0] eq ''; # empty line |
31
|
4
|
|
50
|
|
|
14
|
my $id = $row->[0] // ""; |
32
|
12
|
|
50
|
|
|
79
|
$config->{ $id } = { |
|
|
|
66
|
|
|
|
|
33
|
4
|
|
|
|
|
10
|
map { ( $names->[$_] // "" ) => ( $row->[$_] // $default ) } |
34
|
|
|
|
|
|
|
(1..$columns) |
35
|
|
|
|
|
|
|
}; |
36
|
4
|
100
|
50
|
|
|
5951
|
$config->{ $id }->{ $names->[0] // "" } = $id if $with_key; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
2
|
50
|
|
|
|
191
|
die $csv->error_diag() unless $csv->eof; |
40
|
2
|
|
|
|
|
60
|
close $fh; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
52
|
return $config; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub extensions { |
46
|
3
|
|
|
3
|
0
|
34651
|
return ('csv'); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
__END__ |