note
dws
<i>More experienced programmers are shaking their heads right now, they know it would be much more efficient to ...</i>
<p>
The truly experienced programmer doesn't conflate <i>efficiency</i> with <i>effectiveness,</i> but that's worth a separate discussion.
<p>
Using a regex to parse fixed-width fields does indeed sound like overkill, but using <code>substr</code> isn't necessarily the most effective alternative. Splitting a record with multiple fixed-width fields requires multiple calls to <code>substr,</code> along with cascading math to make sure that the <code>substr</code>'s start at the right places. Contrast that with a regexp-based solution, which requires a single invocation of a regexp, and no additional bookeeping. And then there's the oft-overlooked <code>unpack.</code>
<p>
The <i>psgrep</i> example in [isbn://1565922433|The Perl Cookbook] demonstrates a method for separating fixed-width fields by constructing a format string for <code>unpack.</code> It also demonstrates an elegant way of documenting how the format string is assembled.
<p>
80126
80126