1 package Parse::Binary::Nested;
12 my ($class, $format) = @_;
13 ref $format eq 'ARRAY'
14 or croak "Invalid Parse::Binary::Nested format: should be an array ref";
15 bless $format, $class;
19 my ($self, $format) = @_;
20 # total (flattened) unpack template from nested format definitions
22 my $value = $format->[-($_ << 1) - 1];
23 if (ref $value eq 'ARRAY') {
24 my $count = $value->[0];
25 $value = $self->template($value);
26 $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
27 : $count."X[$count]$count/($value)";
30 $value =~ s/=(?:\d+|.)//g; # hardcoded values
31 $value =~ s{^C/(a)(\d+)}{$1 . ($2 + 1)}e; # maximum length
34 } reverse 0 .. ($#$format - 1) >> 1;
38 my ($self, $format, $data, $pos) = @_;
39 # map flat results into a named and nested hash
42 while (my ($field, $template) = splice @$format, 0, 2) {
43 if (ref $template eq 'ARRAY') {
44 my ($count, @subformat) = @$template;
45 $$pos++ if $count eq 'C';
46 my $max = $count =~ s/^(\d+)// ? $1 : 0;
47 $count = !$count ? $max
48 : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
49 $res{$field}->[$_] = $self->convert([@subformat], $data, $pos)
50 for 0 .. ($max || $count)-1;
51 splice @{ $res{$field} }, $count if $max > $count;
52 $res{$field} = $res{$field}->[0] if $max == 1;
56 for (split m{(?![0-9*/])(?<![/=])}, $template) {
57 my ($type, $count) = m{^(\D+)(\d+)?$} or die 'unsupported';
58 my $mult = $count // 1;
70 when (['s', 'S', 'n', 'v']) {
73 when (['a', 'A', 'Z', 'a*']) {
74 $$pos += length $data->[0];
77 $$pos += $count // 1 + length $data->[0];
79 when (['C/a', 'C/A']) {
80 $$pos += 1 + ($count // length $data->[0]);
81 $data->[0] = unpack 'C/a', $data->[0] if defined $count;
84 unshift @$data, $count;
87 unshift @$data, $$pos;
94 carp "Unrecognised template element '$type'";
97 if (defined $res{$field}) {
98 $res{$field} = [ $res{$field} ] unless ref $res{$field} eq 'ARRAY';
99 push @{ $res{$field} }, shift @$data;
102 $res{$field} = shift @$data;
111 my ($self, $input) = @_;
112 my @data = unpack $self->template($self), $input;
113 return $self->convert([@$self], \@data);
120 Parse::Binary::Nested - Structured unpack
124 use Parse::Binary::Nested;
125 my $parser = Parser::Binary::Nested->new([
134 my $data = $parser->unpackf("\1foo\0.rest");
135 print $data->{foos}->[0]->{message};
139 Converts a string into a hash of values, just like C<unpack>
140 except that it allows you to name and nest the resulting elements.
142 Format declarations are simalar to C<pack> templates,
143 with the following additions:
149 An array ref groups additional declarations,
150 with the first value specifying a repetition. If this count is variable,
151 the resulting value will be an array ref of hashes.
153 repeat => ['C', name => 'a*', value => 'S']
155 With a count of 1, it will return only a hash ref,
156 thereby simply grouping declarations:
159 test => [1, foo => 'C']
163 A template value of C<Ca$length> is recognised as a length-preceded string
164 with a constant (maximal) size, and will return only the string adjusted
166 This behaviour is very similar to C<(C/a@x$length)>, except that it never reads
167 more than the given number of bytes.
171 Hardcoded values can be inserted using C<=$number> values.
172 This can for example be useful to retain forwards-compatibility:
175 type => '=1', # nothing read
188 Mischa POSLAWSKY <perl@shiar.org>