# Re: What to do when a pattern match fails

```> On 13 Mar 2017, at 22:20, ToddAndMargo <toddandma...@zoho.com> wrote:
>
> On 03/13/2017 02:11 PM, Elizabeth Mattijsen wrote:
>>
>>> On 13 Mar 2017, at 22:06, ToddAndMargo <toddandma...@zoho.com> wrote:
>>>
>>> Hi All,
>>>
>>> \$ perl6 -e 'my \$x="ab12cd"; \$x ~~ m/ab(1q2)cd/; say "\$x\n\\$0=<\$0>\n";'
>>> Use of Nil in string context in block <unit> at -e line 1
>>> ab12cd
>>> \$0=<>
>>>
>>> With out the "q" in this, it works.  I deliberately put
>>> the "q" to see what would happen when a patter was not
>>> found.
>>>
>>> Is there a way around the "use of nil" finger wag
>>
>> The Nil is not caused by the smart match, but from your attempt to display
>> the first positional capture (aka \$0) of a match that failed.
>>
>>
>>> Or should I always test for its presence first if
>>> there is a possibility the pattern might not exist?
>>
>> You should test whether the smart match was successful.
>>
>>    my \$x="ab12cd"; say "\$x\n\\$0=<\$0>\n” if \$x ~~ m/ab(1q2)cd/;
>>
>>
>>
>> Liz
>>
>
> Hi Liz,
>
> Thank you!
>
> How would you  do this if there were several matches in the line?
>
> if \$x ~~ m/(ab)(1q2)(cd)/;
>
> Eventually break down an use .defined?```
```
This is about the entire smart match, the result of the ~~ operator.  Either it
returns true, and then everything is set in \$/ (and it’s derived fields like
\$<foo> if you’re using named captures).  Or it returns false, and then nothing
is set, it’s all Nil.

HTH,

Liz```