.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Filter::Simple 3"
.TH Filter::Simple 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
Filter::Simple \- Simplified source filtering
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& # in MyFilter.pm:
.Ve
.PP
.Vb 1
\& package MyFilter;
.Ve
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
.Vb 1
\& FILTER { ... };
.Ve
.PP
.Vb 3
\& # or just:
\& #
\& # use Filter::Simple sub { ... };
.Ve
.PP
.Vb 1
\& # in user's code:
.Ve
.PP
.Vb 1
\& use MyFilter;
.Ve
.PP
.Vb 1
\& # this code is filtered
.Ve
.PP
.Vb 1
\& no MyFilter;
.Ve
.PP
.Vb 1
\& # this code is not
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
.Sh "The Problem"
.IX Subsection "The Problem"
Source filtering is an immensely powerful feature of recent versions of Perl.
It allows one to extend the language itself (e.g. the Switch module), to
simplify the language (e.g. Language::Pythonesque), or to completely recast the
language (e.g. Lingua::Romana::Perligata). Effectively, it allows one to use
the full power of Perl as its own, recursively applied, macro language.
.PP
The excellent Filter::Util::Call module (by Paul Marquess) provides a
usable Perl interface to source filtering, but it is often too powerful
and not nearly as simple as it could be.
.PP
To use the module it is necessary to do the following:
.IP "1." 4
Download, build, and install the Filter::Util::Call module.
(If you have Perl 5.7.1 or later, this is already done for you.)
.IP "2." 4
Set up a module that does a \f(CW\*(C`use Filter::Util::Call\*(C'\fR.
.IP "3." 4
Within that module, create an \f(CW\*(C`import\*(C'\fR subroutine.
.IP "4." 4
Within the \f(CW\*(C`import\*(C'\fR subroutine do a call to \f(CW\*(C`filter_add\*(C'\fR, passing
it either a subroutine reference.
.IP "5." 4
Within the subroutine reference, call \f(CW\*(C`filter_read\*(C'\fR or \f(CW\*(C`filter_read_exact\*(C'\fR
to \*(L"prime\*(R" \f(CW$_\fR with source code data from the source file that will
\&\f(CW\*(C`use\*(C'\fR your module. Check the status value returned to see if any
source code was actually read in.
.IP "6." 4
Process the contents of \f(CW$_\fR to change the source code in the desired manner.
.IP "7." 4
Return the status value.
.IP "8." 4
If the act of unimporting your module (via a \f(CW\*(C`no\*(C'\fR) should cause source
code filtering to cease, create an \f(CW\*(C`unimport\*(C'\fR subroutine, and have it call
\&\f(CW\*(C`filter_del\*(C'\fR. Make sure that the call to \f(CW\*(C`filter_read\*(C'\fR or
\&\f(CW\*(C`filter_read_exact\*(C'\fR in step 5 will not accidentally read past the
\&\f(CW\*(C`no\*(C'\fR. Effectively this limits source code filters to line-by-line
operation, unless the \f(CW\*(C`import\*(C'\fR subroutine does some fancy
pre-pre-parsing of the source code it's filtering.
.PP
For example, here is a minimal source code filter in a module named
\&\s-1BANG\s0.pm. It simply converts every occurrence of the sequence \f(CW\*(C`BANG\es+BANG\*(C'\fR
to the sequence \f(CW\*(C`die 'BANG' if $BANG\*(C'\fR in any piece of code following a
\&\f(CW\*(C`use BANG;\*(C'\fR statement (until the next \f(CW\*(C`no BANG;\*(C'\fR statement, if any):
.PP
.Vb 1
\& package BANG;
.Ve
.PP
.Vb 1
\& use Filter::Util::Call ;
.Ve
.PP
.Vb 19
\& sub import {
\& filter_add( sub {
\& my $caller = caller;
\& my ($status, $no_seen, $data);
\& while ($status = filter_read()) {
\& if (/^\es*no\es+$caller\es*;\es*?$/) {
\& $no_seen=1;
\& last;
\& }
\& $data .= $_;
\& $_ = "";
\& }
\& $_ = $data;
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g
\& unless $status < 0;
\& $_ .= "no $class;\en" if $no_seen;
\& return 1;
\& })
\& }
.Ve
.PP
.Vb 3
\& sub unimport {
\& filter_del();
\& }
.Ve
.PP
.Vb 1
\& 1 ;
.Ve
.PP
This level of sophistication puts filtering out of the reach of
many programmers.
.Sh "A Solution"
.IX Subsection "A Solution"
The Filter::Simple module provides a simplified interface to
Filter::Util::Call; one that is sufficient for most common cases.
.PP
Instead of the above process, with Filter::Simple the task of setting up
a source code filter is reduced to:
.IP "1." 4
Download and install the Filter::Simple module.
(If you have Perl 5.7.1 or later, this is already done for you.)
.IP "2." 4
Set up a module that does a \f(CW\*(C`use Filter::Simple\*(C'\fR and then
calls \f(CW\*(C`FILTER { ... }\*(C'\fR.
.IP "3." 4
Within the anonymous subroutine or block that is passed to
\&\f(CW\*(C`FILTER\*(C'\fR, process the contents of \f(CW$_\fR to change the source code in
the desired manner.
.PP
In other words, the previous example, would become:
.PP
.Vb 2
\& package BANG;
\& use Filter::Simple;
.Ve
.PP
.Vb 3
\& FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& };
.Ve
.PP
.Vb 1
\& 1 ;
.Ve
.PP
Note that the source code is passed as a single string, so any regex that
uses \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.
.Sh "Disabling or changing <no> behaviour"
.IX Subsection "Disabling or changing <no> behaviour"
By default, the installed filter only filters up to a line consisting of one of
the three standard source \*(L"terminators\*(R":
.PP
.Vb 1
\& no ModuleName; # optional comment
.Ve
.PP
or:
.PP
.Vb 1
\& __END__
.Ve
.PP
or:
.PP
.Vb 1
\& __DATA__
.Ve
.PP
but this can be altered by passing a second argument to \f(CW\*(C`use Filter::Simple\*(C'\fR
or \f(CW\*(C`FILTER\*(C'\fR (just remember: there's \fIno\fR comma after the initial block when
you use \f(CW\*(C`FILTER\*(C'\fR).
.PP
That second argument may be either a \f(CW\*(C`qr\*(C'\fR'd regular expression (which is then
used to match the terminator line), or a defined false value (which indicates
that no terminator line should be looked for), or a reference to a hash
(in which case the terminator is the value associated with the key
\&\f(CW'terminator'\fR.
.PP
For example, to cause the previous filter to filter only up to a line of the
form:
.PP
.Vb 1
\& GNAB esu;
.Ve
.PP
you would write:
.PP
.Vb 2
\& package BANG;
\& use Filter::Simple;
.Ve
.PP
.Vb 4
\& FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& }
\& qr/^\es*GNAB\es+esu\es*;\es*?$/;
.Ve
.PP
or:
.PP
.Vb 4
\& FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& }
\& { terminator => qr/^\es*GNAB\es+esu\es*;\es*?$/ };
.Ve
.PP
and to prevent the filter's being turned off in any way:
.PP
.Vb 2
\& package BANG;
\& use Filter::Simple;
.Ve
.PP
.Vb 4
\& FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& }
\& ""; # or: 0
.Ve
.PP
or:
.PP
.Vb 4
\& FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& }
\& { terminator => "" };
.Ve
.PP
\&\fBNote that, no matter what you set the terminator pattern to,
the actual terminator itself \f(BImust\fB be contained on a single source line.\fR
.Sh "All-in-one interface"
.IX Subsection "All-in-one interface"
Separating the loading of Filter::Simple:
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
from the setting up of the filtering:
.PP
.Vb 1
\& FILTER { ... };
.Ve
.PP
is useful because it allows other code (typically parser support code
or caching variables) to be defined before the filter is invoked.
However, there is often no need for such a separation.
.PP
In those cases, it is easier to just append the filtering subroutine and
any terminator specification directly to the \f(CW\*(C`use\*(C'\fR statement that loads
Filter::Simple, like so:
.PP
.Vb 3
\& use Filter::Simple sub {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& };
.Ve
.PP
This is exactly the same as:
.PP
.Vb 6
\& use Filter::Simple;
\& BEGIN {
\& Filter::Simple::FILTER {
\& s/BANG\es+BANG/die 'BANG' if \e$BANG/g;
\& };
\& }
.Ve
.PP
except that the \f(CW\*(C`FILTER\*(C'\fR subroutine is not exported by Filter::Simple.
.Sh "Filtering only specific components of source code"
.IX Subsection "Filtering only specific components of source code"
One of the problems with a filter like:
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
.Vb 1
\& FILTER { s/BANG\es+BANG/die 'BANG' if \e$BANG/g };
.Ve
.PP
is that it indiscriminately applies the specified transformation to
the entire text of your source program. So something like:
.PP
.Vb 2
\& warn 'BANG BANG, YOU'RE DEAD';
\& BANG BANG;
.Ve
.PP
will become:
.PP
.Vb 2
\& warn 'die 'BANG' if $BANG, YOU'RE DEAD';
\& die 'BANG' if $BANG;
.Ve
.PP
It is very common when filtering source to only want to apply the filter
to the non-character-string parts of the code, or alternatively to \fIonly\fR
the character strings.
.PP
Filter::Simple supports this type of filtering by automatically
exporting the \f(CW\*(C`FILTER_ONLY\*(C'\fR subroutine.
.PP
\&\f(CW\*(C`FILTER_ONLY\*(C'\fR takes a sequence of specifiers that install separate
(and possibly multiple) filters that act on only parts of the source code.
For example:
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
.Vb 3
\& FILTER_ONLY
\& code => sub { s/BANG\es+BANG/die 'BANG' if \e$BANG/g },
\& quotelike => sub { s/BANG\es+BANG/CHITTY CHITTY/g };
.Ve
.PP
The \f(CW"code"\fR subroutine will only be used to filter parts of the source
code that are not quotelikes, \s-1POD\s0, or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. The \f(CW\*(C`quotelike\*(C'\fR
subroutine only filters Perl quotelikes (including here documents).
.PP
The full list of alternatives is:
.ie n .IP """code""" 4
.el .IP "\f(CW``code''\fR" 4
.IX Item """code"""
Filters only those sections of the source code that are not quotelikes, \s-1POD\s0, or
\&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
.ie n .IP """executable""" 4
.el .IP "\f(CW``executable''\fR" 4
.IX Item """executable"""
Filters only those sections of the source code that are not \s-1POD\s0 or \f(CW\*(C`_\|_DATA_\|_\*(C'\fR.
.ie n .IP """quotelike""" 4
.el .IP "\f(CW``quotelike''\fR" 4
.IX Item """quotelike"""
Filters only Perl quotelikes (as interpreted by
\&\f(CW&Text::Balanced::extract_quotelike\fR).
.ie n .IP """string""" 4
.el .IP "\f(CW``string''\fR" 4
.IX Item """string"""
Filters only the string literal parts of a Perl quotelike (i.e. the
contents of a string literal, either half of a \f(CW\*(C`tr///\*(C'\fR, the second
half of an \f(CW\*(C`s///\*(C'\fR).
.ie n .IP """regex""" 4
.el .IP "\f(CW``regex''\fR" 4
.IX Item """regex"""
Filters only the pattern literal parts of a Perl quotelike (i.e. the
contents 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).
.ie n .IP """all""" 4
.el .IP "\f(CW``all''\fR" 4
.IX Item """all"""
Filters everything. Identical in effect to \f(CW\*(C`FILTER\*(C'\fR.
.PP
Except for \f(CW\*(C`FILTER_ONLY code => sub {...}\*(C'\fR, each of
the component filters is called repeatedly, once for each component
found in the source code.
.PP
Note that you can also apply two or more of the same type of filter in
a single \f(CW\*(C`FILTER_ONLY\*(C'\fR. For example, here's a simple
macro-preprocessor that is only applied within regexes,
with a final debugging pass that prints the resulting source code:
.PP
.Vb 6
\& use Regexp::Common;
\& FILTER_ONLY
\& regex => sub { s/!\e[/[^/g },
\& regex => sub { s/%d/$RE{num}{int}/g },
\& regex => sub { s/%f/$RE{num}{real}/g },
\& all => sub { print if $::DEBUG };
.Ve
.Sh "Filtering only the code parts of source code"
.IX Subsection "Filtering only the code parts of source code"
Most source code ceases to be grammatically correct when it is broken up
into the pieces between string literals and regexes. So the \f(CW'code'\fR
component filter behaves slightly differently from the other partial filters
described in the previous section.
.PP
Rather than calling the specified processor on each individual piece of
code (i.e. on the bits between quotelikes), the \f(CW'code'\fR partial filter
operates on the entire source code, but with the quotelike bits
\&\*(L"blanked out\*(R".
.PP
That is, a \f(CW'code'\fR filter \fIreplaces\fR each quoted string, quotelike,
regex, \s-1POD\s0, and _\|_DATA_\|_ section with a placeholder. The
delimiters of this placeholder are the contents of the \f(CW$;\fR variable
at the time the filter is applied (normally \f(CW"\e034"\fR). The remaining
four bytes are a unique identifier for the component being replaced.
.PP
This approach makes it comparatively easy to write code preprocessors
without worrying about the form or contents of strings, regexes, etc.
For convenience, during a \f(CW'code'\fR filtering operation, Filter::Simple
provides a package variable (\f(CW$Filter::Simple::placeholder\fR) that contains
a pre-compiled regex that matches any placeholder. Placeholders can be
moved and re-ordered within the source code as needed.
.PP
Once the filtering has been applied, the original strings, regexes,
\&\s-1POD\s0, etc. are re-inserted into the code, by replacing each
placeholder with the corresponding original component.
.PP
For example, the following filter detects concatentated pairs of
strings/quotelikes and reverses the order in which they are
concatenated:
.PP
.Vb 2
\& package DemoRevCat;
\& use Filter::Simple;
.Ve
.PP
.Vb 3
\& FILTER_ONLY code => sub { my $ph = $Filter::Simple::placeholder;
\& s{ ($ph) \es* [.] \es* ($ph) }{ $2.$1 }gx
\& };
.Ve
.PP
Thus, the following code:
.PP
.Vb 1
\& use DemoRevCat;
.Ve
.PP
.Vb 1
\& my $str = "abc" . q(def);
.Ve
.PP
.Vb 1
\& print "$str\en";
.Ve
.PP
would become:
.PP
.Vb 1
\& my $str = q(def)."abc";
.Ve
.PP
.Vb 1
\& print "$str\en";
.Ve
.PP
and hence print:
.PP
.Vb 1
\& defabc
.Ve
.ie n .Sh "Using Filter::Simple with an explicit ""import"" subroutine"
.el .Sh "Using Filter::Simple with an explicit \f(CWimport\fP subroutine"
.IX Subsection "Using Filter::Simple with an explicit import subroutine"
Filter::Simple generates a special \f(CW\*(C`import\*(C'\fR subroutine for
your module (see \*(L"How it works\*(R") which would normally replace any
\&\f(CW\*(C`import\*(C'\fR subroutine you might have explicitly declared.
.PP
However, Filter::Simple is smart enough to notice your existing
\&\f(CW\*(C`import\*(C'\fR and Do The Right Thing with it.
That is, if you explicitly define an \f(CW\*(C`import\*(C'\fR subroutine in a package
that's using Filter::Simple, that \f(CW\*(C`import\*(C'\fR subroutine will still
be invoked immediately after any filter you install.
.PP
The only thing you have to remember is that the \f(CW\*(C`import\*(C'\fR subroutine
\&\fImust\fR be declared \fIbefore\fR the filter is installed. If you use \f(CW\*(C`FILTER\*(C'\fR
to install the filter:
.PP
.Vb 1
\& package Filter::TurnItUpTo11;
.Ve
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
.Vb 1
\& FILTER { s/(\ew+)/\eU$1/ };
.Ve
.PP
that will almost never be a problem, but if you install a filtering
subroutine by passing it directly to the \f(CW\*(C`use Filter::Simple\*(C'\fR
statement:
.PP
.Vb 1
\& package Filter::TurnItUpTo11;
.Ve
.PP
.Vb 1
\& use Filter::Simple sub{ s/(\ew+)/\eU$1/ };
.Ve
.PP
then you must make sure that your \f(CW\*(C`import\*(C'\fR subroutine appears before
that \f(CW\*(C`use\*(C'\fR statement.
.Sh "Using Filter::Simple and Exporter together"
.IX Subsection "Using Filter::Simple and Exporter together"
Likewise, Filter::Simple is also smart enough
to Do The Right Thing if you use Exporter:
.PP
.Vb 3
\& package Switch;
\& use base Exporter;
\& use Filter::Simple;
.Ve
.PP
.Vb 2
\& @EXPORT = qw(switch case);
\& @EXPORT_OK = qw(given when);
.Ve
.PP
.Vb 1
\& FILTER { $_ = magic_Perl_filter($_) }
.Ve
.PP
Immediately after the filter has been applied to the source,
Filter::Simple will pass control to Exporter, so it can do its magic too.
.PP
Of course, here too, Filter::Simple has to know you're using Exporter
before it applies the filter. That's almost never a problem, but if you're
nervous about it, you can guarantee that things will work correctly by
ensuring that your \f(CW\*(C`use base Exporter\*(C'\fR always precedes your
\&\f(CW\*(C`use Filter::Simple\*(C'\fR.
.Sh "How it works"
.IX Subsection "How it works"
The Filter::Simple module exports into the package that calls \f(CW\*(C`FILTER\*(C'\fR
(or \f(CW\*(C`use\*(C'\fRs it directly) \*(-- such as package \*(L"\s-1BANG\s0\*(R" in the above example \*(--
two automagically constructed
subroutines \*(-- \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR \*(-- which take care of all the
nasty details.
.PP
In addition, the generated \f(CW\*(C`import\*(C'\fR subroutine passes its own argument
list to the filtering subroutine, so the \s-1BANG\s0.pm filter could easily
be made parametric:
.PP
.Vb 1
\& package BANG;
.Ve
.PP
.Vb 1
\& use Filter::Simple;
.Ve
.PP
.Vb 4
\& FILTER {
\& my ($die_msg, $var_name) = @_;
\& s/BANG\es+BANG/die '$die_msg' if \e${$var_name}/g;
\& };
.Ve
.PP
.Vb 1
\& # and in some user code:
.Ve
.PP
.Vb 1
\& use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
.Ve
.PP
The specified filtering subroutine is called every time a \f(CW\*(C`use BANG\*(C'\fR is
encountered, and passed all the source code following that call, up to
either the next \f(CW\*(C`no BANG;\*(C'\fR (or whatever terminator you've set) or the
end of the source file, whichever occurs first. By default, any \f(CW\*(C`no
BANG;\*(C'\fR call must appear by itself on a separate line, or it is ignored.
.SH "AUTHOR"
.IX Header "AUTHOR"
Damian Conway (damian@conway.org)
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
.Vb 3
\& Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.
\& This module is free software. It may be used, redistributed
\& and/or modified under the same terms as Perl itself.
.Ve
|