📄 ch03_04.htm
字号:
<html><head><title>The Perl Compiler (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="ch03_03.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="ch03_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">3.4. The Perl Compiler</h2><p><a name="INDEX-173" /><a name="INDEX-174" />Starting with Perl 5.005, the Perl compilerbecame part of the standard Perl distribution.You'll find that with Perl 5.6 and later, the Perlcompiler has become far more stable. The compiler allows you todistribute Perl programs in binary form, which enables easy packagingof Perl-based programs without relying on the source machine to havethe correct version of Perl and the correct modules installed. Afterthe initial compilation, running a compiled program should be fasterbecause it doesn't have to be recompiled each timeit's run. However, you shouldn'texpect that the compiled code itself will run faster than theoriginal Perl source or that the executable will be smaller—inreality, the executable file is likely to be significantly bigger.</p><p>This initial release of the compiler is still considered to be a betaversion. It's distributed as an extension module, B,that comes with the following backends:</p><dl><dt><i>Bytecode</i></dt><dd>Translates a script into platform-independent Perlbytecode<a name="INDEX-175" />.</p></dd><dt><i>C</i></dt><dd>Translates a Perl script into C code<a name="INDEX-176" />.</p></dd><dt><i>CC</i></dt><dd>Translates a Perl script into optimized C code.</p></dd><dt><i>Deparse</i></dt><dd>Regenerates Perl source code from a compiledprogram<a name="INDEX-177" />.</p></dd><dt><i>Lint</i></dt><dd>Extends the Perl <em class="emphasis">-w</em> option. Named after the UnixLint program-checker<a name="INDEX-178" />.</p></dd><dt><i>Showlex</i></dt><dd>Shows lexical variables used in functions orfiles<a name="INDEX-179" />.</p></dd><dt><i>Xref</i></dt><dd>Creates a cross-reference listing for aprogram<a name="INDEX-180" />.</p></dd></dl><p>Once you've generated the C code with either the Cor the CC backend, you run the <em class="emphasis">cc_harness</em>program to compile it into an executable. There<a name="INDEX-181" />is alsoa <em class="emphasis">byteperl</em> interpreter that lets you run thecode you've generated with the Bytecode backend.</p><p>Here's an example that takes a simple"Hello world" program and uses theCC backend to generate C code:</p><blockquote><pre class="code">% perl -MO=CC,-ohi.c hi.plhi.pl syntax OK% perl cc_harness -O2 -ohi hi.c # You may have to provide the full path of where cc_harness livesgcc -B/usr/ccs/bin/ -D_REENTRANT -DDEBUGGING -I/usr/local/include -I/usr/local/lib/perl5/sun4-solaris-thread/5.00466/CORE -O2 -ohi hi.c -L/usr/local/lib /usr/local/lib/perl5/sun4-solaris-thread/5.00466/CORE/libperl.a -lsocket -lnsl -lgdbm -ldl -lm -lposix4 -lpthread -lc -lcrypt% hiHi there, world!</pre></blockquote><p><a name="INDEX-182" /><a name="INDEX-183" />The compiler also comes with afrontend, <em class="emphasis">perlcc</em>. You can use it to compile codeinto a standalone executable or compile a module (a<em class="emphasis">.pm</em> file) into a shared object (an<em class="emphasis">.so</em> file) that can be included in a Perl programvia <em class="emphasis">use</em>. For example:</p><blockquote><pre class="code">% perlcc a.p # Compiles into the executable 'a'% perlcc A.pm # Compiles into A.so</pre></blockquote><p>The following options can be used with <em class="emphasis">perlcc</em>:</p><dl><dt><i>-argv arguments</i></dt><dd><a name="INDEX-184" />Used with <em class="emphasis">-run</em> or<em class="emphasis">-e</em>. Passes the string<em class="emphasis">arguments</em> to the executable as<tt class="literal">@ARGV</tt>.</p></dd><dt><i>-C c_code_name</i></dt><dd><a name="INDEX-185" />Gives the name<em class="emphasis">c_code_name</em> to the generated C code that will becompiled. Valid only if you are compiling one file on the commandline.</p></dd><dt><i>-e perl_line_to_execute</i></dt><dd><a name="INDEX-186" />Works like<em class="emphasis">perl -e</em> to compile"one-liners." The default is tocompile and run the code. With <em class="emphasis">-o</em>, it saves theresulting executable.</p></dd><dt><i><em class="emphasis">-gen</em></i></dt><dd><a name="INDEX-187" />Creates the intermediate C code butdoesn't compile the results; does an implicit<em class="emphasis">-sav</em>.</p></dd><dt><i><em class="emphasis">-I include_directories</em></i></dt><dd><a name="INDEX-188" />Adds directories inside<em class="emphasis">include_directories</em> to the compilation command.</p></dd><dt><i><em class="emphasis">-L library_directories</em></i></dt><dd><a name="INDEX-189" />Adds directories in<em class="emphasis">library_directories</em> to the compilation command.</p></dd><dt><i><em class="emphasis">-log logname</em></i></dt><dd><a name="INDEX-190" />Opens a log file (for append) forsaving text from a compile command.</p></dd><dt><i><em class="emphasis">-mod</em></i></dt><dd><a name="INDEX-191" />Tells <em class="emphasis">perlcc</em> tocompile the files given at the command line as modules. Usually usedwith module files that don't end with<em class="emphasis">.pm</em>.</p></dd><dt><i><em class="emphasis">-o executable_name</em></i></dt><dd><a name="INDEX-192" />Gives the name<em class="emphasis">executable_name</em> to the executable that will becompiled. Only valid if compiling one file on the command line.</p></dd><dt><i><em class="emphasis">-prog</em></i></dt><dd><a name="INDEX-193" />Tells <em class="emphasis">perlcc</em> tocompile the files given at the command line as programs. Usually usedwith program files that don't end with a<em class="emphasis">.p</em>, <em class="emphasis">.pl</em>, or<em class="emphasis">.bat</em> extension.</p></dd><dt><i><em class="emphasis">-regex rename_regex</em></i></dt><dd><a name="INDEX-194" />Provides the rule<em class="emphasis">rename_regex</em> for creating executable filenames,in which <em class="emphasis">rename_regex</em> is a Perl regularexpression.</p></dd><dt><i><em class="emphasis">-run</em></i></dt><dd><a name="INDEX-195" />Immediately runs the generated Perlcode. Note that the rest of <tt class="literal">@ARGV</tt> is interpretedas arguments to the program being compiled.</p></dd><dt><i><em class="emphasis">-sav</em></i></dt><dd><a name="INDEX-196" />Tells Perl to save the intermediate Ccode.</p></dd><dt><i><em class="emphasis">-verbose verbose_level</em></i></dt><dd><a name="INDEX-197" />Compiles verbosely, setting<em class="emphasis">verbose_level</em> to control the degree ofverbosity. <em class="emphasis">verbose_level</em> can be given as eithera sum of bits or a list of letters. Values are listed in thefollowing table.</p><a name="ch03-2-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Bit</p></th><th><p>Letter</p></th><th><p>Action</p></th></tr><tr><td><p>1</p></td><td><p><tt class="literal">g</tt></p></td><td><p>Code generation errors to STDERR.</p></td></tr><tr><td><p>2</p></td><td><p><tt class="literal">a</tt></p></td><td><p>Compilation errors to STDERR.</p></td></tr><tr><td><p>4</p></td><td><p><tt class="literal">t</tt></p></td><td><p>Descriptive text to STDERR.</p></td></tr><tr><td><p>8</p></td><td><p><tt class="literal">f</tt></p></td><td><p>Code generation errors to file. Requires <em class="emphasis">-log</em>.</p></td></tr><tr><td><p>16</p></td><td><p><tt class="literal">c</tt></p></td><td><p>Compilation errors to file. Requires <em class="emphasis">-log</em>.</p></td></tr><tr><td><p>32</p></td><td><p><tt class="literal">d</tt></p></td><td><p>Descriptive text to file. Requires <em class="emphasis">-log</em>.</p></td></tr></table><p><p>With <em class="emphasis">-log</em>, the default level is 63; otherwisethe default level is 7.</p></dd></dl><p>There are two environment variables that you can set for<em class="emphasis">perlcc</em><a name="INDEX-198" /><a name="INDEX-199" />: PERL_SCRIPT_EXTand PERL_MODULE_EXT. These can be used to modify the defaultextensions that <em class="emphasis">perlcc</em> recognizes for programsand modules. The variables take colon-separated Perl regularexpressions.</p><p>The modules that comprise the compiler are described in <a href="ch08_01.htm">Chapter 8, "Standard Modules"</a>. Also see the documentation that comes withthe compiler, which includes more complete information on installingand using it.<a name="INDEX-200" /><a name="INDEX-201" /> </p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_03.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="ch03_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">3.3. Environment Variables</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">3.5. Threads</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 + -