📄 ch18_06.htm
字号:
<html><head><title>Code Development Tools (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Code Development Tools"><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="ch18_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch18_01.htm">Chapter 18: Compiling</a></td><td align="right" valign="top" width="172"><a href="ch18_07.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">18.6. Code Development Tools</h2><p><a name="INDEX-3294"></a><a name="INDEX-3295"></a>The <tt class="literal">O</tt> module has many interesting Modi Operandi beyond feeding theexasperatingly experimental code generators. By providing relativelypainless access to the Perl compiler's output, this module makesit easy to build other tools that need to know everything about aPerl program.</p><p><a name="INDEX-3296"></a><a name="INDEX-3297"></a>The <tt class="literal">B::Lint</tt> module is named after <em class="emphasis">lint</em>(1), the C programverifier. It inspects programs for questionable constructs thatoften trip up beginners but don't normally trigger warnings.Call the module directly:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Lint,all myprog</b></tt></pre></blockquote>Only a few checks are currently defined, such as using an array inan implicit scalar context, relying on default variables, andaccessing another package's (nominally private) identifiers thatstart with <tt class="literal">_</tt>. See <em class="emphasis">B::Lint</em>(3) for details.</p><p><a name="INDEX-3298"></a><a name="INDEX-3299"></a><a name="INDEX-3300"></a><a name="INDEX-3301"></a>The <tt class="literal">B::Xref</tt> module generates cross-reference listings of thedeclaration and use of all variables (both global and lexicallyscoped), subroutines, and formats in a program, broken down by fileand subroutine. Call the module this way:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Xref myprog > myprof.pxref</b></tt></pre></blockquote>For instance, here's a partial report:<blockquote><pre class="programlisting">Subroutine parse_argv Package (lexical) $on i113, 114 $opt i113, 114 %getopt_cfg i107, 113 @cfg_args i112, 114, 116, 116 Package Getopt::Long $ignorecase 101 &GetOptions &124 Package main $Options 123, 124, 141, 150, 165, 169 %$Options 141, 150, 165, 169 &check_read &167 @ARGV 121, 157, 157, 162, 166, 166</pre></blockquote>This shows that the <tt class="literal">parse_argv</tt> subroutine had four lexicalvariables of its own; it also accessed global identifiers from boththe <tt class="literal">main</tt> package and from <tt class="literal">Getopt::Long</tt>. The numbers are thelines where that item was used: a leading <tt class="literal">i</tt> indicates that the itemwas first introduced at the following line number, and a leading <tt class="literal">&</tt>means a subroutine was called there. Dereferences are listedseparately, which is why both <tt class="literal">$Options</tt> and <tt class="literal">%$Options</tt> are shown.</p><p><a name="INDEX-3302"></a>The <tt class="literal">B::Deparse</tt> is a pretty printer that candemystify Perl code and help you understand what transformations theoptimizer has taken with your code. For example, this shows whatdefaults Perl uses for various constructs:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Deparse -ne 'for (1 .. 10) { print if -t }'</b></tt>LINE: while (defined($_ = <ARGV>)) { foreach $_ (1 .. 10) { print $_ if -t STDIN; }}</pre></blockquote>The <tt class="userinput"><b>-p</b></tt> switch adds parentheses so you can seePerl's idea of precedence:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Deparse,-p -e 'print $a ** 3 + sqrt(2) / 10 ** -2 ** $c'</b></tt>print((($a ** 3) + (1.4142135623731 / (10 ** (-(2 ** $c))))));</pre></blockquote>You can use <tt class="userinput"><b>-q</b></tt> to see what primitivesinterpolated strings are compiled into:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Deparse,-q -e '"A $name and some @ARGV\n"'</b></tt>'A ' . $name . ' and some ' . join($", @ARGV) . "\n";</pre></blockquote>And this shows how Perl really compiles a three-part <tt class="literal">for</tt> loopinto a <tt class="literal">while</tt> loop:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Deparse -e 'for ($i=0;$i<10;$i++) { $x++ }'</b></tt>$i = 0;while ($i < 10) { ++$x;}continue { ++$i}</pre></blockquote><a name="INDEX-3303"></a>You could even call <tt class="literal">B::Deparse</tt> on a Perl bytecodefile produced by <em class="emphasis">perlcc -b</em>, and have it decompilethat binary file for you. Serialized Perl opcodes may be a tad toughto read, but strong encryption they are not.</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="ch18_05.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="ch18_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">18.5. Code Generators</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">18.7. Avant-Garde Compiler, Retro Interpreter</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 © 2001</a> O'Reilly & 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 + -