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
In reply to Dreaded Symbolic References by SkipHuffman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |