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:
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.

In reply to Format, empty vars, and fixed-width records by kwaping

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.