Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

printf vs. sprintf

by henrylaxen (Novice)
on Jan 25, 2001 at 06:25 UTC ( #54187=snippet: print w/ replies, xml ) Need Help??

Description: This one bit me today, and seemed quite mysterious for awhile:
@a = ('a%sc','b');
printf @a;          # prints abc
print sprintf @a;   # prints 2
Comment on printf vs. sprintf
Download Code
Re (tilly) 1: printf vs. sprintf
by tilly (Archbishop) on Jan 25, 2001 at 08:46 UTC
    It seemed mysterious only for a while? I am impressed!

    Those who are curious about how you too can induce needless confusion in hapless fools who want to use your code can read FMTYEWTK About Prototypes... (No, this is not one of the corners of the Perl language that I think was a particularly good idea...)

Re: printf vs. sprintf
by petral (Curate) on Jan 25, 2001 at 21:04 UTC
      huh.
      @a = ('a%sc','b'); printf @a; # prints abc print "\n"; printf scalar @a; # prints 2 print "\n"; print sprintf @a; # prints 2 print "\n"; print sprintf @a[0,1]; # prints b print "\n"; print @a[0,1]; # prints a%scb print "\n"; print scalar @a[0,1]; # prints b print "\n"; print $a[1]; # prints b print "\n"; $huh = scalar @a[0,1]; # prints huhb print "huh", $huh; print "\n"; printf sprintf @a[0,1]; # prints b
      so sprintf evals in scalar (?) context, printf in array? Is there a why behind this?

      a

        The explanation I remember from perl5-porters for this odd difference in behavior involves the optional filehandle argument for printf. The way prototypes were designed for the builtin functions, a prototype which allows for an indirect object has to specify list context for all the regular arguments. Since sprintf doesn't have an optional file handle argument, its prototype specifies a scalar for the first argument, and a list for the remaining arguments.

        So, this difference in behavior between printf and sprintf is more by accident than by design. While the prototype for sprintf could be changed to match the prototype for printf, doing so would break backwards compatibility, particularly where the first argument to sprintf is a subroutine call.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://54187]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-08-29 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls