in reply to A more elegant use of unpack
First, since you check the string with an initial match, there's no need to s/// it separately to trim the whitespace: just capture the part you're interested in and use it directly. Also, all those lazy quantifiers should be greedy: that which follows your plus quantifiers can never be matched by that which is quantified (ie \s can never match \S and vice versa), so greedy vs lazy does not change the match semantics. And greedy is both more efficient and makes for less clutter. I'd add an /x for good measure.
elsif ( /^ \s+ ( \S{13} \s+ \S+ \s+ \S.* )/x ) { my @fields = unpack "a21 a9 a9 a2 a13 a8 a9 a9 a4 a5 a6", $1; # ... }
What follows in your case has a lot of repetition: the print, the formatting whitespace, and the reference to @fields is duplicated over and over. You can do better than that:
elsif ( /^ \s+ ( \S{13} \s+ \S+ \s+ \S.* )/x ) { my @field = qw( PIIN FSCM N/A U/I UNIT PRICE AWD DT QTY OPT DT FOB REP TYPE ); my %value; @value{ @field } = unpack "a21 a9 a9 a2 a13 a8 a9 a9 a4 a5 a6", $1 +; printf " %-10s = %s\n", $_, $value{ $_ } for @field; print "\n"; }
Makeshifts last the longest.
|
|---|