1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>pack - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-gb">
<link href="../static/css-20090810.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
</head>
<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
<div id="page">
<div id="header">
<div id="homepage_link">
<a href="../index.html"></a>
</div>
<div id="search_form">
<form action="../search.html" method="GET" id="search">
<input type="text" name="q" id="search_box" alt="Search">
</form>
</div>
</div>
<div id="body">
<div id="left_column">
<div class="side_group">
<div class="side_panel doc_panel">
<p>Manual</p>
<ul>
<li><a href="../index-overview.html">Overview</a>
<li><a href="../index-tutorials.html">Tutorials</a>
<li><a href="../index-faq.html">FAQs</a>
<li><a href="../index-history.html">History / Changes</a>
<li><a href="../index-licence.html">License</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Reference</p>
<ul>
<li><a href="../index-language.html">Language</a>
<li><a href="../index-functions.html">Functions</a>
<li><a href="../perlop.html">Operators</a>
<li><a href="../perlvar.html">Special Variables</a>
<li><a href="../index-pragmas.html">Pragmas</a>
<li><a href="../index-utilities.html">Utilities</a>
<li><a href="../index-internals.html">Internals</a></li>
<li><a href="../index-platforms.html">Platform Specific</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Modules</p>
<ul>
<li>
<a href="../index-modules-A.html">A</a>
•
<a href="../index-modules-B.html">B</a>
•
<a href="../index-modules-C.html">C</a>
•
<a href="../index-modules-D.html">D</a>
•
<a href="../index-modules-E.html">E</a>
<li>
<a href="../index-modules-F.html">F</a>
•
<a href="../index-modules-G.html">G</a>
•
<a href="../index-modules-H.html">H</a>
•
<a href="../index-modules-I.html">I</a>
•
<a href="../index-modules-L.html">L</a>
<li>
<a href="../index-modules-M.html">M</a>
•
<a href="../index-modules-N.html">N</a>
•
<a href="../index-modules-O.html">O</a>
•
<a href="../index-modules-P.html">P</a>
•
<a href="../index-modules-S.html">S</a>
<li>
<a href="../index-modules-T.html">T</a>
•
<a href="../index-modules-U.html">U</a>
•
<a href="../index-modules-X.html">X</a>
</ul>
</div>
</div>
<div class="side_group">
<div class="side_panel links_panel">
<p>News / Blogs</p>
<ul>
<li><a href="http://perlbuzz.com">Perl Buzz</a></li>
<li><a href="http://ironman.enlightenedperl.org">Perl Iron Man</a></li>
<li><a href="http://perlsphere.net">Perlsphere</a></li>
<li><a href="http://planet.perl.org">Planet Perl</a></li>
<li><a href="http://news.perlfoundation.org/">TPF news</a></li>
<li><a href="http://use.perl.org">Use Perl</a></li>
</ul>
</div>
<div class="side_panel links_panel">
<p>See also</p>
<ul>
<li><a href="http://search.cpan.org">CPAN</a></li>
<li><a href="http://www.perl.org">Perl.org</a></li>
<li><a href="http://www.perl.com">Perl.com</a></li>
<li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
<li><a href="http://jobs.perl.org">Perl Jobs</a></li>
<li><a href="http://www.pm.org">Perl Mongers</a></li>
<li><a href="http://www.perlmonks.org">Perl Monks</a></li>
</ul>
</div>
<div class="side_panel links_panel">
<p>Contact</p>
<ul>
<li><a href="http://perl.jonallen.info">Site designed and maintained by Jon Allen (JJ)</a>
<li><a href="http://perl.jonallen.info">http://perl.jonallen.info</a>
</ul>
<ul>
<li><a href="http://perl.jonallen.info/projects/perldoc">View the project page and FAQs</a>
</ul>
</div>
</div>
</div>
<div id="centre_column">
<div id="content_header">
<div id="title_bar">
<div id="page_name">
<h1>pack</h1>
</div>
<div id="perl_version">
Perl 5 version 10.1 documentation
</div>
<div id="page_links">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
</div>
</div>
<div id="breadcrumbs">
<a href="../index.html">Home</a> >
<a href="../index-language.html">Language reference</a> >
<a href="../index-functions.html">Functions</a> >
pack
</div>
</div>
<div id="content_body">
<!--[if lt IE 7]>
<div class="noscript">
<p>
<strong>It looks like you're using Internet Explorer 6. This is a very old
browser which does not offer full support for modern websites.</strong>
</p>
<p>
Unfortunately this means that this website will not work on
your computer.
</p>
<p>
Don't miss out though! To view the site (and get a better experience from
many other websites), simply upgrade to
<a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
or download an alternative browser such as
<a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
<a href="http://www.apple.com/safari/download/">Safari</a>, or
<a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
<p>
All of these browsers are free. If you're using a PC at work, you may
need to contact your IT administrator.
</p>
</div>
<![endif]-->
<noscript>
<div class="noscript">
<p>
<strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
or are running a non-JavaScript capable web browser.</strong>
</p>
<p>
To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
</div>
</noscript>
<div id="from_search"></div>
<h1>pack</h1>
<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>
<li><a name="pack-TEMPLATE%2cLIST"></a><b>pack TEMPLATE,LIST</b>
<p></p>
<p>Takes a LIST of values and converts it into a string using the rules
given by the TEMPLATE. The resulting string is the concatenation of
the converted values. Typically, each converted value looks
like its machine-level representation. For example, on 32-bit machines
an integer may be represented by a sequence of 4 bytes that will be
converted to a sequence of 4 characters.</p>
<p>The TEMPLATE is a sequence of characters that give the order and type
of values, as follows:</p>
<pre class="verbatim"><ol><li> a A string with arbitrary binary data, will be null padded.</li><li> A A text (ASCII) string, will be space padded.</li><li> Z A null terminated (ASCIZ) string, will be null padded.</li><li></li><li> b A bit string (ascending bit order inside each byte, like vec()).</li><li> B A bit string (descending bit order inside each byte).</li><li> h A hex string (low nybble first).</li><li> H A hex string (high nybble first).</li><li></li><li> c A signed char (8-bit) value.</li><li> C An unsigned char (octet) value.</li><li> W An unsigned char value (can be greater than 255).</li><li></li><li> s A signed short (16-bit) value.</li><li> S An unsigned short value.</li><li></li><li> l A signed long (32-bit) value.</li><li> L An unsigned long value.</li><li></li><li> q A signed quad (64-bit) value.</li><li> Q An unsigned quad value.</li><li> (Quads are available only if your system supports 64-bit</li><li> integer values _and_ if Perl has been compiled to support those.</li><li> Causes a fatal error otherwise.)</li><li></li><li> i A signed integer value.</li><li> I A unsigned integer value.</li><li> (This 'integer' is _at_least_ 32 bits wide. Its exact</li><li> size depends on what a local C compiler calls 'int'.)</li><li></li><li> n An unsigned short (16-bit) in "network" (big-endian) order.</li><li> N An unsigned long (32-bit) in "network" (big-endian) order.</li><li> v An unsigned short (16-bit) in "VAX" (little-endian) order.</li><li> V An unsigned long (32-bit) in "VAX" (little-endian) order.</li><li></li><li> j A Perl internal signed integer value (IV).</li><li> J A Perl internal unsigned integer value (UV).</li><li></li><li> f A single-precision float in the native format.</li><li> d A double-precision float in the native format.</li><li></li><li> F A Perl internal floating point value (NV) in the native format</li><li> D A long double-precision float in the native format.</li><li> (Long doubles are available only if your system supports long</li><li> double values _and_ if Perl has been compiled to support those.</li><li> Causes a fatal error otherwise.)</li><li></li><li> p A pointer to a null-terminated string.</li><li> P A pointer to a structure (fixed-length string).</li><li></li><li> u A uuencoded string.</li><li> U A Unicode character number. Encodes to a character in character mode</li><li> and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in byte mode.</li><li></li><li> w A BER compressed integer (not an ASN.1 BER, see perlpacktut for</li><li> details). Its bytes represent an unsigned integer in base 128,</li><li> most significant digit first, with as few digits as possible. Bit</li><li> eight (the high bit) is set on each byte except the last.</li><li></li><li> x A null byte.</li><li> X Back up a byte.</li><li> @ Null fill or truncate to absolute position, counted from the</li><li> start of the innermost ()-group.</li><li> . Null fill or truncate to absolute position specified by value.</li><li> ( Start of a ()-group.</li></ol></pre><p>One or more of the modifiers below may optionally follow some letters in the
TEMPLATE (the second column lists the letters for which the modifier is
valid):</p>
<pre class="verbatim"><ol><li> ! sSlLiI Forces native (short, long, int) sizes instead</li><li> of fixed (16-/32-bit) sizes.</li><li></li><li> xX Make x and X act as alignment commands.</li><li></li><li> nNvV Treat integers as signed instead of unsigned.</li><li></li><li> @. Specify position as byte offset in the internal</li><li> representation of the packed string. Efficient but</li><li> dangerous.</li><li></li><li> > sSiIlLqQ Force big-endian byte-order on the type.</li><li> jJfFdDpP (The "big end" touches the construct.)</li><li></li><li> < sSiIlLqQ Force little-endian byte-order on the type.</li><li> jJfFdDpP (The "little end" touches the construct.)</li></ol></pre><p>The <code class="inline">></code> and <code class="inline"><</code>
modifiers can also be used on <code class="inline"><span class="s">(</span><span class="s">)</span></code>
-groups,
in which case they force a certain byte-order on all components of
that group, including subgroups.</p>
<p>The following rules apply:</p>
<ul>
<li>
<p>Each letter may optionally be followed by a number giving a repeat
count. With all types except <code class="inline"><span class="w">a</span></code>
, <code class="inline"><span class="w">A</span></code>
, <code class="inline"><span class="w">Z</span></code>
, <code class="inline"><span class="w">b</span></code>
, <code class="inline"><span class="w">B</span></code>
, <code class="inline"><span class="w">h</span></code>
,
<code class="inline"><span class="w">H</span></code>
, <code class="inline"><span class="i">@</span></code>
, <code class="inline">.</code>, <code class="inline"><span class="w">x</span></code>
, <code class="inline"><span class="w">X</span></code>
and <code class="inline"><span class="w">P</span></code>
the pack function will gobble up
that many values from the LIST. A <code class="inline"><span class="i">*</span></code>
for the repeat count means to
use however many items are left, except for <code class="inline"><span class="i">@</span></code>
, <code class="inline"><span class="w">x</span></code>
, <code class="inline"><span class="w">X</span></code>
, where it
is equivalent to <code class="inline"><span class="n">0</span></code>
, for <.> where it means relative to string start
and <code class="inline"><span class="w">u</span></code>
, where it is equivalent to 1 (or 45, which is the same).
A numeric repeat count may optionally be enclosed in brackets, as in
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a> <span class="q">'C[80]'</span><span class="cm">,</span> <span class="i">@arr</span></code>
.</p>
<p>One can replace the numeric repeat count by a template enclosed in brackets;
then the packed length of this template in bytes is used as a count.
For example, <code class="inline"><span class="w">x</span><span class="s">[</span><span class="w">L</span><span class="s">]</span></code>
skips a long (it skips the number of bytes in a long);
the template <code class="inline">$t X[$t] $t</code> unpack()s twice what $t unpacks.
If the template in brackets contains alignment commands (such as <code class="inline"><span class="w">x</span>!<span class="s">[</span><span class="w">d</span><span class="s">]</span></code>
),
its packed length is calculated as if the start of the template has the maximal
possible alignment.</p>
<p>When used with <code class="inline"><span class="w">Z</span></code>
, <code class="inline"><span class="i">*</span></code>
results in the addition of a trailing null
byte (so the packed result will be one longer than the byte <code class="inline"><a class="l_k" href="../functions/length.html">length</a></code>
of the item).</p>
<p>When used with <code class="inline"><span class="i">@</span></code>
, the repeat count represents an offset from the start
of the innermost () group.</p>
<p>When used with <code class="inline">.</code>, the repeat count is used to determine the starting
position from where the value offset is calculated. If the repeat count
is 0, it's relative to the current position. If the repeat count is <code class="inline"><span class="i">*</span></code>
,
the offset is relative to the start of the packed string. And if its an
integer <code class="inline"><span class="w">n</span></code>
the offset is relative to the start of the n-th innermost
() group (or the start of the string if <code class="inline"><span class="w">n</span></code>
is bigger then the group
level).</p>
<p>The repeat count for <code class="inline"><span class="w">u</span></code>
is interpreted as the maximal number of bytes
to encode per line of output, with 0, 1 and 2 replaced by 45. The repeat
count should not be more than 65.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">a</span></code>
, <code class="inline"><span class="w">A</span></code>
, and <code class="inline"><span class="w">Z</span></code>
types gobble just one value, but pack it as a
string of length count, padding with nulls or spaces as necessary. When
unpacking, <code class="inline"><span class="w">A</span></code>
strips trailing whitespace and nulls, <code class="inline"><span class="w">Z</span></code>
strips everything
after the first null, and <code class="inline"><span class="w">a</span></code>
returns data verbatim.</p>
<p>If the value-to-pack is too long, it is truncated. If too long and an
explicit count is provided, <code class="inline"><span class="w">Z</span></code>
packs only <code class="inline"><span class="i">$count</span>-<span class="n">1</span></code>
bytes, followed
by a null byte. Thus <code class="inline"><span class="w">Z</span></code>
always packs a trailing null (except when the
count is 0).</p>
</li>
<li>
<p>Likewise, the <code class="inline"><span class="w">b</span></code>
and <code class="inline"><span class="w">B</span></code>
fields pack a string that many bits long.
Each character of the input field of pack() generates 1 bit of the result.
Each result bit is based on the least-significant bit of the corresponding
input character, i.e., on <code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)%2</a></code>. In particular, characters <code class="inline"><span class="q">"0"</span></code>
and <code class="inline"><span class="q">"1"</span></code>
generate bits 0 and 1, as do characters <code class="inline"><span class="q">"\0"</span></code>
and <code class="inline"><span class="q">"\1"</span></code>
.</p>
<p>Starting from the beginning of the input string of pack(), each 8-tuple
of characters is converted to 1 character of output. With format <code class="inline"><span class="w">b</span></code>
the first character of the 8-tuple determines the least-significant bit of a
character, and with format <code class="inline"><span class="w">B</span></code>
it determines the most-significant bit of
a character.</p>
<p>If the length of the input string is not exactly divisible by 8, the
remainder is packed as if the input string were padded by null characters
at the end. Similarly, during unpack()ing the "extra" bits are ignored.</p>
<p>If the input string of pack() is longer than needed, extra characters are
ignored. A <code class="inline"><span class="i">*</span></code>
for the repeat count of pack() means to use all the
characters of the input field. On unpack()ing the bits are converted to a
string of <code class="inline"><span class="q">"0"</span></code>
s and <code class="inline"><span class="q">"1"</span></code>
s.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">h</span></code>
and <code class="inline"><span class="w">H</span></code>
fields pack a string that many nybbles (4-bit groups,
representable as hexadecimal digits, 0-9a-f) long.</p>
<p>Each character of the input field of pack() generates 4 bits of the result.
For non-alphabetical characters the result is based on the 4 least-significant
bits of the input character, i.e., on <code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)%16</a></code>. In particular,
characters <code class="inline"><span class="q">"0"</span></code>
and <code class="inline"><span class="q">"1"</span></code>
generate nybbles 0 and 1, as do bytes
<code class="inline"><span class="q">"\0"</span></code>
and <code class="inline"><span class="q">"\1"</span></code>
. For characters <code class="inline"><span class="q">"a"</span>..<span class="q">"f"</span></code>
and <code class="inline"><span class="q">"A"</span>..<span class="q">"F"</span></code>
the result
is compatible with the usual hexadecimal digits, so that <code class="inline"><span class="q">"a"</span></code>
and
<code class="inline"><span class="q">"A"</span></code>
both generate the nybble <code class="inline"><span class="n">0xa</span>==<span class="n">10</span></code>
. The result for characters
<code class="inline"><span class="q">"g"</span>..<span class="q">"z"</span></code>
and <code class="inline"><span class="q">"G"</span>..<span class="q">"Z"</span></code>
is not well-defined.</p>
<p>Starting from the beginning of the input string of pack(), each pair
of characters is converted to 1 character of output. With format <code class="inline"><span class="w">h</span></code>
the
first character of the pair determines the least-significant nybble of the
output character, and with format <code class="inline"><span class="w">H</span></code>
it determines the most-significant
nybble.</p>
<p>If the length of the input string is not even, it behaves as if padded
by a null character at the end. Similarly, during unpack()ing the "extra"
nybbles are ignored.</p>
<p>If the input string of pack() is longer than needed, extra characters are
ignored.
A <code class="inline"><span class="i">*</span></code>
for the repeat count of pack() means to use all the characters of
the input field. On unpack()ing the nybbles are converted to a string
of hexadecimal digits.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">p</span></code>
type packs a pointer to a null-terminated string. You are
responsible for ensuring the string is not a temporary value (which can
potentially get deallocated before you get around to using the packed result).
The <code class="inline"><span class="w">P</span></code>
type packs a pointer to a structure of the size indicated by the
length. A NULL pointer is created if the corresponding value for <code class="inline"><span class="w">p</span></code>
or
<code class="inline"><span class="w">P</span></code>
is <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, similarly for unpack().</p>
<p>If your system has a strange pointer size (i.e. a pointer is neither as
big as an int nor as big as a long), it may not be possible to pack or
unpack pointers in big- or little-endian byte order. Attempting to do
so will result in a fatal error.</p>
</li>
<li>
<p>The <code class="inline">/</code> template character allows packing and unpacking of a sequence of
items where the packed structure contains a packed item count followed by
the packed items themselves.</p>
<p>For <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> you write <i>length-item</i><code class="inline">/</code><i>sequence-item</i> and the
<i>length-item</i> describes how the length value is packed. The ones likely
to be of most use are integer-packing ones like <code class="inline"><span class="w">n</span></code>
(for Java strings),
<code class="inline"><span class="w">w</span></code>
(for ASN.1 or SNMP) and <code class="inline"><span class="w">N</span></code>
(for Sun XDR).</p>
<p>For <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, the <i>sequence-item</i> may have a repeat count, in which case
the minimum of that and the number of available items is used as argument
for the <i>length-item</i>. If it has no repeat count or uses a '*', the number
of available items is used.</p>
<p>For <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code> an internal stack of integer arguments unpacked so far is
used. You write <code class="inline">/</code><i>sequence-item</i> and the repeat count is obtained by
popping off the last element from the stack. The <i>sequence-item</i> must not
have a repeat count.</p>
<p>If the <i>sequence-item</i> refers to a string type (<code class="inline"><span class="q">"A"</span></code>
, <code class="inline"><span class="q">"a"</span></code>
or <code class="inline"><span class="q">"Z"</span></code>
),
the <i>length-item</i> is a string length, not a number of strings. If there is
an explicit repeat count for pack, the packed string will be adjusted to that
given length.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'W/a'</span><span class="cm">,</span> <span class="q">"\04Gurusamy"</span><span class="sc">;</span> <span class="w">gives</span> <span class="s">(</span><span class="q">'Guru'</span><span class="s">)</span></li><li> <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'a3/A A*'</span><span class="cm">,</span> <span class="q">'007 Bond J '</span><span class="sc">;</span> <span class="w">gives</span> <span class="s">(</span><span class="q">' Bond'</span><span class="cm">,</span> <span class="q">'J'</span><span class="s">)</span></li><li> <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'a3 x2 /A A*'</span><span class="cm">,</span> <span class="q">'007: Bond, J.'</span><span class="sc">;</span> <span class="w">gives</span> <span class="s">(</span><span class="q">'Bond, J'</span><span class="cm">,</span> <span class="q">'.'</span><span class="s">)</span></li><li> <a class="l_k" href="../functions/pack.html">pack</a> <span class="q">'n/a* w/a'</span><span class="cm">,</span><span class="q">'hello,'</span><span class="cm">,</span><span class="q">'world'</span><span class="sc">;</span> <span class="w">gives</span> <span class="q">"\000\006hello,\005world"</span></li><li> <a class="l_k" href="../functions/pack.html">pack</a> <span class="q">'a/W2'</span><span class="cm">,</span> <a class="l_k" href="../functions/ord.html">ord</a><span class="s">(</span><span class="q">'a'</span><span class="s">)</span> .. <a class="l_k" href="../functions/ord.html">ord</a><span class="s">(</span><span class="q">'z'</span><span class="s">)</span><span class="sc">;</span> <span class="w">gives</span> <span class="q">'2ab'</span></li></ol></pre><p>The <i>length-item</i> is not returned explicitly from <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code>.</p>
<p>Adding a count to the <i>length-item</i> letter is unlikely to do anything
useful, unless that letter is <code class="inline"><span class="w">A</span></code>
, <code class="inline"><span class="w">a</span></code>
or <code class="inline"><span class="w">Z</span></code>
. Packing with a
<i>length-item</i> of <code class="inline"><span class="w">a</span></code>
or <code class="inline"><span class="w">Z</span></code>
may introduce <code class="inline"><span class="q">"\000"</span></code>
characters,
which Perl does not regard as legal in numeric strings.</p>
</li>
<li>
<p>The integer types <code class="inline"><a class="l_k" href="../functions/s.html">s</a></code>, <code class="inline"><span class="w">S</span></code>
, <code class="inline"><span class="w">l</span></code>
, and <code class="inline"><span class="w">L</span></code>
may be
followed by a <code class="inline">!</code>
modifier to signify native shorts or
longs--as you can see from above for example a bare <code class="inline"><span class="w">l</span></code>
does mean
exactly 32 bits, the native <code class="inline"><span class="w">long</span></code>
(as seen by the local C compiler)
may be larger. This is an issue mainly in 64-bit platforms. You can
see whether using <code class="inline">!</code>
makes any difference by</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"s"</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">" "</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"s!"</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"l"</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">" "</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"l!"</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">i</span>!</code>
and <code class="inline"><span class="w">I</span>!</code>
also work but only because of completeness;
they are identical to <code class="inline"><span class="w">i</span></code>
and <code class="inline"><span class="w">I</span></code>
.</p>
<p>The actual sizes (in bytes) of native shorts, ints, longs, and long
longs on the platform where Perl was built are also available via
<a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">shortsize</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">intsize</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">longsize</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">longlongsize</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p>(The <code class="inline"><span class="i">$Config</span>{<span class="w">longlongsize</span>}</code>
will be undefined if your system does
not support long longs.)</p>
</li>
<li>
<p>The integer formats <code class="inline"><a class="l_k" href="../functions/s.html">s</a></code>, <code class="inline"><span class="w">S</span></code>
, <code class="inline"><span class="w">i</span></code>
, <code class="inline"><span class="w">I</span></code>
, <code class="inline"><span class="w">l</span></code>
, <code class="inline"><span class="w">L</span></code>
, <code class="inline"><span class="w">j</span></code>
, and <code class="inline"><span class="w">J</span></code>
are inherently non-portable between processors and operating systems
because they obey the native byteorder and endianness. For example a
4-byte integer 0x12345678 (305419896 decimal) would be ordered natively
(arranged in and handled by the CPU registers) into bytes as</p>
<pre class="verbatim"><ol><li> 0x12 0x34 0x56 0x78 # big-endian</li><li> 0x78 0x56 0x34 0x12 # little-endian</li></ol></pre><p>Basically, the Intel and VAX CPUs are little-endian, while everybody
else, for example Motorola m68k/88k, PPC, Sparc, HP PA, Power, and
Cray are big-endian. Alpha and MIPS can be either: Digital/Compaq
used/uses them in little-endian mode; SGI/Cray uses them in big-endian
mode.</p>
<p>The names `big-endian' and `little-endian' are comic references to
the classic "Gulliver's Travels" (via the paper "On Holy Wars and a
Plea for Peace" by Danny Cohen, USC/ISI IEN 137, April 1, 1980) and
the egg-eating habits of the Lilliputians.</p>
<p>Some systems may have even weirder byte orders such as</p>
<pre class="verbatim"><ol><li> 0x56 0x78 0x12 0x34</li><li> 0x34 0x12 0x78 0x56</li></ol></pre><p>You can see your system's preference with</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">" "</span><span class="cm">,</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="../functions/sprintf.html">sprintf</a> <span class="q">"%#02x"</span><span class="cm">,</span> <span class="i">$_</span> <span class="s">}</span></li><li> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">"W*"</span><span class="cm">,</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"L"</span><span class="cm">,</span><span class="n">0x12345678</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p>The byteorder on the platform where Perl was built is also available
via <a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">byteorder</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p>Byteorders <code class="inline"><span class="q">'1234'</span></code>
and <code class="inline"><span class="q">'12345678'</span></code>
are little-endian, <code class="inline"><span class="q">'4321'</span></code>
and <code class="inline"><span class="q">'87654321'</span></code>
are big-endian.</p>
<p>If you want portable packed integers you can either use the formats
<code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
, and <code class="inline"><span class="w">V</span></code>
, or you can use the <code class="inline">></code> and <code class="inline"><</code>
modifiers. These modifiers are only available as of perl 5.9.2.
See also <a href="../perlport.html">perlport</a>.</p>
</li>
<li>
<p>All integer and floating point formats as well as <code class="inline"><span class="w">p</span></code>
and <code class="inline"><span class="w">P</span></code>
and
<code class="inline"><span class="s">(</span><span class="s">)</span></code>
-groups may be followed by the <code class="inline">></code> or <code class="inline"><</code>
modifiers
to force big- or little- endian byte-order, respectively.
This is especially useful, since <code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
and <code class="inline"><span class="w">V</span></code>
don't cover
signed integers, 64-bit integers and floating point values. However,
there are some things to keep in mind.</p>
<p>Exchanging signed integers between different platforms only works
if all platforms store them in the same format. Most platforms store
signed integers in two's complement, so usually this is not an issue.</p>
<p>The <code class="inline">></code> or <code class="inline"><</code>
modifiers can only be used on floating point
formats on big- or little-endian machines. Otherwise, attempting to
do so will result in a fatal error.</p>
<p>Forcing big- or little-endian byte-order on floating point values for
data exchange can only work if all platforms are using the same
binary representation (e.g. IEEE floating point format). Even if all
platforms are using IEEE, there may be subtle differences. Being able
to use <code class="inline">></code> or <code class="inline"><</code>
on floating point values can be very useful,
but also very dangerous if you don't know exactly what you're doing.
It is definitely not a general way to portably store floating point
values.</p>
<p>When using <code class="inline">></code> or <code class="inline"><</code>
on an <code class="inline"><span class="s">(</span><span class="s">)</span></code>
-group, this will affect
all types inside the group that accept the byte-order modifiers,
including all subgroups. It will silently be ignored for all other
types. You are not allowed to override the byte-order within a group
that already has a byte-order modifier suffix.</p>
</li>
<li>
<p>Real numbers (floats and doubles) are in the native machine format only;
due to the multiplicity of floating formats around, and the lack of a
standard "network" representation, no facility for interchange has been
made. This means that packed floating point data written on one machine
may not be readable on another - even if both use IEEE floating point
arithmetic (as the endian-ness of the memory representation is not part
of the IEEE spec). See also <a href="../perlport.html">perlport</a>.</p>
<p>If you know exactly what you're doing, you can use the <code class="inline">></code> or <code class="inline"><</code>
modifiers to force big- or little-endian byte-order on floating point values.</p>
<p>Note that Perl uses doubles (or long doubles, if configured) internally for
all numeric calculation, and converting from double into float and thence back
to double again will lose precision (i.e., <code class="inline">unpack("f", pack("f", $foo)</code>)
will not in general equal $foo).</p>
</li>
<li>
<p>Pack and unpack can operate in two modes, character mode (<code class="inline"><span class="w">C0</span></code>
mode) where
the packed string is processed per character and UTF-8 mode (<code class="inline"><span class="w">U0</span></code>
mode)
where the packed string is processed in its UTF-8-encoded Unicode form on
a byte by byte basis. Character mode is the default unless the format string
starts with an <code class="inline"><span class="w">U</span></code>
. You can switch mode at any moment with an explicit
<code class="inline"><span class="w">C0</span></code>
or <code class="inline"><span class="w">U0</span></code>
in the format. A mode is in effect until the next mode switch
or until the end of the ()-group in which it was entered.</p>
</li>
<li>
<p>You must yourself do any alignment or padding by inserting for example
enough <code class="inline"><span class="q">'x'</span></code>
es while packing. There is no way to pack() and unpack()
could know where the characters are going to or coming from. Therefore
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> (and <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code>) handle their output and input as flat
sequences of characters.</p>
</li>
<li>
<p>A ()-group is a sub-TEMPLATE enclosed in parentheses. A group may
take a repeat count, both as postfix, and for unpack() also via the <code class="inline">/</code>
template character. Within each repetition of a group, positioning with
<code class="inline"><span class="i">@</span></code>
starts again at 0. Therefore, the result of</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span> <span class="q">'@1A((@2A)@3A)'</span><span class="cm">,</span> <span class="q">'a'</span><span class="cm">,</span> <span class="q">'b'</span><span class="cm">,</span> <span class="q">'c'</span> <span class="s">)</span></li></ol></pre><p>is the string "\0a\0\0bc".</p>
</li>
<li>
<p><code class="inline"><span class="w">x</span></code>
and <code class="inline"><span class="w">X</span></code>
accept <code class="inline">!</code>
modifier. In this case they act as
alignment commands: they jump forward/back to the closest position
aligned at a multiple of <code class="inline"><span class="w">count</span></code>
characters. For example, to pack() or
unpack() C's <code class="inline"><span class="i">struct</span> <span class="s">{</span><span class="w">char</span> <span class="w">c</span><span class="sc">;</span> <span class="w">double</span> <span class="w">d</span><span class="sc">;</span> <span class="w">char</span> <span class="w">cc</span><span class="s">[</span><span class="n">2</span><span class="s">]</span><span class="s">}</span></code>
one may need to
use the template <code class="inline"><span class="w">W</span> <span class="w">x</span>!<span class="s">[</span><span class="w">d</span><span class="s">]</span> <span class="w">d</span> <span class="w">W</span><span class="s">[</span><span class="n">2</span><span class="s">]</span></code>
; this assumes that doubles must be
aligned on the double's size.</p>
<p>For alignment commands <code class="inline"><span class="w">count</span></code>
of 0 is equivalent to <code class="inline"><span class="w">count</span></code>
of 1;
both result in no-ops.</p>
</li>
<li>
<p><code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
and <code class="inline"><span class="w">V</span></code>
accept the <code class="inline">!</code>
modifier. In this case they
will represent signed 16-/32-bit integers in big-/little-endian order.
This is only portable if all platforms sharing the packed data use the
same binary representation for signed integers (e.g. all platforms are
using two's complement representation).</p>
</li>
<li>
<p>A comment in a TEMPLATE starts with <code class="inline"><span class="c">#</span></code>
and goes to the end of line.
White space may be used to separate pack codes from each other, but
modifiers and a repeat count must follow immediately.</p>
</li>
<li>
<p>If TEMPLATE requires more arguments to pack() than actually given, pack()
assumes additional <code class="inline"><span class="q">""</span></code>
arguments. If TEMPLATE requires fewer arguments
to pack() than actually given, extra arguments are ignored.</p>
</li>
</ul>
<p>Examples:</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"WWWW"</span><span class="cm">,</span><span class="n">65</span><span class="cm">,</span><span class="n">66</span><span class="cm">,</span><span class="n">67</span><span class="cm">,</span><span class="n">68</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># foo eq "ABCD"</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"W4"</span><span class="cm">,</span><span class="n">65</span><span class="cm">,</span><span class="n">66</span><span class="cm">,</span><span class="n">67</span><span class="cm">,</span><span class="n">68</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># same thing</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"W4"</span><span class="cm">,</span><span class="n">0x24b6</span><span class="cm">,</span><span class="n">0x24b7</span><span class="cm">,</span><span class="n">0x24b8</span><span class="cm">,</span><span class="n">0x24b9</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># same thing with Unicode circled letters.</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"U4"</span><span class="cm">,</span><span class="n">0x24b6</span><span class="cm">,</span><span class="n">0x24b7</span><span class="cm">,</span><span class="n">0x24b8</span><span class="cm">,</span><span class="n">0x24b9</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># same thing with Unicode circled letters. You don't get the UTF-8</span></li><li> <span class="c"># bytes because the U at the start of the format caused a switch to</span></li><li> <span class="c"># U0-mode, so the UTF-8 bytes get joined into characters</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"C0U4"</span><span class="cm">,</span><span class="n">0x24b6</span><span class="cm">,</span><span class="n">0x24b7</span><span class="cm">,</span><span class="n">0x24b8</span><span class="cm">,</span><span class="n">0x24b9</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># foo eq "\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9"</span></li><li> <span class="c"># This is the UTF-8 encoding of the string in the previous example</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"ccxxcc"</span><span class="cm">,</span><span class="n">65</span><span class="cm">,</span><span class="n">66</span><span class="cm">,</span><span class="n">67</span><span class="cm">,</span><span class="n">68</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># foo eq "AB\0\0CD"</span></li><li></li><li> <span class="c"># note: the above examples featuring "W" and "c" are true</span></li><li> <span class="c"># only on ASCII and ASCII-derived systems such as ISO Latin 1</span></li><li> <span class="c"># and UTF-8. In EBCDIC the first example would be</span></li><li> <span class="c"># $foo = pack("WWWW",193,194,195,196);</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"s2"</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># "\1\0\2\0" on little-endian</span></li><li> <span class="c"># "\0\1\0\2" on big-endian</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"a4"</span><span class="cm">,</span><span class="q">"abcd"</span><span class="cm">,</span><span class="q">"x"</span><span class="cm">,</span><span class="q">"y"</span><span class="cm">,</span><span class="q">"z"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># "abcd"</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"aaaa"</span><span class="cm">,</span><span class="q">"abcd"</span><span class="cm">,</span><span class="q">"x"</span><span class="cm">,</span><span class="q">"y"</span><span class="cm">,</span><span class="q">"z"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># "axyz"</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"a14"</span><span class="cm">,</span><span class="q">"abcdefg"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># "abcdefg\0\0\0\0\0\0\0"</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"i9pl"</span><span class="cm">,</span> <a class="l_k" href="../functions/gmtime.html">gmtime</a><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># a real struct tm (on my system anyway)</span></li><li></li><li> <span class="i">$utmp_template</span> = <span class="q">"Z8 Z8 Z16 L"</span><span class="sc">;</span></li><li> <span class="i">$utmp</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="i">$utmp_template</span><span class="cm">,</span> <span class="i">@utmp1</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># a struct utmp (BSDish)</span></li><li></li><li> <span class="i">@utmp2</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="i">$utmp_template</span><span class="cm">,</span> <span class="i">$utmp</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># "@utmp1" eq "@utmp2"</span></li><li></li><li><a name="bintodec"></a> sub <span class="m">bintodec</span> <span class="s">{</span></li><li> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">"N"</span><span class="cm">,</span> <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">"B32"</span><span class="cm">,</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="q">"0"</span> x <span class="n">32</span> . <a class="l_k" href="../functions/shift.html">shift</a><span class="cm">,</span> <span class="n">-32</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'sx2l'</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># short 12, two zero bytes padding, long 34</span></li><li> <span class="i">$bar</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'s@4l'</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># short 12, zero fill to position 4, long 34</span></li><li> <span class="c"># $foo eq $bar</span></li><li> <span class="i">$baz</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'s.l'</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">34</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># short 12, zero fill to position 4, long 34</span></li><li></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'nN'</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">4711</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># pack big-endian 16- and 32-bit unsigned integers</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'S>L>'</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">4711</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># exactly the same</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'s<l<'</span><span class="cm">,</span> <span class="n">-42</span><span class="cm">,</span> <span class="n">4711</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># pack little-endian 16- and 32-bit signed integers</span></li><li> <span class="i">$foo</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">'(sl)<'</span><span class="cm">,</span> <span class="n">-42</span><span class="cm">,</span> <span class="n">4711</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># exactly the same</span></li></ol></pre><p>The same template may generally also be used in unpack().</p>
</li>
</ul>
</div>
<div id="content_footer">
</div>
</div>
<div id="right_column">
<div class="side_group">
<div class="side_panel tools_panel">
<p>Recently read</p>
<div id="recent_pages"></div>
</div>
<div class="side_panel tools_panel">
<p>Tools</p>
<ul>
<li><a href="../preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
</div>
</div>
</div>
<script language="JavaScript" src="../static/combined-20090809.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(1);
perldoc.pageName = 'pack';
perldoc.pageAddress = 'functions/pack.html';
perldoc.contentPage = 1;
</script>
</body>
</html>
|