Special meaning of <> and anonymous arrays inside regex in Perl 6

Viewed 127

Outside regex, <> behaves more or less like single quotes. My shallow understanding seems to tell me that, inside regex, <> allows evaluation and interpolation of codes:

# Outside regex, <> acts like single quotes:
> my $x = <{"one"}>
{"one"}
> $x.WHAT
(Str)

# Inside regex, <> evaluates and interpolates:
> my $b="one";
one
> say "zonez" ~~ m/ <{$b}> /  # Evaluates {$b} then quotes: m/ one /
「one」
> say "zonez" ~~ m/ <$b> /    # Interpolates and quotes without {}
「one」

Because an array variable is allowed inside a regex, I suspect that the Perl 6 regex engine expends the array into OR's when there is <> inside regex surrounding the array.

I also suspect that in a user-defined character class, <[ ]>, the array [] inside <> more or less works like an anonymous array in a way similar to @a below, and the contents of the array (chars in the character class) are expended to OR's.

my @a = $b, "two";
[one two]
> so "zonez" ~~ m/ @a /;
True
> say "ztwoz" ~~ m/ <{[$b, "two"]}> / # {} to eval array, then <> quotes
「two」
> say "ztwoz" ~~ m/ <{@a}> /
「two」
> say "ztwoz" ~~ m/ <@a> /
「two」
> say "ztwoz" ~~ m/ one || two /  # expands @a into ORs: [||] @a;
# [||] is a reduction operator;
「two」

And char class expansion:

> say "ztwoz" ~~ m/ <[onetw]> / # like [||] [<o n e t w>];
「t」
> say "ztwoz" ~~ m/ o|n|e|t|w /
「t」
> my @m = < o n e t w >
[o n e t w]
> say "ztwoz" ~~ m/ @m /
「t」

I have not looked into the Rakudo source code, and my understanding is limited. I have not been able to construct anonymous arrays inside regex to prove that <> indeed constructs arrays inside regex.

So, is <> inside regex something special? Or should I study the Rakudo source code (which I really try not to do at this time)?

2 Answers
Related