Hi Bill,

In your repl examples you're actually passing the True or False as a
positional parameter, which makes it go into the slot for $limit, not
the slot for :$match.

In order to pass true or false for the "match" named parameter you have
different syntactical options:

  comb(/\w/, "a;b;c", match => True) # maybe the simplest is using a pair

  comb(/\w/, "a;b;c", :match) # using "colon pair" syntax; it's syntax
that puts a colon at the beginning and makes a pair

  comb(/\w/, "a;b;c", :match(True)) # :match is short for match => True,
and :match(True) is long for match => True

  comb(/\w/, "a;b;c", :!match) # putting a ! after the : negates the
pair, i.e. it's now match => False

  comb(/\w/, "a;b;c", :match(False)) # same value

And on top of that, you can add the third positional parameter to pass a
value for $limit

  comb(/\w/, "a;b;c", 2, :match) # output up to two results, as match
objects

Here's a few comments on the examples you pasted:


> On another note (or possibly the same note), I tried code similar to > Joe's 
> with fair success. I was able to get the REPL to understand a >
"True" or "False" parameter, but never in conjunction with a > "$limit"
parameter. Is this the correct behaviour, and why?
The surprise here comes from Bool actually being derived from Int, and
therefore being totally acceptable values to pass for $limit.
>> #REPL > Nil >> say comb(/\w/, "a;b;c", False).perl; > ().Seq You can see here
that it gave no results; that's because it interpreted the False as 0 in
the $limit parameter.

>> say comb(/\w/, "a;b;c",  True).perl; > ("a",).Seq
Here the True is interpreted as 1 for $limit, giving you just "a", and
it's a string because the match named parameter wasn't given and
defaulted to False.
>> say comb(/\w+/, "a;b;c",  True).perl; > ("a",).Seq
The difference between \w and \w+ isn't noticeable here, as the source
string only ever has single word character in a row, but you can try
with "ab;cd;ef" for example with both \w and \w+.

>> say comb(/\w+/, "a;b;c",  2).perl; > ("a", "b").Seq >> say comb(/\w+/, 
>> "a;b;c", 3).perl; > ("a", "b",
"c").Seq >> say comb(/\w+/, "a;b;c", 4).perl; > ("a", "b", "c").Seq >>
say comb(/\w+/, "a;b;c", True).perl; > ("a",).Seq
Same as above; True being interpreted as 1

>> say comb(/\w+/, "a;b;c",  2, True).perl; > Too many positionals passed; 
>> expected 2 or 3 arguments but got 4 in >
block <unit> at <unknown file> line 1
There's no syntax here that distinguishes 2, a positional parameter,
from True, also a positional parameter.

>> say comb(/\w+/, "a;b;c",  2, :True).perl; > Unexpected named argument 'True' 
>> passed in block <unit> at <unknown >
file> line 1
The issue here is that :True is short for True => True, i.e. passing the
value True to the named parameter called "True", easy to get confused by
the error message here!
> Any help appreciated, Bill.

I hope the explanations make sense!
  - Timo

Reply via email to