File Coverage

blib/lib/App/RecordStream/Operation/fromsplit.pm
Criterion Covered Total %
statement 56 60 93.3
branch 8 8 100.0
condition 5 5 100.0
subroutine 12 13 92.3
pod 0 8 0.0
total 81 94 86.1


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromsplit;
2              
3             our $VERSION = "4.0.23";
4              
5 2     2   929 use strict;
  2         4  
  2         53  
6              
7 2     2   9 use base qw(App::RecordStream::Operation);
  2         4  
  2         1093  
8              
9             sub init {
10 5     5 0 10 my $this = shift;
11 5         10 my $args = shift;
12              
13 5         9 my $headers = 0;
14 5         10 my $strict = 0;
15             my %options = (
16 3     3   1679 "delim|d=s" => sub { $this->_set_delimiter($_[1]); },
17 1     1   1218 "key|k|field|f=s" => sub { $this->add_field(split(/,/, $_[1])); },
18 5         43 "header" => \$headers,
19             "strict" => \$strict,
20             );
21              
22 5         32 $this->parse_options($args, \%options);
23              
24 5         17 $this->{'HEADER'} = $headers;
25 5         48 $this->{'STRICT'} = $strict;
26             }
27              
28             sub _set_delimiter {
29 3     3   9 my ($this, $value) = @_;
30 3         13 $this->{'DELIMITER'} = $value;
31             }
32              
33             sub get_delimiter {
34 10     10 0 18 my ($this) = @_;
35 10 100       30 if(!defined($this->{'DELIMITER'})) {
36 4         11 return ',';
37             }
38 6         42 return $this->{'DELIMITER'};
39             }
40              
41             sub add_field {
42 3     3 0 7 my $this = shift;
43 3   100     19 $this->{'FIELDS'} ||= [];
44 3         7 push @{$this->{'FIELDS'}}, @_;
  3         13  
45             }
46              
47             sub get_field {
48 26     26 0 52 my ($this, $index) = @_;
49              
50 26 100 100     79 if($this->{'FIELDS'} && $index < @{$this->{'FIELDS'}}) {
  9         36  
51 4         19 return $this->{'FIELDS'}->[$index];
52             }
53             else {
54 22         66 return $index;
55             }
56             }
57              
58             sub accept_line {
59 10     10 0 17 my $this = shift;
60 10         20 my $line = shift;
61              
62 10 100       27 if ($this->{'HEADER'}) {
63 1         3 $this->add_field($_) for @{$this->get_values_for_line($line)};
  1         4  
64 1         4 delete $this->{'HEADER'};
65             }
66             else {
67 9         51 my $record = App::RecordStream::Record->new();
68 9         20 my $index = 0;
69              
70 9         13 foreach my $value (@{$this->get_values_for_line($line)}) {
  9         28  
71 26         45 ${$record->guess_key_from_spec($this->get_field($index))} = $value;
  26         58  
72 26         56 ++$index;
73             }
74              
75 9         36 $this->push_record($record);
76             }
77              
78 10         85 return 1;
79             }
80              
81             sub get_values_for_line {
82 10     10 0 16 my $this = shift;
83 10         19 my $line = shift;
84              
85 10         17 my @values;
86 10         82 my $delim = $this->get_delimiter();
87 10 100       25 if ( $this->{'STRICT'} ) {
88 2         16 @values = split(/\Q$delim\E/, $line, -1);
89             }
90             else {
91 8         65 @values = split(/$delim/, $line, -1);
92             }
93              
94 10         35 return \@values;
95             }
96              
97             sub add_help_types {
98 5     5 0 9 my $this = shift;
99 5         22 $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;