Stef has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I'm new to Perl and wondering about sprintf vs. printf. Can anyone provide examples of when it would be more appropriate to use one instead of the other? Thanks in advance, Stef

Replies are listed 'Best First'.
Re: sprintf question
by kwaping (Priest) on Jan 25, 2006 at 16:39 UTC
    Don't mind the downvotes Stef, it's a perfectly reasonable question for a new Perl user.

    I see that the question has been suitably answered, but I thought I'd elaborate a little more on *how* it was answered. There is a command-line tool called "perldoc" which is invaluable to Perl coders. It is roughly equivalent to "man" in the unix world. If you want to know how to use a module (probably downloaded from CPAN), you can type perldoc Module::Name, replacing "Module::Name" with the actual name of the module.

    Perldoc also has a feature that describes built-in functions in Perl, such as printf and sprintf. You can access this by typing perldoc -f function_name, again replacing "function_name" with the actual function. With this tool, you wouldn't have had to wait for a reply from perlmonks. :)

    For more information on "perldoc", simply type perldoc perldoc. Enjoy!
        Very nice, thank you for the link!
Re: sprintf question
by dorward (Curate) on Jan 25, 2006 at 15:17 UTC

    printf(...) is equivalent to print sprintf(...)

    You'd use sprintf when you need to have a formatted string in memory, and printf when you want to output a formatted string.

Re: sprintf question
by saintmike (Vicar) on Jan 25, 2006 at 15:22 UTC
    The sprintf manual page says it pretty clearly.