I am currently thinking of porting some of my Delphi / Turbo Pascal utilities
over to Perl. But I have met with a design problem which I want to post
for discussion here :
Pascal has the notion of records, that is, a user defined compound type, more or less
like the struc{} thing in C AFAIK. My utilities will parse
many of such records to extract information from the files. My current
idea to handle this
stuff is to map each record to a hash which contains
name->value pairs from the file, this can be done in a number of beautiful ways,
currently I favor a generic "ReadRecord" routine that gets passed a string
suitable for a call to unpack() and a list with the names for each unpacked value.
Another, maybe less error prone approach would be to pass in a list of type->name pairs,
which would make adding and moving record members easier.
Now where is the problem ? The problem comes with finding the binary size of such a record. Before I can parse a record
into a hash, I have to read enough bytes from the disk. Turbo Pascal has the SizeOf() pseudo-function,
but Perl dosen't seem to have this. I _could_ hack it up and write my own unpack()-string parser
that looks at such a string and tells me how many bytes it most likely
would want to unpack correctly, but I would like to know if there is a better way before
hacking up such an ugly kludge ...
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.