Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sprintf and snake envy

3 views
Skip to first unread message

Brad Bowman

unread,
Jul 4, 2006, 3:06:54 AM7/4/06
to perl6language,
Hello,

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

Larry Wall

unread,
Jul 4, 2006, 12:37:20 PM7/4/06
to perl6language,, Brad Bowman
On Tue, Jul 04, 2006 at 05:27:33PM +0100, Nicholas Clark wrote:
: 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.

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

Larry Wall

unread,
Jul 4, 2006, 12:25:34 PM7/4/06
to perl6language,
On Tue, Jul 04, 2006 at 05:06:54PM +1000, Brad Bowman wrote:
: Hello,

:
: 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)

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

Nicholas Clark

unread,
Jul 4, 2006, 12:27:33 PM7/4/06
to Brad Bowman, perl6language,
On Tue, Jul 04, 2006 at 05:06:54PM +1000, Brad Bowman wrote:
> Hello,
>
> 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}

> 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

Dan Kogai

unread,
Jul 5, 2006, 1:14:09 AM7/5/06
to Larry Wall, perl6language,
On Jul 05, 2006, at 01:25 , Larry Wall wrote:
> 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
> 1797693134862315907729305190789024733617976978942306572734300811577326
> 7580550096313270847732240753602112011387987139335765878976881441662249
> 2847430639474124377767893424865485276302219601246094119453082952085005
> 7688381506823424628814739131105408272371633505106845862982399472459384
> 79716304835356329624224137216 quote types."

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

0 new messages