Greetings, fellow citizens of planet earth.

While I do not work in a shipping department, I do spend a great deal of time packing and unpacking. I've got a project which deals with fixed-width data records in various sized based on the kind of record we're getting. On rare occasions (because we all know how rare it is that our customers provide us with erroneous data) I get something a bit ugly from the customer. This can cause me heartache, hassle, and various other forms of gump. The problem occurs if the records are shorter than I expect. Actually, it's more absurd than that. Let me simplify with an example:
example 1: $a = "test test test"; $fmt = "A4 x1 A4 x1 A4 x1 A4"; @a = unpack($fmt, $a); print join ("*",@a); ^D x outside of string at - line 3. example 2: $a = "test test test "; $fmt = "A4 x1 A4 x1 A4 x1 A4"; @a = unpack($fmt, $a); print join ("*",@a); ^D test*test*test*
For those unfamiliar, format strings ($fmt), allow me to cut and paste a drop of data from my infinitely handy Perl CD Bookshelf
A - An ASCII string, will be space padded
x - A null byte

Strangely, if the string being unpacked is too short, but I'm looking for an ASCII char beyond the bounds of the string, things work fine as frogs hair (even though there are other 'x' chunks in the format beyond). If I'm looking for a null byte (ie: skipping a character) at the dead position of the string, the code dies with an 'x outside of string'.

I'm wondering if other people have come across this as an issue, and how they've handled it. My thoughts are to check the length of each string vs. the expected length of the format string, but with a great deal of strings coming through, that would be tedious. Besides, rather than hard-coding the fmt string lengths, I'd rather have something more flexible to changes, which would leave me checking the length of each string vs. the calculated expected length of the format string. Just sounds ugly. Either way, I'm looking to avoid this 'x outside of string' hubbub.

Thoughts? Feelings? Rhyme? Reason?
-=rev=-

In reply to Unpack/format by Reverend Phil

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.