« blib/lib/App/RecordStream/Operation/join.pm
↑ Summary
blib/lib/App/RecordStream/Operation/xform.pm »
| 1 | package App::RecordStream::Operation::grep; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = "4.0.23"; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use App::RecordStream::Executor::Getopt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use App::RecordStream::Executor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub init { | ||||||||||||||||||||||||||||
| 13 | 6 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 6 | my $args = shift; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | my $anti_match; | |||||||||||||||||||||||||||
| 17 | 6 | my $context = 0; | |||||||||||||||||||||||||||
| 18 | 6 | my $after = 0; | |||||||||||||||||||||||||||
| 19 | 6 | my $before = 0; | |||||||||||||||||||||||||||
| 20 | 6 | my $executor_options = App::RecordStream::Executor::Getopt->new(); | |||||||||||||||||||||||||||
| 21 | 6 | my $spec = { | |||||||||||||||||||||||||||
| 22 | "-v" => \$anti_match, | ||||||||||||||||||||||||||||
| 23 | "C=s" => \$context, | ||||||||||||||||||||||||||||
| 24 | "A=s" => \$after, | ||||||||||||||||||||||||||||
| 25 | "B=s" => \$before, | ||||||||||||||||||||||||||||
| 26 | $executor_options->arguments(), | ||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 6 | $this->parse_options($args, $spec, ['bundling']); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | my $expression = $executor_options->get_string($args); | |||||||||||||||||||||||||||
| 32 | 5 | my $executor = App::RecordStream::Executor->new($expression); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | $this->{'ANTI_MATCH'} = $anti_match; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | if ( $context ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | $after = $before = $context; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 5 | $this->{'AFTER'} = $after; | |||||||||||||||||||||||||||
| 41 | 5 | $this->{'BEFORE'} = $before; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | $this->{'ACCUMULATOR'} = []; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | $this->{'EXECUTOR'} = $executor; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub accept_record { | ||||||||||||||||||||||||||||
| 49 | 25 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
accept_record: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 25 | my $record = shift; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 25 | my $executor = $this->{'EXECUTOR'}; | |||||||||||||||||||||||||||
| 53 | 25 | my $result = $executor->execute_code($record); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 25 | ▶ | $result = not $result if ( $this->{'ANTI_MATCH'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 25 | my $pushed_record = 0; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 25 | ▶ | if ( $result ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 8 | ▶ | if ( $this->{'BEFORE'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | while(my $record = shift @{$this->{'ACCUMULATOR'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | $this->push_record($record); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | $this->push_record($record); | |||||||||||||||||||||||||||
| 66 | 8 | $pushed_record = 1; | |||||||||||||||||||||||||||
| 67 | 8 | $this->{'SEEN_RECORD'} = 1; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 8 | ▶ | if ( $this->{AFTER} > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | $this->{'FORCED_OUTPUT'} = $this->{'AFTER'}; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | elsif ( $this->{'BEFORE'} > 0 ) { | ||||||||||||||||||||||||||||
| 74 | 8 | push @{$this->{'ACCUMULATOR'}}, $record; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 8 | ▶ | if ( (scalar @{$this->{'ACCUMULATOR'}}) > $this->{'BEFORE'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 4 | shift @{$this->{'ACCUMULATOR'}}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 25 | ▶ | if ( $this->{'FORCED_OUTPUT'} && (! $pushed_record) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$this->{"FORCED_OUTPUT"} and not $pushed_record
MC/DC100%2/2
$this->{"FORCED_OUTPUT"} and not $pushed_record
$this->{"FORCED_OUTPUT"}
not $pushed_record
Truth table100%3/3
A and not(B)
A= $this->{"FORCED_OUTPUT"}B= not $pushed_record
| |||||||||||||||||||||||||||||
| 82 | 2 | $this->push_record($record); | |||||||||||||||||||||||||||
| 83 | 2 | $this->{'FORCED_OUTPUT'}--; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 25 | return 1; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub stream_done { | ||||||||||||||||||||||||||||
| 90 | 5 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
stream_done: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 5 | ▶ | $this->_set_exit_value(1) unless ( $this->{'SEEN_RECORD'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub add_help_types { | ||||||||||||||||||||||||||||
| 95 | 6 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 6 | $this->use_help_type('keyspecs'); | |||||||||||||||||||||||||||
| 97 | 6 | $this->use_help_type('snippet'); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub usage { | ||||||||||||||||||||||||||||
| 101 | 1 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my $options = [ | |||||||||||||||||||||||||||
| 104 | App::RecordStream::Executor::options_help(), | ||||||||||||||||||||||||||||
| 105 | ['v', 'Anti-match. Records NOT matching <expr> will be returned'], | ||||||||||||||||||||||||||||
| 106 | ['C NUM', 'Provide NUM records of context around matches, equivalent to -A NUM and -B NUM'], | ||||||||||||||||||||||||||||
| 107 | ['A NUM', 'Print out NUM following records after a match'], | ||||||||||||||||||||||||||||
| 108 | ['B NUM', 'Print out the previous NUM records on a match'], | ||||||||||||||||||||||||||||
| 109 | ]; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | my $args_string = $this->options_string($options); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | return <<USAGE; | |||||||||||||||||||||||||||
| 114 | Usage: recs-grep <args> <expr> [<files>] | ||||||||||||||||||||||||||||
| 115 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 116 | <expr> is evaluated as perl on each record of input (or records from | ||||||||||||||||||||||||||||
| 117 | <files>) with \$r set to a App::RecordStream::Record object and \$line set to the current | ||||||||||||||||||||||||||||
| 118 | line number (starting at 1). Records for which the evaluation is a perl | ||||||||||||||||||||||||||||
| 119 | true are printed back out. | ||||||||||||||||||||||||||||
| 120 | __FORMAT_TEXT__ | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Arguments: | ||||||||||||||||||||||||||||
| 123 | $args_string | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Examples: | ||||||||||||||||||||||||||||
| 126 | Filter to records with field 'name' equal to 'John' | ||||||||||||||||||||||||||||
| 127 | recs-grep '\$r->{name} eq "John"' | ||||||||||||||||||||||||||||
| 128 | Find fields without ppid = 3456 | ||||||||||||||||||||||||||||
| 129 | recs-grep -v '{{ppid}} == 3456' | ||||||||||||||||||||||||||||
| 130 | Filter to records with all methods equal to 'PUT' | ||||||||||||||||||||||||||||
| 131 | recs-grep -MList::MoreUtils=all 'all { \$_ eq 'PUT' } \@{\$r->{methods}}' | ||||||||||||||||||||||||||||
| 132 | USAGE | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | 1; | |||||||||||||||||||||||||||