SkipHuffman has asked for the wisdom of the Perl Monks concerning the following question:
I think that I am trying to do something in a relatively portable way.
I have a data set where I have a single file containing different types of records. Each record type has a strictly defined format of fixed length fields. The first four fields are common to all record types, withthe third field defining the type of record. (first is a date, second is a sequence within that date, fourth is a sequence within the record type. none of which is relevent, which is why this comment is parenthetical) I need to be able to both read and write these records.
I have this code
my ($RequestYear,$RequestMonth,$RequestDay,$RequestSequence,$Reco +rdType,$RecordSequence,$Record) =unpack("A2 A2 A2 A4 A2 + A2 A1486",$_) ;
To read the header fields. then I read the $RecordType and use a series of elsif to determine how to read the rest of the record, like this(short example, most have more fields):
elsif ($RecordType==94){ my ($Destination, $DestinationType) =unpack("A8 A40", $Record);
I would like to split the packing/unpacking function from the data definition so that I can put it all in one place, and reuse it. Something like this:
@HeaderRecordNames = qw/$RequestYear $RequestMonth $RequestDay $Requ +estSequence $RecordType $RecordSequence $Record/; @HeaderRecordLengths = qw/A2 A2 A2 A4 + A2 A2 A1486/; ... @RecordNames[94] = qw/$Destination $DestinationType/; @RecordLengths[94] = qw/$A8 A4/; ... my (@HeaderRecordNames) = unpack(@HeaderRecordLengths, $_); my (@RecordNames[$Recordtype])=unpack(@RecordLengths[$RecordType]);
This would eliminate my elsif array and also allow me to keep the data definitions in one place so that I can use them for packing or other purposes. Or change them as needed.
Of course this does not work because I am too novicelike to manage the dreaded symbolic references.
Thoughts?
Skip Huffman
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dreaded Symbolic References
by Limbic~Region (Chancellor) on Mar 08, 2004 at 16:35 UTC | |
by SkipHuffman (Monk) on Mar 08, 2004 at 17:21 UTC | |
by revdiablo (Prior) on Mar 08, 2004 at 19:38 UTC | |
|
Re: Dreaded Symbolic References
by kvale (Monsignor) on Mar 08, 2004 at 16:31 UTC | |
by SkipHuffman (Monk) on Mar 08, 2004 at 16:44 UTC | |
|
Re: Dreaded Symbolic References
by dada (Chaplain) on Mar 08, 2004 at 17:24 UTC | |
|
Re: Dreaded Symbolic References
by matija (Priest) on Mar 08, 2004 at 16:40 UTC | |
|
Re: Dreaded Symbolic References
by chromatic (Archbishop) on Mar 08, 2004 at 16:46 UTC | |
|
Re: Dreaded Symbolic References
by waswas-fng (Curate) on Mar 08, 2004 at 16:32 UTC | |
|
Re: Dreaded Symbolic References
by fizbin (Chaplain) on Mar 08, 2004 at 21:58 UTC | |
|
Re: Dreaded Symbolic References
by UnderMine (Friar) on Mar 08, 2004 at 17:27 UTC |