in reply to Dreaded Symbolic References
For what it's worth, here's what I usually do when confronted with stuff like this: (well, sort of - I actually have a few modules now that make this kind of thing easier)
Now, if you really, really wanted things as plain variables instead of hash entries, that's easily done in the Do stuff here section:my %recordformat = ( '__COMMON__' => [qw( RequestYear 1-2 RequestMonth 3-4 RequestDay 5-6 RequestSequence 7-10 RecordType 11-12 RecordSequence 13-14 )], 94 => [qw( Destination 15-22 DestinationType 23-26 )] # other stuff ); sub fillHashFromData { my ($destination, $recordSpec, $data) = @_; my @spec = @$recordSpec; # make a copy while (@spec) { my $key = shift @spec; my $location = shift @spec; $location =~ /(\d+)(?:-(\d))?/ or die "Syntax error in recordSpec: @$recordSpec"; my ($start, $end) = ($1, $2 || $1); $destination->{$key} = substr($data, $start - 1, $end - $start + 1 +); } } # Much later.... my $dataLine; while ($dataLine = <VENDORFILE>) { my %datavalues; fillHashFromData(\%datavalues, $recordformat{__COMMON__}, $dataLine) +; fillHashFromData(\%datavalues, $recordformat{$datavalues{'RecordType +'}}, $dataLine); # Do stuff here }
However, I'd stay away from that, since keeping things in a hash has the advantage that if you want it's very easy to enumerate the fields.{ no strict qw(vars refs); while(my ($k,$v) = each %datavalues) {$$k = $v;} }
One distinct advantage of using a %recordformat hash in the format above is that a section of it can be cut-and-pasted directly from the vendor's documentation; also, it becomes easy for you to remove a huge number of fields that you're ignoring from consideration without making certain that you have the right number of "x" specifications in the pack string. (Occasionally vendors will give field lengths; in my experience, they always give character position ranges)
The subroutine that lets you construct a data line from a hash and a format is also pretty easy:
sub fillDataFromHash { my ($src, $recordSpec, $data) = @_; $data ||= ' ' x $RECORD_LENGTH; # Maybe add to %recordformat my @spec = @$recordSpec; while (@spec) { my $key = shift @spec; my $location = shift @spec; $location =~ /(\d+)(?:-(\d))?/ or die "Syntax error in recordSpec: @$recordSpec"; my ($start, $end) = ($1, $2 || $1); substr($data, $start - 1, $end - $start + 1) = $src->{$key}; } } # ... # fill %datavalues somehow my $dataLine = fillDataFromHash(\%datavalues, $recordformat{__COMMON__}); $dataLine = fillDataFromHash(\%datavalues, $recordformat{$datavalues{'RecordType'}}, + $dataLine);
|
|---|