Matching a Junction makes Match immutable

190 views Asked by At

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.

2

There are 2 answers

2
grobber On BEST ANSWER

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.

3
raiph On

The following may all be poppycock. But I'm going to publish my immediate reaction, eat dessert, then explore further. :)

Code that works

say any('a','b') ~~ /./; # False
say 'x' ~~ /./;          # 「x」

The difference? I dropped the ms.

Why the difference?

They have different meanings.

What you presumably meant in these cases was to do regex matching of the regexes on the RHS of ~~ against the values on the LHS of ~~.

But I think what you've actually written is sub-expressions on the RHS of the ~~ that first do a regex match against $_, and then a subsequent match (just a smart match, not a regex match) of the result of the preceding regex match (i.e. a match object or Nil) against the values on the LHS of the ~~.

I've yet to explore why you get the immutable stuff but I think the above is a first step. I'll delete this answer later if it turns out to be nonsense. :)