Skip to content

Commit 56ce35b

Browse files
committed
Adds publication of variables, closes #2887
1 parent 923c2df commit 56ce35b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

doc/Language/regexes.pod6

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,19 +1084,29 @@ levels behaving as elements of that list
10841084
say "Inner: $0[0] and $0[1]"; # OUTPUT: Inner: b and c
10851085
}
10861086
1087-
If you need to refer to a capture from within another capture, store
1088-
it in a variable first:
1087+
These capture variables are only available outside the regex.
10891088
10901089
=begin code
10911090
# !!WRONG!! The $0 refers to a capture *inside* the second capture
10921091
say "11" ~~ /(\d) ($0)/; # OUTPUT: «Nil␤»
1092+
=end code
1093+
1094+
In order to make them available inside the regex, you need to insert a code
1095+
block behind the match; this code block may be empty if there's nothing
1096+
meaningful to do:
10931097
1098+
=begin code
10941099
# CORRECT: $0 is saved into a variable outside the second capture
1095-
# before it is used inside (the `{}` is needed to update the current match)
1100+
# before it is used inside
10961101
say "11" ~~ /(\d) {} :my $c = $0; ($c)/; # OUTPUT: «「11」␤ 0 => 「1」␤ 1 => 「1」␤»
10971102
say "Matched $c"; # OUTPUT: «␤Matched 1␤»
10981103
=end code
10991104
1105+
This code block I<publishes> the capture inside the regex, so that it can be
1106+
assigned to other variables or used for subsequent matches
1107+
1108+
=for code
1109+
say "11" ~~ /(\d) {} $0/; # OUTPUT: «「11」␤ 0 => 「1」␤»
11001110
11011111
X<|:my>
11021112
C<:my> helps scoping the C<$c> variable within the regex and beyond; in this

0 commit comments

Comments
 (0)