thistle172 has asked for the wisdom of the Perl Monks concerning the following question:

I have to build a text file in a fixed-width format for feeding into an antiquated autodialer. Using the perl format function I get all the fields properly space-padded with one glaring exception: At the right-hand side I need to pad out the last variable with about fifty spaces before the carriage return, but no matter what I try I cannot get the formatter to pad it. It merely truncates the line at the end of the last character of the last variable without adding the padding specified by my use of @<<<<<<<<<<<< etc. There is no newline char in any of the variables (the line containing one having been chomped off when reading in the data to begin with), so that's not preventing the proper formatting. I can get the formatter to pad spaces up to a final single character of my choosing before the newline, and I can also get the formatter to pad any other characters up to the EOL, but numerous experiments have failed to give me nothing but spaces up to the newline. Please advise.

Replies are listed 'Best First'.
Re: Formats and spaces to the EOL
by jethro (Monsignor) on Aug 06, 2009 at 22:38 UTC
    You might pad spaces with that final character before the newline, but instead of sending that to the autodialer directly, you feed the write to a file, then read the file again and do two chops (for that final character and the newline).

    Instead of a file you could also use a pipe to yourself (see the manpage of open()).

Re: Formats and spaces to the EOL
by olus (Curate) on Aug 06, 2009 at 22:11 UTC
Re: Formats and spaces to the EOL
by Marshall (Canon) on Aug 07, 2009 at 14:15 UTC
    forget this format gizmo. Just use print or printf. You need the "x" operator in the print list, run the below and you will see how to extend this to arbitrary number of spaces.

    #!/usr/bin/perl -w use strict; print "some vars","-" x 60,"\n";
    Oh, instead of just 60, this could be a return value from a subroutine.
    Prints: some vars------------------------------------------------------------
      If you want padding,
      $line .= ' ' x (60-length($line));
      just before you write it out should do the trick.