NAME

Parse::Binary::Iterative - Parse binary structures

SYNOPSIS

A simple structure:

package Simple;
use base 'Parse::Binary::Iterative';
__PACKAGE__->FORMAT([
    One => "A",
    Two => "V",
    Three => "d",
]);

A structure relying on another structure:

package IconFile;
use base 'Parse::Binary::Iterative';
__PACKAGE__->FORMAT([
Magic       => 'a2',
Type        => 'v',
Count       => 'v',
Icon        => [], # This links to another structure
Data        => 'a*',
]);

package Icon;
use base 'Parse::Binary::Iterative';
__PACKAGE__->FORMAT([
Width       => 'C',
Height      => 'C',
ColorCount  => 'C',
Reserved    => 'C',
Planes      => 'v',
BitCount    => 'v',
ImageSize   => 'V',
ImageOffset => 'v',
]);

Reading data:

open IN, "something.ico";
my $iconfile = IconFile->new(\*IN);
my $icons = $iconfile->Count;
my $width = $iconfile->Icon->Width;

DESCRIPTION

This module is more or less a reproduction of Parse::Binary with slightly less functionality, more documentation and some tests, and with the ability to read data sequentially from a filehandle instead of having to slurp it all in at once.

USAGE

You use this module by subclassing it and creating classes which represent the structures you're trying to unpack. As shown in the examples above, you need to set the FORMAT class data accessor to an array reference; this should contain pairs of accessor names and formats suitable for feeding to unpack.

Alternatively, if you want to "contract out" unpacking to a sub-structure, use an array reference instead of a format, and the name of the accessor will be taken as a class name to use instead. The array reference currently should be blank, but later versions of this module will use the elements of array reference to specify more complex unpacking instructions.

When a substructure is used, the accessor returns the object processed by the substructure unpacker as you would expect; this object also has a parent accessor, which refers back to the higher-level structure. So, for instance:

package IconFile;
sub Data {
    my ($self) = @_;
    substr($self->parent->Data, $self->ImageOffset, $self->ImageSize);
}

This allows you to "reach back" into the original structure and manipulate its data.

Forthcoming versions will have the ability to unpack multiple substructures, as Parse::Binary currently does. But I don't think I need that right now.

SEE ALSO

Parse::Binary

AUTHOR

Simon Cozens

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Simon Cozens

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.