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

rethinking printf

Thread Next
Rich Morin
March 6, 2002 09:45
rethinking printf
Message ID:
At Larry's (SVPUG) Perl6 talk last night, I was quite disturbed by his
assertion that printf format statements would have to be wrapped in
single quotes, because "%" would try to interpolate a hash value.  He
suggested, when asked, that folks could put in newlines as follows:

   'yada yada yada \qq{\n}'

This seems pretty ugly to me (Randal didn't like it much either :-)
and I think Larry will get some heat unless he finds a cleaner way
for printf fans to encode newlines.  Some possibilities include:

   *  Defining some '%' operators (e.g., %n and %t) for commonly-used
      control characters.

   *  Letting printf recognize escape sequences (e.g., '\n').

   *  Using a character other than '%' for format specifications.

Going a bit further afield, I also started thinking about the general
nature of printf.  I've been using this basic syntax since 1970 (in
the form of Fortran's FORMAT statements :-), so I'm pretty comfortable
with it.  OTOH, I don't like the fact that the format specifications
can become widely separated from the variables they reference.

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:

     'The value of $foo is %f7.3; ',  $foo,
     'the value of $bar is %f7.3.%n', $bar

     'The values of $foo and $bar are %f7.3 and %f7.3, ', $foo, $bar,
     'but the value of $baz is %f7.3%n', $baz

Each printx argument could be either a string or a variable.  If the
string contained one or more format specifications, succeeding arguments
would be formatted through them.  Note that I'm suggesting a new function
name because printf has a little-used capability that could conflict with
my proposed syntax:

   "The format string is reused as often as necessary
    to satisfy the arguments."

Anyway, that's my idea; what's yours?

email:; phone: +1 650-873-7841    - my home page, resume, etc.   - The FreeBSD Browser, Meta Project, etc. - Prime Time Freeware's DOSSIER series     - Prime Time Freeware's Darwin Collection

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About