1 package Filter::Simple;
3 use vars qw{ $VERSION };
7 use Filter::Util::Call;
12 my ($class, $filter) = @_;
13 croak "Usage: use Filter::Simple sub {...}" unless ref $filter eq CODE;
14 *{"${caller}::import"} = gen_filter_import($caller, $filter);
15 *{"${caller}::unimport"} = \*filter_unimport;
18 sub gen_filter_import {
19 my ($class, $filter) = @_;
21 my ($imported_class, @args) = @_;
26 while ($status = filter_read()) {
27 if (m/^\s*no\s+$class\s*;\s*$/) {
35 $filter->(@args) unless $status < 0;
36 $_ .= "no $class;\n" if $off;
53 Filter::Simple - Simplified source filtering
61 use Filter::Simple sub { ... };
68 # this code is filtered
79 Source filtering is an immensely powerful feature of recent versions of Perl.
80 It allows one to extend the language itself (e.g. the Switch module), to
81 simplify the language (e.g. Language::Pythonesque), or to completely recast the
82 language (e.g. Lingua::Romana::Perligata). Effectively, it allows one to use
83 the full power of Perl as its own, recursively applied, macro language.
85 The excellent Filter::Util::Call module (by Paul Marquess) provides a
86 usable Perl interface to source filtering, but it is often too powerful
87 and not nearly as simple as it could be.
89 To use the module it is necessary to do the following:
95 Download, build, and install the Filter::Util::Call module.
96 (If you are using Perl 5.7.1 or later, you already have Filter::Util::Call.)
100 Set up a module that does a C<use Filter::Util::Call>.
104 Within that module, create an C<import> subroutine.
108 Within the C<import> subroutine do a call to C<filter_add>, passing
109 it either a subroutine reference.
113 Within the subroutine reference, call C<filter_read> or C<filter_read_exact>
114 to "prime" $_ with source code data from the source file that will
115 C<use> your module. Check the status value returned to see if any
116 source code was actually read in.
120 Process the contents of $_ to change the source code in the desired manner.
124 Return the status value.
128 If the act of unimporting your module (via a C<no>) should cause source
129 code filtering to cease, create an C<unimport> subroutine, and have it call
130 C<filter_del>. Make sure that the call to C<filter_read> or
131 C<filter_read_exact> in step 5 will not accidentally read past the
132 C<no>. Effectively this limits source code filters to line-by-line
133 operation, unless the C<import> subroutine does some fancy
134 pre-pre-parsing of the source code it's filtering.
138 For example, here is a minimal source code filter in a module named
139 BANG.pm. It simply converts every occurrence of the sequence C<BANG\s+BANG>
140 to the sequence C<die 'BANG' if $BANG> in any piece of code following a
141 C<use BANG;> statement (until the next C<no BANG;> statement, if any):
145 use Filter::Util::Call ;
150 my ($status, $no_seen, $data);
151 while ($status = filter_read()) {
152 if (/^\s*no\s+$caller\s*;\s*$/) {
160 s/BANG\s+BANG/die 'BANG' if \$BANG/g
162 $_ .= "no $class;\n" if $no_seen;
173 This level of sophistication puts filtering out of the reach of
179 The Filter::Simple module provides a simplified interface to
180 Filter::Util::Call; one that is sufficient for most common cases.
182 Instead of the above process, with Filter::Simple the task of setting up
183 a source code filter is reduced to:
189 Set up a module that does a C<use Filter::Simple sub { ... }>.
193 Within the anonymous subroutine passed to C<use Filter::Simple>, process the
194 contents of $_ to change the source code in the desired manner.
198 In other words, the previous example, would become:
202 use Filter::Simple sub {
203 s/BANG\s+BANG/die 'BANG' if \$BANG/g;
211 The Filter::Simple module exports into the package that C<use>s it (e.g.
212 package "BANG" in the above example) two automagically constructed
213 subroutines -- C<import> and C<unimport> -- which take care of all the
216 In addition, the generated C<import> subroutine passes its own argument
217 list to the filtering subroutine, so the BANG.pm filter could easily
222 use Filter::Simple sub {
223 my ($die_msg, $var_name) = @_;
224 s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g;
227 # and in some user code:
229 use BANG "BOOM", "BAM; # "BANG BANG" becomes: die 'BOOM' if $BAM
232 The specified filtering subroutine is called every time a C<use BANG>
233 is encountered, and passed all the source code following that call,
234 up to either the next C<no BANG;> call or the end of the source file
235 (whichever occurs first). Currently, any C<no BANG;> call must appear
236 by itself on a separate line, or it is ignored.
241 Damian Conway (damian@conway.org)
245 Copyright (c) 2000, Damian Conway. All Rights Reserved.
246 This module is free software; you can redistribute it and/or
247 modify it under the same terms as Perl itself.