Chapter 33. Source Filters

Paul Marquess

Tip

Looking back at this article a number of years after it was written, it strikes me that source filters were a cool idea waiting for a killer application to come along. Things would probably have stayed that way if Damian Conway hadn’t stumbled across them, because he has certainly put them to good use.

Damian currently has five CPAN modules using source filters. The cornerstone module is undoubtedly Filter::Simple, and anyone wanting to play further with source filters must have copy of this module. The interface it provides is much easier to use than the Filter::Util::Call module described in this article and will be adequate for 95% of applications.

Another module worth checking out for a real-life source filter is Switch, which adds a switch statement to Perl 5. The switch statement destined to be included in Perl 6 bears a striking resemblance to this one.

This article is about a little-known feature of Perl called source filters. Source filters alter the program text of a module before Perl sees it, much as a C preprocessor alters the source text of a C program before the compiler sees it. This article tells you more about what source filters are, how they work, and how to write your own.

The original purpose of source filters was to let you encrypt your program source to prevent casual piracy. This isn’t all they can do, as you’ll soon learn. But first, the basics.

Concepts

Before the Perl interpreter can execute a Perl script, the interpreter ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.