simple.html
来自「perl教程」· HTML 代码 · 共 601 行 · 第 1/3 页
HTML
601 行
</dd>
</li>
</dl>
<p>Except for <code>FILTER_ONLY code => sub {...}</code>, each of
the component filters is called repeatedly, once for each component
found in the source code.</p>
<p>Note that you can also apply two or more of the same type of filter in
a single <code>FILTER_ONLY</code>. 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:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Regexp::Common</span><span class="operator">;</span>
<span class="variable">FILTER_ONLY</span>
<span class="string">regex</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="regex">s/!\[/[^/g</span> <span class="operator">},</span>
<span class="string">regex</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="regex">s/%d/$RE{num}{int}/g</span> <span class="operator">},</span>
<span class="string">regex</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="regex">s/%f/$RE{num}{real}/g</span> <span class="operator">},</span>
<span class="string">all</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">print</span> <span class="keyword">if</span> <span class="variable">$::DEBUG</span> <span class="operator">};</span>
</pre>
<p>
</p>
<h2><a name="filtering_only_the_code_parts_of_source_code">Filtering only the code parts of source code</a></h2>
<pre>
Most source code ceases to be grammatically correct when it is broken up
into the pieces between string literals and regexes. So the C<'code'>
and C<'code_no_comments'> component filter behave slightly differently
from the other partial filters described in the previous section.</pre>
<p>Rather than calling the specified processor on each individual piece of
code (i.e. on the bits between quotelikes), the <code>'code...'</code> partial
filters operate on the entire source code, but with the quotelike bits
(and, in the case of <code>'code_no_comments'</code>, the comments) "blanked out".</p>
<p>That is, a <code>'code...'</code> filter <em>replaces</em> each quoted string, quotelike,
regex, POD, and __DATA__ section with a placeholder. The
delimiters of this placeholder are the contents of the <a href="../../lib/Pod/perlvar.html#item___"><code>$;</code></a> variable
at the time the filter is applied (normally <code>"\034"</code>). The remaining
four bytes are a unique identifier for the component being replaced.</p>
<p>This approach makes it comparatively easy to write code preprocessors
without worrying about the form or contents of strings, regexes, etc.</p>
<p>For convenience, during a <code>'code...'</code> filtering operation, Filter::Simple
provides a package variable (<code>$Filter::Simple::placeholder</code>) that
contains a pre-compiled regex that matches any placeholder...and
captures the identifier within the placeholder. Placeholders can be
moved and re-ordered within the source code as needed.</p>
<p>In addition, a second package variable (<code>@Filter::Simple::components</code>)
contains a list of the various pieces of <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>, as they were originally split
up to allow placeholders to be inserted.</p>
<p>Once the filtering has been applied, the original strings, regexes, POD,
etc. are re-inserted into the code, by replacing each placeholder with
the corresponding original component (from <code>@components</code>). Note that
this means that the <code>@components</code> variable must be treated with extreme
care within the filter. The <code>@components</code> array stores the "back-
translations" of each placeholder inserted into <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>, as well as the
interstitial source code between placeholders. If the placeholder
backtranslations are altered in <code>@components</code>, they will be similarly
changed when the placeholders are removed from <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> after the filter
is complete.</p>
<p>For example, the following filter detects concatentated pairs of
strings/quotelikes and reverses the order in which they are
concatenated:</p>
<pre>
<span class="keyword">package</span> <span class="variable">DemoRevCat</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Filter::Simple</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">FILTER_ONLY</span> <span class="string">code</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$ph</span> <span class="operator">=</span> <span class="variable">$Filter::Simple::placeholder</span><span class="operator">;</span>
<span class="regex">s{ ($ph) \s* [.] \s* ($ph) }{ $2.$1 }gx</span>
<span class="operator">};</span>
</pre>
<p>Thus, the following code:</p>
<pre>
<span class="keyword">use</span> <span class="variable">DemoRevCat</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$str</span> <span class="operator">=</span> <span class="string">"abc"</span> <span class="operator">.</span> <span class="string">q(def)</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"$str\n"</span><span class="operator">;</span>
</pre>
<p>would become:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$str</span> <span class="operator">=</span> <span class="string">q(def)</span><span class="operator">.</span><span class="string">"abc"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"$str\n"</span><span class="operator">;</span>
</pre>
<p>and hence print:</p>
<pre>
defabc</pre>
<p>
</p>
<h2><a name="using_filter__simple_with_an_explicit_import_subroutine">Using Filter::Simple with an explicit <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine</a></h2>
<p>Filter::Simple generates a special <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine for
your module (see <a href="#how_it_works">How it works</a>) which would normally replace any
<a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine you might have explicitly declared.</p>
<p>However, Filter::Simple is smart enough to notice your existing
<a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> and Do The Right Thing with it.
That is, if you explicitly define an <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine in a package
that's using Filter::Simple, that <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine will still
be invoked immediately after any filter you install.</p>
<p>The only thing you have to remember is that the <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine
<em>must</em> be declared <em>before</em> the filter is installed. If you use <code>FILTER</code>
to install the filter:</p>
<pre>
<span class="keyword">package</span> <span class="variable">Filter::TurnItUpTo11</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">Filter::Simple</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">FILTER</span> <span class="operator">{</span> <span class="regex">s/(\w+)/\U$1/</span> <span class="operator">};</span>
<span class="variable">that</span> <span class="variable">will</span> <span class="variable">almost</span> <span class="variable">never</span> <span class="variable">be</span> <span class="variable">a</span> <span class="variable">problem</span><span class="operator">,</span> <span class="variable">but</span> <span class="keyword">if</span> <span class="variable">you</span> <span class="variable">install</span> <span class="variable">a</span> <span class="variable">filtering</span>
<span class="variable">subroutine</span> <span class="variable">by</span> <span class="variable">passing</span> <span class="variable">it</span> <span class="variable">directly</span> <span class="variable">to</span> <span class="variable">the</span> <span class="variable">C</span><span class="operator"><</span><span class="keyword">use</span> <span class="variable">Filter::Simple</span><span class="operator">></span>
<span class="variable">statement</span><span class="operator">:</span>
</pre>
<pre>
<span class="keyword">package</span> <span class="variable">Filter::TurnItUpTo11</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">Filter::Simple</span> <span class="keyword">sub</span><span class="operator">{</span> <span class="regex">s/(\w+)/\U$1/</span> <span class="operator">};</span>
</pre>
<p>then you must make sure that your <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine appears before
that <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement.</p>
<p>
</p>
<h2><a name="using_filter__simple_and_exporter_together">Using Filter::Simple and Exporter together</a></h2>
<p>Likewise, Filter::Simple is also smart enough
to Do The Right Thing if you use Exporter:</p>
<pre>
<span class="keyword">package</span> <span class="variable">Switch</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">base</span> <span class="variable">Exporter</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Filter::Simple</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">@EXPORT</span> <span class="operator">=</span> <span class="string">qw(switch case)</span><span class="operator">;</span>
<span class="variable">@EXPORT_OK</span> <span class="operator">=</span> <span class="string">qw(given when)</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">FILTER</span> <span class="operator">{</span> <span class="variable">$_</span> <span class="operator">=</span> <span class="variable">magic_Perl_filter</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span> <span class="operator">}</span>
</pre>
<p>Immediately after the filter has been applied to the source,
Filter::Simple will pass control to Exporter, so it can do its magic too.</p>
<p>Of course, here too, Filter::Simple has to know you're using Exporter
before it applies the filter. That's almost never a problem, but if you're
nervous about it, you can guarantee that things will work correctly by
ensuring that your <code>use base Exporter</code> always precedes your
<code>use Filter::Simple</code>.</p>
<p>
</p>
<h2><a name="how_it_works">How it works</a></h2>
<p>The Filter::Simple module exports into the package that calls <code>FILTER</code>
(or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>s it directly) -- such as package "BANG" in the above example --
two automagically constructed
subroutines -- <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> and <code>unimport</code> -- which take care of all the
nasty details.</p>
<p>In addition, the generated <a href="../../lib/Pod/perlfunc.html#item_import"><code>import</code></a> subroutine passes its own argument
list to the filtering subroutine, so the BANG.pm filter could easily
be made parametric:</p>
<pre>
<span class="keyword">package</span> <span class="variable">BANG</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Filter::Simple</span><span class="operator">;</span>
<span class="variable">FILTER</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$die_msg</span><span class="operator">,</span> <span class="variable">$var_name</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
<span class="regex">s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g</span><span class="operator">;</span>
<span class="operator">};</span>
</pre>
<pre>
<span class="comment"># and in some user code:</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">BANG</span> <span class="string">"BOOM"</span><span class="operator">,</span> <span class="string">"BAM"</span><span class="operator">;</span> <span class="comment"># "BANG BANG" becomes: die 'BOOM' if $BAM</span>
</pre>
<p>The specified filtering subroutine is called every time a <a href="../../lib/Pod/perlfunc.html#item_use"><code>use BANG</code></a> is
encountered, and passed all the source code following that call, up to
either the next <code>no BANG;</code> (or whatever terminator you've set) or the
end of the source file, whichever occurs first. By default, any <code>no
BANG;</code> call must appear by itself on a separate line, or it is ignored.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Damian Conway (<a href="mailto:damian@conway.org">damian@conway.org</a>)</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<pre>
Copyright (c) 2000-2001, 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.
</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?