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.
116 Set up a module that does a C<use Filter::Util::Call>.
120 Within that module, create an C<import> subroutine.
124 Within the C<import> subroutine do a call to C<filter_add>, passing
125 it either a subroutine reference.
129 Within the subroutine reference, call C<filter_read> or C<filter_read_exact>
130 to "prime" $_ with source code data from the source file that will
131 C<use> your module. Check the status value returned to see if any
132 source code was actually read in.
136 Process the contents of $_ to change the source code in the desired manner.
140 Return the status value.
144 If the act of unimporting your module (via a C<no>) should cause source
145 code filtering to cease, create an C<unimport> subroutine, and have it call
146 C<filter_del>. Make sure that the call to C<filter_read> or
147 C<filter_read_exact> in step 5 will not accidentally read past the
148 C<no>. Effectively this limits source code filters to line-by-line
149 operation, unless the C<import> subroutine does some fancy
150 pre-pre-parsing of the source code it's filtering.
154 For example, here is a minimal source code filter in a module named
155 BANG.pm. It simply converts every occurrence of the sequence C<BANG\s+BANG>
156 to the sequence C<die 'BANG' if $BANG> in any piece of code following a
157 C<use BANG;> statement (until the next C<no BANG;> statement, if any):
161 use Filter::Util::Call ;
166 my ($status, $no_seen, $data);
167 while ($status = filter_read()) {
168 if (/^\s*no\s+$caller\s*;\s*?$/) {
176 s/BANG\s+BANG/die 'BANG' if \$BANG/g
178 $_ .= "no $class;\n" if $no_seen;
189 This level of sophistication puts filtering out of the reach of
195 The Filter::Simple module provides a simplified interface to
196 Filter::Util::Call; one that is sufficient for most common cases.
198 Instead of the above process, with Filter::Simple the task of setting up
199 a source code filter is reduced to:
205 Set up a module that does a C<use Filter::Simple> and then
206 calls C<FILTER { ... }>.
210 Within the anonymous subroutine or block that is passed to
211 C<FILTER>, process the contents of $_ to change the source code in
216 In other words, the previous example, would become:
222 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
228 =head2 Disabling or changing <no> behaviour
230 By default, the installed filter only filters to a line of the form:
234 but this can be altered by passing a second argument to C<use Filter::Simple>.
236 That second argument may be either a C<qr>'d regular expression (which is then
237 used to match the terminator line), or a defined false value (which indicates
238 that no terminator line should be looked for).
240 For example, to cause the previous filter to filter only up to a line of the
251 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
253 => qr/^\s*GNAB\s+esu\s*;\s*?$/;
255 and to prevent the filter's being turned off in any way:
261 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
267 =head2 All-in-one interface
269 Separating the loading of Filter::Simple:
273 from the setting up of the filtering:
277 is useful because it allows other code (typically parser support code
278 or caching variables) to be defined before the filter is invoked.
279 However, there is often no need for such a separation.
281 In those cases, it is easier to just append the filtering subroutine and
282 any terminator specification directly to the C<use> statement that loads
283 Filter::Simple, like so:
285 use Filter::Simple sub {
286 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
289 This is exactly the same as:
293 Filter::Simple::FILTER {
294 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
298 except that the C<FILTER> subroutine is not exported by Filter::Simple.
303 The Filter::Simple module exports into the package that calls C<FILTER>
304 (or C<use>s it directly) -- such as package "BANG" in the above example --
305 two automagically constructed
306 subroutines -- C<import> and C<unimport> -- which take care of all the
309 In addition, the generated C<import> subroutine passes its own argument
310 list to the filtering subroutine, so the BANG.pm filter could easily
318 my ($die_msg, $var_name) = @_;
319 s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g;
322 # and in some user code:
324 use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
327 The specified filtering subroutine is called every time a C<use BANG> is
328 encountered, and passed all the source code following that call, up to
329 either the next C<no BANG;> (or whatever terminator you've set) or the
330 end of the source file, whichever occurs first. By default, any C<no
331 BANG;> call must appear by itself on a separate line, or it is ignored.
336 Damian Conway (damian@conway.org)
340 Copyright (c) 2000, Damian Conway. All Rights Reserved.
341 This module is free software. It may be used, redistributed
342 and/or modified under the terms of the Perl Artistic License
343 (see http://www.perl.com/perl/misc/Artistic.html)