Debian Codegraph @

upstream/debian-unstable/man3/Filter::Util::Call.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Filter::Util::Call 3perl"
58 .TH Filter::Util::Call 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Filter::Util::Call \- Perl Source Filter Utility Module
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use Filter::Util::Call ;
69 .Ve
70 .SH DESCRIPTION
71 .IX Header "DESCRIPTION"
72 This module provides you with the framework to write \fISource Filters\fR
73 in Perl.
74 .PP
75 An alternate interface to Filter::Util::Call is now available. See
76 Filter::Simple for more details.
77 .PP
78 A \fIPerl Source Filter\fR is implemented as a Perl module. The structure
79 of the module can take one of two broadly similar formats. To
80 distinguish between them, the first will be referred to as \fImethod
81 filter\fR and the second as \fIclosure filter\fR.
82 .PP
83 Here is a skeleton for the \fImethod filter\fR:
84 .PP
85 .Vb 1
86 \& package MyFilter ;
87 \&
88 \& use Filter::Util::Call ;
89 \&
90 \& sub import
91 \& {
92 \& my($type, @arguments) = @_ ;
93 \& filter_add([]) ;
94 \& }
95 \&
96 \& sub filter
97 \& {
98 \& my($self) = @_ ;
99 \& my($status) ;
100 \&
101 \& $status = filter_read() ;
102 \& $status ;
103 \& }
104 \&
105 \& 1 ;
106 .Ve
107 .PP
108 and this is the equivalent skeleton for the \fIclosure filter\fR:
109 .PP
110 .Vb 1
111 \& package MyFilter ;
112 \&
113 \& use Filter::Util::Call ;
114 \&
115 \& sub import
116 \& {
117 \& my($type, @arguments) = @_ ;
118 \&
119 \& filter_add(
120 \& sub
121 \& {
122 \& my($status) ;
123 \& $status = filter_read() ;
124 \& $status ;
125 \& } )
126 \& }
127 \&
128 \& 1 ;
129 .Ve
130 .PP
131 To make use of either of the two filter modules above, place the line
132 below in a Perl source file.
133 .PP
134 .Vb 1
135 \& use MyFilter;
136 .Ve
137 .PP
138 In fact, the skeleton modules shown above are fully functional \fISource
139 Filters\fR, albeit fairly useless ones. All they does is filter the
140 source stream without modifying it at all.
141 .PP
142 As you can see both modules have a broadly similar structure. They both
143 make use of the \f(CW\*(C`Filter::Util::Call\*(C'\fR module and both have an \f(CW\*(C`import\*(C'\fR
144 method. The difference between them is that the \fImethod filter\fR
145 requires a \fIfilter\fR method, whereas the \fIclosure filter\fR gets the
146 equivalent of a \fIfilter\fR method with the anonymous sub passed to
147 \&\fIfilter_add\fR.
148 .PP
149 To make proper use of the \fIclosure filter\fR shown above you need to
150 have a good understanding of the concept of a \fIclosure\fR. See
151 perlref for more details on the mechanics of \fIclosures\fR.
152 .SS "\fBuse Filter::Util::Call\fP"
153 .IX Subsection "use Filter::Util::Call"
154 The following functions are exported by \f(CW\*(C`Filter::Util::Call\*(C'\fR:
155 .PP
156 .Vb 4
157 \& filter_add()
158 \& filter_read()
159 \& filter_read_exact()
160 \& filter_del()
161 .Ve
162 .SS \fBimport()\fP
163 .IX Subsection "import()"
164 The \f(CW\*(C`import\*(C'\fR method is used to create an instance of the filter. It is
165 called indirectly by Perl when it encounters the \f(CW\*(C`use MyFilter\*(C'\fR line
166 in a source file (See "import" in perlfunc for more details on
167 \&\f(CW\*(C`import\*(C'\fR).
168 .PP
169 It will always have at least one parameter automatically passed by Perl
170 \&\- this corresponds to the name of the package. In the example above it
171 will be \f(CW"MyFilter"\fR.
172 .PP
173 Apart from the first parameter, import can accept an optional list of
174 parameters. These can be used to pass parameters to the filter. For
175 example:
176 .PP
177 .Vb 1
178 \& use MyFilter qw(a b c) ;
179 .Ve
180 .PP
181 will result in the \f(CW@_\fR array having the following values:
182 .PP
183 .Vb 4
184 \& @_ [0] => "MyFilter"
185 \& @_ [1] => "a"
186 \& @_ [2] => "b"
187 \& @_ [3] => "c"
188 .Ve
189 .PP
190 Before terminating, the \f(CW\*(C`import\*(C'\fR function must explicitly install the
191 filter by calling \f(CW\*(C`filter_add\*(C'\fR.
192 .SS \fBfilter_add()\fP
193 .IX Subsection "filter_add()"
194 The function, \f(CW\*(C`filter_add\*(C'\fR, actually installs the filter. It takes one
195 parameter which should be a reference. The kind of reference used will
196 dictate which of the two filter types will be used.
197 .PP
198 If a CODE reference is used then a \fIclosure filter\fR will be assumed.
199 .PP
200 If a CODE reference is not used, a \fImethod filter\fR will be assumed.
201 In a \fImethod filter\fR, the reference can be used to store context
202 information. The reference will be \fIblessed\fR into the package by
203 \&\f(CW\*(C`filter_add\*(C'\fR, unless the reference was already blessed.
204 .PP
205 See the filters at the end of this documents for examples of using
206 context information using both \fImethod filters\fR and \fIclosure
207 filters\fR.
208 .SS "\fBfilter() and anonymous sub\fP"
209 .IX Subsection "filter() and anonymous sub"
210 Both the \f(CW\*(C`filter\*(C'\fR method used with a \fImethod filter\fR and the
211 anonymous sub used with a \fIclosure filter\fR is where the main
212 processing for the filter is done.
213 .PP
214 The big difference between the two types of filter is that the \fImethod
215 filter\fR uses the object passed to the method to store any context data,
216 whereas the \fIclosure filter\fR uses the lexical variables that are
217 maintained by the closure.
218 .PP
219 Note that the single parameter passed to the \fImethod filter\fR,
220 \&\f(CW$self\fR, is the same reference that was passed to \f(CW\*(C`filter_add\*(C'\fR
221 blessed into the filter's package. See the example filters later on for
222 details of using \f(CW$self\fR.
223 .PP
224 Here is a list of the common features of the anonymous sub and the
225 \&\f(CWfilter()\fR method.
226 .ie n .IP \fR\fB$_\fR\fB\fR 5
227 .el .IP \fR\f(CB$_\fR\fB\fR 5
228 .IX Item "$_"
229 Although \f(CW$_\fR doesn't actually appear explicitly in the sample filters
230 above, it is implicitly used in a number of places.
231 .Sp
232 Firstly, when either \f(CW\*(C`filter\*(C'\fR or the anonymous sub are called, a local
233 copy of \f(CW$_\fR will automatically be created. It will always contain the
234 empty string at this point.
235 .Sp
236 Next, both \f(CW\*(C`filter_read\*(C'\fR and \f(CW\*(C`filter_read_exact\*(C'\fR will append any
237 source data that is read to the end of \f(CW$_\fR.
238 .Sp
239 Finally, when \f(CW\*(C`filter\*(C'\fR or the anonymous sub are finished processing,
240 they are expected to return the filtered source using \f(CW$_\fR.
241 .Sp
242 This implicit use of \f(CW$_\fR greatly simplifies the filter.
243 .ie n .IP \fR\fB$status\fR\fB\fR 5
244 .el .IP \fR\f(CB$status\fR\fB\fR 5
245 .IX Item "$status"
246 The status value that is returned by the user's \f(CW\*(C`filter\*(C'\fR method or
247 anonymous sub and the \f(CW\*(C`filter_read\*(C'\fR and \f(CW\*(C`read_exact\*(C'\fR functions take
248 the same set of values, namely:
249 .Sp
250 .Vb 3
251 \& < 0 Error
252 \& = 0 EOF
253 \& > 0 OK
254 .Ve
255 .IP "\fBfilter_read\fR and \fBfilter_read_exact\fR" 5
256 .IX Item "filter_read and filter_read_exact"
257 These functions are used by the filter to obtain either a line or block
258 from the next filter in the chain or the actual source file if there
259 aren't any other filters.
260 .Sp
261 The function \f(CW\*(C`filter_read\*(C'\fR takes two forms:
262 .Sp
263 .Vb 2
264 \& $status = filter_read() ;
265 \& $status = filter_read($size) ;
266 .Ve
267 .Sp
268 The first form is used to request a \fIline\fR, the second requests a
269 \&\fIblock\fR.
270 .Sp
271 In line mode, \f(CW\*(C`filter_read\*(C'\fR will append the next source line to the
272 end of the \f(CW$_\fR scalar.
273 .Sp
274 In block mode, \f(CW\*(C`filter_read\*(C'\fR will append a block of data which is <=
275 \&\f(CW$size\fR to the end of the \f(CW$_\fR scalar. It is important to emphasise
276 the that \f(CW\*(C`filter_read\*(C'\fR will not necessarily read a block which is
277 \&\fIprecisely\fR \f(CW$size\fR bytes.
278 .Sp
279 If you need to be able to read a block which has an exact size, you can
280 use the function \f(CW\*(C`filter_read_exact\*(C'\fR. It works identically to
281 \&\f(CW\*(C`filter_read\*(C'\fR in block mode, except it will try to read a block which
282 is exactly \f(CW$size\fR bytes in length. The only circumstances when it
283 will not return a block which is \f(CW$size\fR bytes long is on EOF or
284 error.
285 .Sp
286 It is \fIvery\fR important to check the value of \f(CW$status\fR after \fIevery\fR
287 call to \f(CW\*(C`filter_read\*(C'\fR or \f(CW\*(C`filter_read_exact\*(C'\fR.
288 .IP \fBfilter_del\fR 5
289 .IX Item "filter_del"
290 The function, \f(CW\*(C`filter_del\*(C'\fR, is used to disable the current filter. It
291 does not affect the running of the filter. All it does is tell Perl not
292 to call filter any more.
293 .Sp
294 See "Example 4: Using filter_del" for details.
295 .IP \fIreal_import\fR 5
296 .IX Item "real_import"
297 Internal function which adds the filter, based on the filter_add
298 argument type.
299 .IP \fR\f(BIunimport()\fR\fI\fR 5
300 .IX Item "unimport()"
301 May be used to disable a filter, but is rarely needed. See filter_del.
302 .SH LIMITATIONS
303 .IX Header "LIMITATIONS"
304 See "LIMITATIONS" in perlfilter for an overview of the general problems
305 filtering code in a textual line-level only.
306 .IP "_\|_DATA_\|_ is ignored" 4
307 .IX Item "__DATA__ is ignored"
308 The content from the _\|_DATA_\|_ block is not filtered.
309 This is a serious limitation, e.g. for the Switch module.
310 See <http://search.cpan.org/perldoc?Switch#LIMITATIONS> for more.
311 .IP "Max. codesize limited to 32\-bit" 4
312 .IX Item "Max. codesize limited to 32-bit"
313 Currently internal buffer lengths are limited to 32\-bit only.
314 .SH EXAMPLES
315 .IX Header "EXAMPLES"
316 Here are a few examples which illustrate the key concepts \- as such
317 most of them are of little practical use.
318 .PP
319 The \f(CW\*(C`examples\*(C'\fR sub-directory has copies of all these filters
320 implemented both as \fImethod filters\fR and as \fIclosure filters\fR.
321 .SS "Example 1: A simple filter."
322 .IX Subsection "Example 1: A simple filter."
323 Below is a \fImethod filter\fR which is hard-wired to replace all
324 occurrences of the string \f(CW"Joe"\fR to \f(CW"Jim"\fR. Not particularly
325 Useful, but it is the first example and I wanted to keep it simple.
326 .PP
327 .Vb 1
328 \& package Joe2Jim ;
329 \&
330 \& use Filter::Util::Call ;
331 \&
332 \& sub import
333 \& {
334 \& my($type) = @_ ;
335 \&
336 \& filter_add(bless []) ;
337 \& }
338 \&
339 \& sub filter
340 \& {
341 \& my($self) = @_ ;
342 \& my($status) ;
343 \&
344 \& s/Joe/Jim/g
345 \& if ($status = filter_read()) > 0 ;
346 \& $status ;
347 \& }
348 \&
349 \& 1 ;
350 .Ve
351 .PP
352 Here is an example of using the filter:
353 .PP
354 .Vb 2
355 \& use Joe2Jim ;
356 \& print "Where is Joe?\en" ;
357 .Ve
358 .PP
359 And this is what the script above will print:
360 .PP
361 .Vb 1
362 \& Where is Jim?
363 .Ve
364 .SS "Example 2: Using the context"
365 .IX Subsection "Example 2: Using the context"
366 The previous example was not particularly useful. To make it more
367 general purpose we will make use of the context data and allow any
368 arbitrary \fIfrom\fR and \fIto\fR strings to be used. This time we will use a
369 \&\fIclosure filter\fR. To reflect its enhanced role, the filter is called
370 \&\f(CW\*(C`Subst\*(C'\fR.
371 .PP
372 .Vb 1
373 \& package Subst ;
374 \&
375 \& use Filter::Util::Call ;
376 \& use Carp ;
377 \&
378 \& sub import
379 \& {
380 \& croak("usage: use Subst qw(from to)")
381 \& unless @_ == 3 ;
382 \& my ($self, $from, $to) = @_ ;
383 \& filter_add(
384 \& sub
385 \& {
386 \& my ($status) ;
387 \& s/$from/$to/
388 \& if ($status = filter_read()) > 0 ;
389 \& $status ;
390 \& })
391 \& }
392 \& 1 ;
393 .Ve
394 .PP
395 and is used like this:
396 .PP
397 .Vb 2
398 \& use Subst qw(Joe Jim) ;
399 \& print "Where is Joe?\en" ;
400 .Ve
401 .SS "Example 3: Using the context within the filter"
402 .IX Subsection "Example 3: Using the context within the filter"
403 Here is a filter which a variation of the \f(CW\*(C`Joe2Jim\*(C'\fR filter. As well as
404 substituting all occurrences of \f(CW"Joe"\fR to \f(CW"Jim"\fR it keeps a count
405 of the number of substitutions made in the context object.
406 .PP
407 Once EOF is detected (\f(CW$status\fR is zero) the filter will insert an
408 extra line into the source stream. When this extra line is executed it
409 will print a count of the number of substitutions actually made.
410 Note that \f(CW$status\fR is set to \f(CW1\fR in this case.
411 .PP
412 .Vb 1
413 \& package Count ;
414 \&
415 \& use Filter::Util::Call ;
416 \&
417 \& sub filter
418 \& {
419 \& my ($self) = @_ ;
420 \& my ($status) ;
421 \&
422 \& if (($status = filter_read()) > 0 ) {
423 \& s/Joe/Jim/g ;
424 \& ++ $$self ;
425 \& }
426 \& elsif ($$self >= 0) { # EOF
427 \& $_ = "print q[Made ${$self} substitutions\en]" ;
428 \& $status = 1 ;
429 \& $$self = \-1 ;
430 \& }
431 \&
432 \& $status ;
433 \& }
434 \&
435 \& sub import
436 \& {
437 \& my ($self) = @_ ;
438 \& my ($count) = 0 ;
439 \& filter_add(\e$count) ;
440 \& }
441 \&
442 \& 1 ;
443 .Ve
444 .PP
445 Here is a script which uses it:
446 .PP
447 .Vb 3
448 \& use Count ;
449 \& print "Hello Joe\en" ;
450 \& print "Where is Joe\en" ;
451 .Ve
452 .PP
453 Outputs:
454 .PP
455 .Vb 3
456 \& Hello Jim
457 \& Where is Jim
458 \& Made 2 substitutions
459 .Ve
460 .SS "Example 4: Using filter_del"
461 .IX Subsection "Example 4: Using filter_del"
462 Another variation on a theme. This time we will modify the \f(CW\*(C`Subst\*(C'\fR
463 filter to allow a starting and stopping pattern to be specified as well
464 as the \fIfrom\fR and \fIto\fR patterns. If you know the \fIvi\fR editor, it is
465 the equivalent of this command:
466 .PP
467 .Vb 1
468 \& :/start/,/stop/s/from/to/
469 .Ve
470 .PP
471 When used as a filter we want to invoke it like this:
472 .PP
473 .Vb 1
474 \& use NewSubst qw(start stop from to) ;
475 .Ve
476 .PP
477 Here is the module.
478 .PP
479 .Vb 1
480 \& package NewSubst ;
481 \&
482 \& use Filter::Util::Call ;
483 \& use Carp ;
484 \&
485 \& sub import
486 \& {
487 \& my ($self, $start, $stop, $from, $to) = @_ ;
488 \& my ($found) = 0 ;
489 \& croak("usage: use Subst qw(start stop from to)")
490 \& unless @_ == 5 ;
491 \&
492 \& filter_add(
493 \& sub
494 \& {
495 \& my ($status) ;
496 \&
497 \& if (($status = filter_read()) > 0) {
498 \&
499 \& $found = 1
500 \& if $found == 0 and /$start/ ;
501 \&
502 \& if ($found) {
503 \& s/$from/$to/ ;
504 \& filter_del() if /$stop/ ;
505 \& }
506 \&
507 \& }
508 \& $status ;
509 \& } )
510 \&
511 \& }
512 \&
513 \& 1 ;
514 .Ve
515 .SH Filter::Simple
516 .IX Header "Filter::Simple"
517 If you intend using the Filter::Call functionality, I would strongly
518 recommend that you check out Damian Conway's excellent Filter::Simple
519 module. Damian's module provides a much cleaner interface than
520 Filter::Util::Call. Although it doesn't allow the fine control that
521 Filter::Util::Call does, it should be adequate for the majority of
522 applications. It's available at
523 .PP
524 .Vb 1
525 \& http://search.cpan.org/dist/Filter\-Simple/
526 .Ve
527 .SH AUTHOR
528 .IX Header "AUTHOR"
529 Paul Marquess
530 .SH DATE
531 .IX Header "DATE"
532 26th January 1996
533 .SH LICENSE
534 .IX Header "LICENSE"
535 Copyright (c) 1995\-2011 Paul Marquess. All rights reserved.
536 Copyright (c) 2011\-2014, 2018\-2022 Reini Urban. All rights reserved.
537 Copyright (c) 2014\-2017 cPanel Inc. All rights reserved.
538 .PP
539 This program is free software; you can redistribute it and/or
540 modify it under the same terms as Perl itself.