Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Friday September 17, 2004
03:03 PM

sprintf oddness

[ #20926 ]

I've found this odd thing with sprintf, probably to do with it's prototype.

I would have thought that sprintf @list would do the same as sprint shift @list, @list, but it's doesn't. The former will return the number of elements in @list.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • From "perldoc -f sprintf":

    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.
    • "Perl will use the count of elements in the array as the format, which is almost never useful."

      When would that be useful? I can't think of a single case.

    • Doh! I feel like a moron for looking up the docs for sprintf and not noticing that.

      It's a good example of getting bitten by an unexpected prototype though...

    • I suspect printf and sprintf behave differently because of print/printf's non-standard prototype. sprintf is $@, as it should be. printf should also be $@ except for the filehandle oddity that can't be expressed in prototypes. It could probably be fixed these days but then the Backwards Compatibility Police will come in the night and rearrange your furniture.

      Another "print sprintf" vs "printf" surprise I noticed a while ago: printf doesn't honor $\.