WHY are args for sprintf in scalar context?
- From: w.c.humann@xxxxxxxx
- Date: Wed, 31 Oct 2007 06:25:24 -0700
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
.
- Follow-Ups:
- Re: WHY are args for sprintf in scalar context?
- From: Mirco Wahab
- Re: WHY are args for sprintf in scalar context?
- Prev by Date: FAQ 8.33 Is there a way to hide perl's command line from programs such as "ps"?
- Next by Date: Re: configurable variables in own file?
- Previous by thread: FAQ 8.33 Is there a way to hide perl's command line from programs such as "ps"?
- Next by thread: Re: WHY are args for sprintf in scalar context?
- Index(es):
Relevant Pages
|