Created
May 15, 2012 15:53
-
-
Save moritz/2702826 to your computer and use it in GitHub Desktop.
Lvalues substr for Rakudo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub lsubstr($s is rw, $from = 0, $chars = $s.chars - $from) { | |
my $substr = substr($s, $from, $chars); | |
Proxy.new( | |
FETCH => sub ($) { $substr }, | |
STORE => sub ($, $new) { | |
$s = $s.substr(0, $from) | |
~ $new | |
~ $s.substr($from + $chars); | |
} | |
); | |
} | |
my $s = 'foo'; | |
lsubstr($s, 0, 1) = 'bar'; | |
say $s; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment