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

📄 ch08_179.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Safe (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 &amp; 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="ch08_178.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="ch08_180.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.179. Safe</h2><p><a name="INDEX-1541" />Createscompartments for evaluating untrusted Perl code. Each compartment hasan associated operator mask for excluding particular Perl operatorsfrom the compartment. (See the Opcode module for more information onoperators and operator masks.) The default mask is applied duringcompilation to prevent all operations that give access to the system.Safe provides the following methods.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em> = new Safe [(<em class="replaceable">namespace</em>, <em class="replaceable">mask</em>)]</pre><p>Constructor. Creates a new compartment. Takes the following arguments:</p><dl><dt><i><em class="replaceable"><tt>namespace</tt></em></i></dt><dd>Optional. Root namespace to use for the compartment. Default is<tt class="literal">Safe::Root0</tt>, which autoincrements for each newcompartment.</p></dd><dt><i><em class="replaceable"><tt>mask</tt></em></i></dt><dd>Optional. Operator mask to use; defaults to a restrictive set.</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>mask</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;mask ([<em class="replaceable">mask</em>])</pre><p>Sets operator mask for the compartment if<em class="replaceable"><tt>mask</tt></em> is specified; otherwise, it gets thecurrent mask.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>rdo</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;rdo (<em class="replaceable">filename</em>)</pre><p>Evaluates the contents of file <em class="replaceable">filename</em>inside the compartment.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>reval</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;reval (<em class="replaceable">string</em>)</pre><p>Evaluates <em class="replaceable">string</em> as Perl code inside thecompartment.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>root</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;root ([<em class="replaceable">namespace</em>])</pre><p>If <em class="replaceable">namespace</em> is specified, sets the rootnamespace for the compartment; otherwise, gets the current namespace.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>share</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;share (<em class="replaceable">varname</em>[, ...])</pre><p>Shares the variable(s) in the argument list with the compartment.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>trap</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;trap (<em class="replaceable">op</em>[, ...])</pre><p>For each operator specified in the argument list, sets thecorresponding bit in the compartment's operatormask.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>untrap</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;untrap (<em class="replaceable">op</em>[, ...])</pre><p>For each operator specified in the argument list, resets thecorresponding bit in the compartment's operatormask.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>varglob</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">cpt</em>-&gt;varglob (<em class="replaceable">varname</em>)</pre><p>Returns a glob for the symbol table entry of<em class="replaceable"><tt>varname</tt></em> in thecompartment's package.</p></div><p>The following subroutines are available for export by Safe. Theoperator names can be found in the array <tt class="literal">op_name</tt>in the file <em class="emphasis">opcode.h</em> in the Perl distribution.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>emptymask</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>emptymask</pre><p>Returns a mask with all operators unmasked.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>fullmask</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>fullmask</pre><p>Returns a mask with all operators masked.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>mask_to_ops</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>mask_to_ops (<em class="replaceable">mask</em>)</pre><p>Takes an operator mask <em class="replaceable">mask</em> and returns alist of the names of operators masked in<em class="replaceable"><tt>mask</tt></em>.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>MAXO</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>MAXO</pre><p>Returns the number of operators in a mask (and hence its length).</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>opcode</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>opcode (<em class="replaceable">op</em>[, ...])</pre><p>Takes a list of operator names and returns the corresponding list ofopcodes.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>op_mask</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>op_mask</pre><p>Returns the operator mask in effect at the time the invocation to thesubroutine was compiled.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>opname</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>opname (<em class="replaceable">op</em>[, ...])</pre><p>Takes a list of opcodes and returns the corresponding list ofoperator names.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ops_to_mask</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>ops_to_mask (<em class="replaceable">op</em>[, ...])</pre><p>Takes a list of operator names and returns an operator mask withthose operators masked.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_178.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="ch08_180.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.178. re</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">8.180. Scalar::Util</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 &copy; 2002</a> O'Reilly &amp; 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 + -