perlfilter.html

来自「perl教程」· HTML 代码 · 共 629 行 · 第 1/3 页

HTML
629
字号
I'll devote the most space to it.</p>
<p>
</p>
<hr />
<h1><a name="writing_a_source_filter_in_c">WRITING A SOURCE FILTER IN C</a></h1>
<p>The first of the three available techniques is to write the filter
completely in C. The external module you create interfaces directly
with the source filter hooks provided by Perl.</p>
<p>The advantage of this technique is that you have complete control over
the implementation of your filter. The big disadvantage is the
increased complexity required to write the filter - not only do you
need to understand the source filter hooks, but you also need a
reasonable knowledge of Perl guts. One of the few times it is worth
going to this trouble is when writing a source scrambler. The
<code>decrypt</code> filter (which unscrambles the source before Perl parses it)
included with the source filter distribution is an example of a C
source filter (see Decryption Filters, below).</p>
<dl>
<dt><strong><a name="item_decryption_filters"><strong>Decryption Filters</strong></a></strong>

<dd>
<p>All decryption filters work on the principle of &quot;security through
obscurity.&quot; Regardless of how well you write a decryption filter and
how strong your encryption algorithm, anyone determined enough can
retrieve the original source code. The reason is quite simple - once
the decryption filter has decrypted the source back to its original
form, fragments of it will be stored in the computer's memory as Perl
parses it. The source might only be in memory for a short period of
time, but anyone possessing a debugger, skill, and lots of patience can
eventually reconstruct your program.</p>
</dd>
<dd>
<p>That said, there are a number of steps that can be taken to make life
difficult for the potential cracker. The most important: Write your
decryption filter in C and statically link the decryption module into
the Perl binary. For further tips to make life difficult for the
potential cracker, see the file <em>decrypt.pm</em> in the source filters
module.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="creating_a_source_filter_as_a_separate_executable">CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE</a></h1>
<p>An alternative to writing the filter in C is to create a separate
executable in the language of your choice. The separate executable
reads from standard input, does whatever processing is necessary, and
writes the filtered data to standard output. <code>Filter:cpp</code> is an
example of a source filter implemented as a separate executable - the
executable is the C preprocessor bundled with your C compiler.</p>
<p>The source filter distribution includes two modules that simplify this
task: <code>Filter::exec</code> and <code>Filter::sh</code>. Both allow you to run any
external executable. Both use a coprocess to control the flow of data
into and out of the external executable. (For details on coprocesses,
see Stephens, W.R. &quot;Advanced Programming in the UNIX Environment.&quot;
Addison-Wesley, ISBN 0-210-56317-7, pages 441-445.) The difference
between them is that <code>Filter::exec</code> spawns the external command
directly, while <code>Filter::sh</code> spawns a shell to execute the external
command. (Unix uses the Bourne shell; NT uses the cmd shell.) Spawning
a shell allows you to make use of the shell metacharacters and
redirection facilities.</p>
<p>Here is an example script that uses <code>Filter::sh</code>:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Filter::sh</span> <span class="string">'tr XYZ PQR'</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">"XYZ a = $a\n"</span><span class="operator">;</span>
</pre>
<p>The output you'll get when the script is executed:</p>
<pre>
    PQR a = 1</pre>
<p>Writing a source filter as a separate executable works fine, but a
small performance penalty is incurred. For example, if you execute the
small example above, a separate subprocess will be created to run the
Unix <code>tr</code> command. Each use of the filter requires its own subprocess.
If creating subprocesses is expensive on your system, you might want to
consider one of the other options for creating source filters.</p>
<p>
</p>
<hr />
<h1><a name="writing_a_source_filter_in_perl">WRITING A SOURCE FILTER IN PERL</a></h1>
<p>The easiest and most portable option available for creating your own
source filter is to write it completely in Perl. To distinguish this
from the previous two techniques, I'll call it a Perl source filter.</p>
<p>To help understand how to write a Perl source filter we need an example
to study. Here is a complete source filter that performs rot13
decoding. (Rot13 is a very simple encryption scheme used in Usenet
postings to hide the contents of offensive posts. It moves every letter
forward thirteen places, so that A becomes N, B becomes O, and Z
becomes M.)</p>
<pre>
   <span class="keyword">package</span> <span class="variable">Rot13</span><span class="operator">;</span>
</pre>
<pre>
   <span class="keyword">use</span> <span class="variable">Filter::Util::Call</span><span class="operator">;</span>
</pre>
<pre>
   <span class="keyword">sub</span><span class="variable"> import </span><span class="operator">{</span>
      <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$type</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
      <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$ref</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">[]</span><span class="operator">;</span>
      <span class="variable">filter_add</span><span class="operator">(</span><span class="keyword">bless</span> <span class="variable">$ref</span><span class="operator">);</span>
   <span class="operator">}</span>
</pre>
<pre>
   <span class="keyword">sub</span><span class="variable"> filter </span><span class="operator">{</span>
      <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$self</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
      <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$status</span><span class="operator">);</span>
</pre>
<pre>
      <span class="regex">tr/n-za-mN-ZA-M/a-zA-Z/</span>
         <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$status</span> <span class="operator">=</span> <span class="variable">filter_read</span><span class="operator">())</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">;</span>
      <span class="variable">$status</span><span class="operator">;</span>
         <span class="operator">}</span>
</pre>
<pre>
   <span class="number">1</span><span class="operator">;</span>
