I need to output data to a fixed-width record file. The lines need to be exactly 129 characters long. I thought
format would be great for this, and it has been - except for one issue... The line only prints up to the last variable that contains a non-pad character. My current code runs out of data at position 92, after which everything is undef. That is also where the output line ends. Unfortunately, I need it to pad out to the 129 character requirement.
Can anyone help? Here's what I've tried already:
- Making the final variable an empty string
- Making the final variable a single space
- Removing everything in the format picture after the last data-containing variable and hard-coding a run of spaces
None of those ideas worked - the line was still truncated at 92 characters in each instance.
I also tried using
sprintf, but if the data is longer than intended, the line becomes too long. I'd rather not
substr and
sprintf every piece of data, but I'll do it if that's what's required.
Example code (disregard unintialized and "not a number" warnings):
#!/usr/bin/perl
use strict;
use warnings;
my (
$emp_number,
$name,
$override_dept,
$job,
$shift,
$d_e,
$d_e_code,
$override_rate,
$hours,
$year,
$month,
$day,
$filler1,
$filler2,
$amount,
$seq_num,
$override_div,
$override_branch,
$override_state,
$override_local,
$state_misc,
$rate,
$ssn,
) = 'A' .. 'P';
format XXX =
@>>>>>@>>>>>>>>>>>>>>>>>>>>>>>>@>>>>>@>>>>>>>>>>>@@@>@#####.##@####.##
+@>>>@>@>@>@>@#####.##@@>>>>>@>>>>>@>@>>>>>>>>>@@@>>>>>>>>>>
$emp_number,$name,$override_dept,$job,$shift,$d_e,$d_e_code,$override_
+rate,$hours,$year,$month,$day,$filler1,$filler2,$amount,$seq_num,$ove
+rride_div,$override_branch,$override_state,$override_local,$state_mis
+c,$rate,$ssn
.
my $record = '';
open my ($out), '>', \$record or die $!;
my $oldfh = select($out);
$~ = 'XXX';
write;
select($oldfh);
close($out) or die $!;
print $record;
Update: I've decided to go with the
substr(sprintf()) method after all. Not the prettiest or most elegant, but it works.
---
It's all fine and dandy until someone has to look at the code.