On Tue, May 26, 2020 at 4:24 PM ToddAndMargo via perl6-users <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> 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


On 2020-05-26 15:00, Brad Gilbert wrote:
Generally you don't need to test for 「Nil」.
You can just test for defined-ness.

True enough


    $ 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」?

Because == did not work


     $ 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」';



Hi Brad,

Did not know about the triple =

Thank you!

-T


$ raku -e 'my $x="abc"; if $x.index( "q" ) === Nil {say "Nil"}else{say "Exists";}'
Nil

$ raku -e 'my $x="abc"; if $x.index( "a" ) === Nil {say "Nil"}else{say "Exists";}'
Exists

And I found buried in my Nil notes that `=:=` works too

$ raku -e 'my $x="abc"; if $x.index( "q" ) =:= Nil {say "Nil"}else{say "Exists";}'
Nil

$ raku -e 'my $x="abc"; if $x.index( "b" ) =:= Nil {say "Nil"}else{say "Exists";}'
Exists

Reply via email to