This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update to Filter::Util::Call 1.6 from CPAN, but keep our
[perl5.git] / ext / Filter / Util / Call / Call.pm
CommitLineData
6ea26ce9
JH
1
2# Call.pm
3#
4# Copyright (c) 1995-2001 Paul Marquess. All rights reserved.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the same terms as Perl itself.
8
2c4bb738
JH
9package Filter::Util::Call ;
10
11require 5.002 ;
12require DynaLoader;
13require Exporter;
14use Carp ;
15use strict;
4176d4e4 16use warnings;
2c4bb738
JH
17use vars qw($VERSION @ISA @EXPORT) ;
18
19@ISA = qw(Exporter DynaLoader);
20@EXPORT = qw( filter_add filter_del filter_read filter_read_exact) ;
7de85b62 21$VERSION = "1.0601" ;
2c4bb738
JH
22
23sub filter_read_exact($)
24{
25 my ($size) = @_ ;
26 my ($left) = $size ;
27 my ($status) ;
28
29 croak ("filter_read_exact: size parameter must be > 0")
30 unless $size > 0 ;
31
32 # try to read a block which is exactly $size bytes long
33 while ($left and ($status = filter_read($left)) > 0) {
34 $left = $size - length $_ ;
35 }
36
37 # EOF with pending data is a special case
38 return 1 if $status == 0 and length $_ ;
39
40 return $status ;
41}
42
43sub filter_add($)
44{
45 my($obj) = @_ ;
46
47 # Did we get a code reference?
48 my $coderef = (ref $obj eq 'CODE') ;
49
50 # If the parameter isn't already a reference, make it one.
51 $obj = \$obj unless ref $obj ;
52
53 $obj = bless ($obj, (caller)[0]) unless $coderef ;
54
55 # finish off the installation of the filter in C.
56 Filter::Util::Call::real_import($obj, (caller)[0], $coderef) ;
57}
58
59bootstrap Filter::Util::Call ;
60
611;
62__END__
63
64=head1 NAME
65
66Filter::Util::Call - Perl Source Filter Utility Module
67
a5de3055 68=head1 SYNOPSIS
bbc7dcd2 69
a5de3055
PM
70 use Filter::Util::Call ;
71
2c4bb738
JH
72=head1 DESCRIPTION
73
74This module provides you with the framework to write I<Source Filters>
6ea26ce9
JH
75in Perl.
76
77An alternate interface to Filter::Util::Call is now available. See
78L<Filter::Simple> for more details.
2c4bb738
JH
79
80A I<Perl Source Filter> is implemented as a Perl module. The structure
81of the module can take one of two broadly similar formats. To
82distinguish between them, the first will be referred to as I<method
83filter> and the second as I<closure filter>.
84
85Here is a skeleton for the I<method filter>:
86
87 package MyFilter ;
bbc7dcd2 88
2c4bb738
JH
89 use Filter::Util::Call ;
90
91 sub import
92 {
93 my($type, @arguments) = @_ ;
94 filter_add([]) ;
95 }
bbc7dcd2 96
2c4bb738
JH
97 sub filter
98 {
99 my($self) = @_ ;
100 my($status) ;
bbc7dcd2 101
2c4bb738
JH
102 $status = filter_read() ;
103 $status ;
104 }
bbc7dcd2 105
2c4bb738
JH
106 1 ;
107
108and this is the equivalent skeleton for the I<closure filter>:
109
110 package MyFilter ;
bbc7dcd2 111
2c4bb738
JH
112 use Filter::Util::Call ;
113
114 sub import
115 {
116 my($type, @arguments) = @_ ;
bbc7dcd2 117
2c4bb738
JH
118 filter_add(
119 sub
120 {
121 my($status) ;
122 $status = filter_read() ;
123 $status ;
124 } )
125 }
bbc7dcd2 126
2c4bb738
JH
127 1 ;
128
129To make use of either of the two filter modules above, place the line
130below in a Perl source file.
131
132 use MyFilter;
133
134In fact, the skeleton modules shown above are fully functional I<Source
135Filters>, albeit fairly useless ones. All they does is filter the
136source stream without modifying it at all.
137
138As you can see both modules have a broadly similar structure. They both
139make use of the C<Filter::Util::Call> module and both have an C<import>
140method. The difference between them is that the I<method filter>
141requires a I<filter> method, whereas the I<closure filter> gets the
142equivalent of a I<filter> method with the anonymous sub passed to
143I<filter_add>.
144
145To make proper use of the I<closure filter> shown above you need to
146have a good understanding of the concept of a I<closure>. See
147L<perlref> for more details on the mechanics of I<closures>.
148
149=head2 B<use Filter::Util::Call>
150
151The following functions are exported by C<Filter::Util::Call>:
152
153 filter_add()
154 filter_read()
155 filter_read_exact()
156 filter_del()
157
158=head2 B<import()>
159
160The C<import> method is used to create an instance of the filter. It is
161called indirectly by Perl when it encounters the C<use MyFilter> line
162in a source file (See L<perlfunc/import> for more details on
163C<import>).
164
165It will always have at least one parameter automatically passed by Perl
166- this corresponds to the name of the package. In the example above it
167will be C<"MyFilter">.
168
169Apart from the first parameter, import can accept an optional list of
170parameters. These can be used to pass parameters to the filter. For
171example:
172
173 use MyFilter qw(a b c) ;
174
175will result in the C<@_> array having the following values:
176
177 @_ [0] => "MyFilter"
178 @_ [1] => "a"
179 @_ [2] => "b"
180 @_ [3] => "c"
181
182Before terminating, the C<import> function must explicitly install the
183filter by calling C<filter_add>.
184
185B<filter_add()>
186
187The function, C<filter_add>, actually installs the filter. It takes one
188parameter which should be a reference. The kind of reference used will
189dictate which of the two filter types will be used.
190
191If a CODE reference is used then a I<closure filter> will be assumed.
192
193If a CODE reference is not used, a I<method filter> will be assumed.
194In a I<method filter>, the reference can be used to store context
195information. The reference will be I<blessed> into the package by
196C<filter_add>.
197
198See the filters at the end of this documents for examples of using
199context information using both I<method filters> and I<closure
200filters>.
201
202=head2 B<filter() and anonymous sub>
203
204Both the C<filter> method used with a I<method filter> and the
205anonymous sub used with a I<closure filter> is where the main
206processing for the filter is done.
207
208The big difference between the two types of filter is that the I<method
209filter> uses the object passed to the method to store any context data,
210whereas the I<closure filter> uses the lexical variables that are
211maintained by the closure.
212
213Note that the single parameter passed to the I<method filter>,
214C<$self>, is the same reference that was passed to C<filter_add>
215blessed into the filter's package. See the example filters later on for
216details of using C<$self>.
217
218Here is a list of the common features of the anonymous sub and the
219C<filter()> method.
220
221=over 5
222
223=item B<$_>
224
225Although C<$_> doesn't actually appear explicitly in the sample filters
226above, it is implicitly used in a number of places.
227
228Firstly, when either C<filter> or the anonymous sub are called, a local
229copy of C<$_> will automatically be created. It will always contain the
230empty string at this point.
231
232Next, both C<filter_read> and C<filter_read_exact> will append any
233source data that is read to the end of C<$_>.
234
235Finally, when C<filter> or the anonymous sub are finished processing,
236they are expected to return the filtered source using C<$_>.
237
238This implicit use of C<$_> greatly simplifies the filter.
239
240=item B<$status>
241
242The status value that is returned by the user's C<filter> method or
243anonymous sub and the C<filter_read> and C<read_exact> functions take
244the same set of values, namely:
245
246 < 0 Error
247 = 0 EOF
248 > 0 OK
249
250=item B<filter_read> and B<filter_read_exact>
251
252These functions are used by the filter to obtain either a line or block
253from the next filter in the chain or the actual source file if there
254aren't any other filters.
255
256The function C<filter_read> takes two forms:
257
258 $status = filter_read() ;
259 $status = filter_read($size) ;
260
261The first form is used to request a I<line>, the second requests a
262I<block>.
263
264In line mode, C<filter_read> will append the next source line to the
265end of the C<$_> scalar.
266
267In block mode, C<filter_read> will append a block of data which is <=
268C<$size> to the end of the C<$_> scalar. It is important to emphasise
269the that C<filter_read> will not necessarily read a block which is
270I<precisely> C<$size> bytes.
271
272If you need to be able to read a block which has an exact size, you can
273use the function C<filter_read_exact>. It works identically to
274C<filter_read> in block mode, except it will try to read a block which
275is exactly C<$size> bytes in length. The only circumstances when it
276will not return a block which is C<$size> bytes long is on EOF or
277error.
278
279It is I<very> important to check the value of C<$status> after I<every>
280call to C<filter_read> or C<filter_read_exact>.
281
282=item B<filter_del>
283
284The function, C<filter_del>, is used to disable the current filter. It
285does not affect the running of the filter. All it does is tell Perl not
286to call filter any more.
287
288See L<Example 4: Using filter_del> for details.
289
290=back
291
292=head1 EXAMPLES
293
294Here are a few examples which illustrate the key concepts - as such
295most of them are of little practical use.
296
297The C<examples> sub-directory has copies of all these filters
298implemented both as I<method filters> and as I<closure filters>.
299
300=head2 Example 1: A simple filter.
301
302Below is a I<method filter> which is hard-wired to replace all
303occurrences of the string C<"Joe"> to C<"Jim">. Not particularly
304Useful, but it is the first example and I wanted to keep it simple.
305
306 package Joe2Jim ;
bbc7dcd2 307
2c4bb738
JH
308 use Filter::Util::Call ;
309
310 sub import
311 {
312 my($type) = @_ ;
bbc7dcd2 313
2c4bb738
JH
314 filter_add(bless []) ;
315 }
bbc7dcd2 316
2c4bb738
JH
317 sub filter
318 {
319 my($self) = @_ ;
320 my($status) ;
bbc7dcd2 321
2c4bb738
JH
322 s/Joe/Jim/g
323 if ($status = filter_read()) > 0 ;
324 $status ;
325 }
bbc7dcd2 326
2c4bb738
JH
327 1 ;
328
329Here is an example of using the filter:
330
331 use Joe2Jim ;
332 print "Where is Joe?\n" ;
333
334And this is what the script above will print:
335
336 Where is Jim?
337
338=head2 Example 2: Using the context
339
340The previous example was not particularly useful. To make it more
341general purpose we will make use of the context data and allow any
342arbitrary I<from> and I<to> strings to be used. This time we will use a
343I<closure filter>. To reflect its enhanced role, the filter is called
344C<Subst>.
345
346 package Subst ;
bbc7dcd2 347
2c4bb738
JH
348 use Filter::Util::Call ;
349 use Carp ;
bbc7dcd2 350
2c4bb738
JH
351 sub import
352 {
353 croak("usage: use Subst qw(from to)")
354 unless @_ == 3 ;
355 my ($self, $from, $to) = @_ ;
356 filter_add(
357 sub
358 {
359 my ($status) ;
360 s/$from/$to/
361 if ($status = filter_read()) > 0 ;
362 $status ;
363 })
364 }
365 1 ;
366
367and is used like this:
368
369 use Subst qw(Joe Jim) ;
370 print "Where is Joe?\n" ;
371
372
373=head2 Example 3: Using the context within the filter
374
375Here is a filter which a variation of the C<Joe2Jim> filter. As well as
376substituting all occurrences of C<"Joe"> to C<"Jim"> it keeps a count
377of the number of substitutions made in the context object.
378
379Once EOF is detected (C<$status> is zero) the filter will insert an
380extra line into the source stream. When this extra line is executed it
381will print a count of the number of substitutions actually made.
382Note that C<$status> is set to C<1> in this case.
383
384 package Count ;
bbc7dcd2 385
2c4bb738 386 use Filter::Util::Call ;
bbc7dcd2 387
2c4bb738
JH
388 sub filter
389 {
390 my ($self) = @_ ;
391 my ($status) ;
bbc7dcd2 392
2c4bb738
JH
393 if (($status = filter_read()) > 0 ) {
394 s/Joe/Jim/g ;
395 ++ $$self ;
396 }
397 elsif ($$self >= 0) { # EOF
398 $_ = "print q[Made ${$self} substitutions\n]" ;
399 $status = 1 ;
400 $$self = -1 ;
401 }
402
403 $status ;
404 }
bbc7dcd2 405
2c4bb738
JH
406 sub import
407 {
408 my ($self) = @_ ;
409 my ($count) = 0 ;
410 filter_add(\$count) ;
411 }
bbc7dcd2 412
2c4bb738
JH
413 1 ;
414
415Here is a script which uses it:
416
417 use Count ;
418 print "Hello Joe\n" ;
419 print "Where is Joe\n" ;
420
421Outputs:
422
423 Hello Jim
424 Where is Jim
425 Made 2 substitutions
426
427=head2 Example 4: Using filter_del
428
429Another variation on a theme. This time we will modify the C<Subst>
430filter to allow a starting and stopping pattern to be specified as well
431as the I<from> and I<to> patterns. If you know the I<vi> editor, it is
432the equivalent of this command:
433
434 :/start/,/stop/s/from/to/
435
436When used as a filter we want to invoke it like this:
437
438 use NewSubst qw(start stop from to) ;
439
440Here is the module.
441
442 package NewSubst ;
bbc7dcd2 443
2c4bb738
JH
444 use Filter::Util::Call ;
445 use Carp ;
bbc7dcd2 446
2c4bb738
JH
447 sub import
448 {
449 my ($self, $start, $stop, $from, $to) = @_ ;
450 my ($found) = 0 ;
451 croak("usage: use Subst qw(start stop from to)")
452 unless @_ == 5 ;
bbc7dcd2 453
2c4bb738
JH
454 filter_add(
455 sub
456 {
457 my ($status) ;
bbc7dcd2 458
2c4bb738 459 if (($status = filter_read()) > 0) {
bbc7dcd2 460
2c4bb738
JH
461 $found = 1
462 if $found == 0 and /$start/ ;
bbc7dcd2 463
2c4bb738
JH
464 if ($found) {
465 s/$from/$to/ ;
466 filter_del() if /$stop/ ;
467 }
bbc7dcd2 468
2c4bb738
JH
469 }
470 $status ;
471 } )
bbc7dcd2 472
2c4bb738 473 }
bbc7dcd2 474
2c4bb738
JH
475 1 ;
476
6ea26ce9
JH
477=head1 Filter::Simple
478
479If you intend using the Filter::Call functionality, I would strongly
480recommend that you check out Damian Conway's excellent Filter::Simple
66874885 481module. Damian's module provides a much cleaner interface than
6ea26ce9
JH
482Filter::Util::Call. Although it doesn't allow the fine control that
483Filter::Util::Call does, it should be adequate for the majority of
484applications. It's available at
485
486 http://www.cpan.org/modules/by-author/Damian_Conway/Filter-Simple.tar.gz
487 http://www.csse.monash.edu.au/~damian/CPAN/Filter-Simple.tar.gz
488
2c4bb738
JH
489=head1 AUTHOR
490
491Paul Marquess
492
493=head1 DATE
494
49526th January 1996
496
497=cut
498