Fox has asked for the wisdom of the Perl Monks concerning the following question:

I was trying to convert some code from perl -> perl6 when had to use capture brackets in a substitution (s///)

so in rakudo specifically, this works:
rakudo/perl6 -e 'my $v = "test"; $v ~~ /(\w)/; say $0' # says t

but this doesn't:
rakudo/perl6 -e 'my $v = "test"; $v ~~ s/(\w)/X/; say $0' # says Any()


is there a different syntax for subst ? or rakudo haven't implemented this yet ?

Replies are listed 'Best First'.
Re: perl6: subst with capture brackets
by moritz (Cardinal) on Aug 16, 2010 at 12:41 UTC
    It's not yet implemented (and not very easy to do :/, I've tried it twice so far, with no luck.)

    If you want to use the match object in the substitution part, there's an ugly workaround, which is to use the method form of subst, and using the first positional parameter:

    my $v = "test"; $v.=subst(/(\w)/, -> $/ { say $0; 'X' }); say $v # output: t Xest

    I know that $/ should be available in the substitution part, but I'm not sure what the spec says about the outside - especially in the case of s:g/// this wouldn't make all too much sense.

    Update: both forms, as well as using $0 in the right-hand side of the s/// work now in rakudo.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Ah ok, thanks for the heads up and the workaround.