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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.