Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Sort a list of valuesDeletes a value from a hash (Berikutnya)
Functions for list data

Convert binary structure into normal perl variables

Daftar Isi

  • unpack TEMPLATE,EXPR

  • unpack TEMPLATE

    unpack does the reverse of pack: it takes a stringand 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.See perlpacktut for an introduction to this function.

    The string is broken into chunks described by the TEMPLATE. Each chunkis converted separately to a value. Typically, either the string is a resultof pack, or the characters of the string represent a C structure of somekind.

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

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

    and then there's

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

    In addition to fields allowed in pack(), you may prefix a field witha %<number> to indicate thatyou want a <number>-bit checksum of the items instead of the itemsthemselves. Default is a 16-bit checksum. Checksum is calculated bysumming numeric values of expanded values (for string fields the sum oford($char) is taken; for bit fields the sum of zeroes and ones).

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

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

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

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

    The p and P formats should be used with care. Since Perlhas no way of checking whether the value passed to unpack()corresponds to a valid memory location, passing a pointer value that'snot 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 groupis larger than what the remainder of the input string allows, the resultis not well defined: the repeat count may be decreased, orunpack() may produce empty strings or zeros, or it may raise an exception.If the input string is longer than one described by the TEMPLATE,the remainder of that input string is ignored.

    See pack for more examples and notes.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Sort a list of valuesDeletes a value from a hash (Berikutnya)