in reply to General Purpose String Padding
Your function won't ever work if the pad character is a zero. Replace
$pad_char ||= ' ';
with
$pad_char = ' ' if not defined $pad_char;
to fix the problem.
Also, you could make your code a bit cleaner by replacing the last three lines with the following two:
my $pad = $pad_char x ($max_length - length $s); return $dir eq 'R' ? "$s$pad" : "$pad$s";
Finally, you documented one of your assumptions ($s will never be longer than $max_length), but you didn't document your assumption that $pad_char will never be longer than one character.
|
|---|