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

📄 ch18_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Compiling (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="Compiling"><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="ch17_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part3.htm">Part 3: Perl as Technology</a></td><td align="right" valign="top" width="172"><a href="ch18_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 18.  Compiling</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch18_01.htm">The Life Cycle of a Perl Program</a><br><a href="ch18_02.htm">Compiling Your Code</a><br><a href="ch18_03.htm">Executing Your Code</a><br><a href="ch18_04.htm">Compiler Backends</a><br><a href="ch18_05.htm">Code Generators</a><br><a href="ch18_06.htm">Code Development Tools</a><br><a href="ch18_07.htm">Avant-Garde Compiler, Retro Interpreter</a><br></p></div><p><a name="INDEX-3220"></a>If you came here looking for a Perl compiler, you may be surprised todiscover that you already have one--your <em class="emphasis">perl</em>program (typically <em class="emphasis">/usr/bin/perl</em>) already containsa Perl compiler.  That might not be what you were thinking, and if itwasn't, you may be pleased to know that we do also provide<em class="emphasis">code generators</em> (which somewell-meaning folks call "compilers"), and we'll discussthose toward the end of this chapter.  But first we want to talk aboutwhat we think of as The Compiler.  Inevitably there's going to be acertain amount of low-level detail in this chapter that some peoplewill be interested in, and some people will not.  If you find thatyou're not, think of it as an opportunity to practice yourspeed-reading skills.</p><p>Imagine that you're a conductor who's ordered the score for a largeorchestral work.  When the box of music arrives, you find severaldozen booklets, one for each member of the orchestra with just theirpart in it.  But curiously, your master copy with all the parts ismissing.  Even more curiously, the parts you <em class="emphasis">do</em>have are written out using plain English instead of musical notation.Before you can put together a program for performance, or even givethe music to your orchestra to play, you'll first have to translatethe prose descriptions into the normal system of notes and bars.  Thenyou'll need to compile the individual parts into one giant score sothat you can get an idea of the overall program.</p><p>Similarly, when you hand the source code of your Perl script over to<em class="emphasis">perl</em> to execute, it is no more useful to thecomputer than the English description of the symphony was to themusicians.  Before your program can run, Perl needs tocompile<a href="#FOOTNOTE-1">[1]</a> these English-lookingdirections into a special symbolic representation.  Your program stillisn't running, though, because the compiler only compiles.  Like theconductor's score, even after your program has been converted to aninstruction format suitable for interpretation, it still needs anactive agent to interpret those instructions.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1]Or translate, or transform, or transfigure, ortransmute, or transmogrify.</p></blockquote><h2 class="sect1">18.1. The Life Cycle of a Perl Program</h2><p><a name="INDEX-3221"></a><a name="INDEX-3222"></a><a name="INDEX-3223"></a>You can break up the life cycle of a Perl program into four distinctphases, each with separate stages of its own.  The first and the lastare the most interesting ones, and the middle two are optional.  Thestages are depicted in <a href="ch18_01.htm#perl3-life-cycle-perl-prog">Figure 18-1</a>.</p><a name="perl3-life-cycle-perl-prog"></a><div class="figure"></div><h4 class="objtitle">Figure 18.1. The life cycle of a Perl program</h4><ol><li><p><em class="emphasis">The Compilation Phase</em></p><a name="INDEX-3224"></a><a name="INDEX-3225"></a><a name="INDEX-3226"></a><a name="INDEX-3227"></a><a name="INDEX-3228"></a><a name="INDEX-3229"></a><a name="INDEX-3230"></a><a name="INDEX-3231"></a><p>During phase 1, the <em class="emphasis">compile phase</em>, the Perlcompiler converts your program into a data structure called a<em class="emphasis">parse tree</em>.  Along with the standard parsingtechniques, Perl employs a much more powerful one: it uses<tt class="literal">BEGIN</tt> blocks to guide further compilation.<tt class="literal">BEGIN</tt> blocks are handed off to the interpreter tobe run as as soon as they are parsed, which effectively runs them inFIFO order (first in, first out).  This includes any<tt class="literal">use</tt> and <tt class="literal">no</tt> declarations; theseare really just <tt class="literal">BEGIN</tt> blocks in disguise.  Any<tt class="literal">CHECK</tt>, <tt class="literal">INIT</tt>, and<tt class="literal">END</tt> blocks are scheduled by the compiler fordelayed execution.</p><p>Lexical declarations are noted, but assignments to them are notexecuted.  All <tt class="literal">eval</tt><em class="replaceable">BLOCK</em>s, <tt class="literal">s///e</tt>constructs, and noninterpolated regular expressions are compiled here,and constant expressions are pre-evaluated.  The compiler is now done,unless it gets called back into service later.  At the end of thisphase, the interpreter is again called up to execute any scheduled<tt class="literal">CHECK</tt> blocks in LIFO order (last in, first out).The presence or absence of a <tt class="literal">CHECK</tt> block determineswhether we next go to phase 2 or skip over to phase 4.</p></li><li><p><em class="emphasis">The Code Generation Phase (optional)</em></p><a name="INDEX-3232"></a><a name="INDEX-3233"></a><a name="INDEX-3234"></a><a name="INDEX-3235"></a><a name="INDEX-3236"></a><p><tt class="literal">CHECK</tt> blocks are installed by code generators, sothis optional phase occurs when you explicitly use one of the codegenerators (described later in "Code Generators").  These convert thecompiled (but not yet run) program into either C source code orserialized Perl bytecodes--a sequence of values expressing internalPerl instructions.  If you choose to generate C source code, it caneventually produce a file called an <em class="emphasis">executableimage</em> in native machine language.<a href="#FOOTNOTE-2">[2]</a></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2] Youroriginal script is an executable file too, but it's not machinelanguage, so we don't call it an image.  An image file is called thatbecause it's a verbatim copy of the machine codes your CPU knows howto execute directly.</p></blockquote><p>At this point, your program goes into suspended animation.If you made an executable image, you can go directly to phase 4; otherwise,you need to reconstitute the freeze-dried bytecodes in phase 3.</p></li><li><p><em class="emphasis">The Parse Tree Reconstruction Phase (optional)</em></p><p><a name="INDEX-3237"></a><a name="INDEX-3238"></a><a name="INDEX-3239"></a>To reanimate the program, its parse tree must be reconstructed. Thisphase exists only if code generation occurred and you chose togenerate bytecode. Perl must first reconstitute its parse trees fromthat bytecode sequence before the program can run.  Perl does not rundirectly from the bytecodes; that would be slow.</p></li><li><p><em class="emphasis">The Execution Phase</em></p><p><a name="INDEX-3240"></a><a name="INDEX-3241"></a><a name="INDEX-3242"></a>Finally, what you've all been waiting for: running yourprogram. Hence, this is also called the <em class="emphasis">runphase</em>.  The interpreter takes the parse tree (which it goteither directly from the compiler or indirectly from code generationand subsequent parse tree reconstruction) and executes it.  (Or, ifyou generated an executable image file, it can be run as a standaloneprogram since it contains an embedded Perl interpreter.)</p><p>At the start of this phase, before your main program gets to run, allscheduled <tt class="literal">INIT</tt> blocks are executed in FIFO order.Then your main program is run.  The interpreter can call back into thecompiler as needed upon encountering an <tt class="literal">eval</tt><em class="replaceable">STRING</em>, a <tt class="literal">do</tt><em class="replaceable">FILE</em> or <tt class="literal">require</tt>statement, an <tt class="literal">s///ee</tt> construct, or a pattern matchwith an interpolated variable that is found to contain a legal codeassertion.</p><p>When your main program finishes, any delayed <tt class="literal">END</tt>blocks are finally executed, this time in LIFO order.  The very firstone seen will execute last, and then you're done.(<tt class="literal">END</tt> blocks are skipped only if you<tt class="literal">exec</tt> or your process is blown away by an uncaughtcatastrophic error.  Ordinary exceptions are not consideredcatastrophic.</p></li></ol><p>Now we'll discuss these phases in greater detail, and in a different order.</p><a name="INDEX-3319"></a><!-- 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="ch17_02.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">17.2. The Thread Model</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.2. Compiling Your Code</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 + -