Search code examples
regexmatchrakujunction

Matching a Junction makes Match immutable


Just for the heck of it, I am trying to match a junction against a regex with the m// operator in raku (search for Explicit topic match on that page).

In the perl6 REPL:

> any('a','b') ~~ m/./
False

Afterwards, no matter how I call m// I get an immutable-match complaint:

> 'x' ~~ m/./
Cannot modify an immutable Match (「a」)
  in block <unit> at <unknown file> line 1

Question

What is happening behind the scenes here?

Discussion

The problem seems to stem from the fact that the $/ special variable is set to the junction

any(「a」, 「b」)

after the junction match, and it seems to be that 「a」 in the junction that's raising the complaint.

As soon as I do anything that changes $/ to something else, functionality is restored:

> $/=Any
(Any)
> 'x' ~~ m/./
「x」

or

> 'x' ~~ /./
「x」
> 'x' ~~ m/./
「x」

(so matching with // first, so as to change $/, and then match with m//).

Clarification

I am not trying to "achieve" anything beyond what the question's asking: I simply want to understand this behavior.

Edit

For cross-reference purposes, this is now also a rakudo github issue, as suggested by @jjmerelo.


Solution

  • The side issue of whether the match should return False or True is settled, I think, in the comment by @raiph.

    On the other hand, the main problem of receiving the immutable Match error was, it seems, a bug, with a commit that at least on my system fixes it.

    So the problem was (as per the commit message) that regex match objects were not expected to be junctions.