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.23";
4              
5 3     3   945 use strict;
  3         7  
  3         76  
6 3     3   14 use warnings;
  3         7  
  3         81  
7              
8 3     3   15 use base qw(App::RecordStream::Operation);
  3         7  
  3         189  
9              
10 3     3   327 use App::RecordStream::Executor::Getopt;
  3         6  
  3         62  
11 3     3   15 use App::RecordStream::Executor;
  3         7  
  3         782  
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 11 my $this = shift;
17 5         9 my $args = shift;
18              
19 5         11 my $chomp = 0;
20 5         29 my $executor_options = App::RecordStream::Executor::Getopt->new();
21 5         19 my $spec = {
22             'chomp' => \$chomp,
23             $executor_options->arguments(),
24             };
25              
26 5         29 $this->parse_options($args, $spec, ['bundling']);
27              
28 5         22 my $expression = $executor_options->get_string($args);
29 5         31 my $executor = App::RecordStream::Executor->new($expression);
30              
31 5         14 $this->{'EXECUTOR'} = $executor;
32 5         73 $this->{'CHOMP'} = $chomp;
33             }
34              
35             sub accept_record {
36 23     23 0 40 my $this = shift;
37 23         37 my $record = shift;
38              
39 23         39 my $executor = $this->{'EXECUTOR'};
40 23         63 my $value = $executor->execute_code($record);
41              
42 23 100       62 chomp $value if($this->{'CHOMP'});
43              
44 23         76 $this->push_line($value);
45              
46 23         116 return 1;
47             }
48              
49             sub add_help_types {
50 5     5 0 11 my $this = shift;
51 5         25 $this->use_help_type('snippet');
52 5         16 $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;