perlcompile.html

来自「perl教程」· HTML 代码 · 共 548 行 · 第 1/2 页

HTML
548
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlcompile - Introduction to the Perl Compiler-Translator</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlcompile - Introduction to the Perl Compiler-Translator</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#layout">Layout</a></li>
	</ul>

	<li><a href="#using_the_back_ends">Using The Back Ends</a></li>
	<ul>

		<li><a href="#the_cross_referencing_back_end">The Cross Referencing Back End</a></li>
		<li><a href="#the_decompiling_back_end">The Decompiling Back End</a></li>
		<li><a href="#the_lint_back_end">The Lint Back End</a></li>
		<li><a href="#the_simple_c_back_end">The Simple C Back End</a></li>
		<li><a href="#the_bytecode_back_end">The Bytecode Back End</a></li>
		<li><a href="#the_optimized_c_back_end">The Optimized C Back End</a></li>
	</ul>

	<li><a href="#module_list_for_the_compiler_suite">Module List for the Compiler Suite</a></li>
	<li><a href="#known_problems">KNOWN PROBLEMS</a></li>
	<li><a href="#author">AUTHOR</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlcompile - Introduction to the Perl Compiler-Translator</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Perl has always had a compiler: your source is compiled into an
internal form (a parse tree) which is then optimized before being
run.  Since version 5.005, Perl has shipped with a module
capable of inspecting the optimized parse tree (<a href="#item_b"><code>B</code></a>), and this has
been used to write many useful utilities, including a module that lets
you turn your Perl into C source code that can be compiled into a
native executable.</p>
<p>The <a href="#item_b"><code>B</code></a> module provides access to the parse tree, and other modules
(&quot;back ends&quot;) do things with the tree.  Some write it out as
bytecode, C source code, or a semi-human-readable text.  Another
traverses the parse tree to build a cross-reference of which
subroutines, formats, and variables are used where.  Another checks
your code for dubious constructs.  Yet another back end dumps the
parse tree back out as Perl source, acting as a source code beautifier
or deobfuscator.</p>
<p>Because its original purpose was to be a way to produce C code
corresponding to a Perl program, and in turn a native executable, the
<a href="#item_b"><code>B</code></a> module and its associated back ends are known as &quot;the
compiler&quot;, even though they don't really compile anything.
Different parts of the compiler are more accurately a &quot;translator&quot;,
or an &quot;inspector&quot;, but people want Perl to have a &quot;compiler
option&quot; not an &quot;inspector gadget&quot;.  What can you do?</p>
<p>This document covers the use of the Perl compiler: which modules
it comprises, how to use the most important of the back end modules,
what problems there are, and how to work around them.</p>
<p>
</p>
<h2><a name="layout">Layout</a></h2>
<p>The compiler back ends are in the <code>B::</code> hierarchy, and the front-end
(the module that you, the user of the compiler, will sometimes
interact with) is the O module.  Some back ends (e.g., <a href="#item_b_3a_3ac"><code>B::C</code></a>) have
programs (e.g., <em>perlcc</em>) to hide the modules' complexity.</p>
<p>Here are the important back ends to know about, with their status
expressed as a number from 0 (outline for later implementation) to
10 (if there's a bug in it, we're very surprised):</p>
<dl>
<dt><strong><a name="item_b_3a_3abytecode">B::Bytecode</a></strong>

