直接贴代码,忘了时看看,尤其是红色的部分。
1 Readonly my %PAD_DEFAULTS => ( 2 cols => 78, 3 centered => 0, 4 filler => $SPACE, 5 # etc. 6 ); 7 8 sub padded { 9 my ($text, $arg_ref) = @_; 10 11 # Unpack optional arguments and set defaults... 12 my %arg = ref $arg_ref eq 'HASH' ? (%PAD_DEFAULTS, %{$arg_ref}) 13 : %PAD_DEFAULTS; 14 15 # Compute left and right spacings... 16 my $gap = $arg{cols} - length $text; 17 my $left = $arg{centered} ? int($gap/2) : 0; 18 my $right = $gap - $left; 19 20 # Prepend and append space... 21 return $arg{filler} x $left . $text . $arg{filler} x $right; 22 }
|
请发表评论