perlfilter.html
来自「perl教程」· HTML 代码 · 共 629 行 · 第 1/3 页
HTML
629 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlfilter - Source Filters</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perlfilter - Source Filters</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#concepts">CONCEPTS</a></li>
<li><a href="#using_filters">USING FILTERS</a></li>
<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="#things_to_look_out_for">THINGS TO LOOK OUT FOR</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>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfilter - Source Filters</p>
<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>
</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>
<p>Although there can be any number of source streams in existence at any
given time, only one will be active.</p>
</li>
<li>
<p>Every source stream is associated with only one file.</p>
</li>
</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>
<pre>
compressed gunzip
Perl program ---> source filter ---> parser</pre>
<p>
</p>
<hr />
<h1><a name="using_filters">USING FILTERS</a></h1>
<p>So how do you use a source filter in a Perl script? Above, I said that
a source filter is just a special kind of module. Like all Perl
modules, a source filter is invoked with a use statement.</p>
<p>Say you want to pass your Perl source through the C preprocessor before
execution. You could use the existing <a href="../../lib/Pod/perlrun.html#item__2dp"><code>-P</code></a> command line option to do
this, but as it happens, the source filters distribution comes with a C
preprocessor filter module called Filter::cpp. Let's use that instead.</p>
<p>Below is an example program, <code>cpp_test</code>, which makes use of this filter.
Line numbers have been added to allow specific lines to be referenced
easily.</p>
<pre>
<span class="number">1</span><span class="operator">:</span> <span class="keyword">use</span> <span class="variable">Filter::cpp</span><span class="operator">;</span>
<span class="number">2</span><span class="operator">:</span> <span class="comment">#define TRUE 1</span>
<span class="number">3</span><span class="operator">:</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">TRUE</span><span class="operator">;</span>
<span class="number">4</span><span class="operator">:</span> <span class="keyword">print</span> <span class="string">"a = $a\n"</span><span class="operator">;</span>
</pre>
<p>When you execute this script, Perl creates a source stream for the
file. Before the parser processes any of the lines from the file, the
source stream looks like this:</p>
<pre>
cpp_test ---------> parser</pre>
<p>Line 1, <code>use Filter::cpp</code>, includes and installs the <code>cpp</code> filter
module. All source filters work this way. The use statement is compiled
and executed at compile time, before any more of the file is read, and
it attaches the cpp filter to the source stream behind the scenes. Now
the data flow looks like this:</p>
<pre>
cpp_test ----> cpp filter ----> parser</pre>
<p>As the parser reads the second and subsequent lines from the source
stream, it feeds those lines through the <code>cpp</code> source filter before
processing them. The <code>cpp</code> filter simply passes each line through the
real C preprocessor. The output from the C preprocessor is then
inserted back into the source stream by the filter.</p>
<pre>
.-> cpp --.
| |
| |
| <-'
cpp_test ----> cpp filter ----> parser</pre>
<p>The parser then sees the following code:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Filter::cpp</span><span class="operator">;</span>
<span class="variable">$a</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"a = $a\n"</span><span class="operator">;</span>
</pre>
<p>Let's consider what happens when the filtered code includes another
module with use:</p>
<pre>
<span class="number">1</span><span class="operator">:</span> <span class="keyword">use</span> <span class="variable">Filter::cpp</span><span class="operator">;</span>
<span class="number">2</span><span class="operator">:</span> <span class="comment">#define TRUE 1</span>
<span class="number">3</span><span class="operator">:</span> <span class="keyword">use</span> <span class="variable">Fred</span><span class="operator">;</span>
<span class="number">4</span><span class="operator">:</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">TRUE</span><span class="operator">;</span>
<span class="number">5</span><span class="operator">:</span> <span class="keyword">print</span> <span class="string">"a = $a\n"</span><span class="operator">;</span>
</pre>
<p>The <code>cpp</code> filter does not apply to the text of the Fred module, only
to the text of the file that used it (<code>cpp_test</code>). Although the use
statement on line 3 will pass through the cpp filter, the module that
gets included (<code>Fred</code>) will not. The source streams look like this
after line 3 has been parsed and before line 4 is parsed:</p>
<pre>
cpp_test ---> cpp filter ---> parser (INACTIVE)</pre>
<pre>
Fred.pm ----> parser</pre>
<p>As you can see, a new stream has been created for reading the source
from <code>Fred.pm</code>. This stream will remain active until all of <code>Fred.pm</code>
has been parsed. The source stream for <code>cpp_test</code> will still exist,
but is inactive. Once the parser has finished reading Fred.pm, the
source stream associated with it will be destroyed. The source stream
for <code>cpp_test</code> then becomes active again and the parser reads line 4
and subsequent lines from <code>cpp_test</code>.</p>
<p>You can use more than one source filter on a single file. Similarly,
you can reuse the same filter in as many files as you like.</p>
<p>For example, if you have a uuencoded and compressed source file, it is
possible to stack a uudecode filter and an uncompression filter like
this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Filter::uudecode</span><span class="operator">;</span> <span class="keyword">use</span> <span class="variable">Filter::uncompress</span><span class="operator">;</span>
<span class="variable">M'XL</span><span class="operator">(</span><span class="string">".H<US4''V9I;F%L')Q;>7/;1I;_>_I3=&E=%:F*I"</span><span class="variable">T</span><span class="operator">?</span><span class="number">22</span><span class="variable">Q</span><span class="operator">/</span>
<span class="variable">M6</span><span class="operator">]</span><span class="number">9</span><span class="operator">*<</span><span class="variable">IQCO</span><span class="variable">*XFT</span><span class="string">"0[PL%%'Y+IG?WN^ZYN-$'J.[.JE$,20/?K=_[>
...
</span>
</pre>
<p>Once the first line has been processed, the flow will look like this:</p>
<pre>
file ---> uudecode ---> uncompress ---> parser
filter filter</pre>
<p>Data flows through filters in the same order they appear in the source
file. The uudecode filter appeared before the uncompress filter, so the
source file will be uudecoded before it's uncompressed.</p>
<p>
</p>
<hr />
<h1><a name="writing_a_source_filter">WRITING A SOURCE FILTER</a></h1>
<p>There are three ways to write your own source filter. You can write it
in C, use an external program as a filter, or write the filter in Perl.
I won't cover the first two in any great detail, so I'll get them out
of the way first. Writing the filter in Perl is most convenient, so
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?