[Israel.pm] pack vs. different architectures

Gaal Yahas gaal at forum2.org
Tue Feb 22 03:13:31 PST 2005


On Tue, Feb 22, 2005 at 12:01:36PM +0200, Gaal Yahas wrote:
 
> Use the slice, Luke:
> 
> my $str = pack( "a8  f    f    f   N     N",
>     @$hdr{ qw/   id  lat  lon  t   nlev  flag/ };

Or, if you do this kind of thing a lot, this is a nice place to use
order-preserving hashes.

     our $header_template = Tie::IxHash->new(
         id   => 'a8',
         lat  => 'f',
         lon  => 'f',
         t    => 'f',
         nlev => 'N',
         flag => 'N',
     );
     
     sub struct_packer {
         my($template, $data) = @_;
     
         # could be made better, no doubt.
         die "template and data mismatch" if $template->Length != $data;
     
         pack join("", $t->Values), $data->{ $t->Keys };
     }
     
     my $str = struct_packer($header_template, $hdr);

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list