Subject: | repeated formatting code |
A number of times, code occurs that follows $string with (' ' x $length - length($string)
This sort of thing is one of the most basic reasons for printf and sprintf. I have attached a patch which abstracts that padding into a subroutine.
diff -u -r Getopt-Simple-old/lib/Getopt/Simple.pm Getopt-Simple-new/lib/Getopt/Simple.pm
--- Getopt-Simple-old/lib/Getopt/Simple.pm 2004-07-21 14:30:22.000000000 -0400
+++ Getopt-Simple-new/lib/Getopt/Simple.pm 2004-11-05 15:50:33.000000000 -0500
@@ -54,22 +54,23 @@
# --------------------------------------------------------------------------
+sub pad { sprintf "%-${fieldWidth}s", @_; }
+
sub dumpOptions
{
my($self) = @_;
- print 'Option', ' ' x ($fieldWidth - length('Option') ), "Value\n";
+ print pad('Option'), 'Value';
for (sort byOrder keys(%{$$self{'switch'} }) )
{
if (ref($$self{'switch'}{$_}) eq 'ARRAY')
{
- print "-$_", ' ' x ($fieldWidth - (1 + length) );
- print '(', join(', ', @{$$self{'switch'}{$_} }), ")\n";
+ print pad("-$_"), '(' . join(', ', @{$$self{'switch'}{$_} }) . ")\n";
}
else
{
- print "-$_", ' ' x ($fieldWidth - (1 + length) ), "$$self{'switch'}{$_}\n";
+ print pad("-$_"), "$$self{'switch'}{$_}\n";
}
}
@@ -131,15 +132,11 @@
print "$$self{'helpText'}\n" if ($$self{'helpText'});
- print 'Option', ' ' x ($fieldWidth - length('Option') ),
- 'Environment var', ' ' x ($fieldWidth - length('Environment var') ),
- "Default\n";
+ print pad('Option'), pad('Environment var'), "Default\n";
for (sort byOrder keys(%{$$self{'default'} }) )
{
- print "-$_", ' ' x ($fieldWidth - (1 + length) ),
- "$$self{'default'}{$_}{'env'}",
- ' ' x ($fieldWidth - length($$self{'default'}{$_}{'env'}) );
+ print pad("-$_"), pad("$$self{'default'}{$_}{'env'}");
if (ref($$self{'default'}{$_}{'default'}) eq 'ARRAY')
{