<dd>
<p>Stores the parse tree in a machine-independent format, suitable
for later reloading through the ByteLoader module.  Status: 5 (some
things work, some things don't, some things are untested).</p>
</dd>
</li>
<dt><strong><a name="item_b_3a_3ac">B::C</a></strong>

<dd>
<p>Creates a C source file containing code to rebuild the parse tree
and resume the interpreter.  Status: 6 (many things work adequately,
including programs using Tk).</p>
</dd>
</li>
<dt><strong><a name="item_b_3a_3acc">B::CC</a></strong>

<dd>
<p>Creates a C source file corresponding to the run time code path in
the parse tree.  This is the closest to a Perl-to-C translator there
is, but the code it generates is almost incomprehensible because it
translates the parse tree into a giant switch structure that
manipulates Perl structures.  Eventual goal is to reduce (given
sufficient type information in the Perl program) some of the
Perl data structure manipulations into manipulations of C-level
ints, floats, etc.  Status: 5 (some things work, including
uncomplicated Tk examples).</p>
</dd>
</li>
<dt><strong><a name="item_b_3a_3alint">B::Lint</a></strong>

<dd>
<p>Complains if it finds dubious constructs in your source code.  Status:
6 (it works adequately, but only has a very limited number of areas
that it checks).</p>
</dd>
</li>
<dt><strong><a name="item_b_3a_3adeparse">B::Deparse</a></strong>

<dd>
<p>Recreates the Perl source, making an attempt to format it coherently.
Status: 8 (it works nicely, but a few obscure things are missing).</p>
</dd>
</li>
<dt><strong><a name="item_b_3a_3axref">B::Xref</a></strong>

<dd>
<p>Reports on the declaration and use of subroutines and variables.
Status: 8 (it works nicely, but still has a few lingering bugs).</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="using_the_back_ends">Using The Back Ends</a></h1>
<p>The following sections describe how to use the various compiler back
ends.  They're presented roughly in order of maturity, so that the
most stable and proven back ends are described first, and the most
experimental and incomplete back ends are described last.</p>
<p>The O module automatically enabled the <strong>-c</strong> flag to Perl, which
prevents Perl from executing your code once it has been compiled.
This is why all the back ends print:</p>
<pre>
  myperlprogram syntax OK</pre>
<p>before producing any other output.</p>
<p>
</p>
<h2><a name="the_cross_referencing_back_end">The Cross Referencing Back End</a></h2>
<p>The cross referencing back end (B::Xref) produces a report on your program,
breaking down declarations and uses of subroutines and variables (and
formats) by file and subroutine.  For instance, here's part of the
report from the <em>pod2man</em> program that comes with Perl:</p>
<pre>
  Subroutine clear_noremap
    Package (lexical)
      $ready_to_print   i1069, 1079
    Package main
      $&amp;                1086
      $.                1086
      $0                1086
      $1                1087
      $2                1085, 1085
      $3                1085, 1085
      $ARGV             1086
      %HTML_Escapes     1085, 1085</pre>
<p>This shows the variables used in the subroutine <code>clear_noremap</code>.  The
variable <code>$ready_to_print</code> is a <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> (lexical) variable,
<strong>i</strong>ntroduced (first declared with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my())</code></a> on line 1069, and used on
line 1079.  The variable <a href="../../lib/Pod/perlvar.html#item___"><code>$&amp;</code></a> from the main package is used on 1086,
and so on.</p>
<p>A line number may be prefixed by a single letter:</p>
<dl>
<dt><strong><a name="item_i">i</a></strong>

<dd>
<p>Lexical variable introduced (declared with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my())</code></a> for the first time.</p>
</dd>
</li>
<dt><strong><a name="item__26">&amp;</a></strong>

<dd>
<p>Subroutine or method call.</p>
</dd>
</li>
<dt><strong><a name="item_s">s</a></strong>

<dd>
<p>Subroutine defined.</p>
</dd>
</li>
<dt><strong><a name="item_r">r</a></strong>

<dd>
<p>Format defined.</p>
</dd>
</li>
</dl>
<p>The most useful option the cross referencer has is to save the report
to a separate file.  For instance, to save the report on
<em>myperlprogram</em> to the file <em>report</em>:</p>
<pre>
  $ perl -MO=Xref,-oreport myperlprogram</pre>
<p>
</p>
<h2><a name="the_decompiling_back_end">The Decompiling Back End</a></h2>
<p>The Deparse back end turns your Perl source back into Perl source.  It
can reformat along the way, making it useful as a de-obfuscator.  The
most basic way to use it is:</p>
<pre>
  $ perl -MO=Deparse myperlprogram</pre>
<p>You'll notice immediately that Perl has no idea of how to paragraph
your code.  You'll have to separate chunks of code from each other
with newlines by hand.  However, watch what it will do with
one-liners:</p>
<pre>
  <span class="operator">$ </span><span class="variable">perl</span> <span class="operator">-</span><span class="variable">MO</span><span class="operator">=</span><span class="variable">Deparse</span> <span class="keyword">-e</span> <span class="string">'$op=shift||die "usage: $0
  code [...]";chomp(@ARGV=&lt;&gt;)unless@ARGV; for(@ARGV){$was=$_;eval$op;
  die$@ if$@; rename$was,$_ unless$was eq $_}'</span>
  <span class="keyword">-e</span> <span class="variable">syntax</span> <span class="variable">OK</span>
  <span class="variable">$op</span> <span class="operator">=</span> <span class="keyword">shift</span> <span class="variable">@ARGV</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">(</span><span class="string">"usage: $0 code [...]"</span><span class="operator">);</span>
  <span class="keyword">chomp</span><span class="operator">(</span><span class="variable">@ARGV</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">ARGV</span><span class="operator">&gt;)</span> <span class="keyword">unless</span> <span class="variable">@ARGV</span><span class="operator">;</span>
  <span class="keyword">foreach</span> <span class="variable">$_</span> <span class="operator">(</span><span class="variable">@ARGV</span><span class="operator">)</span> <span class="operator">{</span>
      <span class="variable">$was</span> <span class="operator">=</span> <span class="variable">$_</span><span class="operator">;</span>
      <span class="keyword">eval</span> <span class="variable">$op</span><span class="operator">;</span>
      <span class="keyword">die</span> <span class="variable">$@</span> <span class="keyword">if</span> <span class="variable">$@</span><span class="operator">;</span>
      <span class="keyword">rename</span> <span class="variable">$was</span><span class="operator">,</span> <span class="variable">$_</span> <span class="keyword">unless</span> <span class="variable">$was</span> <span class="keyword">eq</span> <span class="variable">$_</span><span class="operator">;</span>
  <span class="operator">}</span>
</pre>
<p>The decompiler has several options for the code it generates.  For
instance, you can set the size of each indent from 4 (as above) to
2 with:</p>
<pre>
  $ perl -MO=Deparse,-si2 myperlprogram</pre>
<p>The <strong>-p</strong> option adds parentheses where normally they are omitted:</p>
<pre>
  <span class="operator">$ </span><span class="variable">perl</span> <span class="operator">-</span><span class="variable">MO</span><span class="operator">=</span><span class="variable">Deparse</span> <span class="keyword">-e</span> <span class="string">'print "Hello, world\n"'</span>
  <span class="keyword">-e</span> <span class="variable">syntax</span> <span class="variable">OK</span>
  <span class="keyword">print</span> <span class="string">"Hello, world\n"</span><span class="operator">;</span>
  <span class="operator">$ </span><span class="variable">perl</span> <span class="operator">-</span><span class="variable">MO</span><span class="operator">=</span><span class="variable">Deparse</span><span class="operator">,</span><span class="keyword">-p</span> <span class="keyword">-e</span> <span class="string">'print "Hello, world\n"'</span>
  <span class="keyword">-e</span> <span class="variable">syntax</span> <span class="variable">OK</span>
  <span class="keyword">print</span><span class="operator">(</span><span class="string">"Hello, world\n"</span><span class="operator">);</span>
</pre>
<p>See <a href="../../lib/B/Deparse.html">the B::Deparse manpage</a> for more information on the formatting options.</p>
<p>
</p>
<h2><a name="the_lint_back_end">The Lint Back End</a></h2>
<p>The lint back end (B::Lint) inspects programs for poor style.  One
programmer's bad style is another programmer's useful tool, so options
let you select what is complained about.</p>
<p>To run the style checker across your source code:</p>
<pre>
  $ perl -MO=Lint myperlprogram</pre>
<p>To disable context checks and undefined subroutines:</p>
<pre>
  $ perl -MO=Lint,-context,-undefined-subs myperlprogram</pre>
<p>See <a href="../../lib/B/Lint.html">the B::Lint manpage</a> for information on the options.</p>
<p>
</p>

⌨️ 快捷键说明

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