~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to functions/unpack.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="../index.html">Home</a> &gt; <a href="../index-language.html">Language reference</a> &gt; <a href="../index-functions.html">Functions</a> &gt; unpack</div>
84
87
      <div id="contentBody"><div class="title_container"><div class="page_title">unpack</div></div><div class="mod_az_list"><a href="../index-functions.html">Perl functions A-Z</a> | <a href="../index-functions-by-cat.html">Perl functions by category</a> | <a href="../perlfunc.html">The 'perlfunc' manpage</a></div><ul>
85
88
<li><a name="unpack-TEMPLATE%2cEXPR"></a><b>unpack TEMPLATE,EXPR</b>
86
89
<p></p>
 
90
</li>
 
91
</ul>
 
92
<ul>
 
93
<li><a name="unpack-TEMPLATE"></a><b>unpack TEMPLATE</b>
87
94
<p><code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code> does the reverse of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>: it takes a string
88
95
and expands it out into a list of values.
89
96
(In scalar context, it returns merely the first value produced.)</p>
 
97
<p>If EXPR is omitted, unpacks the <code class="inline"><span class="i">$_</span></code>
 
98
 string.</p>
90
99
<p>The string is broken into chunks described by the TEMPLATE.  Each chunk
91
100
is converted separately to a value.  Typically, either the string is a result
92
 
of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, or the bytes of the string represent a C structure of some
 
101
of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, or the characters of the string represent a C structure of some
93
102
kind.</p>
94
103
<p>The TEMPLATE has the same format as in the <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> function.
95
104
Here's a subroutine that does substring:</p>
98
107
        <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;x$where a$howmuch&quot;</span><span class="cm">,</span> <span class="i">$what</span><span class="s">)</span><span class="sc">;</span>
99
108
    <span class="s">}</span></pre>
100
109
<p>and then there's</p>
101
 
<pre class="verbatim"><a name="ordinal"></a>    sub <span class="m">ordinal</span> <span class="s">{</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;c&quot;</span><span class="cm">,</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span> <span class="s">}</span> <span class="c"># same as ord()</span></pre>
 
110
<pre class="verbatim"><a name="ordinal"></a>    sub <span class="m">ordinal</span> <span class="s">{</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;W&quot;</span><span class="cm">,</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span> <span class="s">}</span> <span class="c"># same as ord()</span></pre>
102
111
<p>In addition to fields allowed in pack(), you may prefix a field with
103
112
a %&lt;number&gt; to indicate that
104
113
you want a &lt;number&gt;-bit checksum of the items instead of the items
109
118
computes the same number as the System V sum program:</p>
110
119
<pre class="verbatim">    <span class="i">$checksum</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span>
111
120
        <a class="l_k" href="../functions/local.html">local</a> <span class="i">$/</span><span class="sc">;</span>  <span class="c"># slurp!</span>
112
 
        <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32C*&quot;</span><span class="cm">,</span>&lt;&gt;<span class="s">)</span> % <span class="n">65535</span><span class="sc">;</span>
 
121
        <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32W*&quot;</span><span class="cm">,</span>&lt;&gt;<span class="s">)</span> % <span class="n">65535</span><span class="sc">;</span>
113
122
    <span class="s">}</span><span class="sc">;</span></pre>
114
123
<p>The following efficiently counts the number of set bits in a bit vector:</p>
115
124
<pre class="verbatim">    <span class="i">$setbits</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32b*&quot;</span><span class="cm">,</span> <span class="i">$selectmask</span><span class="s">)</span><span class="sc">;</span></pre>
116
 
<p>The <code class="inline">p</code>
117
 
 and <code class="inline">P</code>
 
125
<p>The <code class="inline"><span class="w">p</span></code>
 
126
 and <code class="inline"><span class="w">P</span></code>
118
127
 formats should be used with care.  Since Perl
119
128
has no way of checking whether the value passed to <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code>
120
129
corresponds to a valid memory location, passing a pointer value that's
125
134
<code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code> will produce null strings or zeroes, or terminate with an
126
135
error. If the input string is longer than one described by the TEMPLATE,
127
136
the rest is ignored.</p>
128
 
<p>See <a href="pack.html">pack</a> for more examples and notes.</p>
 
137
<p>See <a href="#pack">"pack"</a> for more examples and notes.</p>
129
138
</li>
130
139
</ul>
131
140
</div>
145
154
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
146
155
        </form>
147
156
      </p>
 
157
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
148
158
      <h2>Labels:</h2>
149
159
      <p>
150
160
        <a href="#" onClick="addLabel('unpack','functions/unpack.html')">Add this page</a>