On Sun, 19 Apr 2020, yary wrote:
> Question from today's Raku meetup. This works in a way I expect
> 
> > 'fosdffgg'.subst(/f+/,"( "~ * ~" )", :g);
> ( f )osd( ff )gg
> 
> This one, $0 gets the single f each time
> 
> > 'fosdffgg'.subst(/(f+)/,"( $0 )", :g);
> ( f )osd( f )gg
> 
> Bug or misunderstanding on my part?
> 

Not a bug. You are calling the subst method on a string. Naturally, in order
to perform the call, all the arguments you pass have to be evaluated first.
This includes interpolating $0 into the string "( $0 )". You take whatever
$0 contains, put it into a string and pass that into subst. In your case $0
seemed to confusingly stringify to "f". Consider this instead:

  "oops" ~~ /^(..)/ andthen say 'fosdffgg'.subst(/(f+)/,"( $0 )", :g)
  # OUTPUT: «( oo )osd( oo )gg»

where I deliberately set $0 beforehand.

Your first variant works because you pass a Callable into subst and when you
do that subst will repeatedly evaluate the Callable. Not so when you pass a
string value.

Regards,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Reply via email to