| 1 | package App::RecordStream::Operation::fromsplit; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = "4.0.23"; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | sub init { | ||||||||||||||||||||||||||||
| 10 | 6 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 6 | my $args = shift; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | my $headers = 0; | |||||||||||||||||||||||||||
| 14 | 6 | my $strict = 0; | |||||||||||||||||||||||||||
| 15 | my %options = ( | ||||||||||||||||||||||||||||
| 16 | 3 | ▶ | "delim|d=s" => sub { $this->_set_delimiter($_[1]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | "key|k|field|f=s" => sub { $this->add_field(split(/,/, $_[1])); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 6 | "header" => \$headers, | |||||||||||||||||||||||||||
| 19 | "strict" => \$strict, | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | $this->parse_options($args, \%options); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | $this->{'HEADER'} = $headers; | |||||||||||||||||||||||||||
| 25 | 5 | $this->{'STRICT'} = $strict; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub _set_delimiter { | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | my ($this, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_delimiter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 3 | $this->{'DELIMITER'} = $value; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub get_delimiter { | ||||||||||||||||||||||||||||
| 34 | 10 | ▶ | my ($this) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_delimiter: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 10 | ▶ | if(!defined($this->{'DELIMITER'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 4 | return ','; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 6 | return $this->{'DELIMITER'}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub add_field { | ||||||||||||||||||||||||||||
| 42 | 3 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_field: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $this->{'FIELDS'} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$this->{"FIELDS"} ||= []
MC/DC100%1/1
$this->{"FIELDS"} ||= []
$this->{"FIELDS"}
Truth table100%2/2
A
A= $this->{"FIELDS"}
| |||||||||||||||||||||||||||||
| 44 | 3 | push @{$this->{'FIELDS'}}, @_; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub get_field { | ||||||||||||||||||||||||||||
| 48 | 26 | ▶ | my ($this, $index) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_field: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 26 | ▶ | if($this->{'FIELDS'} && $index < @{$this->{'FIELDS'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$this->{"FIELDS"} and $index < @{$this->{"FIELDS"};}
MC/DC100%2/2
$this->{"FIELDS"} and $index < @{$this->{"FIELDS"};}
$this->{"FIELDS"}
$index < @{$this->{"FIELDS"};}
Truth table100%3/3
A and B
A= $this->{"FIELDS"}B= $index < @{$this->{"FIELDS"};}
| |||||||||||||||||||||||||||||
| 51 | 4 | return $this->{'FIELDS'}->[$index]; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||||||||
| 54 | 22 | return $index; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub accept_line { | ||||||||||||||||||||||||||||
| 59 | 10 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
accept_line: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 10 | my $line = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ($this->{'HEADER'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | $this->add_field($_) for @{$this->get_values_for_line($line)}; | |||||||||||||||||||||||||||
| 64 | 1 | delete $this->{'HEADER'}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | 9 | my $record = App::RecordStream::Record->new(); | |||||||||||||||||||||||||||
| 68 | 9 | my $index = 0; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 9 | foreach my $value (@{$this->get_values_for_line($line)}) { | |||||||||||||||||||||||||||
| 71 | 26 | ${$record->guess_key_from_spec($this->get_field($index))} = $value; | |||||||||||||||||||||||||||
| 72 | 26 | ++$index; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 9 | $this->push_record($record); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 10 | return 1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub get_values_for_line { | ||||||||||||||||||||||||||||
| 82 | 10 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_values_for_line: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 10 | my $line = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 10 | my @values; | |||||||||||||||||||||||||||
| 86 | 10 | my $delim = $this->get_delimiter(); | |||||||||||||||||||||||||||
| 87 | 10 | ▶ | if ( $this->{'STRICT'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | @values = split(/\Q$delim\E/, $line, -1); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 8 | @values = split(/$delim/, $line, -1); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 10 | return \@values; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub add_help_types { | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 6 | $this->use_help_type('keyspecs'); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub usage { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | my $options = [ | |||||||||||||||||||||||||||
| 106 | [ 'delim|-d <delim>', "Delimiter to use for splitting input lines (default ',')."], | ||||||||||||||||||||||||||||
| 107 | [ 'key|-k <key>', '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 | 1 | my $args_string = $this->options_string($options); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | return <<USAGE; | |||||||||||||||||||||||||||
| 115 | Usage: recs-fromsplit <args> [<files>] | ||||||||||||||||||||||||||||
| 116 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 117 | Each line of input (or lines of <files>) 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 | 2 | 1; | |||||||||||||||||||||||||||