📄 ch16_02.htm
字号:
<html><head><title>Manipulating Data with pack and unpack (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><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="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><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="ch16_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch16_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">16.2. Manipulating Data with pack and unpack</h2><p>When storing<a name="INDEX-1050" />data into a DBM file (orin one of the other types of databases we'll see in thischapter), you may need to store more than one item under a singlekey. And sometimes you'll need to be able to prepare someinformation to be sent over a network connection or to a system-levelfunction, or to decode it upon arrival. That's why Perl has the<tt class="literal">pack</tt> and <tt class="literal">unpack</tt> functions.</p><p>The <tt class="literal">pack</tt><a name="INDEX-1051" /> function takes a<a name="INDEX-1052" />format stringand a list of arguments and packs the arguments together to make astring. Here, we can pack three numbers of varying sizes into aseven-byte string using the formats <tt class="literal">c</tt>,<tt class="literal">s</tt>, and <tt class="literal">l</tt> (these might remindsome folks of the words "char", "short", and"long"). The first number gets packed into one byte, thesecond into two bytes, and the third into four bytes, which explainswhy we say this is a seven-byte string:</p><blockquote><pre class="code">my $buffer = pack("c s l", 31, 4159, 265359);</pre></blockquote><p>When you want the original list of items back, you can use the sameformat string with the<tt class="literal">unpack</tt><a name="INDEX-1053" /> function:</p><blockquote><pre class="code">my($char, $short, $long) = unpack("c s l", $buffer);</pre></blockquote><p>There are many different format letters available; some of these arethe same on every machine (so they're useful for sending dataover a network), while others depend upon how your machine likes towork with data (these are useful for interacting with yoursystem's own data). See the<tt class="literal">perlfunc</tt><a name="INDEX-1054" /> <a name="INDEX-1055" /> manpage for the latest list of formatletters, as new ones are being added in every new version of Perl.</p><p><a name="INDEX-1056" />Whitespace may be used at will in aformat string to improve readability, as we did in the previousexample. For most format letters, you can follow the format letterwith a number to indicate a number of times; that is, a format of<tt class="literal">"ccccccc"</tt> may be written more compactly as<tt class="literal">"c7"</tt>. Instead of a number, you may follow the lastformat letter with a star (<tt class="literal">*</tt>), which means to usethat format as many times as needed to use up the remaining items inthe list (in <tt class="literal">pack</tt>) or to use up the rest of thestring (in <tt class="literal">unpack</tt>). So a format of<tt class="literal">"c*"</tt> will either unpack a string into a list ofsmall integers, or pack up those small integers to make a string. Forsome format letters, such as <tt class="literal">a</tt>, the number is nota repeat count; <tt class="literal">"a20"</tt> is a twenty-character ASCIIstring, padded with NUL characters as needed.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch16_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch16_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">16. Simple Databases</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">16.3. Fixed-length Random-access Databases</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><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 + -