📄 ch11_12.htm
字号:
<HTML><HEAD><TITLE>Recipe 11.11. Printing Data Structures (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:40:51Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch11_01.htm"TITLE="11. References and Records"><LINKREL="prev"HREF="ch11_11.htm"TITLE="11.10. Reading and Writing Hash Records to Text Files"><LINKREL="next"HREF="ch11_13.htm"TITLE="11.12. Copying Data Structures"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_11.htm"TITLE="11.10. Reading and Writing Hash Records to Text Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.10. Reading and Writing Hash Records to Text Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch11_01.htm"TITLE="11. References and Records"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_13.htm"TITLE="11.12. Copying Data Structures"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.12. Copying Data Structures"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch11-chap11_printing_0">11.11. Printing Data Structures</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-1156">Problem<ACLASS="indexterm"NAME="ch11-idx-1000004855-0"></A><ACLASS="indexterm"NAME="ch11-idx-1000004855-1"></A></A></H3><PCLASS="para">You want to print out a data structure.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-1162">Solution</A></H3><PCLASS="para">If the output's legibility and layout are important, write your own custom printing routine.</P><PCLASS="para">If you are in the Perl debugger, use the <CODECLASS="literal">x</CODE><ACLASS="indexterm"NAME="ch11-idx-1000004856-0"></A> command:</P><PRECLASS="programlisting">DB<1> $reference = [ { "foo" => "bar" }, 3, sub { print "hello, world\n" } ];DB<2> x $reference<CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 0 ARRAY(0x1d033c)</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 0 HASH(0x7b390)</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 'foo' = 'bar'></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 1 3</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 2 CODE(0x21e3e4)</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> - & in ???></I></CODE></B></CODE></PRE><PCLASS="para">From within your own programs, use the <CODECLASS="literal">Dumper</CODE> function from the CPAN module <ACLASS="indexterm"NAME="ch11-idx-1000004857-0"></A>Data::Dumper:</P><PRECLASS="programlisting">use Data::Dumper;print Dumper($reference);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-1194">Discussion</A></H3><PCLASS="para">Sometimes you'll want to make a dedicated function for your data structure that delivers a particular output format, but often this is overkill. If you're running under the Perl debugger, the <CODECLASS="literal">x</CODE> and <CODECLASS="literal">X</CODE> commands provide nice pretty-printing. The <CODECLASS="literal">x</CODE> command is more useful because it works on both global and lexical variables, whereas <CODECLASS="literal">X</CODE> only works on globals. Pass <CODECLASS="literal">x</CODE> a reference to the data structure you want to print.</P><PRECLASS="programlisting">D<1> x \@INC<CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 0 ARRAY(0x807d0a8)</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 0 '/home/tchrist/perllib' </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 1 '/usr/lib/perl5/i686-linux/5.00403'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 2 '/usr/lib/perl5' </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 3 '/usr/lib/perl5/site_perl/i686-linux' </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 4 '/usr/lib/perl5/site_perl' </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 5 '.'</I></CODE></B></CODE><ACLASS="indexterm"NAME="ch11-idx-1000004863-0"></A></PRE><PCLASS="para">These commands use the <EMCLASS="emphasis">dumpvar.pl</EM> library. Here's an example:</P><PRECLASS="programlisting">{ package main; require "dumpvar.pl" } *dumpvar = \&main::dumpvar if __PACKAGE__ ne 'main';dumpvar("main", "INC"); # show both @INC and %INC</PRE><PCLASS="para">The <EMCLASS="emphasis">dumpvar.pl</EM> library isn't a module, but we wish it were - so we cajole it into exporting its <CODECLASS="literal">dumpvar</CODE> function anyway. The first two lines forcibly import the <CODECLASS="literal">main::dumpvar</CODE> function from package <CODECLASS="literal">main</CODE> into the current package, assuming it's different. Here's the output of that call:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>@INC = (</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 0 '/home/tchrist/perllib/i686-linux'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 1 '/home/tchrist/perllib'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 2 '/usr/lib/perl5/i686-linux/5.00404'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 3 '/usr/lib/perl5'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 4 '/usr/lib/perl5/site_perl/i686-linux'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 5 '/usr/lib/perl5/site_perl'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 6 '.'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>)</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>%INC = (</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 'dumpvar.pl' = '/usr/lib/perl5/i686-linux/5.00404/dumpvar.pl'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 'strict.pm' = '/usr/lib/perl5/i686-linux/5.00404/strict.pm'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>)</I></CODE></B></CODE></PRE><PCLASS="para">The Data::Dumper module, located on CPAN, has a more flexible solution. It provides a <CODECLASS="literal">Dumper</CODE> function that takes a list of references and returns a string with a printable (and <CODECLASS="literal">eval</CODE>able) form of those references.</P><PRECLASS="programlisting">use Data::Dumper; print Dumper(\@INC); <CODECLASS="userinput"><B><CODECLASS="replaceable"><I>$VAR1 = [</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '/home/tchrist/perllib', </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '/usr/lib/perl5/i686-linux/5.00403',</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '/usr/lib/perl5', </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '/usr/lib/perl5/site_perl/i686-linux',</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '/usr/lib/perl5/site_perl', </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> '.'</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>];</I></CODE></B></CODE></PRE><PCLASS="para">Data::Dumper supports a variety of output formats. Check its documentation for details.<ACLASS="indexterm"NAME="ch11-idx-1000004859-0"></A><ACLASS="indexterm"NAME="ch11-idx-1000004859-1"></A><ACLASS="indexterm"NAME="ch11-idx-1000004859-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-1276">See Also</A></H3><PCLASS="para">The documentation for the CPAN module Data::Dumper; the section <ACLASS="olink"HREF="../prog/ch08_01.htm#PERL2-CH-8-SECT-1">"The Perl Debugger"</A> from <ACLASS="olink"HREF="../prog/ch08_01.htm">Chapter 8</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> or <ICLASS="filename">perldebug </I>(1)</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_11.htm"TITLE="11.10. Reading and Writing Hash Records to Text Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.10. Reading and Writing Hash Records to Text Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_13.htm"TITLE="11.12. Copying Data Structures"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.12. Copying Data Structures"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">11.10. Reading and Writing Hash Records to Text Files</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">11.12. Copying Data Structures</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -