📄 deparse.html
字号:
<li>
<p>use utf8;</p>
</li>
<li>
<p>use re;</p>
</li>
</ul>
<p>Ordinarily, if you use B::Deparse on a subroutine which has
been compiled in the presence of one or more of these pragmas,
the output will include statements to turn on the appropriate
directives. So if you then compile the code returned by coderef2text,
it will behave the same way as the subroutine which you deparsed.</p>
<p>However, you may know that you intend to use the results in a
particular context, where some pragmas are already in scope. In
this case, you use the <strong>ambient_pragmas</strong> method to describe the
assumptions you wish to make.</p>
<p>Not all of the options currently have any useful effect. See
<a href="#bugs">BUGS</a> for more details.</p>
<p>The parameters it accepts are:</p>
<dl>
<dt><strong><a name="item_strict">strict</a></strong>
<dd>
<p>Takes a string, possibly containing several values separated
by whitespace. The special values "all" and "none" mean what you'd
expect.</p>
</dd>
<dd>
<pre>
<span class="variable">$deparse</span><span class="operator">-></span><span class="variable">ambient_pragmas</span><span class="operator">(</span><span class="string">strict</span> <span class="operator">=></span> <span class="string">'subs refs'</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item___">$[</a></strong>
<dd>
<p>Takes a number, the value of the array base $[.</p>
</dd>
</li>
<dt><strong><a name="item_bytes">bytes</a></strong>
<dt><strong><a name="item_utf8">utf8</a></strong>
<dt><strong><a name="item_integer">integer</a></strong>
<dd>
<p>If the value is true, then the appropriate pragma is assumed to
be in the ambient scope, otherwise not.</p>
</dd>
</li>
<dt><strong><a name="item_re">re</a></strong>
<dd>
<p>Takes a string, possibly containing a whitespace-separated list of
values. The values "all" and "none" are special. It's also permissible
to pass an array reference here.</p>
</dd>
<dd>
<pre>
<span class="variable">$deparser</span><span class="operator">-></span><span class="variable">ambient_pragmas</span><span class="operator">(</span><span class="string">re</span> <span class="operator">=></span> <span class="string">'eval'</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_warnings">warnings</a></strong>
<dd>
<p>Takes a string, possibly containing a whitespace-separated list of
values. The values "all" and "none" are special, again. It's also
permissible to pass an array reference here.</p>
</dd>
<dd>
<pre>
<span class="variable">$deparser</span><span class="operator">-></span><span class="variable">ambient_pragmas</span><span class="operator">(</span><span class="string">warnings</span> <span class="operator">=></span> <span class="operator">[</span><span class="string">qw[void io]</span><span class="operator">]</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>If one of the values is the string "FATAL", then all the warnings
in that list will be considered fatal, just as with the <strong>warnings</strong>
pragma itself. Should you need to specify that some warnings are
fatal, and others are merely enabled, you can pass the <strong>warnings</strong>
parameter twice:</p>
</dd>
<dd>
<pre>
<span class="variable">$deparser</span><span class="operator">-></span><span class="variable">ambient_pragmas</span><span class="operator">(</span>
<span class="string">warnings</span> <span class="operator">=></span> <span class="string">'all'</span><span class="operator">,</span>
<span class="string">warnings</span> <span class="operator">=></span> <span class="operator">[</span><span class="string">FATAL</span> <span class="operator">=></span> <span class="string">qw/void io/</span><span class="operator">]</span><span class="operator">,</span>
<span class="operator">);</span>
</pre>
</dd>
<dd>
<p>See <a href="../../lib/Pod/perllexwarn.html">the perllexwarn manpage</a> for more information about lexical warnings.</p>
</dd>
</li>
<dt><strong><a name="item_hint_bits">hint_bits</a></strong>
<dt><strong><a name="item_warning_bits">warning_bits</a></strong>
<dd>
<p>These two parameters are used to specify the ambient pragmas in
the format used by the special variables $^H and ${^WARNING_BITS}.</p>
</dd>
<dd>
<p>They exist principally so that you can write code like:</p>
</dd>
<dd>
<pre>
<span class="operator">{</span> <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$hint_bits</span><span class="operator">,</span> <span class="variable">$warning_bits</span><span class="operator">);</span>
<span class="keyword">BEGIN</span> <span class="operator">{(</span><span class="variable">$hint_bits</span><span class="operator">,</span> <span class="variable">$warning_bits</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$^H</span><span class="operator">,</span> <span class="operator">${^</span><span class="variable">WARNING_BITS</span><span class="operator">})}</span>
<span class="variable">$deparser</span><span class="operator">-></span><span class="variable">ambient_pragmas</span> <span class="operator">(</span>
<span class="string">hint_bits</span> <span class="operator">=></span> <span class="variable">$hint_bits</span><span class="operator">,</span>
<span class="string">warning_bits</span> <span class="operator">=></span> <span class="variable">$warning_bits</span><span class="operator">,</span>
<span class="string">'$['</span> <span class="operator">=></span> <span class="number">0</span> <span class="operator">+</span> <span class="variable">$[</span>
<span class="operator">);</span> <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>which specifies that the ambient pragmas are exactly those which
are in scope at the point of calling.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="coderef2text">coderef2text</a></h2>
<pre>
<span class="variable">$body</span> <span class="operator">=</span> <span class="variable">$deparse</span><span class="operator">-></span><span class="variable">coderef2text</span><span class="operator">(\&</span><span class="variable">func</span><span class="operator">)</span>
<span class="variable">$body</span> <span class="operator">=</span> <span class="variable">$deparse</span><span class="operator">-></span><span class="variable">coderef2text</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">(</span>$$<span class="operator">)</span> <span class="operator">{</span> <span class="operator">...</span> <span class="operator">})</span>
</pre>
<p>Return source code for the body of a subroutine (a block, optionally
preceded by a prototype in parens), given a reference to the
sub. Because a subroutine can have no names, or more than one name,
this method doesn't return a complete subroutine definition -- if you
want to eval the result, you should prepend "sub subname ", or "sub "
for an anonymous function constructor. Unless the sub was defined in
the main:: package, the code will include a package declaration.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<ul>
<li>
<p>The only pragmas to be completely supported are: <code>use warnings</code>,
<code>use strict 'refs'</code>, <code>use bytes</code>, and <code>use integer</code>. (<a href="#item___"><code>$[</code></a>, which
behaves like a pragma, is also supported.)</p>
<p>Excepting those listed above, we're currently unable to guarantee that
B::Deparse will produce a pragma at the correct point in the program.
(Specifically, pragmas at the beginning of a block often appear right
before the start of the block instead.)
Since the effects of pragmas are often lexically scoped, this can mean
that the pragma holds sway over a different portion of the program
than in the input file.</p>
</li>
<li>
<p>In fact, the above is a specific instance of a more general problem:
we can't guarantee to produce BEGIN blocks or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> declarations in
exactly the right place. So if you use a module which affects compilation
(such as by over-riding keywords, overloading constants or whatever)
then the output code might not work as intended.</p>
<p>This is the most serious outstanding problem, and will require some help
from the Perl core to fix.</p>
</li>
<li>
<p>If a keyword is over-ridden, and your program explicitly calls
the built-in version by using CORE::keyword, the output of B::Deparse
will not reflect this. If you run the resulting code, it will call
the over-ridden version rather than the built-in one. (Maybe there
should be an option to <strong>always</strong> print keyword calls as <code>CORE::name</code>.)</p>
</li>
<li>
<p>Some constants don't print correctly either with or without <strong>-d</strong>.
For instance, neither B::Deparse nor Data::Dumper know how to print
dual-valued scalars correctly, as in:</p>
<pre>
<span class="keyword">use</span> <span class="variable">constant</span> <span class="string">E2BIG</span> <span class="operator">=></span> <span class="operator">(</span><span class="variable">$!</span><span class="operator">=</span><span class="number">7</span><span class="operator">);</span> <span class="variable">$y</span> <span class="operator">=</span> <span class="variable">E2BIG</span><span class="operator">;</span> <span class="keyword">print</span> <span class="variable">$y</span><span class="operator">,</span> <span class="number">0</span><span class="operator">+</span><span class="variable">$y</span><span class="operator">;</span>
</pre>
</li>
<li>
<p>An input file that uses source filtering probably won't be deparsed into
runnable code, because it will still include the <strong>use</strong> declaration
for the source filtering module, even though the code that is
produced is already ordinary Perl which shouldn't be filtered again.</p>
</li>
<li>
<p>Optimised away statements are rendered as '???'. This includes statements that
have a compile-time side-effect, such as the obscure</p>
<pre>
<span class="keyword">my</span> <span class="variable">$x</span> <span class="keyword">if</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>which is not, consequently, deparsed correctly.</p>
</li>
<li>
<p>There are probably many more bugs on non-ASCII platforms (EBCDIC).</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Stephen McCamant <<a href="mailto:smcc@CSUA.Berkeley.EDU">smcc@CSUA.Berkeley.EDU</a>>, based on an earlier version
by Malcolm Beattie <<a href="mailto:mbeattie@sable.ox.ac.uk">mbeattie@sable.ox.ac.uk</a>>, with contributions from
Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,
Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael
Garcia-Suarez.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -