Skip to content

Commit 3b5dbf4

Browse files
committed
[doc] Add /some/ documentation for .subst
1 parent 31748bc commit 3b5dbf4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/Str.pod

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,33 @@ Remove the white-space charecters from the end of a string. See also L<trim>.
424424
425425
Remove the white-space charecters from the beginning of a string. See also L<trim>.
426426
427+
=head2 Substitution and replacement operations
428+
429+
=head2 subst
430+
431+
Does string substitution, these behave like s/// does in perl5.
432+
433+
my $some-string = "Some foo";
434+
my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
435+
$some-string.=subst(/foo/, "string); # in-place substitution. $some-string is now 'Some string'
436+
437+
The syntax usually goes like this:
438+
STRING_TO_REPLACE.subst(/PATTERN/, REPLACEMENT);
439+
440+
In the case of subst, REPLACEMENT can also be a closure:
441+
my $i = 41;
442+
my $str = "The answer is secret.";
443+
my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
444+
445+
Here are other examples of usage:
446+
my $str = "Hey foo foo foo";
447+
$str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
448+
449+
$str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
450+
$str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
451+
452+
$str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
453+
454+
427455
=end pod
456+

0 commit comments

Comments
 (0)