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

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

Thread Previous | Thread Next
Linda Walsh
September 29, 2011 18:59
Re: [perl #100190] RFE: fix sprintf to be consistent with printf and be useful!
Message ID:
Eric Brine via RT wrote:
>  On Wed, Sep 28, 2011 at 7:27 PM, Linda W <> wrote:
> > but expecting sprintf @arr to work would mean that open @arr should also
> >> work, and so on; That road leads to madness.
> >>
> > open @arr would work?   Why?  It's NOT a simple list of scalers...
> >
>  First, that's not true. C<open> only takes scalars for operands.


  open FOOBAR, "file", "mode";

FOOBAR doesn't look like a simple scalar.  Doesn't need quotes, has no $ ...
Why do you consider it a simple scalar?


  open $scalar-target, $scalar, .....

Again, the 1st can't be a R/O var, it has to be a writeable's not
the same as the rest.  

But in:

  sprintf"  FOOBAR, "arg", $arg2,[list]...

FOOBAR would be illegal -- (in strict)... it's not a a string constant, and
has no quotes.  It is a read-only parameter passed to sprintf (by R/O, I
mean sprintf doesn't try to write to it.  In printf it's similar  compare
the syntax:

  printf [HANDLE] @list...

  sprintf @list....

only difference about printf is it can take an optional handle where you 
use a colon.

There is no form of 'open' where it doesn't take a list (either literal or
implied).  The same is not true for printf -- as it can have the optional
FH, as the first param that is NOT part of a list.

Why there should be a feature in the language for sprintf
to specifically disallow an array, which would "almost never do anything
useful", when it really wants a list at that posistion?

    Can anyone think of some example where the 'length' of an array can be
used as teh format string for the variables that follow?  I.e. I see no
reason why it couldn't go through a deprecation cycle (I thought
I could come up with an example, but it doesn't work, so I have no clue
how you could use such a feature.  I.e., I tried:

simply forming "%s\n" which in hex would be: 0x0025730a, or, on an LE
machine, 0x000a5225, would be that 4 bytes string in 1 32-bit word, but perl
keeps track that it isn't really to be interpreted as a string, but as a
number, so all it does is print out the number:

  perl -e 'my @ar;
  my $out=sprintf @ar, "my string";
  print "$out\n"; '


Bummer.  But make for a good case about the change not breaking previous

>  Secondly, you just answered your own question. What's special is the
>  syntax.  c<printf> has a special syntax just like c<open>, which is why
>  C<printf> bhves different than C<sprintf>, C<substr>, C<length>, etc.
> > It's first arg is something to be *written to*.
>  Actually, it's not. The first operand is a file handle. C<open> and
>  C<printf> write to the handle, but not to the first arg.

open writes to the first, print does not.  Sorry for any confusion.

If you think open doesn't write to the file handle, you are
in error:

>  perl -e '
>  use Readonly;
>  Readonly my $fh => undef;
>  open $fh, "/etc/passwd" || die "open failed: $!\n";
>  '
Modification of a read-only value attempted at -e line 4

Perl does write to the first arg in open.  

>  Even if it was being written to, you think you can write to one scalar,
>  but not $args[0] which is also a scalar?

    What made you think that I thought that?

>   There is no special type in perl (that I'm aware of),
> > called 'FORMAT STRING'....
>  That's eaxctly what sprintf is not special and printf is.
    printf is special in that it can take an optional, *non-list*, (i.e. not
something you can put in an array) arg.   After that optional arg, it takes
a list -- just like spritnf.

Lists can be represented by arrays.  

"sprintf's" list can't accept an array,  even though it's arguments
exactly the same list that can be passed to 'printf' for it's list

Thus it breaks the list-as array paradigm.

Thus I consider it a bug (though as it was designed that way, I
called it an RFE).

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About