Skip to content

Commit 8ef3c50

Browse files
committed
Revert "Restructure .subst with Callables section"
This reverts commit d54b955.
1 parent d54b955 commit 8ef3c50

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

doc/Type/Str.pod6

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -826,30 +826,21 @@ of type L<Cool> are coerced to C<Str> for literal matching.
826826
my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
827827
$some-string.=subst(/foo/, "string"); # in-place substitution. $some-string is now 'Some string'
828828
829-
=head3 Callable
829+
=head3 Closures
830830
831-
The replacement can be a L<Callable> in which the current L<Match> object will be
832-
placed in the C<$/> variable, as well as the C<$_> topic variable.
833-
No thunking of arguments is done, so you need to use a L<Callable> replacement, to
834-
refer to any of the captures made in the regex:
831+
The replacement can be a closure:
835832
836-
# Using capture from $/ variable (the $0 is the first positional capture)
837-
say 'abc123defg'.subst(/(\d+)/, { " before $0 after " });
838-
# OUTPUT: «abc before 123 after defg␤»
839-
840-
# Using capture from $/ variable (the $<foo> is a named capture)
841-
say 'abc123defg'.subst(/$<foo=\d+/, { " before $<foo> after " });
842-
# OUTPUT: «abc before 123 after defg␤»
843-
844-
# Using WhateverCode to operate on the Match given in $_:
845-
say "abc123defg".subst(/(\d+)/, *.flip);
846-
# OUTPUT: «abc321defg␤»
847-
848-
# Using a Callable to generate substitution without involving current Match:
849833
my $i = 41;
850834
my $str = "The answer is secret.";
851835
my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
852836
837+
=head3 Captures
838+
839+
If you are going to use captures, you have to use a closure like so:
840+
841+
say 'abc123defg'.subst(/(\d+)/, { " before $0 after " })
842+
# OUTPUT: «abc before 123 after defg␤»
843+
853844
=head3 More Examples
854845
855846
Here are other examples of usage:

0 commit comments

Comments
 (0)