Skip to content

Commit 4f2611b

Browse files
committed
.subst: give examples on how to use captures
Hopefully this will help others out, as it is somewhat counterintuitive.
1 parent 2ebb405 commit 4f2611b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

doc/Type/Str.pod6

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,16 +813,29 @@ C<s/matcher/replacement/>.
813813
C<$matcher> can be a L<Regex>, or a literal C<Str>. Non-Str matcher arguments
814814
of type L<Cool> are coerced to C<Str> for literal matching.
815815
816+
=head3 Literal replacement substitution
817+
816818
my $some-string = "Some foo";
817819
my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
818820
$some-string.=subst(/foo/, "string"); # in-place substitution. $some-string is now 'Some string'
819821
822+
=head3 Closures
823+
820824
The replacement can be a closure:
821825
822826
my $i = 41;
823827
my $str = "The answer is secret.";
824828
my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
825829
830+
=head3 Captures
831+
832+
If you are going to use captures, you have to use a closure like so:
833+
834+
say 'abc123defg'.subst(/(\d+)/, { " before $0 after " })
835+
# OUTPUT: «abc before 123 after defg␤»
836+
837+
=head3 More Examples
838+
826839
Here are other examples of usage:
827840
828841
my $str = "Hey foo foo foo";
@@ -840,6 +853,8 @@ The C<:nth> adverb has readable English-looking variants:
840853
say 'ooooo'.subst: 'o', 'x', :3rd; # OUTPUT: «ooxoo␤»
841854
say 'ooooo'.subst: 'o', 'x', :4th; # OUTPUT: «oooxo␤»
842855
856+
=head3 Adverbs
857+
843858
The following adverbs are supported
844859
845860
=begin table

0 commit comments

Comments
 (0)