Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Definitive guide to pack/unpack?

by Anonymous Monk
on Apr 10, 2007 at 14:11 UTC ( [id://609150]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've never been too good with pack/unpack myself and every time I find the need to use them, I spend hours trudging through docs figuring it out. I accept this as being one of my own personal roadblocks, but I will soon find myself working with people who have no experience with them, and little experience with perl.

I'd rather it not be a blind-leading-the-blind sort of thing, and have been searching for a tutorial that covers (or links to coverage of) everything a junior programmer from A higher-level language wouldn't be familiar with, such as hex nybbles, big and little endians, bitwise operations, and so on.

Does anyone know of such an in-depth guide?

Replies are listed 'Best First'.
Re: Definitive guide to pack/unpack?
by chargrill (Parson) on Apr 10, 2007 at 14:13 UTC
Re: Definitive guide to pack/unpack?
by polettix (Vicar) on Apr 10, 2007 at 14:52 UTC
    As already said in the article already pointed by chargrill, the official documentation has a tutorial of its own: perlpacktut. It's quite comprehensive and easy to read - and to forget :)

    <blatant_promotion>Just in case... there is also an Italian translation.</blatant_promotion>

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Definitive guide to pack/unpack?
by Anonymous Monk on Apr 10, 2007 at 20:04 UTC
    i'm trying to use unpack to read what is essentially a memory dump of several c variables, and i think perhaps i've overestimated the power of unpack...how far off am I?
    # should unpack to: # an int32 (4 byte) value of -1 # a char (1 byte) value of 'A' # a null-terminated string value of "ANOTHER" # a null-terminated string value of "CSTRING" $packed_string = "\xFF\xFF\xFF\xFF" . "\x41" . "\x41\x4e\x4f\x54\x48\x45\x52\x00" . "\x43\x53\x54\x52\x49\x4e\x47\x00"; # should just be able to plug in some kind of # format for unpack, right? ($int,$char,$another,$cstring) = unpack('',$packed_string );
    would i instead have to put together subs like get_int(), get_char(), and get_cstring(), calling each of them in the order needed? if so, could I still use pack to make life easier?

      What problem are you encountering?

      #! perl -slw use strict; # should unpack to: # an int32 (4 byte) value of -1 # a char (1 byte) value of 'A' # a null-terminated string value of "ANOTHER" # a null-terminated string value of "CSTRING" my $packed_string = "\xFF\xFF\xFF\xFF" . "\x41" . "\x41\x4e\x4f\x54\x48\x45\x52\x00" . "\x43\x53\x54\x52\x49\x4e\x47\x00"; # should just be able to plug in some kind of # format for unpack, right? my ($int,$char,$another,$cstring) = unpack('l A1 Z* Z*',$packed_strin +g ); print "int: $int Char:$char String:$another String:$cstring"; __END__ c:\test>junk7 int: -1 Char:A String:ANOTHER String:CSTRING

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://609150]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found