Generally you don't need to test for 「Nil」.
You can just test for defined-ness.
$ raku -e 'my $x="abc"; with $x.index( "q" ) {say "Exists"} else {say
"Nil";}'
Also 「Nil」 is not a 「Str」, so why would you use 「eq」?
$ raku -e 'Nil.Str'
Use of Nil in string context
If you really need to check specifically for 「Nil」 (which you probably
don't), then you can use 「===」.
for Str, Int, Nil {
say 'Nil' if $_ === Nil;
}
The 「//」 operator can also be useful to deal with undefined values such as
「Nil」.
my $x = 'abc';
say $x.index('q') // 'cannot find the index of 「q」';
On Tue, May 26, 2020 at 4:24 PM ToddAndMargo via perl6-users <
[email protected]> wrote:
> Hi All,
>
> How do I turn this:
>
> $ raku -e 'my $x="abc"; say $x.index( "q" );'
> Nil
>
> into a test?
>
> $ raku -e 'my $x="abc"; if $x.index( "q" ) eq Nil {say "Nil"}else{say
> "Exists";}'
> Use of Nil in string context
> in block <unit> at -e line 1
> Use of Nil in string context
> in block <unit> at -e line 1
> Nil
>
>
> Many thanks,
> -T
>