Parse::Binary(3pm)

πŸ“¦
version 0.11 Β· 2 versions
  • 0.11 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.11 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Parse::Binary ✨

okay bestie, gather round, because this is Parse::Binary, a Perl module (yes, the (3pm) kind, we're in manpage territory πŸ’…) and it's genuinely one of those "wait this is so smart" situations. It unpacks binary data structures into object hierarchies. iconic behavior tbh.


🌸 VERSION 🌸

This doc describes version 0.11 of Parse::Binary, released January 25, 2009. vintage! retro! a little elder-millennial but we respect it 🫢


πŸŽ€ SYNOPSIS πŸŽ€

okay so first up, this class represents a Win32 .ico file:

package IconFile;
use base 'Parse::Binary';
use constant FORMAT => (
    Magic           => 'a2',
    Type            => 'v',
    Count           => 'v',
    'Icon'          => [ 'a16', '{$Count}', 1 ],
    Data            => 'a*',
);

and this one is a single icon resource, like the little guy inside:

package Icon;
use base 'Parse::Binary';
use constant FORMAT => (
    Width           => 'C',
    Height          => 'C',
    ColorCount      => 'C',
    Reserved        => 'C',
    Planes          => 'v',
    BitCount        => 'v',
    ImageSize       => 'V',
    ImageOffset     => 'v',
);
sub Data {
    my ($self) = @_;
    return $self->parent->substr($self->ImageOffset, $self->ImageSize);
}

and here's a simple little .ico dumper that uses both of them, because of course:

use IconFile;
my $icon_file = IconFile->new('input.ico');
foreach my $icon ($icon_file->members) {
    print "Dimension: ", $icon->Width, "x", $icon->Height, $/;
    print "Colors: ", 2 ** $icon->BitCount, $/;
    print "Image Size: ", $icon->ImageSize, " bytes", $/;
    print "Actual Size: ", length($icon->Data), " bytes", $/, $/;
}
$icon_file->write('output.ico'); # save as another .ico file

like. it reads the file, prints the stats, and writes it back out. main character energy fr fr πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

So the whole point of this module is that parsing binary data structures gets so much easier. It works by being a base class for classes that represent the binary data β€” and those classes can contain objects of other classes to represent parts of themselves. it's giving nesting dolls but make it bytes πŸ’–

ngl, the documentation is unfortunately a bit lacking at this moment 😭 that's literally what the original says, not me being dramatic.

If you want actual examples of how to use this thing, go read the tests and the source code of Parse::AFP and Win32::Exe. that's where the real ones learn. iykyk ✨


πŸ‘‘ AUTHORS πŸ‘‘

Audrey Tang <cpan@audreyt.org> β€” legend behavior, no cap 🫢


Copyright 2004-2009 by Audrey Tang <cpan@audreyt.org>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See <http://www.perl.com/perl/misc/Artistic.html>


🌈 one last thing bestie 🌈

This page ships with perl v5.36.0, dated 2022-12-04. yeah the module is from 2009 and the manpage footer is from 2022, we love a timeless queen πŸ’… go parse something. periodt. πŸŽ€πŸ¦‹βœ¨

πŸ“¦ where this page comes from