📄 ch08_113.htm
字号:
<html><head><title>Filter::Simple (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_112.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_114.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.113. Filter::Simple </h2><p><a name="INDEX-1461" />Source filtering is a nice feature ofnewer versions of Perl (5.6 and later) because it allows theprogrammer to write extensions to the Perl language without tamperingwith the Perl source code itself. That is, you can create a macrolanguage out of Perl.</p><p>Filter::Simple is based on Filter:Util::Call, but simplifies themeans by which you can begin doing your own source filtering withPerl. Filter::Simple ships with the Perl 5.8 source kit.</p><p>Using Filter::Simple is, well, easy. Basically, Filter::Simpleimplements <tt class="literal">FILTER { ... }</tt>, which you can use tohandle many of your simple source-filtering needs.Let's say that you want a good glass of ale, and youdecide that you don't want Perl to<tt class="literal">print</tt>, but <tt class="literal">pint</tt> instead. Youcan implement something like the following with Filter::Simple.First, create a module called Print_to_Pint:</p><blockquote><pre class="code">package Print_to_Pint; use Filter::Simple;FILTER { s/pint/print/g; } # true1;</pre></blockquote><p> Now, use Print_to_Pint to do something with <tt class="literal">pint</tt>:</p><blockquote><pre class="code">#!/usr/local/bin/perl -w use Print_to_Pint; my $bottles = 99;my $last = 1; foreach my $bottle (reverse($last .. $bottles)) { pint "$bottle -> burp\n";}</pre></blockquote><p>By default, Filter::Simple ignores <tt class="literal">no</tt> behavior,i.e., it stops filtering after a <tt class="literal">no Module</tt> isencountered. You can alter this behavior by passing another argumentto <tt class="literal">use Filter::Simple</tt> or to <tt class="literal">FILTER { ...}</tt>. For example:</p><blockquote><pre class="code">package Print_to_Pint;use Filter::Simple; FILTER { s/pint/print/g;}"";</pre></blockquote><p>Filter::Simple also supports <tt class="literal">FILTER_ONLY</tt>, whichallows you to support multiple filters to handle different parts ofyour source code. <tt class="literal">FILTER_ONLY</tt> takes severalsubroutines as options: <tt class="literal">code</tt>,<tt class="literal">executable</tt>, <tt class="literal">string</tt>,<tt class="literal">regex</tt>, <tt class="literal">quotelike</tt>, and<tt class="literal">all</tt>. <tt class="literal">code</tt> or<tt class="literal">executable</tt> filters all Perl code, except Pod or<tt class="literal">_ _DATA_ _</tt> types. <tt class="literal">quotelike</tt>filters other Perl quotelike stuff, including here documents.<tt class="literal">string</tt> filters all string-related parts.<tt class="literal">regex</tt> filters all pattern-literal parts of<tt class="literal">quotelike</tt>.</p><p>For example, the following module will skip all incidences of Pod inthe PodSucks module:</p><blockquote><pre class="code">package PodSucks; use Filter::Simple; FILTER_ONLY executable => sub { s/x/X/g }, executable => sub { print } # True. True.1;</pre></blockquote><p>The above code might not make that much sense, but take a look at thetest program now, and it should become clear:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse PodSucks;print "Flocks of Red Sox cause shocks to the Bronx.\n"; =podYou will fear the wrath of Pod! Fear my wrath!=cut</pre></blockquote><p>This prints:</p><blockquote><pre class="code">Flocks of Red SoX cause shocks to the BronX.</pre></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_112.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_114.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.112. FileHandle</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.114. Filter::Util::Call</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -