You might find something in
perlform. The following is close but puts the extra whitespace on the left instead of the right in the case of a length of one.
Update: In this code
format is not print the trailing
\s characters. Sample output updated to show the whitespace is missing.
#! /usr/bin/perl -w
use strict;
for my $val (qw/a ab abc abcd/) {
format =
@||||
$val
.
write \*STDOUT;
}
Output:
$ ./centre-via-format.pl
a
ab
abc
abcd
$ ./centre-via-format.pl | od -ch
0000000 a \n a b \n a b c \n a b c
2020 0a61 6120 0a62 6120 6362 610a 6362
0000020 d \n
0a64
0000022