Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Clarity in parsing fixed length data.

by Ieronim (Friar)
on Aug 11, 2006 at 08:59 UTC ( [id://566772]=note: print w/replies, xml ) Need Help??


in reply to Clarity in parsing fixed length data.

As i can see, your problem can be limited to finding a better way to create and document 'unpack' templates.

I myself would do it in an OO way (let's call the class for format templates 'Unpack::Format'):

my $format = Unpack::Format->new( spec =>[ status => 'n', # 0 time => 'n', # 2 date => 'N', # 4 code => 'a16', # 8 key msid => 'a10', # 24 key ] ); #overloaded @test_vals{ @$format } = unpack "$format", $rec; # pure OO style @test-vals{$format->fieldnames) = unpack $format->templatestr, $rec;
It's easy to implement, and i think it's a much more clear way to create templates than using constants for field names and unpack string.

UPD: according to Hofmator's note, name-format pairs must be passed as an array reference. It seems to be a very easy typo to make :)

UPDATE 2: Working implementation (tested)

#!/usr/bin/perl package Unpack::Format; use warnings; use strict; use Carp; use overload '""' => \&templatestr, '@{}' => \&fieldnames; sub new { my $class = shift; my $self = {}; bless $self, $class; @_ % 2 and croak "Odd number of parameters passed to ".__PACKAGE__ +."::new!"; my %params = @_; $self->_load_fields($params{spec}); return $self; } sub _load_fields { my $self = shift; my $spec = shift; @$spec % 2 and croak "Incorrect format specification: odd number o +f elements"; $self->{fieldnames} = [@{$spec}[grep { !($_ %2) } 0..$#{$spec}]]; $self->{template_ary} = [@{$spec}[grep { $_ %2 } 0..$#{$spec}]]; } sub fieldnames { $_[0]->{fieldnames} } sub templatestr { my $self = shift; return join " ", @{$self->{template_ary}}; } 1; package main; use warnings; use strict; my $format = Unpack::Format->new( spec => [ status => 'n ', # 0 time => 'n ', # 2 date => 'N ', # 4 code => 'a16', # 8 key msid => 'a10', # 24 key ] ); my $rec = "\x{00}\x{7B}\x{06}\x{B3}\x{01}\x{32}\x{1A}\x{83}" .'code_value------msid_value'; my %test_vals; print "$format\n"; @test_vals{ @$format } = unpack $format, $rec; foreach (@$format) { printf " %6s => %s\n",$_, $test_vals{$_}; }

     s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print

Replies are listed 'Best First'.
Re^2: Clarity in parsing fixed length data.
by Hofmator (Curate) on Aug 11, 2006 at 11:17 UTC
    In the way it is written, your solution suffers from the same problems as BrowserUK's solution: The order of the fields gets lost by using a hashref as the spec-argument to Unpack::Format::new.

    -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://566772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found