develooper Front page | perl.fwp | Postings from March 2002

Re: rethinking printf

Thread Previous | Thread Next
From:
Paul Makepeace
Date:
March 6, 2002 11:13
Subject:
Re: rethinking printf
Message ID:
20020306191354.GA783@tantrix.realprogrammers.com
On Wed, Mar 06, 2002 at 09:45:18AM -0800, Rich Morin wrote:
> With all of Larry's talk about making "x" mode the standard in REs and
> having more "pair-based" syntax here and there, I started thinking
> about a replacement for printf, as:
> 
>   printx(
>     'The value of $foo is %f7.3; ',  $foo,
>     'the value of $bar is %f7.3.%n', $bar
>   );
> 
>   printx(
>     'The values of $foo and $bar are %f7.3 and %f7.3, ', $foo, $bar,
>     'but the value of $baz is %f7.3%n', $baz
>   );

I like the idea, but wonder how to avoid problems that could arise with
things like,

$raise = "15%";
printx(
	'My raise was %s this year!', $raise
);

FWIW, C# has a format specification that looks like,
string somenums = String.Format("Some formatted numbers: dec={0,15:E} doub={1,20}", dec, doub);

...encoding the width, justification, type and format of the numbers.

http://msdn.microsoft.com/net/ecma/docs.zip -- look for the String class
and IFormattable interface. They seem pretty comprehensive, certainly a
superset of printf(2) and include some neat support for printing
currencies etc. Perhaps another discussion but certainly there's a lot
Perl could learn from C#...

Paul


-- 
Paul Makepeace ....................................... http://paulm.com/

"What is the reason where? Another chance to get an ice cream."
   -- http://paulm.com/toys/surrealism/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About