WHY are args for sprintf in scalar context?



Just ran into this nasty trap:

perl -we'@a=("%s %s\n", qw/foo bar/); printf @a'
foo bar
perl -we'@a=("%s %s\n", qw/foo bar/); print sprintf @a'
3

And indeed, 'perldoc -f sprintf' says:
"Unlike printf, sprintf does not do what you probably mean when you
pass it an array as your first argument. The array is given scalar
context, and instead of using the 0th element of the array as the
format, Perl will use the count of elements in the array as the
format, which is almost never useful."

But *why* are args for printf in array context and for sprintf in
scalar context? Especially if it's "not what I probably mean" and
"almost never useful". It's not like Perl to make easy things
harder...

Wolfram

.



Relevant Pages

  • Re: sprintf and printf in Perl
    ... While 'printf' print the result directly to screen. ... sprintf and printf in Perl ... Output2 from screen (use sprintf for string) ... Useless use of sprintf in void context at math9.pl ...
    (perl.beginners)
  • Re: Dynamic no. of arguments
    ... Put the values in an array, and pass a pointer (to the array's first ... nor write my own sprintf function that takes an array. ... passes it to sprintf in a loop. ... iterate the string or array nor call sprintf repeatedly. ...
    (comp.lang.c)
  • setting MAX characters emitted in printf() functions?
    ... Where printf() here is generic and refers to sprintf, fprintf, etc. ... 256 characters on some platforms a double is even larger than that, ... string containing the formatted variable if the desired field ...
    (comp.lang.c)
  • Re: setting MAX characters emitted in printf() functions?
    ... >Where printf() here is generic and refers to sprintf, fprintf, etc. ... C99 snprintf returns the number of characters that would have been ...
    (comp.lang.c)
  • Re: setting MAX characters emitted in printf() functions?
    ... > Where printf() here is generic and refers to sprintf, fprintf, etc. ... > 256 characters on some platforms a double is even larger than that, ... > string containing the formatted variable if the desired field ...
    (comp.lang.c)