> [EMAIL PROTECTED] - Mon Oct 07 07:17:39 2002]:
>
> This is a bug report for perl from [EMAIL PROTECTED],
> generated with the help of perlbug 1.33 running under perl v5.6.1.
>
>
> -----------------------------------------------------------------
> [Please enter your report here]
>
> At a minimum, this is a documentation issue, as perlfunc says
>
> =item printf FORMAT, LIST
>
> Equivalent to C<print FILEHANDLE sprintf(FORMAT, LIST)>, except that
> C<$\>
> (the output record separator) is not appended.
>
>
> This is not true. printf places its arguments in list context, then
> uses the
> first item in the list as the format, whereas sprintf has a prototype
> $@, and
> so places the first argument in scalar context.
>
> Hence, you get differences if you pass an array or hash as the first
> argument:
>
> $ perl -lwe '%hash = ("hello %s", "world\n"); printf %hash'
> hello world
> $ perl -lwe '%hash = ("hello %s", "world\n"); print sprintf %hash'
> 1/8
> $ perl -lwe '@array = ("hello %s", "world\n"); printf @array'
> hello world
> $ perl -lwe '@array = ("hello %s", "world\n"); print sprintf @array'
> 2
>
> Note also that there are no warnings in the sprintf case that you're
> doing
> something screwy. I doubt many people (apart from JAPH writers and
> obfuscators) want to flatten a hash for either printf or sprintf, but
> being
> able to pass a list to sprintf might be useful. (however, changing
> this
> would break existing behaviour)
>
> I'm not sure whether we should just update the docs to reflect
> reality,
> or instead whether we should update reality to be less weird.
>
> Behaviour consistent in all the versions of perl I've tested. I expect
> that the documentation bug is present in them, because the docs for
> printf
> last changed when I submitted a bug report a long time ago that caused
> the
> section ", except that C<$\> (the output record separator) is not
> appended."
> to be added.
>
> Nicholas Clark
>
I should have caught this before (reading the documentation would have
helped :) ), but it appears that some kind soul has added this farther
down in the sprintf documentation.
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.
This seems to explain what's happening pretty well.