Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

perl6: subst with capture brackets

by Fox (Pilgrim)
on Aug 16, 2010 at 12:15 UTC ( [id://855251]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://855251]
Approved by moritz
Front-paged by sierpinski
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found