Here is a small working example.
use strict;
use warnings;
my $INT_SIZE = 2;
my $filename = 'somefile.bin';
open my $fh, '<', $filename or die "Couldn't open file $filename: $!\n";
binmode $fh;
while ( read $fh, my $packed_length, $INT_SIZE ) {
my $text = '';
my $length = unpack 'v', $packed_length;
read $fh, $text, $length;
print $length, "\t", $text, "\n";
}
Change INT_SIZE, as well as the size and essence of the decompression pattern (either "v", or "n" or "V" or "N"). See the unpack manpage for more details .
source
share