Re: WHY are args for sprintf in scalar context?



w.c.humann@xxxxxxxx wrote:
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...

This seems to be old news ;-)

http://www.perl.com/pub/a/1999/11/sins.html#2
http://www.perl.com/doc/manual/html/pod/perltrap.html#Context_Traps_scalar_list_con

IIRC, this distinction (printf/sprintf) came in
after porting from Perl4 to Perl5 because printf
accepts an optional filehandle as first argument
and sprintf allows format construction by subroutine
return value there. One of Perls oddities, true.

...
@a = ("%s %s\n", qw/foo bar/);
print sprintf shift @a, @a;
...

Regards

M.
.



Relevant Pages

  • Re: implicit split to @_ is deprecated ? but, but,
    ... you check the built-in perl documentation for the functions your using, ... In scalar context, ... The intent was both to inform you of where you could have ...
    (comp.lang.perl.misc)
  • Re: While query
    ... > Ben Morrow wrote: ... >> void context and the last in scalar context, ... > So the Perl FAQ is wrong? ... always compiles the comma operator into the same ops: ...
    (comp.lang.perl.misc)
  • Re: Style question: map versus foreach
    ... perl in fact builds a list only to discard all but the last element ... > this one returns the result of evaluating an array in scalar context: ... > But I'd rather say that this returns an array. ... In fact, as Randal keeps saying, there is *no such thing* as 'a list ...
    (comp.lang.perl.misc)
  • Re: whats wrong in this Perl Regex expression?
    ... >this expression always don't work in my Perl compiler, ... >and compiler don't give me error information, ... in a scalar context, perl returns the number of elements in the list ...
    (comp.lang.perl.misc)
  • Re: Problem with Hashes
    ... daniel kaplan wrote: ... am still new to Perl so I mark off threads where i might learn ... @_ in scalar context results in the number of items in it, ...
    (comp.lang.perl.misc)