use strict; use warnings; for ( ["test", "l", 10, "*"], ["test", "r", 10, "*"], ["test", "l", 4, "*"], ["test", "r", 4, "*"], ["test", "l", 2, "*"], ["test", "r", 2, "*"], ["test", "l", 10], ["test", "r", 10], ["test", "l", 4], ["test", "r", 4], ["test", "l", 2], ["test", "r", 2], ) { my $padded = pad (@{$_}); print "=$padded=\n"; } sub pad { my $string = shift || die "no string"; my $direction = shift || die "no direction"; my $length = shift || die "no lenght"; my $char = shift || " "; my $pad_length = $length - length($string); return $string if $pad_length <= 0; my $pad_string = $char x $pad_length; if ( $direction =~ /^l/i ) { return $pad_string.$string; } elsif ( $direction =~ /^r/i ) { return $string.$pad_string; } else { die "unknown direction"; } }