Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

say without newline in Raku

Tags:

raku

I want to print the even numbers in a row but I can't.

use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4>
{ $_ %2 == 0 ?? say color('red'),$_,color('reset') !! say $_ }

printf doesn't seem to work with the Terminal::ANSIColor directives and put doesn't work either.

Is there any switch to say which makes it print without newline? How to print those Terminal::ANSIColor formatted sections in a row?

like image 497
Lars Malmsteen Avatar asked Mar 03 '23 18:03

Lars Malmsteen


1 Answers

say is basically defined as:

sub say ( +@_ ) {
    for @_ {
        $*OUT.print( $_.gist )
    }

    $*OUT.print( $*OUT.nl-out );
}

If you don't want the newline, you can either change the value of $*OUT.nl-out or use print and gist.

say $_;

print $_.gist;

In many cases the result of calling .gist is the same as .Str. Which means you don't even need to call .gist.

use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4> {
    $_ %% 2 ?? print color('red'), $_, color('reset') !! print $_
}

(Note that I used the evenly divisible by operator %%.)


say is for humans, which is why it uses .gist and adds the newline.

If you want more fine-grained control, don't use say. Use print or put instead.

like image 120
Brad Gilbert Avatar answered Apr 30 '23 08:04

Brad Gilbert