sub blank_formatter { my ($printf_format, $length) = @_; if (!defined($length)) { $printf_format =~ /^%(\d+)/ or die "Can't deduce length from $printf_format"; $length = $1; } my ($blank) = ' ' x $length; sub { $_[0] ? sprintf($printf_format,$_[0]) : $blank; }; } sub my_printf { my ($format, @args) = @_; my (@printfargs) = (); while (@args) { my $arg = shift @args; if (ref($arg) and ref($arg) eq 'CODE') {push @printfargs, $arg->(shift @args);} else {push @printfargs, $arg;} } printf($format, @printfargs); } # similarly, if you need it, my_sprintf # Was: # printf( "A float: %12.4f$/", $root_beer ); my_printf( "A float: %s$/", blank_formatter('%12.4f'), $root_beer );