Skip to content

Instantly share code, notes, and snippets.

@moritz
Created May 15, 2012 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritz/2702826 to your computer and use it in GitHub Desktop.
Save moritz/2702826 to your computer and use it in GitHub Desktop.
Lvalues substr for Rakudo
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