Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man3 / Filter::Simple.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Filter::Simple 3"
132.TH Filter::Simple 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134Filter::Simple \- Simplified source filtering
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& # in MyFilter.pm:
139.Ve
140.PP
141.Vb 1
142\& package MyFilter;
143.Ve
144.PP
145.Vb 1
146\& use Filter::Simple;
147.Ve
148.PP
149.Vb 1
150\& FILTER { ... };
151.Ve
152.PP
153.Vb 3
154\& # or just:
155\& #
156\& # use Filter::Simple sub { ... };
157.Ve
158.PP
159.Vb 1
160\& # in user's code:
161.Ve
162.PP
163.Vb 1
164\& use MyFilter;
165.Ve
166.PP
167.Vb 1
168\& # this code is filtered
169.Ve
170.PP
171.Vb 1
172\& no MyFilter;
173.Ve
174.PP
175.Vb 1
176\& # this code is not
177.Ve
178.SH "DESCRIPTION"
179.IX Header "DESCRIPTION"
180.Sh "The Problem"
181.IX Subsection "The Problem"
182Source filtering is an immensely powerful feature of recent versions of Perl.
183It allows one to extend the language itself (e.g. the Switch module), to
184simplify the language (e.g. Language::Pythonesque), or to completely recast the
185language (e.g. Lingua::Romana::Perligata). Effectively, it allows one to use
186the full power of Perl as its own, recursively applied, macro language.
187.PP
188The excellent Filter::Util::Call module (by Paul Marquess) provides a
189usable Perl interface to source filtering, but it is often too powerful
190and not nearly as simple as it could be.
191.PP
192To use the module it is necessary to do the following:
193.IP "1." 4
194Download, build, and install the Filter::Util::Call module.
195(If you have Perl 5.7.1 or later, this is already done for you.)
196.IP "2." 4
197Set up a module that does a \f(CW\*(C`use Filter::Util::Call\*(C'\fR.
198.IP "3." 4
199Within that module, create an \f(CW\*(C`import\*(C'\fR subroutine.
200.IP "4." 4
201Within the \f(CW\*(C`import\*(C'\fR subroutine do a call to \f(CW\*(C`filter_add\*(C'\fR, passing
202it either a subroutine reference.
203.IP "5." 4
204Within the subroutine reference, call \f(CW\*(C`filter_read\*(C'\fR or \f(CW\*(C`filter_read_exact\*(C'\fR
205to \*(L"prime\*(R" \f(CW$_\fR with source code data from the source file that will
206\&\f(CW\*(C`use\*(C'\fR your module. Check the status value returned to see if any
207source code was actually read in.
208.IP "6." 4
209Process the contents of \f(CW$_\fR to change the source code in the desired manner.
210.IP "7." 4
211Return the status value.
212.IP "8." 4
213If the act of unimporting your module (via a \f(CW\*(C`no\*(C'\fR) should cause source
214code filtering to cease, create an \f(CW\*(C`unimport\*(C'\fR subroutine, and have it call
215\&\f(CW\*(C`filter_del\*(C'\fR. Make sure that the call to \f(CW\*(C`filter_read\*(C'\fR or
216\&\f(CW\*(C`filter_read_exact\*(C'\fR in step 5 will not accidentally read past the
217\&\f(CW\*(C`no\*(C'\fR. Effectively this limits source code filters to line-by-line
218operation, unless the \f(CW\*(C`import\*(C'\fR subroutine does some fancy
219pre-pre-parsing of the source code it's filtering.
220.PP
221For example, here is a minimal source code filter in a module named
222\&\s-1BANG\s0.pm. It simply converts every occurrence of the sequence \f(CW\*(C`BANG\es+BANG\*(C'\fR
223to the sequence \f(CW\*(C`die 'BANG' if $BANG\*(C'\fR in any piece of code following a
224\&\f(CW\*(C`use BANG;\*(C'\fR statement (until the next \f(CW\*(C`no BANG;\*(C'\fR statement, if any):
225.PP
226.Vb 1
227\& package BANG;
228.Ve
229.PP
230.Vb 1
231\& use Filter::Util::Call ;
232.Ve
233.PP
234.Vb 19
235\& sub import {
236\& filter_add( sub {
237\& my $caller = caller;
238\& my ($status, $no_seen, $data);
239\& while ($status = filter_read()) {
240\& if (/^\es*no\es+$caller\es*;\es*?$/) {
241\& $no_seen=1;
242\& last;
243\& }
244\& $data .= $_;
245\& $_ = "";
246\& }
247\& $_ = $data;
248\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g
249\& unless $status < 0;
250\& $_ .= "no $class;\en" if $no_seen;
251\& return 1;
252\& })
253\& }
254.Ve
255.PP
256.Vb 3
257\& sub unimport {
258\& filter_del();
259\& }
260.Ve
261.PP
262.Vb 1
263\& 1 ;
264.Ve
265.PP
266This level of sophistication puts filtering out of the reach of
267many programmers.
268.Sh "A Solution"
269.IX Subsection "A Solution"
270The Filter::Simple module provides a simplified interface to
271Filter::Util::Call; one that is sufficient for most common cases.
272.PP
273Instead of the above process, with Filter::Simple the task of setting up
274a source code filter is reduced to:
275.IP "1." 4
276Download and install the Filter::Simple module.
277(If you have Perl 5.7.1 or later, this is already done for you.)
278.IP "2." 4
279Set up a module that does a \f(CW\*(C`use Filter::Simple\*(C'\fR and then
280calls \f(CW\*(C`FILTER { ... }\*(C'\fR.
281.IP "3." 4
282Within the anonymous subroutine or block that is passed to
283\&\f(CW\*(C`FILTER\*(C'\fR, process the contents of \f(CW$_\fR to change the source code in
284the desired manner.
285.PP
286In other words, the previous example, would become:
287.PP
288.Vb 2
289\& package BANG;
290\& use Filter::Simple;
291.Ve
292.PP
293.Vb 3
294\& FILTER {
295\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
296\& };
297.Ve
298.PP
299.Vb 1
300\& 1 ;
301.Ve
302.PP
303Note that the source code is passed as a single string, so any regex that
304uses \f(CW\*(C`^\*(C'\fR or \f(CW\*(C`$\*(C'\fR to detect line boundaries will need the \f(CW\*(C`/m\*(C'\fR flag.
305.Sh "Disabling or changing <no> behaviour"
306.IX Subsection "Disabling or changing <no> behaviour"
307By default, the installed filter only filters up to a line consisting of one of
308the three standard source \*(L"terminators\*(R":
309.PP
310.Vb 1
311\& no ModuleName; # optional comment
312.Ve
313.PP
314or:
315.PP
316.Vb 1
317\& __END__
318.Ve
319.PP
320or:
321.PP
322.Vb 1
323\& __DATA__
324.Ve
325.PP
326but this can be altered by passing a second argument to \f(CW\*(C`use Filter::Simple\*(C'\fR
327or \f(CW\*(C`FILTER\*(C'\fR (just remember: there's \fIno\fR comma after the initial block when
328you use \f(CW\*(C`FILTER\*(C'\fR).
329.PP
330That second argument may be either a \f(CW\*(C`qr\*(C'\fR'd regular expression (which is then
331used to match the terminator line), or a defined false value (which indicates
332that no terminator line should be looked for), or a reference to a hash
333(in which case the terminator is the value associated with the key
334\&\f(CW'terminator'\fR.
335.PP
336For example, to cause the previous filter to filter only up to a line of the
337form:
338.PP
339.Vb 1
340\& GNAB esu;
341.Ve
342.PP
343you would write:
344.PP
345.Vb 2
346\& package BANG;
347\& use Filter::Simple;
348.Ve
349.PP
350.Vb 4
351\& FILTER {
352\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
353\& }
354\& qr/^\es*GNAB\es+esu\es*;\es*?$/;
355.Ve
356.PP
357or:
358.PP
359.Vb 4
360\& FILTER {
361\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
362\& }
363\& { terminator => qr/^\es*GNAB\es+esu\es*;\es*?$/ };
364.Ve
365.PP
366and to prevent the filter's being turned off in any way:
367.PP
368.Vb 2
369\& package BANG;
370\& use Filter::Simple;
371.Ve
372.PP
373.Vb 4
374\& FILTER {
375\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
376\& }
377\& ""; # or: 0
378.Ve
379.PP
380or:
381.PP
382.Vb 4
383\& FILTER {
384\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
385\& }
386\& { terminator => "" };
387.Ve
388.PP
389\&\fBNote that, no matter what you set the terminator pattern to,
390the actual terminator itself \f(BImust\fB be contained on a single source line.\fR
391.Sh "All-in-one interface"
392.IX Subsection "All-in-one interface"
393Separating the loading of Filter::Simple:
394.PP
395.Vb 1
396\& use Filter::Simple;
397.Ve
398.PP
399from the setting up of the filtering:
400.PP
401.Vb 1
402\& FILTER { ... };
403.Ve
404.PP
405is useful because it allows other code (typically parser support code
406or caching variables) to be defined before the filter is invoked.
407However, there is often no need for such a separation.
408.PP
409In those cases, it is easier to just append the filtering subroutine and
410any terminator specification directly to the \f(CW\*(C`use\*(C'\fR statement that loads
411Filter::Simple, like so:
412.PP
413.Vb 3
414\& use Filter::Simple sub {
415\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
416\& };
417.Ve
418.PP
419This is exactly the same as:
420.PP
421.Vb 6
422\& use Filter::Simple;
423\& BEGIN {
424\& Filter::Simple::FILTER {
425\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
426\& };
427\& }
428.Ve
429.PP
430except that the \f(CW\*(C`FILTER\*(C'\fR subroutine is not exported by Filter::Simple.
431.Sh "Filtering only specific components of source code"
432.IX Subsection "Filtering only specific components of source code"
433One of the problems with a filter like:
434.PP
435.Vb 1
436\& use Filter::Simple;
437.Ve
438.PP
439.Vb 1
440\& FILTER { s/BANG\es+BANG/die 'BANG' if \e$BANG/g };
441.Ve
442.PP
443is that it indiscriminately applies the specified transformation to
444the entire text of your source program. So something like:
445.PP
446.Vb 2
447\& warn 'BANG BANG, YOU'RE DEAD';
448\& BANG BANG;
449.Ve
450.PP
451will become:
452.PP
453.Vb 2
454\& warn 'die 'BANG' if $BANG, YOU'RE DEAD';
455\& die 'BANG' if $BANG;
456.Ve
457.PP
458It is very common when filtering source to only want to apply the filter
459to the non-character-string parts of the code, or alternatively to \fIonly\fR
460the character strings.
461.PP
462Filter::Simple supports this type of filtering by automatically
463exporting the \f(CW\*(C`FILTER_ONLY\*(C'\fR subroutine.
464.PP
465\&\f(CW\*(C`FILTER_ONLY\*(C'\fR takes a sequence of specifiers that install separate
466(and possibly multiple) filters that act on only parts of the source code.
467For example:
468.PP
469.Vb 1
470\& use Filter::Simple;
471.Ve
472.PP
473.Vb 3
474\& FILTER_ONLY
475\& code => sub { s/BANG\es+BANG/die 'BANG' if \e$BANG/g },
476\& quotelike => sub { s/BANG\es+BANG/CHITTY CHITTY/g };
477.Ve
478.PP
479The \f(CW"code"\fR subroutine will only be used to filter parts of the source
480code that are not quotelikes, \s-1POD\s0, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. The \f(CW\*(C`quotelike\*(C'\fR
481subroutine only filters Perl quotelikes (including here documents).
482.PP
483The full list of alternatives is:
484.ie n .IP """code""" 4
485.el .IP "\f(CW``code''\fR" 4
486.IX Item """code"""
487Filters only those sections of the source code that are not quotelikes, \s-1POD\s0, or
488\&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
489.ie n .IP """code_no_comments""" 4
490.el .IP "\f(CW``code_no_comments''\fR" 4
491.IX Item """code_no_comments"""
492Filters only those sections of the source code that are not quotelikes, \s-1POD\s0,
493comments, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
494.ie n .IP """executable""" 4
495.el .IP "\f(CW``executable''\fR" 4
496.IX Item """executable"""
497Filters only those sections of the source code that are not \s-1POD\s0 or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
498.ie n .IP """executable_no_comments""" 4
499.el .IP "\f(CW``executable_no_comments''\fR" 4
500.IX Item """executable_no_comments"""
501Filters only those sections of the source code that are not \s-1POD\s0, comments, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
502.ie n .IP """quotelike""" 4
503.el .IP "\f(CW``quotelike''\fR" 4
504.IX Item """quotelike"""
505Filters only Perl quotelikes (as interpreted by
506\&\f(CW&Text::Balanced::extract_quotelike\fR).
507.ie n .IP """string""" 4
508.el .IP "\f(CW``string''\fR" 4
509.IX Item """string"""
510Filters only the string literal parts of a Perl quotelike (i.e. the
511contents of a string literal, either half of a \f(CW\*(C`tr///\*(C'\fR, the second
512half of an \f(CW\*(C`s///\*(C'\fR).
513.ie n .IP """regex""" 4
514.el .IP "\f(CW``regex''\fR" 4
515.IX Item """regex"""
516Filters only the pattern literal parts of a Perl quotelike (i.e. the
517contents of a \f(CW\*(C`qr//\*(C'\fR or an \f(CW\*(C`m//\*(C'\fR, the first half of an \f(CW\*(C`s///\*(C'\fR).
518.ie n .IP """all""" 4
519.el .IP "\f(CW``all''\fR" 4
520.IX Item """all"""
521Filters everything. Identical in effect to \f(CW\*(C`FILTER\*(C'\fR.
522.PP
523Except for \f(CW\*(C`FILTER_ONLY code => sub {...}\*(C'\fR, each of
524the component filters is called repeatedly, once for each component
525found in the source code.
526.PP
527Note that you can also apply two or more of the same type of filter in
528a single \f(CW\*(C`FILTER_ONLY\*(C'\fR. For example, here's a simple
529macro-preprocessor that is only applied within regexes,
530with a final debugging pass that prints the resulting source code:
531.PP
532.Vb 6
533\& use Regexp::Common;
534\& FILTER_ONLY
535\& regex => sub { s/!\e[/[^/g },
536\& regex => sub { s/%d/$RE{num}{int}/g },
537\& regex => sub { s/%f/$RE{num}{real}/g },
538\& all => sub { print if $::DEBUG };
539.Ve
540.Sh "Filtering only the code parts of source code"
541.IX Subsection "Filtering only the code parts of source code"
542Most source code ceases to be grammatically correct when it is broken up
543into the pieces between string literals and regexes. So the \f(CW'code'\fR
544and \f(CW'code_no_comments'\fR component filter behave slightly differently
545from the other partial filters described in the previous section.
546.PP
547Rather than calling the specified processor on each individual piece of
548code (i.e. on the bits between quotelikes), the \f(CW'code...'\fR partial
549filters operate on the entire source code, but with the quotelike bits
550(and, in the case of \f(CW'code_no_comments'\fR, the comments) \*(L"blanked out\*(R".
551.PP
552That is, a \f(CW'code...'\fR filter \fIreplaces\fR each quoted string, quotelike,
553regex, \s-1POD\s0, and _\|_DATA_\|_ section with a placeholder. The
554delimiters of this placeholder are the contents of the \f(CW$;\fR variable
555at the time the filter is applied (normally \f(CW"\e034"\fR). The remaining
556four bytes are a unique identifier for the component being replaced.
557.PP
558This approach makes it comparatively easy to write code preprocessors
559without worrying about the form or contents of strings, regexes, etc.
560.PP
561For convenience, during a \f(CW'code...'\fR filtering operation, Filter::Simple
562provides a package variable (\f(CW$Filter::Simple::placeholder\fR) that
563contains a pre-compiled regex that matches any placeholder...and
564captures the identifier within the placeholder. Placeholders can be
565moved and re-ordered within the source code as needed.
566.PP
567In addition, a second package variable (\f(CW@Filter::Simple::components\fR)
568contains a list of the various pieces of \f(CW$_\fR, as they were originally split
569up to allow placeholders to be inserted.
570.PP
571Once the filtering has been applied, the original strings, regexes, \s-1POD\s0,
572etc. are re-inserted into the code, by replacing each placeholder with
573the corresponding original component (from \f(CW@components\fR). Note that
574this means that the \f(CW@components\fR variable must be treated with extreme
575care within the filter. The \f(CW@components\fR array stores the \*(L"back\-
576translations\*(R" of each placeholder inserted into \f(CW$_\fR, as well as the
577interstitial source code between placeholders. If the placeholder
578backtranslations are altered in \f(CW@components\fR, they will be similarly
579changed when the placeholders are removed from \f(CW$_\fR after the filter
580is complete.
581.PP
582For example, the following filter detects concatentated pairs of
583strings/quotelikes and reverses the order in which they are
584concatenated:
585.PP
586.Vb 2
587\& package DemoRevCat;
588\& use Filter::Simple;
589.Ve
590.PP
591.Vb 4
592\& FILTER_ONLY code => sub {
593\& my $ph = $Filter::Simple::placeholder;
594\& s{ ($ph) \es* [.] \es* ($ph) }{ $2.$1 }gx
595\& };
596.Ve
597.PP
598Thus, the following code:
599.PP
600.Vb 1
601\& use DemoRevCat;
602.Ve
603.PP
604.Vb 1
605\& my $str = "abc" . q(def);
606.Ve
607.PP
608.Vb 1
609\& print "$str\en";
610.Ve
611.PP
612would become:
613.PP
614.Vb 1
615\& my $str = q(def)."abc";
616.Ve
617.PP
618.Vb 1
619\& print "$str\en";
620.Ve
621.PP
622and hence print:
623.PP
624.Vb 1
625\& defabc
626.Ve
627.ie n .Sh "Using Filter::Simple with an explicit ""import"" subroutine"
628.el .Sh "Using Filter::Simple with an explicit \f(CWimport\fP subroutine"
629.IX Subsection "Using Filter::Simple with an explicit import subroutine"
630Filter::Simple generates a special \f(CW\*(C`import\*(C'\fR subroutine for
631your module (see \*(L"How it works\*(R") which would normally replace any
632\&\f(CW\*(C`import\*(C'\fR subroutine you might have explicitly declared.
633.PP
634However, Filter::Simple is smart enough to notice your existing
635\&\f(CW\*(C`import\*(C'\fR and Do The Right Thing with it.
636That is, if you explicitly define an \f(CW\*(C`import\*(C'\fR subroutine in a package
637that's using Filter::Simple, that \f(CW\*(C`import\*(C'\fR subroutine will still
638be invoked immediately after any filter you install.
639.PP
640The only thing you have to remember is that the \f(CW\*(C`import\*(C'\fR subroutine
641\&\fImust\fR be declared \fIbefore\fR the filter is installed. If you use \f(CW\*(C`FILTER\*(C'\fR
642to install the filter:
643.PP
644.Vb 1
645\& package Filter::TurnItUpTo11;
646.Ve
647.PP
648.Vb 1
649\& use Filter::Simple;
650.Ve
651.PP
652.Vb 1
653\& FILTER { s/(\ew+)/\eU$1/ };
654.Ve
655.PP
656that will almost never be a problem, but if you install a filtering
657subroutine by passing it directly to the \f(CW\*(C`use Filter::Simple\*(C'\fR
658statement:
659.PP
660.Vb 1
661\& package Filter::TurnItUpTo11;
662.Ve
663.PP
664.Vb 1
665\& use Filter::Simple sub{ s/(\ew+)/\eU$1/ };
666.Ve
667.PP
668then you must make sure that your \f(CW\*(C`import\*(C'\fR subroutine appears before
669that \f(CW\*(C`use\*(C'\fR statement.
670.Sh "Using Filter::Simple and Exporter together"
671.IX Subsection "Using Filter::Simple and Exporter together"
672Likewise, Filter::Simple is also smart enough
673to Do The Right Thing if you use Exporter:
674.PP
675.Vb 3
676\& package Switch;
677\& use base Exporter;
678\& use Filter::Simple;
679.Ve
680.PP
681.Vb 2
682\& @EXPORT = qw(switch case);
683\& @EXPORT_OK = qw(given when);
684.Ve
685.PP
686.Vb 1
687\& FILTER { $_ = magic_Perl_filter($_) }
688.Ve
689.PP
690Immediately after the filter has been applied to the source,
691Filter::Simple will pass control to Exporter, so it can do its magic too.
692.PP
693Of course, here too, Filter::Simple has to know you're using Exporter
694before it applies the filter. That's almost never a problem, but if you're
695nervous about it, you can guarantee that things will work correctly by
696ensuring that your \f(CW\*(C`use base Exporter\*(C'\fR always precedes your
697\&\f(CW\*(C`use Filter::Simple\*(C'\fR.
698.Sh "How it works"
699.IX Subsection "How it works"
700The Filter::Simple module exports into the package that calls \f(CW\*(C`FILTER\*(C'\fR
701(or \f(CW\*(C`use\*(C'\fRs it directly) \*(-- such as package \*(L"\s-1BANG\s0\*(R" in the above example \*(--
702two automagically constructed
703subroutines \*(-- \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR \*(-- which take care of all the
704nasty details.
705.PP
706In addition, the generated \f(CW\*(C`import\*(C'\fR subroutine passes its own argument
707list to the filtering subroutine, so the \s-1BANG\s0.pm filter could easily
708be made parametric:
709.PP
710.Vb 1
711\& package BANG;
712.Ve
713.PP
714.Vb 1
715\& use Filter::Simple;
716.Ve
717.PP
718.Vb 4
719\& FILTER {
720\& my ($die_msg, $var_name) = @_;
721\& s/BANG\es+BANG/die '$die_msg' if \e${$var_name}/g;
722\& };
723.Ve
724.PP
725.Vb 1
726\& # and in some user code:
727.Ve
728.PP
729.Vb 1
730\& use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
731.Ve
732.PP
733The specified filtering subroutine is called every time a \f(CW\*(C`use BANG\*(C'\fR is
734encountered, and passed all the source code following that call, up to
735either the next \f(CW\*(C`no BANG;\*(C'\fR (or whatever terminator you've set) or the
736end of the source file, whichever occurs first. By default, any \f(CW\*(C`no
737BANG;\*(C'\fR call must appear by itself on a separate line, or it is ignored.
738.SH "AUTHOR"
739.IX Header "AUTHOR"
740Damian Conway (damian@conway.org)
741.SH "COPYRIGHT"
742.IX Header "COPYRIGHT"
743.Vb 3
744\& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
745\& This module is free software. It may be used, redistributed
746\& and/or modified under the same terms as Perl itself.
747.Ve