| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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
|
|
|
|
|
|
|
|
|
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) |
|
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). |
|
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 |
|
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 and L) |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item I<$state_href> - HASH ref to state |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item B |
|
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 and L) |
|
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 |
|
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 and L) |
|
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 |
|
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 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 |
|
153
|
|
|
|
|
|
|
option has been specified, then all output lines are buffered (into the state variable L) then written out at the end of processing all input. Similarly, if the L |
|
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 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 - Input full file path |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item I - Basename of input file (excluding extension) |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item I - Alias for L |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item I - Directory path of input file |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item I - Extension of input file |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item I - 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/ |
|
295
|
|
|
|
|
|
|
** |
|
296
|
|
|
|
|
|
|
** |
|
297
|
|
|
|
|
|
|
** |
|
298
|
|
|
|
|
|
|
.. |
|
299
|
|
|
|
|
|
|
** |
|
300
|
|
|
|
|
|
|
** |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
File ListHere is a list of all files with brief descriptions: |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
** Generated on Fri Jun 5 13:43:31 2009 for rctu4_test by |
|
314
|
|
|
|
|
|
|
** |
|
315
|
|
|
|
|
|
|
** 1.5.5 |
|
316
|
|
|
|
|
|
|
|