ch18_04.htm

来自「编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,」· HTM 代码 · 共 151 行

HTM
151
字号
<html><head><title>Compiler Backends (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="Compiler Backends"><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_03.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_05.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.4. Compiler Backends</h2><p><a name="INDEX-3271"></a><a name="INDEX-3272"></a><a name="INDEX-3273"></a>So, if Apache can compile a Perl program now and execute it later, whycan't you?  Apache and other programs that contain embedded Perlinterpreters have it easy--they never store the parse tree to anexternal file.  If you're content with that approach, and don't mindusing the C API to get at it, you can do the same thing.  See thesection <a href="ch21_04.htm#ch21-sect-ep">Section 18.4, "Embedding Perl (Using Perl from C)"</a> in <a href="ch21_01.htm">Chapter 21, "Internals and Externals"</a>, to learnhow to access Perl from an enclosing C framework.<a name="INDEX-3274"></a></p><p>If you don't want to go that route, or have other needs, then thereare a few options available.  Instead of feeding the opcode outputfrom the Perl compiler immediately into a Perl interpreter, you caninvoke any of several alternative backends instead.  These backends canserialize and store the compiled opcodes to an external file or evenconvert them into a couple different flavors of C code.</p><p><a name="INDEX-3275"></a>Please be aware that the code generators are all extremelyexperimental utilities that shouldn't be expected to work in aproduction environment.  In fact, they shouldn't even be expected towork in a nonproduction environment except maybe once in a blue moon.Now that we've set your expectations low enough that any success atall will necessarily surpass them, it's safe to tell you how thebackends work.</p><p><a name="INDEX-3276"></a><a name="INDEX-3277"></a><a name="INDEX-3278"></a>Some of the backend modules are code generators, like<tt class="literal">B::Bytecode</tt>, <tt class="literal">B::C</tt>, and<tt class="literal">B::CC</tt>.  Others are really code-analysis anddebugging tools, like <tt class="literal">B::Deparse</tt>,<tt class="literal">B::Lint</tt>, and <tt class="literal">B::Xref</tt>.  Beyondthose backends, the standard release includes several other low-levelmodules of potential interest to would-be authors of Perlcode-development tools.  Other backend modules can be found on CPAN,including (as of this writing) <tt class="literal">B::Fathom</tt>,<tt class="literal">B::Graph</tt>, <tt class="literal">B::JVM::Jasmin</tt>, and<tt class="literal">B::Size</tt>.</p><p><a name="INDEX-3279"></a><a name="INDEX-3280"></a><a name="INDEX-3281"></a>When you're using the Perl compiler for anything other than feedingthe interpreter, the <tt class="literal">O</tt> module (that is, using the<em class="emphasis">O.pm</em> file) stands between the compiler andassorted backend modules.  You don't call the backends directly;instead, you call the middle end, which in turn calls the designatedbackend.  So, if you had a module called<tt class="literal">B::Backend</tt>, you would invoke it on a given scriptthis way:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Backend </b></tt><em class="replaceable">SCRIPTNAME</em></pre></blockquote>Some backends take options, specified as:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MO=Backend,</b></tt><em class="replaceable">OPTS</em> <em class="replaceable">SCRIPTNAME</em></pre></blockquote><a name="INDEX-3282"></a>Some backends already have their own frontends to invoke their middleends for you so you don't have to remember their M.O.  In particular,<em class="emphasis">perlcc</em>(1) invokes that code generator, which can be cumbersome tofire up.</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_03.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_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">18.3. Executing Your Code</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.5. Code Generators</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 + =
减小字号Ctrl + -
显示快捷键?