🔬15. Variants of ‘say’ in Perl 6

Yesterday, we saw four different variants of the multi sub called say. Today, let’s look at them more precisely. The functions are located in the src/core/io_operators.pm file.

Start with the first and the simplest one:

multi sub say() { $*OUT.print-nl }

It just prints the newline to the $*OUT stream. Probably, it would be wise mentioning that parentheses are required in the call:

$ ./perl6 -e'say'
Argument to "say" seems to be malformed
at -e:1
------> say⏏

The following code is correct:

$ ./perl6 -e'say()'

Move on to the sub that expects a defined string:

multi sub say(Str:D \x) {    
    my $out := $*OUT;

Even if not everything is clear here, the general idea can be seen: this function passes its argument to the print method if $*OUT (which equals to STDIN by default) and adds a new line in the end.

The next variant is suitable for the variables of other types:

multi sub say(\x) {
    my $out := $*OUT;

Can you spot the difference with the previous sub?

It is x.gist instead of x. In the case of a string, there is no need to stringify it. In all other cases, say, for integers, the gist method is called. We already talked about the gist method of the Bool class. That’s how the call of say with a Boolean argument gets a string representation of it: its gist method just returns a string, either ‘True’ or ‘False’.

OK, one more variant for calls with multiple arguments:

multi sub say(**@args is raw) {
    my str $str;
    my $iter := @args.iterator;
        nqp::eqaddr(($_ := $iter.pull-one), IterationEnd),
        $str = nqp::concat($str, nqp::unbox_s(.gist)));
    my $out := $*OUT;

Well, it looks complex but again, the main idea is visible with the naked eye: iterate over all arguments, concatenate them and print the resulting string with a newline after it:

$ ./perl6 -e'say(1, 2, 3)'
say(**@args is raw)

I would avoid digging in into the details of the NQP calls in this subroutine for now. Especially, if you compare the implementation of say with similar functions print and put:

multi sub print(**@args is raw) { $*OUT.print: @args.join }

multi sub put(**@args is raw) {
    my $out := $*OUT;
    $out.print: @args.join ~ $out.nl-out

Finally, the variant of say for junctions:

multi sub say(Junction:D \j) {

In this implementation, printing a junction means creating a junction, each branch of which is a call of say with the corresponding value. So, say(1|2) is something equivalent to say(1) | say(2), and I assume that the result that you see in the console may be different in each run.

$ ./perl6 -e'say 1|2'
say(Junction:D \j)

Notice that say 1|2 is not the same as say 1 ~~ 1|2. In the first case, the sub gets a junction, while in the second case it is called with a single Boolean value:

$ ./perl6 -e'say 1 ~~ 1|2'

One thought on “🔬15. Variants of ‘say’ in Perl 6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s