line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::RecordStream::Operation::fromsplit; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = "4.0.24"; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
826
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
63
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
14
|
use base qw(App::RecordStream::Operation); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1219
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub init { |
10
|
5
|
|
|
5
|
0
|
13
|
my $this = shift; |
11
|
5
|
|
|
|
|
12
|
my $args = shift; |
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
|
|
13
|
my $headers = 0; |
14
|
5
|
|
|
|
|
13
|
my $strict = 0; |
15
|
|
|
|
|
|
|
my %options = ( |
16
|
3
|
|
|
3
|
|
1679
|
"delim|d=s" => sub { $this->_set_delimiter($_[1]); }, |
17
|
1
|
|
|
1
|
|
1256
|
"key|k|field|f=s" => sub { $this->add_field(split(/,/, $_[1])); }, |
18
|
5
|
|
|
|
|
67
|
"header" => \$headers, |
19
|
|
|
|
|
|
|
"strict" => \$strict, |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
5
|
|
|
|
|
42
|
$this->parse_options($args, \%options); |
23
|
|
|
|
|
|
|
|
24
|
5
|
|
|
|
|
21
|
$this->{'HEADER'} = $headers; |
25
|
5
|
|
|
|
|
73
|
$this->{'STRICT'} = $strict; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _set_delimiter { |
29
|
3
|
|
|
3
|
|
9
|
my ($this, $value) = @_; |
30
|
3
|
|
|
|
|
12
|
$this->{'DELIMITER'} = $value; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub get_delimiter { |
34
|
10
|
|
|
10
|
0
|
30
|
my ($this) = @_; |
35
|
10
|
100
|
|
|
|
37
|
if(!defined($this->{'DELIMITER'})) { |
36
|
4
|
|
|
|
|
17
|
return ','; |
37
|
|
|
|
|
|
|
} |
38
|
6
|
|
|
|
|
44
|
return $this->{'DELIMITER'}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub add_field { |
42
|
3
|
|
|
3
|
0
|
11
|
my $this = shift; |
43
|
3
|
|
100
|
|
|
27
|
$this->{'FIELDS'} ||= []; |
44
|
3
|
|
|
|
|
10
|
push @{$this->{'FIELDS'}}, @_; |
|
3
|
|
|
|
|
18
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get_field { |
48
|
26
|
|
|
26
|
0
|
63
|
my ($this, $index) = @_; |
49
|
|
|
|
|
|
|
|
50
|
26
|
100
|
100
|
|
|
227
|
if($this->{'FIELDS'} && $index < @{$this->{'FIELDS'}}) { |
|
9
|
|
|
|
|
49
|
|
51
|
4
|
|
|
|
|
33
|
return $this->{'FIELDS'}->[$index]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
22
|
|
|
|
|
80
|
return $index; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub accept_line { |
59
|
10
|
|
|
10
|
0
|
28
|
my $this = shift; |
60
|
10
|
|
|
|
|
25
|
my $line = shift; |
61
|
|
|
|
|
|
|
|
62
|
10
|
100
|
|
|
|
36
|
if ($this->{'HEADER'}) { |
63
|
1
|
|
|
|
|
4
|
$this->add_field($_) for @{$this->get_values_for_line($line)}; |
|
1
|
|
|
|
|
7
|
|
64
|
1
|
|
|
|
|
5
|
delete $this->{'HEADER'}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
else { |
67
|
9
|
|
|
|
|
80
|
my $record = App::RecordStream::Record->new(); |
68
|
9
|
|
|
|
|
20
|
my $index = 0; |
69
|
|
|
|
|
|
|
|
70
|
9
|
|
|
|
|
20
|
foreach my $value (@{$this->get_values_for_line($line)}) { |
|
9
|
|
|
|
|
32
|
|
71
|
26
|
|
|
|
|
52
|
${$record->guess_key_from_spec($this->get_field($index))} = $value; |
|
26
|
|
|
|
|
75
|
|
72
|
26
|
|
|
|
|
67
|
++$index; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
9
|
|
|
|
|
56
|
$this->push_record($record); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
10
|
|
|
|
|
122
|
return 1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub get_values_for_line { |
82
|
10
|
|
|
10
|
0
|
22
|
my $this = shift; |
83
|
10
|
|
|
|
|
28
|
my $line = shift; |
84
|
|
|
|
|
|
|
|
85
|
10
|
|
|
|
|
19
|
my @values; |
86
|
10
|
|
|
|
|
93
|
my $delim = $this->get_delimiter(); |
87
|
10
|
100
|
|
|
|
34
|
if ( $this->{'STRICT'} ) { |
88
|
2
|
|
|
|
|
16
|
@values = split(/\Q$delim\E/, $line, -1); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else { |
91
|
8
|
|
|
|
|
89
|
@values = split(/$delim/, $line, -1); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
10
|
|
|
|
|
47
|
return \@values; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub add_help_types { |
98
|
5
|
|
|
5
|
0
|
15
|
my $this = shift; |
99
|
5
|
|
|
|
|
29
|
$this->use_help_type('keyspecs'); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub usage { |
103
|
0
|
|
|
0
|
0
|
|
my $this = shift; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $options = [ |
106
|
|
|
|
|
|
|
[ 'delim|-d ', "Delimiter to use for splitting input lines (default ',')."], |
107
|
|
|
|
|
|
|
[ 'key|-k ', 'Comma separated list of key names. May be specified multiple times, may be key specs'], |
108
|
|
|
|
|
|
|
[ 'header', 'Take key names from the first line of input.'], |
109
|
|
|
|
|
|
|
[ 'strict', 'Delimiter is not treated as a regex'], |
110
|
|
|
|
|
|
|
]; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $args_string = $this->options_string($options); |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
return <
|
115
|
|
|
|
|
|
|
Usage: recs-fromsplit [] |
116
|
|
|
|
|
|
|
__FORMAT_TEXT__ |
117
|
|
|
|
|
|
|
Each line of input (or lines of ) is split on provided delimiter to |
118
|
|
|
|
|
|
|
produce an output record. Keys are named numerically (0, 1, etc.) or as |
119
|
|
|
|
|
|
|
given by --key. |
120
|
|
|
|
|
|
|
__FORMAT_TEXT__ |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Arguments: |
123
|
|
|
|
|
|
|
$args_string |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Examples: |
126
|
|
|
|
|
|
|
Parse space separated keys x and y. |
127
|
|
|
|
|
|
|
recs-fromsplit --key x,y --delim ' ' |
128
|
|
|
|
|
|
|
Parse comma separated keys a, b, and c. |
129
|
|
|
|
|
|
|
recs-fromsplit --key a,b,c |
130
|
|
|
|
|
|
|
USAGE |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |