Skip to content

Commit 744bf16

Browse files
committed
Merge branch 'master' of github.com:perl6/doc
2 parents b2d6863 + 0f60edd commit 744bf16

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

WANTED

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,3 @@ Syntax features:
1919

2020
API docs:
2121
* KeyReducer
22-
23-
Builtins:
24-
* substr-rw

lib/Type/Str.pod

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,50 @@ Also using a different value or an incorrect starting index won't match:
493493
say $integer.substr-eq(42, 3); #-> False
494494
say $integer.substr-eq(7342, 0); #-> False
495495
496+
=head2 method substr-rw
497+
498+
method substr-rw($from, $length?)
499+
500+
A version of C<substr> that returns a writable reference to a part of a
501+
string variable. Its first argument, C<$from> specifies the index in the
502+
string from which a substitution should occur, and its last argument,
503+
C<$length> specifies how many characters are to be replaced.
504+
505+
For example, in its method form, if one wants to take the string C<"abc">
506+
and replace the second character (at index 1) with the letter C<"z">, then
507+
one do this:
508+
509+
my $string = "abc";
510+
$string.substr-rw(1, 1) = "z";
511+
$string.say; #-> azc
512+
513+
C<substr-rw> also has a function form, so the above example can also be
514+
written like so:
515+
516+
my $string = "abc";
517+
substr-rw($string, 1, 1) = "z";
518+
$string.say; #-> azc
519+
520+
It is also possible to alias the writable reference returned by C<substr-rw>
521+
for repeated operations:
522+
523+
my $string = "A character in the 'Flintstones' is: barney";
524+
$string ~~ /(barney)/;
525+
my $ref := substr-rw($string, $0.from, $0.to);
526+
$string.say;
527+
#-> A character in the 'Flintstones' is: barney
528+
$ref = "fred";
529+
$string.say;
530+
#-> A character in the 'Flintstones' is: fred
531+
$ref = "wilma";
532+
$string.say;
533+
#-> A character in the 'Flintstones' is: wilma
534+
535+
Notice that the start position and length of string to replace has been
536+
specified via the C<.from> and C<.to> methods on the C<Match> object, C<$0>.
537+
It is thus not necessary to count characters in order to replace a
538+
substring, hence making the code more flexible.
539+
496540
=head2 method succ
497541
498542
method succ(Str:D) returns Str:D

0 commit comments

Comments
 (0)