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

📄 ch32_37.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Safe (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="Safe"><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="ch32_36.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_38.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">32.37. Safe</h2><p><blockquote><pre class="programlisting">use Safe;$sandbox = Safe-&gt;new();             # anonymous sandbox$sandbox = Safe-&gt;new("PackName");   # in that symbol table# Enable or disable opcodes by group or name.$sandbox-&gt;permit(qw(:base_core));$sandbox-&gt;permit_only(qw(:base_core :base_loop :base_mem));$sandbox-&gt;deny("die");# like do(), but in the sandbox$ok = $sandbox-&gt;rdo($filename);# like do(), but in the sandbox$ok = $sandbox-&gt;reval($code);       # without 'use strict'$ok = $sandbox-&gt;reval($code, 1);    # with 'use strict'</pre></blockquote>The <tt class="literal">Safe</tt> module attempts to provide a restricted environment toprotect the rest of the program from dangerous operations.  It uses twodifferent strategies to do this.  Much as an anonymous FTP daemon's useof <em class="emphasis">chroot</em>(2) alters the view of the root of the filesystem, creatinga compartment object with <tt class="literal">Safe-&gt;new("PackName")</tt> alters thatcompartment's view of its own namespace.  The compartment now sees asits root symbol table (<tt class="literal">main::</tt>) the symbol table that the rest of theprogram sees as <tt class="literal">PackName::</tt>.  What looks like <tt class="literal">Frobnitz::</tt> on theinside of the compartment is really <tt class="literal">PackName::Frobnitz::</tt> on theoutside.  If you don't give an argument to the constructor, a randomnew package name is selected for you.</p><p>The second and more important facility that a <tt class="literal">Safe</tt>compartment provides is a way to limit code that is deemed legalwithin an <tt class="literal">eval</tt>.  You can tweak the allowable opcodeset (legal Perl operations) using method calls on your<tt class="literal">Safe</tt> object.  Two methods are available to compilecode in a <tt class="literal">Safe</tt> compartment: <tt class="literal">rdo</tt>("restricted do") for files and <tt class="literal">reval</tt> ("restrictedeval") for strings.  These are like <tt class="literal">do</tt> on afilename and <tt class="literal">eval</tt> on a string but execute in arestricted namespace with limited opcodes.  The first argument is thefilename or string to compile, and the optional second argument iswhether the code should be compiled under <tt class="literal">usestrict</tt>.</p><p>This module is scheduled for a rewrite (we intend to isolate the sandbox intoa different interpreter thread for additional safety), so be sure to check the <tt class="literal">Safe</tt> manpage for updates.  See also <a href="ch23_01.htm">Chapter 23, "Security"</a>.</p><!-- 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="ch32_36.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="ch32_38.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.36. POSIX</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">32.38. Socket</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 + -