Both of your alternatives (one-line-ish, and using a sub) are very hard to read, and require lots of duplicate code for each message type. Hide the gory details, leaving a clean interface (such as those below). This process is called encapsulation.
A procedural interface:
my $TEST_REC_FORMAT = get_msg_format [ status => 'n ', # 0 time => 'n ', # 2 date => 'N ', # 4 code => 'a16', # 8 key msid => 'a10', # 24 key ]; my %test_vals = parse_msg($TEST_REC_FORMAT, $rec);
An OO interface:
my $TEST_REC_FORMAT = MessageFormat->new([ status => 'n ', # 0 time => 'n ', # 2 date => 'N ', # 4 code => 'a16', # 8 key msid => 'a10', # 24 key ]); my %test_vals = $TEST_REC_FORMAT->parse($rec);
What exactly get_msg_format or new returns is not important. How exactly it is calculate is not important either.
In reply to Re: Clarity in parsing fixed length data.
by ikegami
in thread Clarity in parsing fixed length data.
by rodion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |