⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch31_17.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>use re (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="use re"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_16.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch31_18.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">31.17. use re</h2><p>This pragma controls the use of regular expressions. It has four possible invocations: "<tt class="literal">taint</tt>" and "<tt class="literal">eval</tt>",which are lexically scoped, plus "<tt class="literal">debug</tt>" and "<tt class="literal">debugcolor</tt>",which aren't.<blockquote><pre class="programlisting">use re 'taint';                # Contents of $match are tainted if $dirty was also tainted.($match) = ($dirty =~ /^(.*)$/s); # Allow code interpolation:use re 'eval';$pat = '(?{ $var = 1 })';      # embedded code execution/alpha${pat}omega/;            # won't fail unless under -T                               # and $pat is tainteduse re 'debug';                # like "perl -Dr"/^(.*)$/s;                     # output debugging info during                               #     compile time and run timeuse re 'debugcolor';           # same as 'debug',                                #    but with colored output</pre></blockquote>When <tt class="literal">use re 'taint'</tt> is in effect and a tainted string is thetarget of a regex, the numbered regex variables and values returnedby the <tt class="literal">m//</tt> operator in list context are all tainted.  This isuseful when regex operations on tainted data aren't meant to extractsafe substrings, but to perform other transformations.  See the discussionon tainting in <a href="ch23_01.htm">Chapter 23, "Security"</a>.</p><p>When <tt class="literal">use re 'eval'</tt> is in effect, a regex is allowedto contain assertions that execute Perl code, which are of the form<tt class="literal">(?{ ... })</tt>, even when the regex containsinterpolated variables.  Execution of code segments resulting fromvariable interpolation into a regex is normally disallowed forsecurity reasons: you don't want programs that read patterns fromconfig files, command-line arguments, or CGI form fields to suddenlystart executing arbitrary code if they weren't designed to expect thispossibility.  This use of the pragma allows only untainted strings tobe interpolated; tainted data will still cause an exception to beraised (if you're running with taint checks enabled).  See also <a href="ch05_01.htm">Chapter 5, "Pattern Matching"</a>, and <a href="ch23_01.htm">Chapter 23, "Security"</a>.</p><p>For the purposes of this pragma, interpolation of precompiled regularexpressions (produced by the <tt class="literal">qr//</tt> operator) is not consideredvariable interpolation.  Nevertheless, when you build the<tt class="literal">qr//</tt> pattern it needs to have <tt class="literal">use re 'eval'</tt> in effectif any of its interpolated strings contain code assertions.  For example:<blockquote><pre class="programlisting">$code = '(?{ $n++ })';      # code assertion$str  = '\b\w+\b' . $code;  # build string to interpolate$line =~ /$str/;    # this needs use re 'eval'$pat = qr/$str/;    # this also needs use re 'eval'$line =~ /$pat/;    # but this doesn't need use re 'eval'</pre></blockquote>Under <tt class="literal">use re 'debug'</tt>, Perl emits debugging messages when compilingand when executing regular expressions.  The output is the same as thatobtained by running a "debugging Perl" (one compiled with<tt class="userinput"><b>-DDEBUGGING</b></tt> passed to the C compiler) and then executing your Perlprogram under Perl's <tt class="userinput"><b>-Dr</b></tt> command-line switch.  Depending on howcomplicated your pattern is, the resulting output can be overwhelming.Calling <tt class="literal">use re 'debugcolor'</tt> enables more colorful output that can beuseful, provided your terminal understands color sequences.  Set your<tt class="literal">PERL_RE_TC</tt> environment variable to a comma-separated list of relevant<em class="emphasis">termcap</em>(5) properties for highlighting.  For more details, see <a href="ch20_01.htm">Chapter 20, "The Perl Debugger"</a>.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_16.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch31_18.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.16. use overload</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">31.18. use sigtrap</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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