"Michael Lazzaro" <[EMAIL PROTECTED]> wrote :
>
> 1) "Formats" as classes.  What I _want_ to do is to be able to
> associate a named "format" with a given class/instance/output, because
> I tend to use the same few formats over and over.  So if I want to
> frequently output numbers as '%-4.2d', I just call it "MoneyFormat" or
> something:
>
> [ ... snip ... ]
>
> and not deal with sprintf syntax at all, but still have something
> that's eminently adjustable, self-documenting, and has decent
> compile-time optimization possibilities.  And could change, runtime,
> according to user preferences.

An alternative, with almost the same benefits, is

  $fmt{money} = "%-4.2d";
  printf "The amount is: $fmt{money}\n", $v;

One of the things that I like about the printf style is that the values are
separated
from the format. So $v can be spelt @_.

> 2) An analogue to a "rule", but for output instead of input.  As we can
> say:
> [... cut ... ]
>
> print "blah blah: <$i:MoneyFormat>";

The inverse-regex was discussed on perl6-language a few months
back (I think the context was an alternative to pack/unpack): I
don't recall any definitive conclusions. A simple proposal to pass
a match object/hash as an input to the rule falls foul of too many
reverse ambiguities. But a constrained regex syntax could be made
to work.

For your specific example, the following is probably valid syntax:

print "blah blah $( Money.fmt $v )\n";

(assuming a class-method named fmt on the Money class). If Money
were a rule, insead of a class, then a similar thing might work.

If $v were of type Money, then Money's AS_STRING method could
be over-ridden for the interpolation. I guess that's how the fmt
property would be implemented: it creates a run-time subclass that
overrides the method.


Dave.


Reply via email to