1Call(3)               User Contributed Perl Documentation              Call(3)
2
3
4

NAME

6       Filter::Util::Call - Perl Source Filter Utility Module
7

SYNOPSIS

9           use Filter::Util::Call ;
10

DESCRIPTION

12       This module provides you with the framework to write Source Filters in
13       Perl.
14
15       An alternate interface to Filter::Util::Call is now available. See
16       Filter::Simple for more details.
17
18       A Perl Source Filter is implemented as a Perl module. The structure of
19       the module can take one of two broadly similar formats. To distinguish
20       between them, the first will be referred to as method filter and the
21       second as closure filter.
22
23       Here is a skeleton for the method filter:
24
25           package MyFilter ;
26
27           use Filter::Util::Call ;
28
29           sub import
30           {
31               my($type, @arguments) = @_ ;
32               filter_add([]) ;
33           }
34
35           sub filter
36           {
37               my($self) = @_ ;
38               my($status) ;
39
40               $status = filter_read() ;
41               $status ;
42           }
43
44           1 ;
45
46       and this is the equivalent skeleton for the closure filter:
47
48           package MyFilter ;
49
50           use Filter::Util::Call ;
51
52           sub import
53           {
54               my($type, @arguments) = @_ ;
55
56               filter_add(
57                   sub
58                   {
59                       my($status) ;
60                       $status = filter_read() ;
61                       $status ;
62                   } )
63           }
64
65           1 ;
66
67       To make use of either of the two filter modules above, place the line
68       below in a Perl source file.
69
70           use MyFilter;
71
72       In fact, the skeleton modules shown above are fully functional Source
73       Filters, albeit fairly useless ones. All they does is filter the source
74       stream without modifying it at all.
75
76       As you can see both modules have a broadly similar structure. They both
77       make use of the "Filter::Util::Call" module and both have an "import"
78       method. The difference between them is that the method filter requires
79       a filter method, whereas the closure filter gets the equivalent of a
80       filter method with the anonymous sub passed to filter_add.
81
82       To make proper use of the closure filter shown above you need to have a
83       good understanding of the concept of a closure. See perlref for more
84       details on the mechanics of closures.
85
86   use Filter::Util::Call
87       The following functions are exported by "Filter::Util::Call":
88
89           filter_add()
90           filter_read()
91           filter_read_exact()
92           filter_del()
93
94   import()
95       The "import" method is used to create an instance of the filter. It is
96       called indirectly by Perl when it encounters the "use MyFilter" line in
97       a source file (See "import" in perlfunc for more details on "import").
98
99       It will always have at least one parameter automatically passed by Perl
100       - this corresponds to the name of the package. In the example above it
101       will be "MyFilter".
102
103       Apart from the first parameter, import can accept an optional list of
104       parameters. These can be used to pass parameters to the filter. For
105       example:
106
107           use MyFilter qw(a b c) ;
108
109       will result in the @_ array having the following values:
110
111           @_ [0] => "MyFilter"
112           @_ [1] => "a"
113           @_ [2] => "b"
114           @_ [3] => "c"
115
116       Before terminating, the "import" function must explicitly install the
117       filter by calling "filter_add".
118
119   filter_add()
120       The function, "filter_add", actually installs the filter. It takes one
121       parameter which should be a reference. The kind of reference used will
122       dictate which of the two filter types will be used.
123
124       If a CODE reference is used then a closure filter will be assumed.
125
126       If a CODE reference is not used, a method filter will be assumed.  In a
127       method filter, the reference can be used to store context information.
128       The reference will be blessed into the package by "filter_add".
129
130       See the filters at the end of this documents for examples of using
131       context information using both method filters and closure filters.
132
133   filter() and anonymous sub
134       Both the "filter" method used with a method filter and the anonymous
135       sub used with a closure filter is where the main processing for the
136       filter is done.
137
138       The big difference between the two types of filter is that the method
139       filter uses the object passed to the method to store any context data,
140       whereas the closure filter uses the lexical variables that are
141       maintained by the closure.
142
143       Note that the single parameter passed to the method filter, $self, is
144       the same reference that was passed to "filter_add" blessed into the
145       filter's package. See the example filters later on for details of using
146       $self.
147
148       Here is a list of the common features of the anonymous sub and the
149       "filter()" method.
150
151       $_   Although $_ doesn't actually appear explicitly in the sample
152            filters above, it is implicitly used in a number of places.
153
154            Firstly, when either "filter" or the anonymous sub are called, a
155            local copy of $_ will automatically be created. It will always
156            contain the empty string at this point.
157
158            Next, both "filter_read" and "filter_read_exact" will append any
159            source data that is read to the end of $_.
160
161            Finally, when "filter" or the anonymous sub are finished
162            processing, they are expected to return the filtered source using
163            $_.
164
165            This implicit use of $_ greatly simplifies the filter.
166
167       $status
168            The status value that is returned by the user's "filter" method or
169            anonymous sub and the "filter_read" and "read_exact" functions
170            take the same set of values, namely:
171
172                < 0  Error
173                = 0  EOF
174                > 0  OK
175
176       filter_read and filter_read_exact
177            These functions are used by the filter to obtain either a line or
178            block from the next filter in the chain or the actual source file
179            if there aren't any other filters.
180
181            The function "filter_read" takes two forms:
182
183                $status = filter_read() ;
184                $status = filter_read($size) ;
185
186            The first form is used to request a line, the second requests a
187            block.
188
189            In line mode, "filter_read" will append the next source line to
190            the end of the $_ scalar.
191
192            In block mode, "filter_read" will append a block of data which is
193            <= $size to the end of the $_ scalar. It is important to emphasise
194            the that "filter_read" will not necessarily read a block which is
195            precisely $size bytes.
196
197            If you need to be able to read a block which has an exact size,
198            you can use the function "filter_read_exact". It works identically
199            to "filter_read" in block mode, except it will try to read a block
200            which is exactly $size bytes in length. The only circumstances
201            when it will not return a block which is $size bytes long is on
202            EOF or error.
203
204            It is very important to check the value of $status after every
205            call to "filter_read" or "filter_read_exact".
206
207       filter_del
208            The function, "filter_del", is used to disable the current filter.
209            It does not affect the running of the filter. All it does is tell
210            Perl not to call filter any more.
211
212            See "Example 4: Using filter_del" for details.
213
214       real_import
215            Internal function which adds the filter, based on the filter_add
216            argument type.
217