</pre>
<p>All Perl source filters are implemented as Perl classes and have the
same basic structure as the example above.</p>
<p>First, we include the <code>Filter::Util::Call</code> module, which exports a
number of functions into your filter's namespace. The filter shown
above uses two of these functions, <code>filter_add()</code> and
<code>filter_read()</code>.</p>
<p>Next, we create the filter object and associate it with the source
stream by defining the <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> function. If you know Perl well
enough, you know that <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> is called automatically every time a
module is included with a use statement. This makes <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> the ideal
place to both create and install a filter object.</p>
<p>In the example filter, the object (<code>$ref</code>) is blessed just like any
other Perl object. Our example uses an anonymous array, but this isn't
a requirement. Because this example doesn't need to store any context
information, we could have used a scalar or hash reference just as
well. The next section demonstrates context data.</p>
<p>The association between the filter object and the source stream is made
with the <code>filter_add()</code> function. This takes a filter object as a
parameter (<code>$ref</code> in this case) and installs it in the source stream.</p>
<p>Finally, there is the code that actually does the filtering. For this
type of Perl source filter, all the filtering is done in a method
called <code>filter()</code>. (It is also possible to write a Perl source filter
using a closure. See the <code>Filter::Util::Call</code> manual page for more
details.) It's called every time the Perl parser needs another line of
source to process. The <code>filter()</code> method, in turn, reads lines from
the source stream using the <code>filter_read()</code> function.</p>
<p>If a line was available from the source stream, <code>filter_read()</code>
returns a status value greater than zero and appends the line to <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>.
A status value of zero indicates end-of-file, less than zero means an
error. The filter function itself is expected to return its status in
the same way, and put the filtered line it wants written to the source
stream in <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>. The use of <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> accounts for the brevity of most Perl
source filters.</p>
<p>In order to make use of the rot13 filter we need some way of encoding
the source file in rot13 format. The script below, <code>mkrot13</code>, does
just that.</p>
<pre>
    <span class="keyword">die</span> <span class="string">"usage mkrot13 filename\n"</span> <span class="keyword">unless</span> <span class="variable">@ARGV</span><span class="operator">;</span>
    <span class="keyword">my</span> <span class="variable">$in</span> <span class="operator">=</span> <span class="variable">$ARGV</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">;</span>
    <span class="keyword">my</span> <span class="variable">$out</span> <span class="operator">=</span> <span class="string">"$in.tmp"</span><span class="operator">;</span>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">IN</span><span class="operator">,</span> <span class="string">"&lt;$in"</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Cannot open file $in: $!\n"</span><span class="operator">;</span>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">OUT</span><span class="operator">,</span> <span class="string">"&gt;$out"</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Cannot open file $out: $!\n"</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="variable">OUT</span> <span class="string">"use Rot13;\n"</span><span class="operator">;</span>
    <span class="keyword">while</span> <span class="operator">(&lt;</span><span class="variable">IN</span><span class="operator">&gt;)</span> <span class="operator">{</span>
       <span class="regex">tr/a-zA-Z/n-za-mN-ZA-M/</span><span class="operator">;</span>
       <span class="keyword">print</span> <span class="variable">OUT</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">close</span> <span class="variable">IN</span><span class="operator">;</span>
    <span class="keyword">close</span> <span class="variable">OUT</span><span class="operator">;</span>
    <span class="keyword">unlink</span> <span class="variable">$in</span><span class="operator">;</span>
    <span class="keyword">rename</span> <span class="variable">$out</span><span class="operator">,</span> <span class="variable">$in</span><span class="operator">;</span>
</pre>
<p>If we encrypt this with <code>mkrot13</code>:</p>
<pre>
    <span class="keyword">print</span> <span class="string">" hello fred \n"</span><span class="operator">;</span>
</pre>
<p>the result will be this:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Rot13</span><span class="operator">;</span>
    <span class="variable">cevag</span> <span class="string">"uryyb serq\a"</span><span class="operator">;</span>
</pre>
<p>Running it produces this output:</p>
<pre>
    hello fred</pre>
<p>
</p>
<hr />
<h1><a name="using_context__the_debug_filter">USING CONTEXT: THE DEBUG FILTER</a></h1>
<p>The rot13 example was a trivial example. Here's another demonstration
that shows off a few more features.</p>
<p>Say you wanted to include a lot of debugging code in your Perl script
during development, but you didn't want it available in the released
product. Source filters offer a solution. In order to keep the example
simple, let's say you wanted the debugging output to be controlled by
an environment variable, <code>DEBUG</code>. Debugging code is enabled if the
variable exists, otherwise it is disabled.</p>
<p>Two special marker lines will bracket debugging code, like this:</p>
<pre>
    <span class="comment">## DEBUG_BEGIN</span>
    <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$year</span> <span class="operator">&gt;</span> <span class="number">1999</span><span class="operator">)</span> <span class="operator">{</span>
       <span class="keyword">warn</span> <span class="string">"Debug: millennium bug in year $year\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
    <span class="comment">## DEBUG_END</span>
</pre>
<p>When the <code>DEBUG</code> environment variable exists, the filter ensures that
Perl parses only the code between the <code>DEBUG_BEGIN</code> and <code>DEBUG_END</code>
markers. That means that when <code>DEBUG</code> does exist, the code above
should be passed through the filter unchanged. The marker lines can
also be passed through as-is, because the Perl parser will see them as

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?