12

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.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Stefanus
  • 1,619
  • 3
  • 12
  • 23

2 Answers2

14

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.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
11

Handy Perl 6 FAQ: How and why do say, put and print differ?

The most obvious difference is that say and put append a newline at the end of the output, and print does not.

But there's another difference: print and put converts its arguments to a string by calling the Str method on each item passed to, say uses the gist method instead. The gist 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.

...

mr_ron
  • 471
  • 3
  • 8
  • 3
    Notes: 1) While printing a type object warns, `say`ing it, or printing its gist, does not. 2) `.perl`, which is used by `dd`, is also of interest; see the FAQ. 3) `note` is like `say`, except it prints on STDERR. (@mr_ron, please consider pasting the whole FAQ answer -- it all seems useful -- or giving me a thumbs up to do so and I'll make sure it's nicely formatted. Also, consider adding a note about `note`. Then I'll delete this comment.) – raiph Mar 31 '18 at 19:17