📄 ch08_50.htm
字号:
<html><head><title>Data::Dumper (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="ch08_49.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_51.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.50. Data::Dumper</h2><p><a name="INDEX-1341" />Converts Perl data structures intostrings that can be printed or used with <tt class="literal">eval</tt> toreconstruct the original structures. Takes a list of scalars orreference variables and writes out their contents in Perl syntax.Several interfaces to Data::Dumper are provided:</p><ul><li><p>Simple procedural interface: </p><blockquote><pre class="code">print Dumper($foo, $bar);</pre></blockquote></li><li><p>Extended usage with names: </p><blockquote><pre class="code">print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);</pre></blockquote></li><li><p>Object-oriented interface: </p><blockquote><pre class="code">$d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]); ...print $d->Dump;</pre></blockquote></li></ul><p>By default, references are identified as<tt class="literal">$VAR</tt><em class="replaceable"><tt>n</tt></em>, in which<em class="replaceable"><tt>n</tt></em> is a numeric suffix. References tosubstructures within<tt class="literal">$VAR</tt><em class="replaceable"><tt>n</tt></em> are labeled usingarrow notation. In the extended usage form, references can be givenuser-specified names. See the Data::Dumper manpage for examples ofthe module's use.</p><p>Several configuration variables can be used to control the outputgenerated using the procedural interface. These variables control thedefault state of the object created by the new method. Each variablehas a corresponding method that can be used later to query or modifythe object. In the following list, each variable is followed by thecorresponding method:</p><dl><dt><b><tt class="literal">$Data::Dumper::Bless</tt></b></dt><dt><b><tt class="literal">$obj->Bless([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>Can be set to a string that specifies an alternative to the<tt class="literal">bless</tt> built-in operator used to create objects. Afunction with the specified name should exist and should accept thesame arguments as the built-in. Default is <tt class="literal">bless</tt>.</p></dd><dt><b><tt class="literal">$Data::Dumper::Deepcopy</tt></b></dt><dt><b><tt class="literal">$obj->Deepcopy([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>If set to a Boolean value, enables deep copies of structures;cross-referencing is then done only when absolutely essential.Default is <tt class="literal">0</tt>.</p></dd><dt><b><tt class="literal">$Data::Dumper::Indent</tt></b></dt><dt><b><tt class="literal">$obj->Indent([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>Controls the style of indentation for the output. Can be set to:</p><dl><dt><b><tt class="literal">0</tt></b></dt><dd>No newlines, indentation, or spaces between list items</p></dd><dt><b><tt class="literal">1</tt></b></dt><dd>Newlines, but each level in the structure is indented a fixed amount</p></dd><dt><b><tt class="literal">2</tt></b></dt><dd>Default. Takes into account the length of hash keys so the hashvalues line up.</p></dd><dt><b><tt class="literal">3</tt></b></dt><dd>Like <tt class="literal">2</tt>, but also annotates array elements withtheir index, with each annotation on its own line</p></dd></dl></dd><dt><b><tt class="literal">$Data::Dumper::Freezer</tt></b></dt><dt><b><tt class="literal">$obj->Freezer([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>If set to a method name, Data::Dumper invokes that method via theobject before attempting to turn it into a string. Set to an emptystring to disable. Default is empty string.</p></dd><dt><b><tt class="literal">$Data::Dumper::Pad</tt></b></dt><dt><b><tt class="literal">$obj->Pad([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>Specifies the string prefixed to every output line. Default is anempty string.</p></dd><dt><b><tt class="literal">$Data::Dumper::Purity</tt></b></dt><dt><b><tt class="literal">$obj->Purity([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>Controls degree to which <tt class="literal">eval</tt> can recreate theoriginal reference structures. Setting the value to<tt class="literal">1</tt> outputs additional Perl statements to correctlyrecreate nested references. Default is <tt class="literal">0</tt>.</p></dd><dt><b><tt class="literal">$Data::Dumper::Quotekeys</tt></b></dt><dt><b><tt class="literal">$obj->Quotekeys([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>If set to a Boolean value, controls whether hash keys are quoted. Iffalse, avoids quoting hash keys that look like a simple string.Default is <tt class="literal">1</tt>, which always quotes hash keys.</p></dd><dt><b><tt class="literal">$Data::Dumper::Terse</tt></b></dt><dt><b><tt class="literal">$obj->Terse([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>When set, Data::Dumper outputs single, non-self-referential values asatoms or terms rather than as statements.<tt class="literal">$VAR</tt><em class="replaceable"><tt>n</tt></em> names are avoidedas much as possible. Such output may not be parsable by<tt class="literal">eval</tt>.</p></dd><dt><b><tt class="literal">$Data::Dumper::Toaster</tt></b></dt><dt><b><tt class="literal">$obj->Toaster([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>If set to a method name, Data::Dumper issues a method call for anyobjects that will be dumped using the syntax <tt class="literal">bless(data,class)->method( )</tt>. Set to an empty string to disable.Default is empty string.</p></dd><dt><b><tt class="literal">$Data::Dumper::Useqq</tt></b></dt><dt><b><tt class="literal">$obj->Useqq([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>When set, enables the use of double quotes to represent stringvalues. In addition, <tt class="literal">\n</tt>, <tt class="literal">\t</tt>,and <tt class="literal">\r</tt> are used to represent non-space whitespace;"unsafe" characters arebackslashed; and unprintable characters are output as quoted octalintegers. Default is <tt class="literal">0</tt>. Currently, the<tt class="literal">Dumpxs</tt> method does not honor this flag.</p></dd><dt><b><tt class="literal">$Data::Dumper::Varnam</tt></b></dt><dt><b><tt class="literal">$obj->Varname([</tt><em class="replaceable">newval</em><tt class="literal">])</tt></b></dt><dd>Specifies the prefix to use for tagging variable names in the output.Default is <tt class="literal">VAR</tt>.</p></dd></dl><p>The following methods and functions are provided.</p><a name="INDEX-1342" /><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">obj</em> = Data::Dumper->new(<em class="replaceable">arrayref</em>[, <em class="replaceable">arrayref</em>])</pre><p><a name="INDEX-1342" />Constructor.Creates a new Data::Dumper object. The first argument is an anonymousarray of values to be dumped. The optional second argument is ananonymous array of names for the values. The namesdon't need a leading <tt class="literal">$</tt> and mustbe comprised of alphanumeric characters. You can begin a name with a<tt class="literal">*</tt> to specify that the dereferenced type must bedumped instead of the reference itself for array and hash references.</p></div><a name="INDEX-1343" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Dump</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>$<em class="replaceable">obj</em>->DumpData::Dumper->Dump(<em class="replaceable">arrayref</em>[, <em class="replaceable">arrayref</em>])</pre></td><td align="right" /></tr></table><p><p><a name="INDEX-1343" />Returns stringified form ofvalues stored in the object with their order preserved, subject tothe configuration options. In array context, returns a list ofstrings corresponding to the supplied values.</p></div><a name="INDEX-1344" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Dumper</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Dumper (<em class="replaceable">list</em>)</pre><p><a name="INDEX-1344" />Function that returns thestringified form of the values in the list, subject to theconfiguration options. The values are named<tt class="literal">$VAR</tt><em class="replaceable"><tt>n</tt></em> in the output.Returns a list of strings in array context.</p></div><a name="INDEX-1345" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>DumperX</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>DumperX (<em class="replaceable">list</em>)</pre><p><a name="INDEX-1345" />Identical to<tt class="literal">Dumper</tt>, except that it calls the<tt class="literal">xsub</tt> implementation. Available only if the<tt class="literal">xsub</tt> extension to Data::Dumper is installed.</p></div><a name="INDEX-1346" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Dumpxs</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>$<em class="replaceable">obj</em>->Dumpxs<a name="INDEX-1346" />Data::Dumper->Dumpxs(<em class="replaceable">arrayref</em>[, <em class="replaceable">arrayref</em>])</pre></td><td align="right" /></tr></table><p><p>Identical to <tt class="literal">Dump</tt>, but written in C and thereforemuch faster. Available only if the <tt class="literal">xsub</tt> extensionto Data::Dumper is installed.</p></div><a name="INDEX-1347" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Reset</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">obj</em>->Reset</pre><p><a name="INDEX-1347" />Clearsthe internal table of "seen"references, returning the object itself.</p></div><a name="INDEX-1348" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Seen</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">obj</em>->Seen([<em class="replaceable">hashref</em>])</pre><p><a name="INDEX-1348" />Queries or adds to theinternal table of references that have been encountered. Thereferences aren't dumped, but their names areinserted when they are encountered subsequently. With no argument,returns the "seen" list of<tt class="literal">name => value</tt> pairs, in array context.Otherwise, returns the object itself.</p></div><a name="INDEX-1349" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Values</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">obj</em>->Values([<em class="replaceable">arrayref</em>])</pre><p><a name="INDEX-1349" />Queries or replaces theinternal array of values to be dumped. With no arguments, returns thenames. Otherwise, returns the object itself.</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_49.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_51.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.49. Cwd</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.51. DB</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 + -