Manual

- Overview
- Tutorials
- FAQs
- History / Changes
- License

Reference

- Language
- Functions
- Operators
- Special Variables
- Pragmas
- Utilities
- Internals
- Platform Specific

Modules

- A • B • C • D • E
- F • G • H • I • L
- M • N • O • P • S
- T • U • X

News / Blogs

- Perl Buzz
- Perl Iron Man
- Perlsphere
- Planet Perl
- TPF news
- Use Perl

See also

- CPAN
- Perl.org
- Perl.com
- Perl 5 Wiki
- Perl Jobs
- Perl Mongers
- Perl Monks

Contact

- Site designed and maintained by Jon Allen (JJ)
- http://perl.jonallen.info

- View the project page and FAQs

# unpack

Perl 5 version 10.1 documentation
Go to top

# unpack

Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage

- unpack TEMPLATE,EXPR

- unpack TEMPLATE

  `unpack` does the reverse of `pack`: it takes a string and expands it out into a list of values. (In scalar context, it returns merely the first value produced.)

  If EXPR is omitted, unpacks the `$_` string.

  The string is broken into chunks described by the TEMPLATE. Each chunk is converted separately to a value. Typically, either the string is a result of `pack`, or the characters of the string represent a C structure of some kind.

  The TEMPLATE has the same format as in the `pack` function. Here's a subroutine that does substring:

          sub substr {	my($what,$where,$howmuch) = @_;	unpack("x$where a$howmuch", $what);    }

  and then there's

          sub ordinal { unpack("W",$_[0]); } # same as ord()

  In addition to fields allowed in pack(), you may prefix a field with a %<number> to indicate that you want a <number>-bit checksum of the items instead of the items themselves. Default is a 16-bit checksum. Checksum is calculated by summing numeric values of expanded values (for string fields the sum of `ord($char)` is taken, for bit fields the sum of zeroes and ones).

  For example, the following computes the same number as the System V sum program:

          $checksum = do {	local $/;  # slurp!	unpack("%32W*",<>) % 65535;    };

  The following efficiently counts the number of set bits in a bit vector:

          $setbits = unpack("%32b*", $selectmask);

  The `p` and `P` formats should be used with care. Since Perl has no way of checking whether the value passed to `unpack()` corresponds to a valid memory location, passing a pointer value that's not known to be valid is likely to have disastrous consequences.

  If there are more pack codes or if the repeat count of a field or a group is larger than what the remainder of the input string allows, the result is not well defined: in some cases, the repeat count is decreased, or `unpack()` will produce null strings or zeroes, or terminate with an error. If the input string is longer than one described by the TEMPLATE, the rest is ignored.

  See "pack" for more examples and notes.

Recently read

Tools

- Preferences