1 package Filter::Simple;
3 use vars qw{ $VERSION };
7 use Filter::Util::Call;
11 if (@_>1) { shift; goto &FILTER }
12 else { *{caller()."::FILTER"} = \&FILTER }
17 my ($filter, $terminator) = @_;
18 croak "Usage: use Filter::Simple sub {...}, $terminator_opt;"
19 unless ref $filter eq CODE;
20 *{"${caller}::import"} = gen_filter_import($caller,$filter,$terminator);
21 *{"${caller}::unimport"} = \*filter_unimport;
24 sub gen_filter_import {
25 my ($class, $filter, $terminator) = @_;
27 my ($imported_class, @args) = @_;
28 $terminator = qr/^\s*no\s+$imported_class\s*;\s*$/
29 unless defined $terminator;
35 while ($status = filter_read()) {
36 return $status if $status < 0;
37 if ($terminator && m/$terminator/) {
46 $filter->(@args) unless $status < 0;
47 $_ .= "no $imported_class;\n" if $off;
64 Filter::Simple - Simplified source filtering
79 # use Filter::Simple sub { ... };
85 # this code is filtered
96 Source filtering is an immensely powerful feature of recent versions of Perl.
97 It allows one to extend the language itself (e.g. the Switch module), to
98 simplify the language (e.g. Language::Pythonesque), or to completely recast the
99 language (e.g. Lingua::Romana::Perligata). Effectively, it allows one to use
100 the full power of Perl as its own, recursively applied, macro language.
102 The excellent Filter::Util::Call module (by Paul Marquess) provides a
103 usable Perl interface to source filtering, but it is often too powerful
104 and not nearly as simple as it could be.
106 To use the module it is necessary to do the following:
112 Download, build, and install the Filter::Util::Call module.
113 (If you have Perl 5.7.1 or later you already have Filter::Util::Call.)
117 Set up a module that does a C<use Filter::Util::Call>.
121 Within that module, create an C<import> subroutine.
125 Within the C<import> subroutine do a call to C<filter_add>, passing
126 it either a subroutine reference.
130 Within the subroutine reference, call C<filter_read> or C<filter_read_exact>
131 to "prime" $_ with source code data from the source file that will
132 C<use> your module. Check the status value returned to see if any
133 source code was actually read in.
137 Process the contents of $_ to change the source code in the desired manner.
141 Return the status value.
145 If the act of unimporting your module (via a C<no>) should cause source
146 code filtering to cease, create an C<unimport> subroutine, and have it call
147 C<filter_del>. Make sure that the call to C<filter_read> or
148 C<filter_read_exact> in step 5 will not accidentally read past the
149 C<no>. Effectively this limits source code filters to line-by-line
150 operation, unless the C<import> subroutine does some fancy
151 pre-pre-parsing of the source code it's filtering.
155 For example, here is a minimal source code filter in a module named
156 BANG.pm. It simply converts every occurrence of the sequence C<BANG\s+BANG>
157 to the sequence C<die 'BANG' if $BANG> in any piece of code following a
158 C<use BANG;> statement (until the next C<no BANG;> statement, if any):
162 use Filter::Util::Call ;
167 my ($status, $no_seen, $data);
168 while ($status = filter_read()) {
169 if (/^\s*no\s+$caller\s*;\s*?$/) {
177 s/BANG\s+BANG/die 'BANG' if \$BANG/g
179 $_ .= "no $class;\n" if $no_seen;
190 This level of sophistication puts filtering out of the reach of
196 The Filter::Simple module provides a simplified interface to
197 Filter::Util::Call; one that is sufficient for most common cases.
199 Instead of the above process, with Filter::Simple the task of setting up
200 a source code filter is reduced to:
206 Set up a module that does a C<use Filter::Simple> and then
207 calls C<FILTER { ... }>.
211 Within the anonymous subroutine or block that is passed to
212 C<FILTER>, process the contents of $_ to change the source code in
217 In other words, the previous example, would become:
223 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
229 =head2 Disabling or changing <no> behaviour
231 By default, the installed filter only filters to a line of the form:
235 but this can be altered by passing a second argument to C<use Filter::Simple>.
237 That second argument may be either a C<qr>'d regular expression (which is then
238 used to match the terminator line), or a defined false value (which indicates
239 that no terminator line should be looked for).
241 For example, to cause the previous filter to filter only up to a line of the
252 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
254 => qr/^\s*GNAB\s+esu\s*;\s*?$/;
256 and to prevent the filter's being turned off in any way:
262 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
268 =head2 All-in-one interface
270 Separating the loading of Filter::Simple:
274 from the setting up of the filtering:
278 is useful because it allows other code (typically parser support code
279 or caching variables) to be defined before the filter is invoked.
280 However, there is often no need for such a separation.
282 In those cases, it is easier to just append the filtering subroutine and
283 any terminator specification directly to the C<use> statement that loads
284 Filter::Simple, like so:
286 use Filter::Simple sub {
287 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
290 This is exactly the same as:
294 Filter::Simple::FILTER {
295 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
299 except that the C<FILTER> subroutine is not exported by Filter::Simple.
304 The Filter::Simple module exports into the package that calls C<FILTER>
305 (or C<use>s it directly) -- such as package "BANG" in the above example --
306 two automagically constructed
307 subroutines -- C<import> and C<unimport> -- which take care of all the
310 In addition, the generated C<import> subroutine passes its own argument
311 list to the filtering subroutine, so the BANG.pm filter could easily
319 my ($die_msg, $var_name) = @_;
320 s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g;
323 # and in some user code:
325 use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
328 The specified filtering subroutine is called every time a C<use BANG> is
329 encountered, and passed all the source code following that call, up to
330 either the next C<no BANG;> (or whatever terminator you've set) or the
331 end of the source file, whichever occurs first. By default, any C<no
332 BANG;> call must appear by itself on a separate line, or it is ignored.
337 Damian Conway (damian@conway.org)
341 Copyright (c) 2000, Damian Conway. All Rights Reserved.
342 This module is free software. It may be used, redistributed
343 and/or modified under the terms of the Perl Artistic License
344 (see http://www.perl.com/perl/misc/Artistic.html)