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

Howdy, I'm a newbie so apologies if this is a no-brainer.

I'm trying to use the write command to print a format to a file. Trouble is the trailing spaces are being truncated no matter what I do.

Example:

$test="Hi."; $~="TEST"; write; format TEST= @<<<<<<<<<<<<<<<<<<< $test .
The output would be simply "Hi." ending at the dot, but I need it to be padded out to the full twenty spaces. How do you do this?? I figured this would be easy, but I can't find the answer.

Any help would be amazingly appreciated.
Thanks!

Replies are listed 'Best First'.
Re: Preserve trailing spaces with write format?
by tachyon (Chancellor) on Apr 17, 2002 at 18:40 UTC
    s/@<<<<<<<<<<<<<<<<<<</@>>>>>>>>>>>>>>>>>>>>/; # < left justify > :-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Preserve trailing spaces with write format?
by stephen (Priest) on Apr 17, 2002 at 18:42 UTC
    Instead of using formats, it may be easier to use sprintf.
    # Print "Hi." left-justified padded out to 20 spaces. print sprintf('%-20s', "Hi.");

    stephen

      Or perhaps simply:

      printf '%-20s', "Hi.";

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print