<LI><A HREF="#writing a source filter">WRITING A SOURCE FILTER</A></LI>
<LI><A HREF="#writing a source filter in c">WRITING A SOURCE FILTER IN C</A></LI>
<LI><A HREF="#creating a source filter as a separate executable">CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE</A></LI>
<LI><A HREF="#writing a source filter in perl">WRITING A SOURCE FILTER IN PERL</A></LI>
<LI><A HREF="#using context: the debug filter">USING CONTEXT: THE DEBUG FILTER</A></LI>
<LI><A HREF="#conclusion">CONCLUSION</A></LI>
<LI><A HREF="#requirements">REQUIREMENTS</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#copyrights">Copyrights</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>perlfilter - Source Filters</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This article is about a little-known feature of Perl called
<EM>source filters</EM>. 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.</P>
<P>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.</P>
<P>
<HR>
<H1><A NAME="concepts">CONCEPTS</A></H1>
<P>Before the Perl interpreter can execute a Perl script, it must first
read it from a file into memory for parsing and compilation. If that
script itself includes other scripts with a <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_require"><CODE>require</CODE></A>
statement, then each of those scripts will have to be read from their
respective files as well.</P>
<P>Now think of each logical connection between the Perl parser and an
individual file as a <EM>source stream</EM>. A source stream is created when
the Perl parser opens a file, it continues to exist as the source code
is read into memory, and it is destroyed when Perl is finished parsing
the file. If the parser encounters a <A HREF="../../lib/Pod/perlfunc.html#item_require"><CODE>require</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> statement in
a source stream, a new and distinct stream is created just for that
file.</P>
<P>The diagram below represents a single source stream, with the flow of
source from a Perl script file on the left into the Perl parser on the
right. This is how Perl normally operates.</P>
<PRE>
file -------> parser</PRE>
<P>There are two important points to remember:</P>
<OL>
<LI>
Although there can be any number of source streams in existence at any
given time, only one will be active.
<P></P>
<LI>
Every source stream is associated with only one file.
<P></P></OL>
<P>A source filter is a special kind of Perl module that intercepts and
modifies a source stream before it reaches the parser. A source filter
changes our diagram like this:</P>
<PRE>
file ----> filter ----> parser</PRE>
<P>If that doesn't make much sense, consider the analogy of a command
pipeline. Say you have a shell script stored in the compressed file
<EM>trial.gz</EM>. The simple pipeline command below runs the script without
needing to create a temporary file to hold the uncompressed file.</P>
<PRE>
gunzip -c trial.gz | sh</PRE>
<P>In this case, the data flow from the pipeline can be represented as follows:</P>
<PRE>
trial.gz ----> gunzip ----> sh</PRE>
<P>With source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser:</P>