File Coverage

blib/lib/App/RecordStream/Executor/Getopt.pm
Criterion Covered Total %
statement 54 55 98.1
branch 8 10 80.0
condition n/a
subroutine 14 14 100.0
pod 0 7 0.0
total 76 86 88.3


line stmt bran cond sub pod time code
1             package App::RecordStream::Executor::Getopt;
2              
3 11     11   74 use strict;
  11         30  
  11         330  
4 11     11   64 use warnings;
  11         28  
  11         6989  
5              
6             sub new {
7 44     44 0 112 my $class = shift;
8              
9 44         188 my $this = {
10             'STRINGS' => [],
11             'MODULES' => [],
12             };
13              
14 44         123 bless $this, $class;
15              
16 44         129 return $this;
17             }
18              
19             sub arguments {
20 44     44 0 107 my $this = shift;
21              
22             return (
23 1     1   1572 'e=s' => sub { $this->push_string($_[1]); },
24 1     1   1066 'E=s' => sub { $this->push_file($_[1]); },
25 3     3   4690 'M=s' => sub { $this->push_module($_[1], 1); },
26 2     2   3515 'm=s' => sub { $this->push_module($_[1], 0); },
27 44         889 );
28             }
29              
30             sub get_strings {
31 44     44 0 111 my $this = shift;
32 44         100 my $args = shift;
33              
34 44         140 my $strings = $this->{'STRINGS'};
35 44 100       170 if(!@$strings) {
36 42 50       140 if(!@$args) {
37 0         0 die "Missing expression.\n";
38             }
39 42         139 push @$strings, shift @$args;
40             }
41              
42             # Use map to avoid the undesired comma operator behaviour if we're ever
43             # called in scalar context. return @{[ ..., ... ]} could also be used.
44 44         155 return map { @$_ } $this->{'MODULES'}, $strings;
  88         395  
45             }
46              
47             sub get_string {
48 44     44 0 133 my $this = shift;
49              
50 44         203 return join("", $this->get_strings(@_));
51             }
52              
53             sub push_string {
54 1     1 0 5 my $this = shift;
55 1         3 my $string = shift;
56              
57 1         3 push @{$this->{'STRINGS'}}, $string;
  1         5  
58             }
59              
60             sub push_file {
61 1     1 0 3 my $this = shift;
62 1         3 my $file = shift;
63              
64 1         4 my $string = $this->_slurp($file);
65              
66 1         3 push @{$this->{'STRINGS'}}, $string;
  1         5  
67             }
68              
69             sub _slurp {
70 1     1   3 my $this = shift;
71 1         2 my $file = shift;
72              
73 1         5 local $/;
74 1         2 undef $/;
75              
76 1 50       62 open (my $fh, '<', $file) or die "Could not open code snippet file: $file: $!";
77 1         21 my $code = <$fh>;
78 1         10 close $fh;
79              
80 1         6 return $code;
81             }
82              
83             sub push_module {
84 5     5 0 18 my $this = shift;
85 5         31 my ($module, $import) = split /=/, shift, 2;
86 5         16 my $import_default = shift;
87 5         15 my $statement;
88              
89 5 100       27 if (defined $import) {
    100          
90             # This syntax mimics the output of:
91             # perl -MO=Deparse -MList::Util=sum,max -e1
92 3         14 $import =~ s/(?=[\\'])/\\/g;
93 3         23 $statement = "use $module (split(/,/, '$import', 0));";
94             } elsif ($import_default) {
95 1         5 $statement = "use $module;";
96             } else {
97 1         5 $statement = "use $module ();";
98             }
99              
100 5         15 push @{$this->{'MODULES'}}, $statement;
  5         32  
101             }
102              
103             1;