EXAMPLES

219       Here are a few examples which illustrate the key concepts - as such
220       most of them are of little practical use.
221
222       The "examples" sub-directory has copies of all these filters
223       implemented both as method filters and as closure filters.
224
225   Example 1: A simple filter.
226       Below is a method filter which is hard-wired to replace all occurrences
227       of the string "Joe" to "Jim". Not particularly Useful, but it is the
228       first example and I wanted to keep it simple.
229
230           package Joe2Jim ;
231
232           use Filter::Util::Call ;
233
234           sub import
235           {
236               my($type) = @_ ;
237
238               filter_add(bless []) ;
239           }
240
241           sub filter
242           {
243               my($self) = @_ ;
244               my($status) ;
245
246               s/Joe/Jim/g
247                   if ($status = filter_read()) > 0 ;
248               $status ;
249           }
250
251           1 ;
252
253       Here is an example of using the filter:
254
255           use Joe2Jim ;
256           print "Where is Joe?\n" ;
257
258       And this is what the script above will print:
259
260           Where is Jim?
261
262   Example 2: Using the context
263       The previous example was not particularly useful. To make it more
264       general purpose we will make use of the context data and allow any
265       arbitrary from and to strings to be used. This time we will use a
266       closure filter. To reflect its enhanced role, the filter is called
267       "Subst".
268
269           package Subst ;
270
271           use Filter::Util::Call ;
272           use Carp ;
273
274           sub import
275           {
276               croak("usage: use Subst qw(from to)")
277                   unless @_ == 3 ;
278               my ($self, $from, $to) = @_ ;
279               filter_add(
280                   sub
281                   {
282                       my ($status) ;
283                       s/$from/$to/
284                           if ($status = filter_read()) > 0 ;
285                       $status ;
286                   })
287           }
288           1 ;
289
290       and is used like this:
291
292           use Subst qw(Joe Jim) ;
293           print "Where is Joe?\n" ;
294
295   Example 3: Using the context within the filter
296       Here is a filter which a variation of the "Joe2Jim" filter. As well as
297       substituting all occurrences of "Joe" to "Jim" it keeps a count of the
298       number of substitutions made in the context object.
299
300       Once EOF is detected ($status is zero) the filter will insert an extra
301       line into the source stream. When this extra line is executed it will
302       print a count of the number of substitutions actually made.  Note that
303       $status is set to 1 in this case.
304
305           package Count ;
306
307           use Filter::Util::Call ;
308
309           sub filter
310           {
311               my ($self) = @_ ;
312               my ($status) ;
313
314               if (($status = filter_read()) > 0 ) {
315                   s/Joe/Jim/g ;
316                   ++ $$self ;
317               }
318               elsif ($$self >= 0) { # EOF
319                   $_ = "print q[Made ${$self} substitutions\n]" ;
320                   $status = 1 ;
321                   $$self = -1 ;
322               }
323
324               $status ;
325           }
326
327           sub import
328           {
329               my ($self) = @_ ;
330               my ($count) = 0 ;
331               filter_add(\$count) ;
332           }
333
334           1 ;
335
336       Here is a script which uses it:
337
338           use Count ;
339           print "Hello Joe\n" ;
340           print "Where is Joe\n" ;
341
342       Outputs:
343
344           Hello Jim
345           Where is Jim
346           Made 2 substitutions
347
348   Example 4: Using filter_del
349       Another variation on a theme. This time we will modify the "Subst"
350       filter to allow a starting and stopping pattern to be specified as well
351       as the from and to patterns. If you know the vi editor, it is the
352       equivalent of this command:
353
354           :/start/,/stop/s/from/to/
355
356       When used as a filter we want to invoke it like this:
357
358           use NewSubst qw(start stop from to) ;
359
360       Here is the module.
361
362           package NewSubst ;
363
364           use Filter::Util::Call ;
365           use Carp ;
366
367           sub import
368           {
369               my ($self, $start, $stop, $from, $to) = @_ ;
370               my ($found) = 0 ;
371               croak("usage: use Subst qw(start stop from to)")
372                   unless @_ == 5 ;
373
374               filter_add(
375                   sub
376                   {
377                       my ($status) ;
378
379                       if (($status = filter_read()) > 0) {
380
381                           $found = 1
382                               if $found == 0 and /$start/ ;
383
384                           if ($found) {
385                               s/$from/$to/ ;
386                               filter_del() if /$stop/ ;
387                           }
388
389                       }
390                       $status ;
391                   } )
392
393           }
394
395           1 ;
396

Filter::Simple

398       If you intend using the Filter::Call functionality, I would strongly
399       recommend that you check out Damian Conway's excellent Filter::Simple
400       module. Damian's module provides a much cleaner interface than
401       Filter::Util::Call. Although it doesn't allow the fine control that
402       Filter::Util::Call does, it should be adequate for the majority of
403       applications. It's available at
404
405          http://search.cpan.org/dist/Filter-Simple/
406

AUTHOR

408       Paul Marquess
409

DATE

411       26th January 1996
412
413
414
415perl v5.16.3                      2013-04-02                           Call(3)
Impressum