Quiq/ExampleCode.pm

filter: Statementstmt 79.7% 59 / 74 Branchbran 38.4% 10 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 70.2% 85 / 121 CC 15 SCAR 26.7
CC
15
Cov
68%
execute
26.5
setVariable
3.4
BEGIN
1.0
CRAP 14.4
« Quiq/ImagePool.pm ↑ Summary Quiq/Duration.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::ExampleCode - Führe Beispielcode aus
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 (1) Code $code ausführen und den Code selbst und sein Resultat nach
16 STDOUT schreiben. Das Resultat (Skalar- oder List-Kontext) wird
17 direkt zurückgeliefert.
18  
19 my $exa = Quiq::ExampleCode->new;
20 $res|@res = $exa->execute($code,-variables=>\@keyval);
21 ...
22  
23 (2) Code $code ausführen und den Code selbst und sein Resultat in
24 Form eines Objektes zurückliefern. Die Ausgabe auf STDOUT
25 unterbleibt.
26  
27 my $exa = Quiq::ExampleCode->new(
28 -objectReturn => 1,
29 -verbose => 0,
30 );
31  
32 Ausführung im Skalar-Kontext:
33  
34 $obj = $exa->execute($code,-variables=>\@keyval);
35 my $code = $obj->code;
36 my $res = $obj->result;
37 ...
38  
39 Im List-Kontext:
40  
41 $obj = $exa->execute($code,-variables=>\@keyval,-listContext=>1);
42 my $code = $obj->code;
43 my $resA = $obj->result;
44 ...
45  
46 =head1 DESCRIPTION
47  
48 Die Klasse dient zum Ausführen von Beispielcode in Perl. Beispielcode
49 ist dadurch gekennzeichnet, dass der Code und sein Resultat gegenüber
50 gestellt werden.
51  
52 =head1 EXAMPLES
53  
54 =head2 Setzen von Variablen
55  
56 Variable vorab setzen:
57  
58 $exa->setVariable(
59 ffm => $ffm,
60 );
61 my $cmd = $exa->execute(q|
62 $ffm->videoToImages('video.mp4','img',
63 -aspectRatio => '4:3',
64 -framestep => 6,
65 -start => 3,
66 -stop => 10,
67 );
68 |);
69  
70 Variable per Option -variables bei Aufruf setzen:
71  
72 my $cmd = $exa->execute(q|
73 $ffm->videoToImages('video.mp4','img',
74 -aspectRatio => '4:3',
75 -framestep => 6,
76 -start => 3,
77 -stop => 10,
78 );|,
79 -variables => [
80 ffm => $ffm,
81 ],
82 );
83  
84 oder (in anderer Reihenfolge):
85  
86 my $cmd = $exa->execute(
87 -variables => [
88 ffm => $ffm,
89 ],q|
90 $ffm->videoToImages('video.mp4','img',
91 -aspectRatio => '4:3',
92 -framestep => 6,
93 -start => 3,
94 -stop => 10,
95 );
96 |,
97 );
98  
99 =cut
100  
101 # -----------------------------------------------------------------------------
102  
103 package Quiq::ExampleCode;
104 2 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 2 our $VERSION = '1.240';
111  
112 2 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶use Quiq::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 # -----------------------------------------------------------------------------
117  
118 =head1 METHODS
119  
120 =head2 Konstruktor
121  
122 =head3 new() - Erzeuge Instanz
123  
124 =head4 Synopsis
125  
126 $exa = $class->new(@opt);
127  
128 =head4 Options
129  
130 =over 4
131  
132 =item -fileHandle => $fh (Default: \*STDOUT)
133  
134 FileHandle, über die Informationen über die ausgeführten Beispiele
135 ausgegeben wird.
136  
137 =item -objectReturn => $bool (Default: 0)
138  
139 Liefere bei Aufruf von $exa->L<execute|"execute() - Führe Beispielcode aus">() nicht den Wert des
140 Beispielcode, sondern ein Objekt, das den Beispielcode und
141 den Wert enthält.
142  
143 =item -verbose => $bool (Default: 1)
144  
145 Gib Informationen über die ausgeführten Beispiele auf
146 der FileHandle -fileHandle aus.
147  
148 =back
149  
150 =head4 Description
151  
152 Instantiiere ein Example-Objekt und liefere eine Referenz auf
153 dieses Objekt zurück.
154  
155 =cut
156  
157 # -----------------------------------------------------------------------------
158  
159 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
160 5 my $class = shift;
161 # @_: $dir,@opt
162  
163 # Optionen
164  
165 5 my $fileHandle = \*STDOUT;
166 5 my $objectReturn = 0;
167 5 my $verbose = 1;
168  
169 5 Quiq::Option->extract(-mode=>'sloppy',\@_,
170 -fileHandle => \$fileHandle,
171 -objectReturn => \$objectReturn,
172 -verbose => \$verbose,
173 );
174  
175 # Instantiiere Objekt
176  
177 5 return $class->SUPER::new(
178 fileHandle => $fileHandle,
179 objectReturn => $objectReturn,
180 verbose => $verbose,
181 );
182 }
183
184  
185 # -----------------------------------------------------------------------------
186  
187 =head2 Variablen setzen
188  
189 =head3 setVariable() - Setze Variable für Beispielcode-Ausführung
190  
191 =head4 Synopsis
192  
193 $exa->setVariable(@keyVal);
194  
195 =head4 Description
196  
197 Setze Variable, so dass die folgende Kommando-Ausführung (Methode
198 L<execute|"execute() - Führe Beispielcode aus">()) im Kontext der Klasse Quiq::ExampleCode ausgeführt werden
199 kann. Die Methode liefert keinen Wert zurück.
200  
201 Anstelle mittels dieser Methode, können die Variablen auch beim Aufruf
202 selbst mit der Option -variables angegeben werden.
203  
204 =head4 Example
205  
206 $exa->setVariable(
207 obj => $obj,
208 x => $x,
209 y => $y,
210 );
211  
212 =cut
213  
214 # -----------------------------------------------------------------------------
215  
216 0 ▶sub setVariable {
Subroutine setVariable: not called CC=2 SCAR=12.2
Pod documented
217 0 my $self = shift;
218 # @_: @keyVal
219  
220 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
222 0 my $key = shift;
223 0 $$key = shift; # Erzeuge Package-Variable
224 }
225  
226 0 return;
227 }
228  
229 # -----------------------------------------------------------------------------
230  
231 =head2 Beispielcode ausführen
232  
233 =head3 execute() - Führe Beispielcode aus
234  
235 =head4 Synopsis
236  
237 $res|@res = $exa->execute($code,@opt);
238  
239 =head4 Options
240  
241 =over 4
242  
243 =item -asStringCallback => $subRef (Default: undef)
244  
245 Subroutine-Referenz, die das Resultat des Beispiel-Code in
246 eine Stringrepräsentation wandelt, die auf dem Bildschirm ausgegeben
247 werden kann.
248  
249 =item -listContext => $bool (Default: Wert von wantarray)
250  
251 Führe den Beispiel-Code im List-Kontext aus. Eine explizite
252 Setzung ist nur im Falle von -objectReturn=>1 nötig
253  
254 =item -objectReturn => $bool (Default: 0)
255  
256 Liefere bei Aufruf von $exa->L<execute|"execute() - Führe Beispielcode aus">() nicht den Wert des
257 Beispielcode, sondern ein Objekt, das den Beispielcode und
258 den Wert enthält.
259  
260 =item -variables => \@keyVal (Default: [])
261  
262 Setze die angegebenen Variablen im Ausführungskontext des
263 Beispiel-Code.
264  
265 =item -verbose => $bool (Default: Konstruktor-Setzung)
266  
267 Gib Informationen über die ausgeführten Beispiele auf
268 der FileHandle -fileHandle aus.
269  
270 =back
271  
272 =head4 Description
273  
274 Führe Beispielcode $code aus und liefere das Resultat der Ausführung
275 $res (Skalar-Kontext) oder @res (Listen-Kontext) zurück.
276  
277 Ist beim Konstruktor die Option -verbose gesetzt worde, wird
278 zusätzlich der Beispielcode und das Resultat ausgegeben.
279  
280 =head4 Example
281  
282 Der Aufruf
283  
284 $cmd = $exa->execute(q|
285 $ffm->videoToImages('video.mp4','img',
286 -aspectRatio => '4:3',
287 -framestep => 6,
288 -start => 3,
289 -stop => 10,
290 );
291 |);
292  
293 führt zu der Ausgabe
294  
295 $ffm->videoToImages('video.mp4','img',
296 -aspectRatio => '4:3',
297 -framestep => 6,
298 -start => 3,
299 -stop => 10,
300 );
301 =>
302 ffmpeg -y -loglevel error -stats -i 'video.mp4'
303 -vf 'framestep=6,crop=ih/3*4:ih'
304 -ss 3 -t 7 -qscale:v 2 'img/%06d.jpg'
305  
306 Der Text nach '=>' ist der gelieferte Wert $cmd.
307  
308 =cut
309  
310 # -----------------------------------------------------------------------------
311  
312 7 ▶sub execute {
Subroutine execute: called CC=14 SCAR=32.8
Pod documented
313 7 my $self = shift;
314 # @_: @opt
315  
316 # Optionen
317  
318 7 my $asStringCallback = undef;
319 7 my $listContext = undef;
320 7 my $objectReturn = $self->objectReturn;
321 7 my $variableA = undef;
322 7 my $verbose = $self->verbose;
323
324 7 Quiq::Option->extract(\@_,
325 -asStringCallback => \$asStringCallback,
326 -listContext => \$listContext,
327 -objectReturn => \$objectReturn,
328 -variables => \$variableA,
329 -verbose => \$verbose,
330 );
331 7 ▶ $listContext //= wantarray;
Condition33%1/3
$listContext //= wantarray
l!l&&r!l&&!r
070
MC/DC0%0/2
$listContext //= wantarray
$listContext wantarray
Truth table33%1/3
A //= B
A= $listContextB= wantarray
ABresult
1X1
011
000
332
333 7 ▶ if (@_ != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 1) 0 7 7
334 0 $self->throw('Falsche Anzahl Argumente');
335 }
336 7 my $code = shift;
337  
338 # Operation ausführen
339
340 7 my $fh = $self->get('fileHandle');
341  
342 7 ▶ if ($variableA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variableA) 0 7 7
343 0 $self->setVariable(@$variableA);
344 }
345
346 7 $code = Quiq::String->removeIndentation($code);
347 7 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 7 7
348 0 print $fh "\n$code\n=>\n";
349 }
350  
351 7 my (@res,$res);
352 7 ▶ if ($listContext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listContext) { } 0 7 7
353 2 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
354 0 @res = eval $code;
355 }
356 else {
357 2 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
358 7 $res = eval $code;
359 }
360 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
361 0 die;
362 }
363 7 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 7 7
364 0 my $outp;
365 0 ▶ if (my $sub = $asStringCallback) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $asStringCallback) { } 0 0 0
366 0 ▶ $outp = $asStringCallback->($listContext? \@res: $res);
Branch: 1 branch
BranchTrueFalseTotal
$listContext ? : 0 0 0
367 }
368 else {
369 0 ▶ $outp = wantarray? "@res": $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
370 }
371 0 print $fh "$outp\n";
372 }
373  
374 7 ▶ if ($objectReturn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objectReturn) 1 6 7
375 1 ▶ return Quiq::Hash->new(
Branch: 1 branch
BranchTrueFalseTotal
$listContext ? : 0 1 1
376 code => $code,
377 result => $listContext? \@res: $res,
378 );
379 }
380
381 6 ▶ return wantarray? @res: $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
382 }
383  
384 # -----------------------------------------------------------------------------
385  
386 =head1 VERSION
387  
388 1.240
389  
390 =head1 AUTHOR
391  
392 Frank Seitz, L<http://fseitz.de/>
393  
394 =head1 COPYRIGHT
395  
396 Copyright (C) 2026 Frank Seitz
397  
398 =head1 LICENSE
399  
400 This code is free software; you can redistribute it and/or modify
401 it under the same terms as Perl itself.
402  
403 =cut
404  
405 # -----------------------------------------------------------------------------
406  
407 2 1;
408  
409 # eof
« Quiq/ImagePool.pm ↑ Summary Quiq/Duration.pm »