Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: printf vs. sprintf

by petral (Curate)
on Jan 25, 2001 at 21:04 UTC ( [id://54295]=note: print w/replies, xml ) Need Help??


in reply to printf vs. sprintf

and
print sprintf @a[0,1] # prints b


p

Replies are listed 'Best First'.
Re: Re: printf vs. sprintf
by a (Friar) on Feb 10, 2001 at 10:15 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found