[% setvar title Object Class hooks into C %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Object Class hooks into printf

VERSION

  Maintainer: Mark Biggar <mark.biggar@TrustedSysLabs.com>
  Date: 30 Aug 2000
  Mailing List: perl6-language-io@perl.org
  Number: 180
  Version: 1
  Status: Developing

ABSTRACT

There needs to be a way for an object class to define printf format specifiers for use in formatting objects into strings with printf and sprintf.

DESCRIPTION

It would be nice to be able to do things like:

   use BigInt ':constant';
    
   printf "%53d",(3**34);

and get a nicely formatted output.

This could also simplify other proposals like RFC 48 where instead of cluttering up the proposed date and gmtdate operators with POSIX strftime() functionality, it could be done using printf instead.

IMPLEMENTATION

I have thought of two possible implementations:

1) When ever a blessed object reference appears in a printf argument list the internal printf code invokes the objects STRING method (see RFC 49) with an extra argument consisting of the printf format specifier (in the above example the extra argument would be "%53d". The STRING method would return either a properly formatted string scalar which would be incorporated in the output string being constructed by printf or undef, if the STRING method does not understand the specifier, which should probably cause an exception to be thrown.

2) When ever a blessed object reference appears in s printf argument list the internal printf code attempts to invoke the objects PRINTF_spec method with an extra argument consisting of the inside of the printf specifier (in the above example anon-ref->PRINTF_d("53") would be invoked. If the method exists then it returns a properly formatted string scalar or undef if it can not interpreted the specifier argument. If either undef is returned or there is no such method then a exception is thrown.

REFERENCES

RFC 49 Objects should have builtin stringifying STRING method

RFC 48 Replace localtime() and gmtime() with date() and gmtdate()