« blib/lib/App/RecordStream/Aggregator/Internal/Ord2UnivariateMap.pm
↑ Summary
blib/lib/App/RecordStream/Aggregator/Array.pm »
| 1 | package App::RecordStream::Operation::eval; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | our $VERSION = "4.0.23"; | |||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use base qw(App::RecordStream::Operation); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use App::RecordStream::Executor::Getopt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use App::RecordStream::Executor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | # TODO: if we want --no-newline back we'll need to change push_line to push_text, make Stream::Base stateful (spooling text to parse lines to parse records), etc. | ||||||||||
| 14 | |||||||||||
| 15 | sub init { | ||||||||||
| 16 | 6 | ▶ | my $this = shift; | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 6 | my $args = shift; | |||||||||
| 18 | |||||||||||
| 19 | 6 | my $chomp = 0; | |||||||||
| 20 | 6 | my $executor_options = App::RecordStream::Executor::Getopt->new(); | |||||||||
| 21 | 6 | my $spec = { | |||||||||
| 22 | 'chomp' => \$chomp, | ||||||||||
| 23 | $executor_options->arguments(), | ||||||||||
| 24 | }; | ||||||||||
| 25 | |||||||||||
| 26 | 6 | $this->parse_options($args, $spec, ['bundling']); | |||||||||
| 27 | |||||||||||
| 28 | 5 | my $expression = $executor_options->get_string($args); | |||||||||
| 29 | 5 | my $executor = App::RecordStream::Executor->new($expression); | |||||||||
| 30 | |||||||||||
| 31 | 5 | $this->{'EXECUTOR'} = $executor; | |||||||||
| 32 | 5 | $this->{'CHOMP'} = $chomp; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub accept_record { | ||||||||||
| 36 | 23 | ▶ | my $this = shift; | ||||||||
|
Subroutine
accept_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 37 | 23 | my $record = shift; | |||||||||
| 38 | |||||||||||
| 39 | 23 | my $executor = $this->{'EXECUTOR'}; | |||||||||
| 40 | 23 | my $value = $executor->execute_code($record); | |||||||||
| 41 | |||||||||||
| 42 | 23 | ▶ | chomp $value if($this->{'CHOMP'}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 23 | $this->push_line($value); | |||||||||
| 45 | |||||||||||
| 46 | 23 | return 1; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | sub add_help_types { | ||||||||||
| 50 | 6 | ▶ | my $this = shift; | ||||||||
|
Subroutine
add_help_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 6 | $this->use_help_type('snippet'); | |||||||||
| 52 | 6 | $this->use_help_type('keyspecs'); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | sub usage { | ||||||||||
| 56 | 1 | ▶ | my $this = shift; | ||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 57 | |||||||||||
| 58 | 1 | my $options = [ | |||||||||
| 59 | App::RecordStream::Executor::options_help(), | ||||||||||
| 60 | ['chomp', 'Chomp eval results (to avoid duplicate newlines when already newline-terminated)'], | ||||||||||
| 61 | ]; | ||||||||||
| 62 | |||||||||||
| 63 | 1 | my $args_string = $this->options_string($options); | |||||||||
| 64 | |||||||||||
| 65 | 1 | my $usage = <<USAGE; | |||||||||
| 66 | Usage: recs-eval <args> <expr> [<files>] | ||||||||||
| 67 | __FORMAT_TEXT__ | ||||||||||
| 68 | <expr> is evaluated as perl on each record of input (or records from | ||||||||||
| 69 | <files>) with \$r set to a App::RecordStream::Record object and \$line set | ||||||||||
| 70 | to the current line number (starting at 1). The result of each evaluation | ||||||||||
| 71 | is printed on a line by itself (this is not a recs stream). See | ||||||||||
| 72 | App::RecordStream::Record for help on what the \$r object can do. See | ||||||||||
| 73 | --help-snippets for more information on code snippets | ||||||||||
| 74 | __FORMAT_TEXT__ | ||||||||||
| 75 | |||||||||||
| 76 | $args_string | ||||||||||
| 77 | |||||||||||
| 78 | Examples: | ||||||||||
| 79 | Print the host field from each record. | ||||||||||
| 80 | recs-eval '\$r->{host}' | ||||||||||
| 81 | Prepare to gnuplot field y against field x. | ||||||||||
| 82 | recs-eval '\$r->{x} . " " . \$r->{y}' | ||||||||||
| 83 | Set up a script (this would be presumably piped to sh) | ||||||||||
| 84 | recs-eval '"./myscript --value \$r->{foo}"' | ||||||||||
| 85 | USAGE | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 3 | 1; | |||||||||