develooper Front page | perl.perl6.language | Postings from September 2000

Re: pack/unpack is damn unperlish. Explain them as Perl.

Thread Previous | Thread Next
From:
Casey R. Tweten
Date:
September 18, 2000 09:40
Subject:
Re: pack/unpack is damn unperlish. Explain them as Perl.
Message ID:
Pine.OSF.4.21.0009181224180.13089-100000@home.kiski.net
Today around 12:32pm, Sam Tregar hammered out this masterpiece:

: On Mon, 18 Sep 2000, Michael G Schwern wrote:
: 
: > Perhaps someone could attempt to write an explaination of pack and
: > unpack in completely Perl terms.  No bits, no ints, no nybbles, no
: > IEEE floating point arithmetic, no prior knowledge of C necessary.
: > Those are not Perl.  Scalars, arrays, hashes, functions, methods,
: > loops, contexts.  These are Perl.
: 
: "Describe to me how you use a supermarket shopping-cart in terms of a
: hardware store.  Don't mention any words for food.  Just talk about nuts
: and bolts."
: 
: You've pretty much nailed the primary use of pack/unpack - bridging the
: gap between scalars and the various binary formats of Le Monde Cyber.
: 
: I used pack recently in order to get Perl talking with Glimpse, a search
: engine written in C.  For some bizare reason the Glimpse server won't talk
: to you unless you send your PID to it.  As if that weren't strange enough,
: it needs your PID split into 4 bytes in reverse order.  If you can think
: of a better solution to this problem than pack then by all means propose
: it.  I think pack is pretty good at what it does!
: 
: Maybe you don't get to know how good it is until you need it?

I think pack/unpack are perlish enough.  Especially if we believe that
printf/sprintf are perlish.

An example:

  Take a MySQL time stamp ( $ts = '20000918123847' ) and 'unpack' it into an
  array and print in in a pretty manner.

  printf(
         "%4d/%02d/%02d %02d:%02d%02d\n",
         unpack( 'A4A2A2A2A2A2', '20000918123847' )
        );

If printf is perlish, so is unpack, IMHO ;-)


-- 
print(join(' ', qw(Casey R. Tweten)));my $sig={mail=>'crt@kiski.net',site=>
'http://home.kiski.net/~crt'};print "\n",'.'x(length($sig->{site})+6),"\n";
print map{$_.': '.$sig->{$_}."\n"}sort{$sig->{$a}cmp$sig->{$b}}keys%{$sig};
my $VERSION = '0.01'; #'patched' by Jerrad Pierce <belg4mit at MIT dot EDU>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About