develooper Front page | perl.perl5.porters | Postings from October 2011

Re: [perl #100190] RFE: fix sprintf to be consistent with printf andbe useful!

Thread Previous | Thread Next
From:
Eric Brine
Date:
October 1, 2011 00:49
Subject:
Re: [perl #100190] RFE: fix sprintf to be consistent with printf andbe useful!
Message ID:
CALJW-qFoBd9xQqRwYskzi9fxnvF=z=q7rL29QmugXPtWFDTT4Q@mail.gmail.com
On Sat, Oct 1, 2011 at 1:32 AM, Linda W <perl-diddler@tlinx.org> wrote:

> Tom Christiansen wrote:
> >> I'm glad you agree, it's a bug in the design of the language.
> >>
> > Now you're just being unnecessarily silly.  Just because you
> > don't like something doesn't mean it is a bug.
> ----
>    Well, officially, I did file it as an RFE (wishlist)...  if you really
> wanna get serious about it and all.
>
> But along the lines of  silly:
>
> Eric Brine via RT wrote:
> > On Wed, Sep 28, 2011 at 8:51 AM, Johan Vromans <jvromans@squirrel.nl>
> wrote
> >> Yes, we all know that. It's just stupid you can't write:
> >>     sub my_printf { print ">> " . sprintf(@_) }
> >> but need
> >>     sub my_printf { print ">> " . sprintf( $_[0], @_[1..$#_] ) }
> >> (OTOH, this *does* look like Real Perl [tm] ...
> > Except the same thing applies to every single builtin (e.g. length,
> splice,
> > substr, exit, etc.
> ------
>    Hmm...
>    Let's examine this strawman argument are any of these command that
> would take a list of scalars
> that are not written to?
>
>    length -- <$> - takes 1 expression
>

Are you saying array cannot hold one element?


>    substr -- <$,$>[,$[,$] --- maybe, but it's not a variable length list
>

By that logic, neither is sprintf, it's 1 scalar but a variable length list.

   exit   -- $ -- one expression
>

Are you saying array cannot hold one element?

But more importantly is that printf and sprintf are designed to do the
>
same type
> of thing with those args -- they both want a format and optional vars
> that it uses
> to format a string sent to output or another string.
>

Yes, so, printf should be like sprintf.

I.e. why not just allow a list?


Because sprintf takes a scalar plus a list, and printf takes a handle/block
plus a scalar plus a list.

I don't think anyone has put forth an argument as to why sprintf not be able
>
to handle the same list-arguments as printf.
>

Actually, I did. The purpose of the sprintf prototype is to make it hard to
accidentally write bad code. Removing the prototype will cause sprintf to
fail more subtly when used incorrectly.

That leaves changing printf to be like sprintf. I don't know the technical
reasons for why printf doesn't impose scalar context on the format arg.

Both know that they will NEVER look forthe length of an array as a 'format'
> string


Of course not. It helps finding the missing format argument.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About