File Coverage

blib/lib/App/RecordStream/Operation/eval.pm
Criterion Covered Total %
statement 35 39 89.7
branch 2 2 100.0
condition n/a
subroutine 8 9 88.8
pod 0 4 0.0
total 45 54 83.3


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::eval;
2              
3             our $VERSION = "4.0.24";
4              
5 3     3   854 use strict;
  3         6  
  3         75  
6 3     3   13 use warnings;
  3         6  
  3         77  
7              
8 3     3   13 use base qw(App::RecordStream::Operation);
  3         5  
  3         186  
9              
10 3     3   250 use App::RecordStream::Executor::Getopt;
  3         7  
  3         65  
11 3     3   14 use App::RecordStream::Executor;
  3         6  
  3         679  
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 5     5 0 10 my $this = shift;
17 5         10 my $args = shift;
18              
19 5         7 my $chomp = 0;
20 5         25 my $executor_options = App::RecordStream::Executor::Getopt->new();
21 5         17 my $spec = {
22             'chomp' => \$chomp,
23             $executor_options->arguments(),
24             };
25              
26 5         28 $this->parse_options($args, $spec, ['bundling']);
27              
28 5         20 my $expression = $executor_options->get_string($args);
29 5         28 my $executor = App::RecordStream::Executor->new($expression);
30              
31 5         11 $this->{'EXECUTOR'} = $executor;
32 5         68 $this->{'CHOMP'} = $chomp;
33             }
34              
35             sub accept_record {
36 23     23 0 29 my $this = shift;
37 23         29 my $record = shift;
38              
39 23         36 my $executor = $this->{'EXECUTOR'};
40 23         52 my $value = $executor->execute_code($record);
41              
42 23 100       58 chomp $value if($this->{'CHOMP'});
43              
44 23         72 $this->push_line($value);
45              
46 23         110 return 1;
47             }
48              
49             sub add_help_types {
50 5     5 0 7 my $this = shift;
51 5         21 $this->use_help_type('snippet');
52 5         12 $this->use_help_type('keyspecs');
53             }
54              
55             sub usage {
56 0     0 0   my $this = shift;
57              
58 0           my $options = [
59             App::RecordStream::Executor::options_help(),
60             ['chomp', 'Chomp eval results (to avoid duplicate newlines when already newline-terminated)'],
61             ];
62              
63 0           my $args_string = $this->options_string($options);
64              
65 0           my $usage = <
66             Usage: recs-eval []
67             __FORMAT_TEXT__
68             is evaluated as perl on each record of input (or records from
69             ) 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             1;