Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Filter::Simple.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
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 "2002-06-01" "perl v5.8.0" "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 """executable""" 4
490.el .IP "\f(CW``executable''\fR" 4
491.IX Item """executable"""
492Filters only those sections of the source code that are not \s-1POD\s0 or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
493.ie n .IP """quotelike""" 4
494.el .IP "\f(CW``quotelike''\fR" 4
495.IX Item """quotelike"""
496Filters only Perl quotelikes (as interpreted by
497\&\f(CW&Text::Balanced::extract_quotelike\fR).
498.ie n .IP """string""" 4
499.el .IP "\f(CW``string''\fR" 4
500.IX Item """string"""
501Filters only the string literal parts of a Perl quotelike (i.e. the
502contents of a string literal, either half of a \f(CW\*(C`tr///\*(C'\fR, the second
503half of an \f(CW\*(C`s///\*(C'\fR).
504.ie n .IP """regex""" 4
505.el .IP "\f(CW``regex''\fR" 4
506.IX Item """regex"""
507Filters only the pattern literal parts of a Perl quotelike (i.e. the
508contents 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).
509.ie n .IP """all""" 4
510.el .IP "\f(CW``all''\fR" 4
511.IX Item """all"""
512Filters everything. Identical in effect to \f(CW\*(C`FILTER\*(C'\fR.
513.PP
514Except for \f(CW\*(C`FILTER_ONLY code => sub {...}\*(C'\fR, each of
515the component filters is called repeatedly, once for each component
516found in the source code.
517.PP
518Note that you can also apply two or more of the same type of filter in
519a single \f(CW\*(C`FILTER_ONLY\*(C'\fR. For example, here's a simple
520macro-preprocessor that is only applied within regexes,
521with a final debugging pass that prints the resulting source code:
522.PP
523.Vb 6
524\& use Regexp::Common;
525\& FILTER_ONLY
526\& regex => sub { s/!\e[/[^/g },
527\& regex => sub { s/%d/$RE{num}{int}/g },
528\& regex => sub { s/%f/$RE{num}{real}/g },
529\& all => sub { print if $::DEBUG };
530.Ve
531.Sh "Filtering only the code parts of source code"
532.IX Subsection "Filtering only the code parts of source code"
533Most source code ceases to be grammatically correct when it is broken up
534into the pieces between string literals and regexes. So the \f(CW'code'\fR
535component filter behaves slightly differently from the other partial filters
536described in the previous section.
537.PP
538Rather than calling the specified processor on each individual piece of
539code (i.e. on the bits between quotelikes), the \f(CW'code'\fR partial filter
540operates on the entire source code, but with the quotelike bits
541\&\*(L"blanked out\*(R".
542.PP
543That is, a \f(CW'code'\fR filter \fIreplaces\fR each quoted string, quotelike,
544regex, \s-1POD\s0, and _\|_DATA_\|_ section with a placeholder. The
545delimiters of this placeholder are the contents of the \f(CW$;\fR variable
546at the time the filter is applied (normally \f(CW"\e034"\fR). The remaining
547four bytes are a unique identifier for the component being replaced.
548.PP
549This approach makes it comparatively easy to write code preprocessors
550without worrying about the form or contents of strings, regexes, etc.
551For convenience, during a \f(CW'code'\fR filtering operation, Filter::Simple
552provides a package variable (\f(CW$Filter::Simple::placeholder\fR) that contains
553a pre-compiled regex that matches any placeholder. Placeholders can be
554moved and re-ordered within the source code as needed.
555.PP
556Once the filtering has been applied, the original strings, regexes,
557\&\s-1POD\s0, etc. are re-inserted into the code, by replacing each
558placeholder with the corresponding original component.
559.PP
560For example, the following filter detects concatentated pairs of
561strings/quotelikes and reverses the order in which they are
562concatenated:
563.PP
564.Vb 2
565\& package DemoRevCat;
566\& use Filter::Simple;
567.Ve
568.PP
569.Vb 3
570\& FILTER_ONLY code => sub { my $ph = $Filter::Simple::placeholder;
571\& s{ ($ph) \es* [.] \es* ($ph) }{ $2.$1 }gx
572\& };
573.Ve
574.PP
575Thus, the following code:
576.PP
577.Vb 1
578\& use DemoRevCat;
579.Ve
580.PP
581.Vb 1
582\& my $str = "abc" . q(def);
583.Ve
584.PP
585.Vb 1
586\& print "$str\en";
587.Ve
588.PP
589would become:
590.PP
591.Vb 1
592\& my $str = q(def)."abc";
593.Ve
594.PP
595.Vb 1
596\& print "$str\en";
597.Ve
598.PP
599and hence print:
600.PP
601.Vb 1
602\& defabc
603.Ve
604.ie n .Sh "Using Filter::Simple with an explicit ""import"" subroutine"
605.el .Sh "Using Filter::Simple with an explicit \f(CWimport\fP subroutine"
606.IX Subsection "Using Filter::Simple with an explicit import subroutine"
607Filter::Simple generates a special \f(CW\*(C`import\*(C'\fR subroutine for
608your module (see \*(L"How it works\*(R") which would normally replace any
609\&\f(CW\*(C`import\*(C'\fR subroutine you might have explicitly declared.
610.PP
611However, Filter::Simple is smart enough to notice your existing
612\&\f(CW\*(C`import\*(C'\fR and Do The Right Thing with it.
613That is, if you explicitly define an \f(CW\*(C`import\*(C'\fR subroutine in a package
614that's using Filter::Simple, that \f(CW\*(C`import\*(C'\fR subroutine will still
615be invoked immediately after any filter you install.
616.PP
617The only thing you have to remember is that the \f(CW\*(C`import\*(C'\fR subroutine
618\&\fImust\fR be declared \fIbefore\fR the filter is installed. If you use \f(CW\*(C`FILTER\*(C'\fR
619to install the filter:
620.PP
621.Vb 1
622\& package Filter::TurnItUpTo11;
623.Ve
624.PP
625.Vb 1
626\& use Filter::Simple;
627.Ve
628.PP
629.Vb 1
630\& FILTER { s/(\ew+)/\eU$1/ };
631.Ve
632.PP
633that will almost never be a problem, but if you install a filtering
634subroutine by passing it directly to the \f(CW\*(C`use Filter::Simple\*(C'\fR
635statement:
636.PP
637.Vb 1
638\& package Filter::TurnItUpTo11;
639.Ve
640.PP
641.Vb 1
642\& use Filter::Simple sub{ s/(\ew+)/\eU$1/ };
643.Ve
644.PP
645then you must make sure that your \f(CW\*(C`import\*(C'\fR subroutine appears before
646that \f(CW\*(C`use\*(C'\fR statement.
647.Sh "Using Filter::Simple and Exporter together"
648.IX Subsection "Using Filter::Simple and Exporter together"
649Likewise, Filter::Simple is also smart enough
650to Do The Right Thing if you use Exporter:
651.PP
652.Vb 3
653\& package Switch;
654\& use base Exporter;
655\& use Filter::Simple;
656.Ve
657.PP
658.Vb 2
659\& @EXPORT = qw(switch case);
660\& @EXPORT_OK = qw(given when);
661.Ve
662.PP
663.Vb 1
664\& FILTER { $_ = magic_Perl_filter($_) }
665.Ve
666.PP
667Immediately after the filter has been applied to the source,
668Filter::Simple will pass control to Exporter, so it can do its magic too.
669.PP
670Of course, here too, Filter::Simple has to know you're using Exporter
671before it applies the filter. That's almost never a problem, but if you're
672nervous about it, you can guarantee that things will work correctly by
673ensuring that your \f(CW\*(C`use base Exporter\*(C'\fR always precedes your
674\&\f(CW\*(C`use Filter::Simple\*(C'\fR.
675.Sh "How it works"
676.IX Subsection "How it works"
677The Filter::Simple module exports into the package that calls \f(CW\*(C`FILTER\*(C'\fR
678(or \f(CW\*(C`use\*(C'\fRs it directly) \*(-- such as package \*(L"\s-1BANG\s0\*(R" in the above example \*(--
679two automagically constructed
680subroutines \*(-- \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR \*(-- which take care of all the
681nasty details.
682.PP
683In addition, the generated \f(CW\*(C`import\*(C'\fR subroutine passes its own argument
684list to the filtering subroutine, so the \s-1BANG\s0.pm filter could easily
685be made parametric:
686.PP
687.Vb 1
688\& package BANG;
689.Ve
690.PP
691.Vb 1
692\& use Filter::Simple;
693.Ve
694.PP
695.Vb 4
696\& FILTER {
697\& my ($die_msg, $var_name) = @_;
698\& s/BANG\es+BANG/die '$die_msg' if \e${$var_name}/g;
699\& };
700.Ve
701.PP
702.Vb 1
703\& # and in some user code:
704.Ve
705.PP
706.Vb 1
707\& use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
708.Ve
709.PP
710The specified filtering subroutine is called every time a \f(CW\*(C`use BANG\*(C'\fR is
711encountered, and passed all the source code following that call, up to
712either the next \f(CW\*(C`no BANG;\*(C'\fR (or whatever terminator you've set) or the
713end of the source file, whichever occurs first. By default, any \f(CW\*(C`no
714BANG;\*(C'\fR call must appear by itself on a separate line, or it is ignored.
715.SH "AUTHOR"
716.IX Header "AUTHOR"
717Damian Conway (damian@conway.org)
718.SH "COPYRIGHT"
719.IX Header "COPYRIGHT"
720.Vb 3
721\& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
722\& This module is free software. It may be used, redistributed
723\& and/or modified under the same terms as Perl itself.
724.Ve