In Perl 6, what is the difference between print
, put
and say
?
I can see how print 5
is different, but put 5
and say 5
look the same.
But we must also print a newline manually. One major difference between print and puts is that print does not append a newline automatically. Newlines must be manually added while using the print method. In puts method, the newline is automatically appended.
The only difference between print and say is that the say function adds a new line at the end of its output. So, if I come down here to our say function, and I just copy and paste this a few times, when I run it, you notice that the output of each say function is on its own line.
print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.
You could use puts() or putchar() . puts("Hello, world!\ n"); There's a also fputs() , putc() , and fputc() if you want/need to specify a FILE* to write to.
put $a
is like print $a.Str ~ “\n”
say $a
is like print $a.gist ~ “\n”
put
is more computer readable.say
is more human readable.
put 1 .. 8 # 1 2 3 4 5 6 7 8
say 1 .. 8 # 1..8
Learn more about .gist
here.
———
More accurately, put
and say
append the value of the nl-out
attribute of the output filehandle, which by default is \n
. You can override it, though. Thanks Brad Gilbert for pointing that out.
Handy Perl 6 FAQ: How and why do say, put and print differ?
The most obvious difference is that
say
andput
append a newline at the end of the output, andBut there's another difference:
put
converts its arguments to a string by calling theStr
method on each item passed to,say
uses thegist
method instead. Thegist
method, which you can also create for your own classes, is intended to create a Str for human interpretation. So it is free to leave out information about the object deemed unimportant to understand the essence of the object....
So, say is optimized for casual human interpretation, dd is optimized for casual debugging output and print and put are more generally suitable for producing output.
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With