Framework/Extension/Filter.pm

filter: Statementstmt 96.1% 175 / 182 Branchbran 90.9% 40 / 44 Conditioncond 54.1% 13 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 95.4% 21 / 22 Podpod 100% 4 / 4 totaltotal 88.3% 257 / 291 CC 33 SCAR 18.4
CC
33
Cov
91%
filter_run
14.1
_open_output
8.0
_handle_output
5.0
CRAP 6.3
« Framework/Feature/Options.pm ↑ Summary Framework/Feature/Logging.pm »
1 package App::Framework::Extension::Filter ;
2  
3 =head1 NAME
4  
5 App::Framework::Extension::Filter - Script filter application object
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework '::Filter' ;
10  
11  
12 =head1 DESCRIPTION
13  
14 Application that filters a file or files to produce some other output
15  
16  
17 =head2 Application Subroutines
18  
19 This extension modifies the normal call flow for the application subroutines. The extension calls the subroutines
20 for each input file being filtered. Also, the main 'app' subroutine is called for each of the lines of text in the input file.
21  
22 The pseudo-code for the extension is:
23  
24 FOREACH input file
25 <init variables, state HASH>
26 call 'app_start' subroutine
27 FOREACH input line
28 call 'app' subroutine
29 END
30 call 'app_end' subroutine
31 END
32  
33 For each input file, a state HASH is created and passed as a reference to the application subroutines. The state HASH contains
34 various values maintained by the extension, but the application may add it's own additional values to the HASH. These values will
35 be passed unmodified to each of the application subroutine calls.
36  
37 The state HASH contains the following fields:
38  
39 =over 4
40  
41 =item * num_files
42  
43 Total number of input files.
44  
45 =item * file_number
46  
47 Current input file number (1 to B<num_files>)
48  
49 =item * file_list
50  
51 ARRAY ref. List of input filenames.
52  
53 =item * vars
54  
55 HASH ref. Empty HASH created so that any application-specific variables may be stored here.
56  
57 =item * line_num
58  
59 Current line number of line being processed (1 to N).
60  
61 =item * output_lines
62  
63 ARRAY ref. List of the output lines that are to be written to the output file (maintained by the extension).
64  
65 =item * file
66  
67 Current file name of the file being processed.
68  
69 =item * line
70  
71 String of line being processed.
72  
73 =item * output
74  
75 Special variable used by application to tell extension what to output (see L</Output>).
76  
77 =back
78  
79 The state HASH reference is passed to all 3 of the application subroutines. In addition, the input line of text is also passed
80 to the main 'app' subroutine. The interface for the subroutines is:
81  
82 =over 4
83  
84 =item B<app_start($app, $opts_href, $state_href)>
85  
86 Called once for each input file. Called at the start of processing. Allows any setting up of variables stored in the state HASH.
87  
88 Arguments are:
89  
90 =over 4
91  
92 =item I<$app> - The application object
93  
94 =item I<$opts_href> - HASH ref to the command line options (see L<App::Framework::Feature::Options> and L</Filter Options>)
95  
96 =item I<$state_href> - HASH ref to state
97  
98 =back
99  
100 =item B<app($app, $opts_href, $state_href, $line)>
101  
102 Called once for each input file. Called at the start of processing. Allows any setting up of variables stored in the state HASH.
103  
104 Arguments are:
105  
106 =over 4
107  
108 =item I<$app> - The application object
109  
110 =item I<$opts_href> - HASH ref to the command line options (see L<App::Framework::Feature::Options> and L</Filter Options>)
111  
112 =item I<$state_href> - HASH ref to state
113  
114 =item I<$line> - Text of input line
115  
116 =back
117  
118 =item B<app_end($app, $opts_href, $state_href)>
119  
120 Called once for each input file. Called at the end of processing. Allows for any end of file tidy up, data sorting etc.
121  
122 Arguments are:
123  
124 =over 4
125  
126 =item I<$app> - The application object
127  
128 =item I<$opts_href> - HASH ref to the command line options (see L<App::Framework::Feature::Options> and L</Filter Options>)
129  
130 =item I<$state_href> - HASH ref to state
131  
132 =back
133  
134 =back
135  
136  
137 =head2 Output
138  
139 By default, each time the extension calls the 'app' subroutine it sets the B<output> field of the state HASH to undef. The 'app'
140 subroutine must set this field to some value for the extension to write anything to the output file.
141  
142 For examples, the following simple 'app' subroutine causes all input files to be output uppercased:
143  
144 sub app
145 {
146 my ($app, $opts_href, $state_href, $line) = @_ ;
147
148 # uppercase
149 $state_href->{output} = uc $line ;
150 }
151  
152 If no L</outfile> option is specified, then all output will be written to STDOUT. Also, normally the output is written line-by-line after each line has been processed. If the L</buffer>
153 option has been specified, then all output lines are buffered (into the state variable L</output_lines>) then written out at the end of processing all input. Similarly, if the L</inplace>
154 option is specified, then buffering is used to process the complete input file then overwrite it with the output.
155  
156 =head2 Outfile option
157  
158 The L</outfile> option may be used to set the output filename. This may include variables that are specific to the Filter extension, where the variables value is updated for each
159 input file being processed. The following Filter-sepcific variables may be used:
160  
161 $filter{'filter_file'} = $state_href->{file} ;
162 $filter{'filter_filenum'} = $state_href->{file_number} ;
163 my ($base, $path, $ext) = fileparse($file, '\..*') ;
164 $filter{'filter_name'} = $base ;
165 $filter{'filter_base'} = $base ;
166 $filter{'filter_path'} = $path ;
167 $filter{'filter_ext'} = $ext ;
168  
169 =over 4
170  
171 =item I<filter_file> - Input full file path
172  
173 =item I<filter_base> - Basename of input file (excluding extension)
174  
175 =item I<filter_name> - Alias for L</filter_base>
176  
177 =item I<filter_path> - Directory path of input file
178  
179 =item I<filter_ext> - Extension of input file
180  
181 =item I<filter_filenum> - Input file number (starting from 1)
182  
183 =back
184  
185  
186 NOTE: Specifying these variables for options at the command line will require you to escape the variables per the operating system you are using (e.g. use single quotes ' ' around
187 the value in Linux).
188  
189 For example, with the command line arguments:
190  
191 -outfile '/tmp/$filter_name-$filter_filenum.txt' afile.doc /doc/bfile.text
192  
193 Processes './afile.doc' into '/tmp/afile-1.txt', and '/doc/bfile.text' into '/tmp/bfile-2.txt'
194  
195  
196 =head2 Example
197  
198 As an example, here is a script that filters one or more HTML files to strip out unwanted sections (they are actually Doxygen HTML files
199 that I wanted to convert into a pdf book):
200  
201 #!/usr/bin/perl
202 #
203 use strict ;
204 use App::Framework '::Filter' ;
205
206 # VERSION
207 our $VERSION = '1.00' ;
208
209 ## Create app
210 go() ;
211
212 #----------------------------------------------------------------------
213 sub app_begin
214 {
215 my ($app, $opts_href, $state_href, $line) = @_ ;
216
217 # force in-place editing
218 $app->set(inplace => 1) ;
219
220 # set to start state
221 $state_href->{vars} = {
222 'state' => 'start',
223 } ;
224 }
225
226 #----------------------------------------------------------------------
227 # Main execution
228 #
229 sub app
230 {
231 my ($app, $opts_href, $state_href, $line) = @_ ;
232
233 my $ok = 1 ;
234 if ($state_href->{'vars'}{'state'} eq 'start')
235 {
236 if ($line =~ m/<!-- Generated by Doxygen/i)
237 {
238 $ok = 0 ;
239 $state_href->{'vars'}{'state'} = 'doxy-head' ;
240 }
241 }
242 elsif ($state_href->{'vars'}{'state'} eq 'doxy-head')
243 {
244 $ok = 0 ;
245 if ($line =~ m/<div class="contents">/i)
246 {
247 $ok = 1 ;
248 $state_href->{'vars'}{'state'} = 'contents' ;
249 }
250 }
251 elsif ($state_href->{'vars'}{'state'} eq 'contents')
252 {
253 if ($line =~ m/<hr size="1"><address style="text-align: right;"><small>Generated/i)
254 {
255 $ok = 0 ;
256 $state_href->{'vars'}{'state'} = 'doxy-foot' ;
257 }
258 }
259 elsif ($state_href->{'vars'}{'state'} eq 'doxy-foot')
260 {
261 $ok = 0 ;
262 if ($line =~ m%</body>%i)
263 {
264 $ok = 1 ;
265 $state_href->{'vars'}{'state'} = 'end' ;
266 }
267 }
268
269 # only output if ok to do so
270 $state_href->{'output'} = $line if $ok ;
271 }
272
273
274 #=================================================================================
275 # SETUP
276 #=================================================================================
277 __DATA__
278
279 [SUMMARY]
280 Filter Doxygen created html removing frames etc.
281
282 [DESCRIPTION]
283 B<$name> does some stuff.
284  
285  
286 The script takes in HTML of the form:
287  
288 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
289 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
290 <title>rctu4_test: File Index</title>
291 <link href="doxygen.css" rel="stylesheet" type="text/css">
292 <link href="tabs.css" rel="stylesheet" type="text/css">
293 </head><body>
294 **<!-- Generated by Doxygen 1.5.5 -->
295 **<div class="navigation" id="top">
296 ** <div class="tabs">
297 ** <ul>
298 ..
299 ** </div>
300 **</div>
301 <div class="contents">
302 <h1>File List</h1>Here is a list of all files with brief descriptions:<table>
303 <tr><td class="indexkey">src/<a class="el" href="rctu4__tests_8c.html">rctu4_tests.c</a></td><td class="indexvalue"></td></tr>
304 <tr><td class="indexkey">src/common/<a class="el" href="ate__general_8c.html">ate_general.c</a></td><td class="indexvalue"></td></tr>
305 ...
306
307 <tr><td class="indexkey">src/tests/<a class="el" href="test__star__daisychain__specific_8c.html">test_star_daisychain_specific.c</a></td><td class="indexvalue"></td></tr>
308 <tr><td class="indexkey">src/tests/<a class="el" href="test__version__functions_8c.html">test_version_functions.c</a></td><td class="indexvalue"></td></tr>
309 </table>
310
311 </div>
312
313 **<hr size="1"><address style="text-align: right;"><small>Generated on Fri Jun 5 13:43:31 2009 for rctu4_test by&nbsp;
314 **<a href="http://www.doxygen.org/index.html">
315 **<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address>
316 </body>
317 </html>
318  
319 And removes the lines beginning '**'.
320  
321 The script does in-place updating of the HTML files and can be run as:
322  
323 filter-script *.html
324  
325 =cut
326  
327 7 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
328 7 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
329  
330 7 our $VERSION = "1.001" ;
331  
332  
333  
334  
335  
336 #============================================================================================
337 # USES
338 #============================================================================================
339 7 ▶use File::Path ;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 7 ▶use File::Basename ;
Subroutine BEGIN: called CC=1 SCAR=0.0
341 7 ▶use File::Spec ;
Subroutine BEGIN: called CC=1 SCAR=0.0
342 7 ▶use App::Framework::Core ;
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344  
345 #============================================================================================
346 # OBJECT HIERARCHY
347 #============================================================================================
348 7 ▶use App::Framework::Extension ;
Subroutine BEGIN: called CC=1 SCAR=0.0
349 7 our @ISA ;
350  
351 #============================================================================================
352 # GLOBALS
353 #============================================================================================
354  
355 =head2 ADDITIONAL COMMAND LINE OPTIONS
356  
357 This extension adds the following additional command line options to any application:
358  
359 =over 4
360  
361 =item B<-skip_empty> - Skip blanks
362  
363 Do not process empty lines (lines that contain only whitespace)
364  
365 =item B<-trim_space> - Trim spaces
366  
367 Remove spaces from start and end of lines
368  
369 =item B<-trim_comment> - Trim comments
370  
371 Remove any comments from the line, starting from the comment string to the end of the line
372  
373 =item B<-inplace> - In-place filter
374  
375 Read file, process, then overwrite original input file with processed output
376  
377 =item B<-outdir> - Specify output directory
378  
379 Write file(s) into specified directory rather that into same directory as input file
380  
381 =item B<-outfile> - Specify output file
382  
383 Specify the output filename, which may include variables (see L</Output Filename>)
384  
385 =item B<-comment> - Specify command string
386  
387 Specify the comment start string. Used in conjuntion with L</-trim_comment>.
388  
389 =back
390  
391 =cut
392  
393 # Set of script-related default options
394 7 my @OPTIONS = (
395 ['skip_empty', 'Skip blanks', 'Do not process empty lines', ],
396 ['trim_space', 'Trim spaces', 'Remove spaces from start/end of line', ],
397 ['trim_comment', 'Trim comments', 'Remove comments from line'],
398 ['inplace', 'In-place filter', 'Read file, process, then overwrite input file'],
399 ['outdir=s', 'Output directory', 'Write files into specified directory (rather than into same directory as input file)'],
400 ['outfile=s', 'Output filename', 'Specify the output filename which may include variables'],
401 ['comment=s', 'Comment', 'Specify the comment start string', '#'],
402 ) ;
403  
404 =head2 COMMAND LINE ARGUMENTS
405  
406 This extension sets the following additional command line arguments for any application:
407  
408 =over 4
409  
410 =item B<file> - Input file(s)
411  
412 Specify one of more input files to be processed. If no files are specified on the command line then reads from STDIN.
413  
414 =back
415  
416 =cut
417  
418 # Arguments spec
419 7 my @ARGS = (
420 ['file=f*', 'Input file(s)', 'Specify one (or more) input file to be processed']
421 ) ;
422  
423 7 our $class_debug=0;
424  
425 #============================================================================================
426  
427 =head2 FIELDS
428  
429 Note that the fields match with the command line options.
430  
431 =over 4
432  
433 =item B<skip_empty> - Skip blanks
434  
435 Do not process empty lines (lines that contain only whitespace)
436  
437 =item B<trim_space> - Trim spaces
438  
439 Remove spaces from start and end of lines
440  
441 =item B<trim_comment> - Trim comments
442  
443 Remove any comments from the line, starting from the comment string to the end of the line
444  
445 =item B<inplace> - In-place filter
446  
447 Read file, process, then overwrite original input file with processed output
448  
449 =item B<buffer> - Buffer output
450  
451 Store output lines into a buffer, then write out file at end of processing
452  
453 =item B<outdir> - Specify output directory
454  
455 Write file(s) into specified directory rather that into same directory as input file
456  
457 =item B<outfile> - Specify output file
458  
459 Specify the output filename, which may include variables (see L</Output Filename>)
460  
461 =item B<comment> - Specify command string
462  
463 Specify the comment start string. Used in conjuntion with L</trim_comment>.
464  
465 =item B<out_fh> - Output file handle
466  
467 Read only. File handle of current output file.
468  
469 =back
470  
471 =cut
472  
473 7 my %FIELDS = (
474 ## Object Data
475 'skip_empty' => 0,
476 'trim_space' => 0,
477 'trim_comment' => 0,
478 'comment' => '#',
479 'buffer' => 0,
480 'inplace' => 0,
481 'outfile' => undef,
482 'outdir' => undef,
483
484 ## internal
485 'out_fh' => undef,
486
487 '_filter_state' => {},
488 '_filter_opts' => undef,
489 ) ;
490  
491 #============================================================================================
492  
493 =head2 CONSTRUCTOR METHODS
494  
495 =over 4
496  
497 =cut
498  
499 #============================================================================================
500  
501 =item B<new([%args])>
502  
503 Create a new App::Framework::Extension::Filter.
504  
505 The %args are specified as they would be in the B<set> method, for example:
506  
507 'mmap_handler' => $mmap_handler
508  
509 The full list of possible arguments are :
510  
511 'fields' => Either ARRAY list of valid field names, or HASH of field names with default values
512  
513 =cut
514  
515 sub new
516 6 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
517 6 my ($obj, %args) = @_ ;
518  
519 6 ▶ my $class = ref($obj) || $obj ;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
520  
521 ## create object dynamically
522 6 my $this = App::Framework::Core->inherit($class, %args) ;
523  
524 #print "Filter - $class ISA=@ISA\n" if $class_debug ;
525  
526 ## Set options
527 6 $this->feature('Options')->append_options(\@OPTIONS) ;
528
529 ## Update option defaults
530 6 $this->feature('Options')->defaults_from_obj($this, [keys %FIELDS]) ;
531  
532 ## Set args
533 6 $this->feature('Args')->append_args(\@ARGS) ;
534  
535 #$this->debug(2) ;
536
537 ## hi-jack the app function
538 $this->extend_fn(
539 11 ▶ 'app_fn' => sub {$this->filter_run(@_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
540 11 ▶ 'app_start_fn' => sub {$this->_filter_start(@_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
541 11 ▶ 'app_end_fn' => sub {$this->_filter_end(@_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
542 6 ) ;
543  
544 6 return($this) ;
545 }
546  
547  
548  
549 #============================================================================================
550  
551 =back
552  
553 =head2 CLASS METHODS
554  
555 =over 4
556  
557 =cut
558  
559 #============================================================================================
560  
561 #-----------------------------------------------------------------------------
562  
563 =item B<init_class([%args])>
564  
565 Initialises the object class variables.
566  
567 =cut
568  
569 sub init_class
570 6 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
571 6 my $class = shift ;
572 6 my (%args) = @_ ;
573  
574 # Add extra fields
575 6 $class->add_fields(\%FIELDS, \%args) ;
576  
577 # init class
578 6 $class->SUPER::init_class(%args) ;
579  
580 }
581  
582  
583 #============================================================================================
584  
585 =back
586  
587 =head2 OBJECT METHODS
588  
589 =over 4
590  
591 =cut
592  
593 #============================================================================================
594  
595 #----------------------------------------------------------------------------
596  
597 =item B<filter_run($app, $opts_href, $args_href)>
598  
599 Filter the specified file(s) one at a time.
600
601 =cut
602  
603  
604 sub filter_run
605 11 ▶{
Subroutine filter_run: called CC=14 SCAR=26.5
Pod documented
606 11 my $this = shift ;
607 11 my ($app, $opts_href, $args_href) = @_ ;
608  
609 ## save for later
610 11 $this->_filter_opts($opts_href) ;
611  
612 11 $this->_dbg_prt(["Args=", $args_href, "Opts=", $opts_href]) ;
613
614 # Get command line arguments
615 11 ▶ my @args = @{ $args_href->{'file'} || [] } ;
Condition50%1/2
$args_href->{'file'} || []
l!l
110
MC/DC0%0/1
$args_href->{'file'} || []
$args_href->{'file'}
Truth table50%1/2
A
A= $args_href->{'file'}
Aresult
11
00
616 11 ▶ my @args_fh = @{ $args_href->{'file_fh'} || [] } ;
Condition50%1/2
$args_href->{'file_fh'} || []
l!l
110
MC/DC0%0/1
$args_href->{'file_fh'} || []
$args_href->{'file_fh'}
Truth table50%1/2
A
A= $args_href->{'file_fh'}
Aresult
11
00
617  
618 ## check for in-place editing on STDIN
619 11 ▶ if ($opts_href->{inplace})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_href->{"inplace"}) 1 10 11
620 {
621 1 ▶ if ( (scalar(@args) == 1) && ($args_fh[0] == \*STDIN) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1 and $args_fh[0] == \*STDIN) 0 1 1
Condition33%1/3
scalar @args == 1 and $args_fh[0] == \*STDIN
!ll&&!rl&&r
010
MC/DC0%0/2
scalar @args == 1 and $args_fh[0] == \*STDIN
scalar @args == 1 $args_fh[0] == \*STDIN
Truth table33%1/3
A and B
A= scalar @args == 1B= $args_fh[0] == \*STDIN
ABresult
0X0
100
111
622 {
623 0 $this->throw_fatal("Cannot do in-place editing of standard input") ;
624 }
625 }
626  
627 11 $this->_dispatch_entry_features(@_) ;
628  
629 #$this->debug(2) ;
630  
631 11 $this->_dbg_prt(["#!# Hello, Ive started filter_run()...\n"]) ;
632  
633 ## Update from options
634 11 $this->feature('Options')->obj_vars($this, [keys %FIELDS]) ;
635  
636 ## Set up filter state
637 11 my $state_href = $this->_filter_state ;
638 11 $state_href->{num_files} = scalar(@args) ;
639 11 $state_href->{file_number} = 1 ;
640 11 $state_href->{file_list} = \@args ;
641 11 $state_href->{vars} = {} ;
642  
643 ## do each file
644 11 ▶ for (my $fnum=0; $fnum < $state_href->{num_files}; ++$fnum)
Branch: 1 branch
BranchTrueFalseTotal
if ($fnum < $state_href->{"num_files"}) 11 11 22
645 {
646  
647 11 $state_href->{file_number} = $fnum+1 ;
648 11 $state_href->{outfile} = '' ;
649 11 $state_href->{line_num} = 1 ;
650 11 $state_href->{output_lines} = [] ;
651 11 $state_href->{file} = $args[$fnum] ;
652  
653 11 $this->_dispatch_label_entry_features('file', $app, $opts_href, $state_href) ;
654
655 11 $this->_start_output($state_href, $opts_href) ;
656
657 ## call application start
658 11 $this->call_extend_fn('app_start_fn', $state_href) ;
659  
660 ## Process file
661 11 my $fh = $args_fh[$fnum] ;
662 11 my $line ;
663 11 ▶ while(defined($line = <$fh>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 484 11 495
664 {
665 484 chomp $line ;
666
667 ## see if line needs processing
668 484 ▶ if ($opts_href->{trim_space})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_href->{"trim_space"}) 220 264 484
669 {
670 220 $line =~ s/^\s+// ;
671 220 $line =~ s/\s+$// ;
672 }
673 484 ▶ if ($opts_href->{trim_comment} && $opts_href->{comment})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_href->{"trim_comment"} and $opts_href->{"comment"}) 220 264 484
Condition66%2/3
$opts_href->{"trim_comment"} and $opts_href->{"comment"}
!ll&&!rl&&r
2640220
MC/DC50%1/2
$opts_href->{"trim_comment"} and $opts_href->{"comment"}
$opts_href->{"trim_comment"} $opts_href->{"comment"}
Truth table66%2/3
A and B
A= $opts_href->{"trim_comment"}B= $opts_href->{"comment"}
ABresult
0X0
100
111
674 {
675 220 $line =~ s/$opts_href->{comment}.*$// ;
676 }
677
678 484 $state_href->{line} = $line ;
679 484 $state_href->{output} = undef ;
680
681 484 $this->_dispatch_label_entry_features('line', $app, $opts_href, $state_href) ;
682  
683 ## see if we skip this line
684 484 my $skip = 0 ;
685 484 ▶ if ($opts_href->{skip_empty})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_href->{"skip_empty"}) 220 264 484
686 {
687 220 ▶ $skip=1 if $line =~ m/^\s*$/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 78 142 220
688 }
689  
690 ## call application (if not skipped)
691 484 ▶ $this->call_extend_fn('app_fn', $state_href, $line) unless $skip ;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 406 78 484
692
693 484 $this->_handle_output($state_href, $opts_href) ;
694  
695 484 $state_href->{line_num}++ ;
696  
697 484 $this->_dispatch_label_exit_features('line', $app, $opts_href, $state_href) ;
698 }
699 11 close $fh ;
700  
701 ## call application end
702 11 $this->call_extend_fn('app_end_fn', $state_href) ;
703  
704 11 $this->_end_output($state_href, $opts_href) ;
705  
706 # $state_href->{file_number}++ ;
707  
708 11 $this->_dispatch_label_exit_features('file', $app, $opts_href, $state_href) ;
709 }
710  
711 11 $this->_dispatch_exit_features(@_) ;
712  
713 }
714  
715  
716 #----------------------------------------------------------------------------
717 # start
718 sub _filter_start
719 11 ▶{
Subroutine _filter_start: called CC=1 SCAR=0.0
720 11 my $this = shift ;
721 11 my ($app, $opts_href, @args) = @_ ;
722  
723 ## Do nothing
724  
725 }
726  
727 #----------------------------------------------------------------------------
728 # end
729 sub _filter_end
730 11 ▶{
Subroutine _filter_end: called CC=1 SCAR=0.0
731 11 my $this = shift ;
732 11 my ($app, $opts_href, @args) = @_ ;
733  
734 ## Do nothing
735 }
736  
737 #----------------------------------------------------------------------------
738  
739 =item B<write_output($output)>
740  
741 Application interface for writing out extra lines
742
743 =cut
744  
745  
746 sub write_output
747 0 ▶{
Subroutine write_output: not called CC=1 SCAR=6.9
Pod documented
748 0 my $this = shift ;
749 0 my ($output) = @_ ;
750
751 0 my $state_href = $this->_filter_state ;
752 0 $state_href->{'output'} = $output ;
753
754 0 $this->_handle_output($state_href, $this->_filter_opts) ;
755 }
756  
757  
758 # ============================================================================================
759 # PRIVATE METHODS
760 # ============================================================================================
761  
762 #----------------------------------------------------------------------------
763  
764 =item B<_start_output($state_href, $opts_href)>
765  
766 Start of output file
767
768 =cut
769  
770  
771 sub _start_output
772 11 ▶{
Subroutine _start_output: called CC=3 SCAR=11.0
773 11 my $this = shift ;
774 11 my ($state_href, $opts_href) = @_ ;
775  
776 11 $this->set('out_fh' => undef) ;
777  
778 11 $this->_dbg_prt(["_start_output\n"]) ;
779
780 ## do nothing if buffering or in-place editing
781 11 ▶ return if ($this->buffer || $this->inplace) ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->buffer or $this->inplace 1 10 11
Condition66%2/3
$this->buffer or $this->inplace
l!l&&r!l&&!r
0110
MC/DC50%1/2
$this->buffer or $this->inplace
$this->buffer $this->inplace
Truth table66%2/3
A or B
A= $this->bufferB= $this->inplace
ABresult
1X1
011
000
782  
783 10 $this->_dbg_prt([" + not buffering/inplace\n"]) ;
784  
785 # open output file (and set up output dir)
786 10 $this->_open_output($state_href, $opts_href) ;
787
788 }
789  
790 #----------------------------------------------------------------------------
791  
792 =item B<_handle_output($state_href, $opts_href)>
793  
794 Write out line (if required)
795
796 =cut
797  
798  
799 sub _handle_output
800 484 ▶{
Subroutine _handle_output: called CC=5 SCAR=16.1
801 484 my $this = shift ;
802 484 my ($state_href, $opts_href) = @_ ;
803  
804 ## buffer line(s)
805 484 my $out = $state_href->{output} ;
806 484 $this->_dbg_prt(["_handle_output : output=", $out, "\n"]) ;
807 484 ▶ push @{$state_href->{output_lines}}, $out if defined($out) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out 406 78 484
808  
809 ## do nothing if buffering or in-place editing
810 484 ▶ return if ($this->buffer || $this->inplace) ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->buffer or $this->inplace 44 440 484
Condition66%2/3
$this->buffer or $this->inplace
l!l&&r!l&&!r
044440
MC/DC50%1/2
$this->buffer or $this->inplace
$this->buffer $this->inplace
Truth table66%2/3
A or B
A= $this->bufferB= $this->inplace
ABresult
1X1
011
000
811  
812 440 $this->_dbg_prt([" + not buffering/inplace\n"]) ;
813  
814 ## ok to write
815 440 ▶ $this->_wr_output($state_href, $opts_href, $out) if defined($out) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out 378 62 440
816 }
817  
818  
819 #----------------------------------------------------------------------------
820  
821 =item B<_end_output($state_href, $opts_href)>
822  
823 End of output file
824
825 =cut
826  
827  
828 sub _end_output
829 11 ▶{
Subroutine _end_output: called CC=4 SCAR=13.9
830 11 my $this = shift ;
831 11 my ($state_href, $opts_href) = @_ ;
832  
833 11 $this->_dbg_prt(["_end_output : buffer=", $this->buffer, ", inplace=", $this->inplace, ", # lines=", scalar(@{$state_href->{output_lines}}),"\n"]) ;
834  
835 ## if buffering or in-place editing, now need to write file
836 11 ▶ if ($this->buffer || $this->inplace)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->buffer or $this->inplace) 1 10 11
Condition66%2/3
$this->buffer or $this->inplace
l!l&&r!l&&!r
0110
MC/DC50%1/2
$this->buffer or $this->inplace
$this->buffer $this->inplace
Truth table66%2/3
A or B
A= $this->bufferB= $this->inplace
ABresult
1X1
011
000
837 {
838 1 $this->_dbg_prt([" + writing\n"]) ;
839  
840 # open output file (and set up output dir)
841 1 $this->_open_output($state_href, $opts_href) ;
842  
843 1 foreach my $line (@{$state_href->{output_lines}})
844 {
845 28 $this->_wr_output($state_href, $opts_href, $line) ;
846 }
847 }
848
849 # close output file
850 11 $this->_close_output($state_href, $opts_href) ;
851 }
852  
853  
854  
855 #----------------------------------------------------------------------------
856  
857 =item B<_open_output($state_href, $opts_href)>
858  
859 Open the file (or STDOUT) depending on settings
860
861 =cut
862  
863  
864 sub _open_output
865 11 ▶{
Subroutine _open_output: called CC=8 SCAR=20.8
866 11 my $this = shift ;
867 11 my ($state_href, $opts_href) = @_ ;
868  
869 11 $this->set('out_fh' => undef) ;
870  
871 11 $this->_dbg_prt(["_open_output\n"]) ;
872
873 11 my $outfile ;
874 11 ▶ if ($this->inplace)
Branch: 2 branches
BranchTrueFalseTotal
if ($this->inplace) { } 1 10 11
elsif ($this->outfile) { } 5 5 10
875 {
876 ## Handle in-place editing
877 1 $outfile = $state_href->{file} ;
878 1 $this->_dbg_prt([" + inplace file=$outfile\n"]) ;
879 }
880 elsif ($this->outfile)
881 {
882 ## See if writing to dir
883 5 my $dir = $this->outdir ;
884 5 ▶ if ($dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 5 5
885 {
886 ## create path
887 0 mkpath([$dir], $this->debug, 0755) ;
888 }
889 5 ▶ $dir ||= '.' ;
Condition50%1/2
$dir ||= "."
l!l
05
MC/DC0%0/1
$dir ||= "."
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
890
891 5 my %opts = $this->options() ;
892 5 my %app_vars = $this->vars() ;
893 5 my %filter ;
894 5 $filter{'filter_fmt'} = $this->outfile ;
895 5 $filter{'filter_file'} = $state_href->{file} ;
896 5 $filter{'filter_filenum'} = $state_href->{file_number} ;
897 5 my ($base, $path, $ext) = fileparse($state_href->{file}, '\..*') ;
898 5 $filter{'filter_name'} = $base ;
899 5 $filter{'filter_base'} = $base ;
900 5 $filter{'filter_path'} = $path ;
901 5 $filter{'filter_ext'} = $ext ;
902  
903 5 $this->expand_keys(\%filter, [\%opts, \%app_vars, \%ENV]) ;
904
905 5 $outfile = $filter{'filter_fmt'} ;
906
907 5 $this->_dbg_prt([" + eval=$@\n"]) ;
908 5 $this->_dbg_prt([" + outfile=$outfile: dir=$dir fmt=$filter{'filter_fmt'} file=$filter{'filter_file'} num=$filter{'filter_filenum'} base=$base path=$path\n"]) ;
909
910 5 $outfile = File::Spec->catfile($dir, $outfile) ;
911 }
912
913 ## Output file specified?
914 11 ▶ if ($outfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) { } 6 5 11
915 {
916 6 $outfile = File::Spec->rel2abs($outfile) ;
917 6 my $infile = File::Spec->rel2abs($state_href->{file}) ;
918  
919 6 ▶ if ($outfile eq $infile)
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile eq $infile) 1 5 6
920 {
921 # In place editing - make sure flag is set
922 1 $this->inplace(1) ;
923  
924 1 $this->_dbg_prt([" + inplace $outfile\n"]) ;
925 }
926  
927 # else
928 # {
929 ## Open output
930 6 ▶ open my $outfh, ">$outfile" or $this->throw_fatal("Unable to write \"$outfile\" : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
open my $outfh, ">$outfile" or $this->throw_fatal(qq[Unable to write "$outfile" : $!]) 6 0 6
931 6 $this->out_fh($outfh) ;
932  
933 6 $this->_dbg_prt([" + opened $outfile fh=$outfh\n"]) ;
934
935 6 $state_href->{outfile} = $outfile ;
936 # }
937
938 }
939 else
940 {
941 ## STDOUT
942 5 $this->out_fh(\*STDOUT) ;
943 }
944 }
945  
946 #----------------------------------------------------------------------------
947  
948 =item B<_close_output($state_href, $opts_href)>
949  
950 Close the file if open
951
952 =cut
953  
954  
955 sub _close_output
956 11 ▶{
Subroutine _close_output: called CC=2 SCAR=6.9
957 11 my $this = shift ;
958 11 my ($state_href, $opts_href) = @_ ;
959  
960 11 my $fh = $this->out_fh ;
961 11 $this->set('out_fh' => undef) ;
962
963 11 ▶ if ($this->outfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->outfile) { } 6 5 11
964 {
965 6 close $fh ;
966 }
967 else
968 {
969 ## STDOUT - so ignore
970 }
971 }
972  
973 #----------------------------------------------------------------------------
974  
975 =item B<_wr_output($state_href, $opts_href, $line)>
976  
977 End of output file
978
979 =cut
980  
981  
982 sub _wr_output
983 406 ▶{
Subroutine _wr_output: called CC=2 SCAR=7.0
984 406 my $this = shift ;
985 406 my ($state_href, $opts_href, $line) = @_ ;
986  
987 406 my $fh = $this->out_fh ;
988  
989 406 $this->_dbg_prt(["_wr_output($line) fh=$fh\n"]) ;
990 406 ▶ if ($fh)
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 406 0 406
991 {
992 406 print $fh "$line\n" ;
993 }
994 }
995  
996  
997 # ============================================================================================
998 # END OF PACKAGE
999  
1000 =back
1001  
1002 =head1 DIAGNOSTICS
1003  
1004 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
1005  
1006 =head1 AUTHOR
1007  
1008 Steve Price C<< <sdprice at cpan.org> >>
1009  
1010 =head1 BUGS
1011  
1012 None that I know of!
1013  
1014 =cut
1015  
1016 7 1;
1017  
1018 __END__
« Framework/Feature/Options.pm ↑ Summary Framework/Feature/Logging.pm »