It seems that sprintf is will still be around in Perl 6 [1],
and that sprintf formats will be available using the .as() method.
While looking at some Python docs [2] I noticed two things that might
be worth stealing; a sprintf operator (%) and named parameters in
the format string:
a = '%(lang)s has %(c)03d quote types.' % {'c':2, 'lang':'Python}
(This example made me laugh)
Since perl6 has named parameters, pairs and slurpy hashes, I think
sprintf should be taught to use them. Syntax is an exercise for the
reader. (Unifying the '%2$d' with the named syntax would be nice..)
The operator need is less clear cut. Overloading '%' certainly wouldn't
make sense in Perl 6 and the presence of .as() may cover the common cases.
Some alternatives are idiomatic combinations of interpolation and .as(),
Perl6::Form forms, and custom quote operators/adverbs.
So, any takers?
Brad
[1] http://dev.perl.org/perl6/doc/design/exe/E07.html
[2] http://www.python.org/doc/QuickRef.html#BasicTypes
Search for: "Format operator"
--
Bravery and cowardice are not things which can be conjectured in times
of peace. They are in different categories. -- Hagakure
Well, we kinda do have an operator already, if you consider infix:<as> to
be a variant of .as().
$c as '%03d'
$c.as('%03d')
but it's arguable which is better in a string:
$a = "{$c as '%03d'}"
$a = "$c.as('%03d')"
But that's backwards from Python's % operator. Actually, I think I like
the Perl way better for single patterns... :)
Larry
What made me laugh is that Pugs knows the exact value of infinity:
pugs> my $a = {"$^lang has $^c.as('%03d') quote types."}(:c(Inf),:lang<Perl>)
"Perl has 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 quote types."
: Since perl6 has named parameters, pairs and slurpy hashes, I think
: sprintf should be taught to use them. Syntax is an exercise for the
: reader. (Unifying the '%2$d' with the named syntax would be nice..)
Yes, I18n and L10n requires some kind of templating engine. Whether sprintf
is that engine is another question.
: The operator need is less clear cut. Overloading '%' certainly wouldn't
: make sense in Perl 6 and the presence of .as() may cover the common cases.
It's also possible that some ~~ variant also binds a capture to
an interpolation. That would have the advantage of working in
either order.
: Some alternatives are idiomatic combinations of interpolation and .as(),
: Perl6::Form forms, and custom quote operators/adverbs.
:
: So, any takers?
We don't need any takers, we need any givers. :)
Larry
> The operator need is less clear cut. Overloading '%' certainly wouldn't
> make sense in Perl 6 and the presence of .as() may cover the common cases.
I'm not convinced that sprintf needs an operator. It's not commonly used in
any code I've looked at, which to me suggests that it's not good huffman
coding to use up a terse symbol for it, denying that symbol to something
else.
Nicholas Clark
Larry, you almost wrecked my MacBook Pro with the coffee I was
sipping :)
I think I found the reason why Pugs knows the exact value of
infinity. That's GHC that tells pugs the exact value thereof.
This is Infinity according to Pugs.
./src/Pugs/AST/Internals.hs
> doCast (VStr "Inf") = return $ 1/0
And this is what GHC thinks what Infinity is.
> Prelude> 2^1023 == 1/0
> False
> Prelude> 2^1024 == 1/0
> True
Dan the Pugs Walker