Regexes & grammars
Substitution with a closure
FullReplace each match with the result of running code on it.
The replacement side of .subst (or s///) can be a closure instead of a fixed string — it runs per match and its return value is substituted. This turns find-and-replace into find-and-transform.
Compute the replacement #
The block receives the match as $/; here each digit is doubled.
say "a1b2c3".subst(/\d/, -> $/ { $/ * 2 }, :g);
Output
a2b4c6A method as the replacement #
A Whatever/method form works too — *.tc title-cases each matched word.
my $s = "hello world"; say $s.subst(/\w+/, *.tc, :g);
Output
Hello WorldNotes #
- Inside the closure the match is the topic, so
$/,$0, and$<name>are all available — replace(\d+)with{ $0 + 1 }to increment every number. - This is the method form; the
s/pattern/{ code }/operator form does the same with the closure in the replacement slot. - Because the replacement is computed, one substitution can do context-dependent edits that a static string never could.