I've combed the docs but I can't seem to find how to do this in perl6.
In perl5 I would have done (just an example):
sub func { ... }
$str =~ s/needle/func($1)/e;
i.e. to replace 'needle' with the output of a call to 'func'
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) { $a x 5 };
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/{func($/)}/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use {}
. No need for the e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle