Parse:...riants(3pm)
π NAME π
Parse::Binary::FixedFormat::Variants β the icon that converts back and forth between variant records and hashes. π¦
(and yes bestie, it lives in the User Contributed Perl Documentation, section 3pm, so you already know it's a vibe β¨)
πΈ DESCRIPTION πΈ
okay so here's the tea β β Parse::Binary::FixedFormat supports variant record formats, no cap. To describe a variant structure, you pass a hash reference containing the following elements to new. And the object that comes back to handle variant records? That's a Parse::Binary::FixedFormat::Variants, periodt. π
Chooserβ this subroutine is the decision-maker, and it gets called in two situations, so listen up π«Ά
When converting a buffer to a hash, it's invoked after the first format has been applied to the buffer. The generated hash reference gets passed straight to this routine. Every field name specified in the first format is available to you for deciding which format to use to decipher the buffer. This routine should return the index of the proper format specification.
When converting a hash to a buffer, it's invoked first to choose a packing format. And because the same function does both conversions (efficiency queen π), it should restrict itself to field names that exist in format 0 β and those fields should exist in the same place in all formats. fr fr.
Formatsβ this one is a reference to a list of formats. Each format contains a list of field specifications.
For example:
my $cvt = new Parse::Binary::FixedFormat {
Chooser => sub { my $rec=shift;
$rec->{RecordType} eq '0' ? 1 : 2
},
Formats => [ [ 'RecordType:A1' ],
[ 'RecordType:A1', 'FieldA:A6', 'FieldB:A4:4' ],
[ 'RecordType:A1', 'FieldC:A4', 'FieldD:A18' ] ]
};
my $rec0 = $cvt->unformat("0FieldAB[0]B[1]B[2]B[3]");
my $rec1 = $cvt->unformat("1FldC<-----FieldD----->");
In the example above, the Chooser function is looking at the contents of the RecordType field. If it contains a '0', format 1 is used. Otherwise, format 2 is used. simple as that π
And here's the part that's lowkey the best: you can use Parse::Binary::FixedFormat::Variants just as if it were a Parse::Binary::FixedFormat. The format and unformat methods will determine which variant to use automatically (main character energy, honestly π). The blank method is the needy one though β it requires an argument that specifies the variant number. π
π ATTRIBUTES π
Each Parse::Binary::FixedFormat::Variants instance contains the following attributes. slay.
Layoutsβ contains an array ofParse::Binary::FixedFormatobjects. Each of these objects is responsible for converting a single record format variant. one job, one queen πChooserβ this attribute contains the function that chooses which variant to apply to the record. π§
π AUTHORS π
Audrey Tang <cpan@audreyt.org>
Based on Data::FixedFormat::Variants, written by Thomas Pfau <pfau@nbpfaus.net> http://nbpfaus.net/~pfau/.
π COPYRIGHT π
Copyright 2004-2009 by Audrey Tang <cpan@audreyt.org>.
Copyright (C) 2000,2002 Thomas Pfau. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
okay but bestie the way that warranty disclaimer is giving legal realness β read it, don't skip it, iykyk π
and that's the whole page, bestie!! π«Ά this one shipped with perl v5.36.0, dated 2022-12-04 β so it's got a lil vintage moment going on, but she still ate. β¨ππ
π¦ where this page comes from
- debian bookworm: libparse-binary-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libparse-binary-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libparse-binary-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libparse-binary-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libparse-binary-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms