| 1 | package App::RecordStream::Operation::fromre; | ||||||||||||||||||||||||||||
| 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 | 4 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 4 | my $args = shift; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my %options = ( | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | "key|k|field|f=s" => sub { $this->add_field(split(/,/, $_[1])); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | $this->parse_options($args, \%options); | |||||||||||||||||||||||||||
| 18 | 3 | ▶ | if(!@$args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | die "Missing expression\n"; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | 3 | $this->_set_pattern(shift @$args); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub _set_pattern { | ||||||||||||||||||||||||||||
| 25 | 3 | ▶ | my ($this, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_pattern: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | $this->{'PATTERN'} = $value; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub get_pattern { | ||||||||||||||||||||||||||||
| 30 | 10 | ▶ | my ($this) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_pattern: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | return $this->{'PATTERN'} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$this->{'PATTERN'} || 0
MC/DC0%0/1
$this->{'PATTERN'} || 0
$this->{'PATTERN'}
Truth table50%1/2
A
A= $this->{'PATTERN'}
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub add_field { | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_field: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | $this->{'FIELDS'} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$this->{"FIELDS"} ||= []
MC/DC0%0/1
$this->{"FIELDS"} ||= []
$this->{"FIELDS"}
Truth table50%1/2
A
A= $this->{"FIELDS"}
| |||||||||||||||||||||||||||||
| 37 | 2 | push @{$this->{'FIELDS'}}, @_; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub get_field { | ||||||||||||||||||||||||||||
| 41 | 14 | ▶ | my ($this, $index) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_field: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 14 | ▶ | 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"};}
| |||||||||||||||||||||||||||||
| 44 | 9 | return $this->{'FIELDS'}->[$index]; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||
| 47 | 5 | return $index; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub accept_line { | ||||||||||||||||||||||||||||
| 52 | 10 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
accept_line: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 10 | my $line = shift; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 10 | ▶ | if(my @groups = ($line =~ $this->get_pattern())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 6 | my $record = App::RecordStream::Record->new(); | |||||||||||||||||||||||||||
| 57 | 6 | my $index = 0; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | foreach my $value (@groups) { | |||||||||||||||||||||||||||
| 60 | 14 | ${$record->guess_key_from_spec($this->get_field($index))} = $value; | |||||||||||||||||||||||||||
| 61 | 14 | ++$index; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 6 | $this->push_record($record); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub add_help_types { | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 4 | $this->use_help_type('keyspecs'); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub usage { | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | my $options = [ | |||||||||||||||||||||||||||
| 79 | [ 'key|-k <key>', 'Comma separated list of key names. May be specified multiple times. may be a key spec, see \'man recs\' for more'], | ||||||||||||||||||||||||||||
| 80 | ]; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | my $args_string = $this->options_string($options); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | return <<USAGE; | |||||||||||||||||||||||||||
| 85 | Usage: recs-fromre <args> <re> [<files>] | ||||||||||||||||||||||||||||
| 86 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 87 | <re> is matched against each line of input (or lines of <files>). Each | ||||||||||||||||||||||||||||
| 88 | successfully match results in one output record whose field values are the | ||||||||||||||||||||||||||||
| 89 | capture groups from the match. Lines that do not match are ignored. Keys | ||||||||||||||||||||||||||||
| 90 | are named numerically (0, 1, etc.) or as given by --key. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | For spliting on a delimeter, see recs-fromsplit. | ||||||||||||||||||||||||||||
| 93 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Arguments: | ||||||||||||||||||||||||||||
| 96 | $args_string | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Examples: | ||||||||||||||||||||||||||||
| 99 | Parse greetings | ||||||||||||||||||||||||||||
| 100 | recs-fromre --key name,age '^Hello, my name is (.*) and I am (\\d*) years? old\$' | ||||||||||||||||||||||||||||
| 101 | Parse a single key named time from a group of digits at the beginning of the line | ||||||||||||||||||||||||||||
| 102 | recs-fromre --key time '^(\\d+)' | ||||||||||||||||||||||||||||
| 103 | Map three sets of <>s to a record with keys named 0, 1, and 2 | ||||||||||||||||||||||||||||
| 104 | recs-fromre '<(.*)>\\s*<(.*)>\\s*<(.*)>' | ||||||||||||||||||||||||||||
| 105 | USAGE | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | 1; | |||||||||||||||||||||||||||