blib/lib/App/RecordStream/Executor.pm

filter: Statementstmt 88.8% 223 / 251 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.8% 55 / 58 Podpod 0.0% 0 / 17 totaltotal 84.6% 286 / 338 CC 11 SCAR 2.4
CC
11
Cov
88%
clear_vars
6.3
create_safe_package
3.0
create_snippets
3.0
CRAP 1.3
« blib/lib/App/RecordStream/Aggregator/FirstRecord.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Library.pm »
1 # This class handles the execution of "perl" code from the commandline on a
2 # record. Handles magic of variable hiding and also special syntax issues.
3  
4 package App::RecordStream::Executor;
5  
6 132 our $VERSION = "4.0.25";
7  
8 119 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 119 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 114 ▶use App::RecordStream::Operation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 130 my $NEXT_ID = 0;
14 130 my $DEFAULT_METHOD_NAME = '__MY__DEFAULT';
15  
16 # snippets is of the form:
17 # name => {
18 # arg_names => ['a', 'b'],
19 #
20 # # one of these:
21 # code => 'code string',
22 # }
23 #
24 60 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
25 60 my $class = shift;
26 60 my $snippets = shift;
27  
28 60 ▶ if ( ref($snippets) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $snippets ne "HASH") 30 30 60
29 30 my $code = <<CODE;
30 \$filename = App::RecordStream::Operation::get_current_filename();
31 \$line++;
32 $snippets;
33 CODE
34  
35 30 $snippets = {
36 $DEFAULT_METHOD_NAME => {
37 code => $code,
38 arg_names => ['r'],
39 },
40 };
41 }
42  
43 60 my $this = {
44 ID => $NEXT_ID,
45 SNIPPETS => $snippets,
46 };
47  
48 60 $NEXT_ID++;
49  
50 60 bless $this, $class;
51  
52 60 $this->init();
53  
54 60 return $this;
55 }
56  
57 60 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
58 60 my $this = shift;
59 60 $this->create_safe_package();
60 }
61  
62 60 ▶sub create_snippets {
Subroutine create_snippets: called CC=3 SCAR=11.0
Pod undocumented
63 60 my $this = shift;
64  
65 60 my $code = '';
66  
67 60 foreach my $name (keys %{$this->{'SNIPPETS'}} ) {
68 118 my $arg_names = $this->{'SNIPPETS'}->{$name}->{'arg_names'};
69 118 my $args_spec = '';
70  
71 118 ▶ if ( $arg_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_names) 60 58 118
72 60 $args_spec = 'my (';
73 60 $args_spec .= join(',', map { "\$$_"} @$arg_names);
74 60 $args_spec .= ') = @_;';
75 }
76  
77 118 my $method_name = $this->get_method_name($name);
78 118 my $snippet = $this->transform_code($this->{'SNIPPETS'}->{$name}->{'code'});
79  
80 118 $code .= <<CODE;
81 sub $method_name {
82 $args_spec
83  
84 $snippet
85 }
86 CODE
87 }
88  
89 60 return $code;
90 }
91  
92 236 ▶sub get_method_name {
Subroutine get_method_name: called CC=1 SCAR=0.0
Pod undocumented
93 236 my $this = shift;
94 236 my $name = shift;
95  
96 236 return '__MY__' . $name;
97 }
98  
99 118 ▶sub get_safe_package_name {
Subroutine get_safe_package_name: called CC=1 SCAR=0.0
Pod undocumented
100 118 my $this = shift;
101 118 return '__MY__SafeCompartment_' . $this->{'ID'};
102 }
103  
104 60 ▶sub create_safe_package {
Subroutine create_safe_package: called CC=3 SCAR=11.1
Pod undocumented
105 60 my $this = shift;
106 60 my $package_name = $this->get_safe_package_name();
107 60 my $snippets = $this->create_snippets();
108  
109 60 my $code = <<CODE;
110 package $package_name;
111  
112 $snippets
113  
114 1;
115 CODE
116  
117 60 eval_safe_package($code);
118 60 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 60 60
119 0 die $@;
120 }
121  
122 60 foreach my $name (keys %{$this->{'SNIPPETS'}}) {
123 118 my $method_name = $this->get_method_name($name);
124 118 my $code_ref = \&{$package_name . '::' . $method_name};
125 118 $this->{'SNIPPETS'}->{$name}->{'CODE_REF'} = $code_ref;
126 }
127 }
128  
129 0 ▶ sub clear_vars {
Subroutine clear_vars: not called CC=3 SCAR=18.4
Pod undocumented
130 0 my $this = shift;
131  
132 0 my $package_name = $this->get_safe_package_name();
133  
134 0 my %method_names = map { $this->get_method_name($_) => 1 } keys %{$this->{'SNIPPETS'}};
135  
136 {
137 0 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.2
138 114 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 0 foreach my $variable (keys %{$package_name . '::'}) {
141 0 ▶ next if ( exists $method_names{$variable} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $method_names{$variable} 0 0 0
142 0 undef *{$package_name . '::' . $variable};
143 0 delete ${$package_name . '::'}{$variable};
144 }
145 }
146 }
147  
148 0 ▶ sub set_scalar {
Subroutine set_scalar: not called CC=1 SCAR=1.5
Pod undocumented
149 0 my $this = shift;
150 0 my $name = shift;
151 0 my $val = shift;
152  
153 0 my $package_name = $this->get_safe_package_name();
154  
155 {
156 0 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.2
157 114 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 0 *{$package_name . '::' . $name} = \$val;
160 }
161 }
162  
163 0 ▶ sub get_scalar {
Subroutine get_scalar: not called CC=1 SCAR=1.2
Pod undocumented
164 0 my $this = shift;
165 0 my $name = shift;
166  
167 0 my $package_name = $this->get_safe_package_name();
168  
169 {
170 0 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.2
171 114 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 0 return ${$package_name . '::' . $name};
174 }
175 }
176  
177 58 ▶ sub set_executor_method {
Subroutine set_executor_method: called CC=1 SCAR=0.0
Pod undocumented
178 58 my $this = shift;
179 58 my $name = shift;
180 58 my $ref = shift;
181  
182 58 my $package_name = $this->get_safe_package_name();
183  
184 {
185 113 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 113 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 58 *{$package_name . "::" . $name} = $ref;
189 }
190 }
191  
192 176 ▶ sub get_code_ref {
Subroutine get_code_ref: called CC=1 SCAR=0.0
Pod undocumented
193 176 my $this = shift;
194 176 my $name = shift;
195 176 $this->{'SNIPPETS'}->{$name}->{'CODE_REF'};
196 }
197  
198 60 ▶ sub eval_safe_package {
Subroutine eval_safe_package: called CC=2 SCAR=6.9
Pod undocumented
199 60 my $__MY__code = shift;
200  
201 60 my $code = <<CODE;
202 no strict;
203 no warnings;
204  
205 $__MY__code
206 CODE
207  
208 60 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __MY____MY__DEFAULT: called CC=1 SCAR=0.0
Subroutine __MY____MY__DEFAULT: called CC=1 SCAR=0.0
209 60 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 60 60
210 0 die $@;
211 }
212 }
213  
214 93 ▶sub execute_code {
Subroutine execute_code: called CC=1 SCAR=0.0
Pod undocumented
215 93 my ($this, @args) = @_;
216 91 return $this->execute_method($DEFAULT_METHOD_NAME, @args);
217 }
218  
219 149 ▶sub execute_method {
Subroutine execute_method: called CC=1 SCAR=0.0
Pod undocumented
220 171 my ($this, $name, @args) = @_;
221 171 return $this->get_code_ref($name)->(@args);
222 }
223  
224 970 ▶sub transform_code {
Subroutine transform_code: called CC=2 SCAR=6.9
Pod undocumented
225 970 my $this = shift;
226 970 my $code = shift;
227  
228 953 ▶ while ( $code =~ m/\{\{(.*?)\}\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\{\{(.*?)\}\}/) 85 947 1032
229 116 my $specifier = $1;
230 110 my $guessing_code = '${App::RecordStream::KeySpec::find_key($r, qq{\@' . $specifier . '})}';
231 110 $code =~ s/\{\{.*?\}\}/$guessing_code/;
232 }
233  
234 972 return $code;
235 }
236  
237 33 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
238 16 return <<USAGE;
239 CODE SNIPPETS:
240 __FORMAT_TEXT__
241 Recs code snippets are perl code, with one exception. There a couple of
242 variables predefined for you, and one piece of special syntax to assist in
243 modifying hashes.
244 __FORMAT_TEXT__
245  
246 Special Variables:
247 __FORMAT_TEXT__
248 \$r - the current record object. This may be used exactly like a hash,
249 or you can use some of the special record functions, see App::RecordStream::Record for
250 more information
251  
252 \$line - This is the number of records run through the code snippet,
253 starting at 1. For most scripts this corresponds to the line number of the
254 input to the script.
255  
256 \$filename - The filename of the originating record. Note: This is only
257 useful if you're passing filenames directly to the recs script, piping from
258 other recs scripts or from cat, for instance, will not have a useful
259 filename.
260 __FORMAT_TEXT__
261  
262 Special Syntax
263 __FORMAT_TEXT__
264 Use {{search_string}} to look for a string in the keys of a record, use /
265 to nest keys. You can nest into arrays by using an index. If you are
266 vivifying arrays (if the array doesn't exist, prefix your key with # so
267 that an array rather than a hash will be created to put a / in your key,
268 escape it twice, i.e. \\/
269  
270 This is exactly the same as a key spec that is always prefaced with a @,
271 see 'man recs' for more info on key specs
272 __FORMAT_TEXT__
273  
274 For example: A record that looks like:
275 { "foo" : { "bar 1" : 1 }, "zoo" : 2}
276 Could be accessed like this:
277  
278 # value of zoo # value of \$r->{foo}->{bar 1}: (comma separate nested keys)
279 {{zoo}} {{foo/ar 1}}
280  
281 # Even assign to values (set the foo key to the value 1)
282 {{foo}} = 1
283  
284 # And auto, vivify
285 {{new_key/array_key/#0}} = 3 # creates an array within a hash within a hash
286  
287 # Index into an array
288 {{array_key/#3}} # The value of index 3 of the array ref under the
289 'array_key' hash key.
290  
291 __FORMAT_TEXT__
292 This matching is a fuzzy keyspec matching, see --help-keyspecs for more
293 details.
294 __FORMAT_TEXT__
295 USAGE
296 }
297  
298 23 ▶sub options_help {
Subroutine options_help: called CC=1 SCAR=0.0
Pod undocumented
299 return (
300 29 ['e', 'a perl snippet to execute, optional'],
301 ['E', 'the name of a file to read as a perl snippet'],
302 ['M module[=...]', 'execute "use module..." before executing snippet; same behaviour as perl -M'],
303 ['m module[=...]', 'same as -M, but by default import nothing'],
304 );
305 }
306  
307 125 1;
« blib/lib/App/RecordStream/Aggregator/FirstRecord.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Library.pm »