Filter::Simple(3perl)
๐ Filter::Simple ๐
okay bestie, buckle UP, because we're about to make source filtering cute AND easy. This is Filter::Simple, a lil module from the Perl Programmers Reference Guide, and it's here to save you from the absolute saga that is doing source filtering the hard way. โจ
๐ธ NAME ๐ธ
Filter::Simple โ simplified source filtering. that's it, that's the whole vibe. ๐
โจ SYNOPSIS โจ
here's the whole mood in one glance, bestie:
# in MyFilter.pm:
package MyFilter;
use Filter::Simple;
FILTER { ... };
# or just:
#
# use Filter::Simple sub { ... };
# in user's code:
use MyFilter;
# this code is filtered
no MyFilter;
# this code is not
๐ฆ DESCRIPTION ๐ฆ
๐ The Problem
so. source filtering is genuinely an immensely powerful feature of recent Perl versions. like, it lets you:
- extend the language itself (hi,
Switchmodule ๐) - simplify the language (looking at you,
Language::Pythonesque) - or completely recast the language (
Lingua::Romana::Perligata, you absolute icon)
basically it lets you use the full power of Perl as its own, recursively applied, macro language. main character energy fr. ๐
and yes, the excellent Filter::Util::Call module (by Paul Marquess, legend) gives you a usable Perl interface to source filteringโฆ but ngl, it is often too powerful and not nearly as simple as it could be. ๐ญ
to use that module you gotta do all of this:
- Download, build, and install the
Filter::Util::Callmodule. (If you have Perl 5.7.1 or later, this is already done for you. slay.) - Set up a module that does a
use Filter::Util::Call. - Within that module, create an
importsubroutine. - Within the
importsubroutine do a call tofilter_add, passing it either a subroutine reference. - Within the subroutine reference, call
filter_readorfilter_read_exactto "prime"$_with source code data from the source file that will "use" your module. Check the status value returned to see if any source code was actually read in. - Process the contents of
$_to change the source code in the desired manner. - Return the status value.
- If the act of unimporting your module (via a
no) should cause source code filtering to cease, create anunimportsubroutine, and have it callfilter_del. Make sure that the call tofilter_readorfilter_read_exactin step 5 will not accidentally read past theno. Effectively this limits source code filters to line-by-line operation, unless theimportsubroutine does some fancy pre-pre-parsing of the source code it's filtering.
likeโฆ that's a lot. ๐ฎโ๐จ
for example, here's a minimal source code filter in a module named BANG.pm. It simply converts every occurrence of the sequence BANG\s+BANG to the sequence die 'BANG' if $BANG in any piece of code following a use BANG; statement (until the next no BANG; statement, if any):
package BANG;
use Filter::Util::Call ;
sub import {
filter_add( sub {
my $caller = caller;
my ($status, $no_seen, $data);
while ($status = filter_read()) {
if (/^\s*no\s+$caller\s*;\s*?$/) {
$no_seen=1;
last;
}
$data .= $_;
$_ = "";
}
$_ = $data;
s/BANG\s+BANG/die 'BANG' if \$BANG/g
unless $status < 0;
$_ .= "no $class;\n" if $no_seen;
return 1;
})
}
sub unimport {
filter_del();
}
1 ;
yeah. this level of sophistication puts filtering out of the reach of many programmers. it's giving gatekeeping, and we do not stan. ๐
๐ A Solution
enter Filter::Simple. ๐
The Filter::Simple module provides a simplified interface to Filter::Util::Call; one that is sufficient for most common cases. (iykyk)
instead of that whole saga above, with Filter::Simple the task of setting up a source code filter is reduced to:
- Download and install the
Filter::Simplemodule. (If you have Perl 5.7.1 or later, this is already done for you. ๐ซถ) - Set up a module that does a
use Filter::Simpleand then callsFILTER { ... }. - Within the anonymous subroutine or block that is passed to
FILTER, process the contents of$_to change the source code in the desired manner.
literally so much shorter, we love to see it. ๐
in other words, the previous example, would become:
package BANG;
use Filter::Simple;
FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
};
1 ;
โ ๏ธ Note that the source code is passed as a single string, so any regex that uses^or$to detect line boundaries will need the/mflag.
๐ง Disabling or changing <no> behaviour
By default, the installed filter only filters up to a line consisting of one of the three standard source "terminators":
no ModuleName; # optional comment
or:
__END__
or:
__DATA__
but this can be altered by passing a second argument to use Filter::Simple or FILTER (just remember: there's no comma after the initial block when you use FILTER).
That second argument may be either:
- a
qr'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 'terminator'.
for example, to cause the previous filter to filter only up to a line of the form:
GNAB esu;
you would write:
package BANG;
use Filter::Simple;
FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
}
qr/^\s*GNAB\s+esu\s*;\s*?$/;
or:
FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
}
{ terminator => qr/^\s*GNAB\s+esu\s*;\s*?$/ };
and to prevent the filter's being turned off in any way:
package BANG;
use Filter::Simple;
FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
}
""; # or: 0
or:
FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
}
{ terminator => "" };
๐จ Note that, no matter what you set the terminator pattern to, the actual terminator itself must be contained on a single source line. no exceptions bestie.
โญ All-in-one interface
Separating the loading of Filter::Simple:
use Filter::Simple;
from the setting up of the filtering:
FILTER { ... };
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.
In those cases, it is easier to just append the filtering subroutine and any terminator specification directly to the use statement that loads Filter::Simple, like so:
use Filter::Simple sub {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
};
This is exactly the same as:
use Filter::Simple;
BEGIN {
Filter::Simple::FILTER {
s/BANG\s+BANG/die 'BANG' if \$BANG/g;
};
}
except that the FILTER subroutine is not exported by Filter::Simple. noted. ๐
๐ Filtering only specific components of source code
One of the problems with a filter like:
use Filter::Simple;
FILTER { s/BANG\s+BANG/die 'BANG' if \$BANG/g };
is that it indiscriminately applies the specified transformation to the entire text of your source program. So something like:
warn 'BANG BANG, YOU'RE DEAD';
BANG BANG;
will become:
warn 'die 'BANG' if $BANG, YOU'RE DEAD';
die 'BANG' if $BANG;
โฆwhich is not it. ๐ฌ 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 only the character strings.
Filter::Simple supports this type of filtering by automatically exporting the FILTER_ONLY subroutine. ๐
FILTER_ONLY takes a sequence of specifiers that install separate (and possibly multiple) filters that act on only parts of the source code. For example:
use Filter::Simple;
FILTER_ONLY
code => sub { s/BANG\s+BANG/die 'BANG' if \$BANG/g },
quotelike => sub { s/BANG\s+BANG/CHITTY CHITTY/g };
The code subroutine will only be used to filter parts of the source code that are not quotelikes, POD, or __DATA__. The quotelike subroutine only filters Perl quotelikes (including here documents).
The full list of alternatives is:
codeโ Filters only those sections of the source code that are not quotelikes, POD, or__DATA__.code_no_commentsโ Filters only those sections of the source code that are not quotelikes, POD, comments, or__DATA__.executableโ Filters only those sections of the source code that are not POD or__DATA__.executable_no_commentsโ Filters only those sections of the source code that are not POD, comments, or__DATA__.quotelikeโ Filters only Perl quotelikes (as interpreted by&Text::Balanced::extract_quotelike).stringโ Filters only the string literal parts of a Perl quotelike (i.e. the contents of a string literal, either half of atr///, the second half of ans///).regexโ Filters only the pattern literal parts of a Perl quotelike (i.e. the contents of aqr//or anm//, the first half of ans///).allโ Filters everything. Identical in effect toFILTER.
Except for FILTER_ONLY code => sub {...}, each of the component filters is called repeatedly, once for each component found in the source code.
๐ก Note that you can also apply two or more of the same type of filter in a single FILTER_ONLY.
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:
use Regexp::Common;
FILTER_ONLY
regex => sub { s/!\[/[^/g },
regex => sub { s/%d/$RE{num}{int}/g },
regex => sub { s/%f/$RE{num}{real}/g },
all => sub { print if $::DEBUG };
look at that stack of filters, it's giving pipeline realness. ๐ซถ
๐ 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 'code' and 'code_no_comments' component filter behave slightly differently from the other partial filters described in the previous section.
Rather than calling the specified processor on each individual piece of code (i.e. on the bits between quotelikes), the 'code...' partial filters operate on the entire source code, but with the quotelike bits (and, in the case of 'code_no_comments', the comments) "blanked out".
That is, a 'code...' filter replaces each quoted string, quotelike, regex, POD, and __DATA__ section with a placeholder. The delimiters of this placeholder are the contents of the $; variable at the time the filter is applied (normally "\034"). The remaining four bytes are a unique identifier for the component being replaced.
to be continued, bestieโฆ there's more where this came from ๐ซ
So basically: this whole approach means writing code preprocessors is comparatively easy, because you never have to worry about the form or contents of strings, regexes, etc. ๐
And for convenience โ during a 'code...' filtering operation โ Filter::Simple hands you a package variable called $Filter::Simple::placeholder, which holds a pre-compiled regex that matches any placeholder... and captures the identifier inside that placeholder. You can move placeholders around and re-order them within the source code however you need. ๐
Plus there's a second package variable, @Filter::Simple::components, holding a list of the various pieces of $_ exactly as they were originally split up so placeholders could get inserted in the first place.
Once the filtering is done, all the original strings, regexes, POD, etc. get re-inserted into the code by swapping each placeholder back out for its corresponding original component (from @components).
๐จ OK this is the scary part, bestie: that means@componentshas to be treated with extreme care inside the filter. That array stores the "back-translations" of every placeholder you put into$_, plus the interstitial source code living between placeholders. So if you go messing with those placeholder backtranslations in@components, they'll get changed in exactly the same way when the placeholders are pulled out of$_once the filter's finished. Fr fr, hands off unless you mean it. ๐ญ
So for example, this filter here catches concatenated pairs of strings/quotelikes and flips the order they're concatenated in:
package DemoRevCat;
use Filter::Simple;
FILTER_ONLY code => sub {
my $ph = $Filter::Simple::placeholder;
s{ ($ph) \s* [.] \s* ($ph) }{ $2.$1 }gx
};
Which means this code:
use DemoRevCat;
my $str = "abc" . q(def);
print "$str\n";
would become:
my $str = q(def)."abc";
print "$str\n";
and hence print:
defabc
๐ซถ Using Filter::Simple with an explicit import subroutine
Filter::Simple generates a special import subroutine for your module (peep "How it works" below for the tea) โ and normally that would just steamroll any import sub you declared yourself.
But nah, Filter::Simple is smart enough to notice your existing import and Do The Right Thing with it. Meaning: if you explicitly define an import subroutine in a package that's using Filter::Simple, that import subroutine will still get invoked, immediately after any filter you install. Slay. โจ
The one thing you gotta remember: your import subroutine has to be declared before the filter is installed. So if you install the filter with FILTER:
package Filter::TurnItUpTo11;
use Filter::Simple;
FILTER { s/(\w+)/\U$1/ };
that's basically never a problem. But if you install a filtering subroutine by passing it straight to the use Filter::Simple statement:
package Filter::TurnItUpTo11;
use Filter::Simple sub{ s/(\w+)/\U$1/ };
then you must make sure your import subroutine shows up before that use statement. ๐
๐ Using Filter::Simple and Exporter together
Same energy here: Filter::Simple is also smart enough to Do The Right Thing if you're using Exporter:
package Switch;
use base Exporter;
use Filter::Simple;
@EXPORT = qw(switch case);
@EXPORT_OK = qw(given when);
FILTER { $_ = magic_Perl_filter($_) }
The instant the filter has been applied to the source, Filter::Simple passes control over to Exporter so it can go do its magic too.
And yeah, same deal as before: Filter::Simple has to know you're using Exporter before it applies the filter. Again, almost never an issue, but if you're feeling nervous about it, you can guarantee things will work correctly by making sure your use base Exporter always comes before your use Filter::Simple. ๐ง
๐ฆ How it works
The Filter::Simple module exports into the package that calls FILTER (or uses it directly) โ like package BANG in the example up above โ two automagically constructed subroutines: import and unimport. Those two handle all the nasty details for you. ๐
And bonus: the generated import subroutine passes its own argument list through to the filtering subroutine, so the BANG.pm filter could easily be made parametric:
package BANG;
use Filter::Simple;
FILTER {
my ($die_msg, $var_name) = @_;
s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g;
};
# and in some user code:
use BANG "BOOM", "BAM"; # "BANG BANG" becomes: die 'BOOM' if $BAM
The filtering subroutine you specify gets called every single time a use BANG shows up, and it's passed all the source code following that call, up until either the next no BANG; (or whatever terminator you've set) or the end of the source file โ whichever comes first. And by default, any no BANG; call has to appear by itself on its own separate line, or else it gets ignored. ๐ซก
โจ AUTHOR โจ
Damian Conway
๐ CONTACT ๐
Filter::Simple is now maintained by the Perl5-Porters. Got a bug? Please submit it via the perlbug tool that comes with your perl. For usage instructions, read perldoc perlbug or possibly man perlbug. For mostly anything else, please contact <perl5-porters@perl.org>.
Maintainer of the CPAN release is Steffen Mueller <smueller@cpan.org>. Hit him up with technical difficulties regarding the packaging of the CPAN module. ๐ ๏ธ
Praise of the module, flowers, and presents still go to the author, Damian Conway <damian@conway.org>. ๐ธ
๐ COPYRIGHT AND LICENSE ๐
Copyright (c) 2000-2014, 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.
And that's the whole page, bestie ๐ฅน this one's from perl v5.44.0, dated 2026-10-17, and it's the Filter::Simple(3perl) manual page. Go write somewhere it feels like home ๐
โจ
๐ฆ where this page comes from
- arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms