File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -1084,19 +1084,29 @@ levels behaving as elements of that list
1084
1084
say "Inner: $0[0] and $0[1]"; # OUTPUT: Inner: b and c
1085
1085
}
1086
1086
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.
1089
1088
1090
1089
= begin code
1091
1090
# !!WRONG!! The $0 refers to a capture *inside* the second capture
1092
1091
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:
1093
1097
1098
+ = begin code
1094
1099
# 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
1096
1101
say "11" ~~ /(\d) {} :my $c = $0; ($c)/; # OUTPUT: «「11」 0 => 「1」 1 => 「1」»
1097
1102
say "Matched $c"; # OUTPUT: «Matched 1»
1098
1103
= end code
1099
1104
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」»
1100
1110
1101
1111
X < |:my >
1102
1112
C < :my > helps scoping the C < $c > variable within the regex and beyond; in this
You can’t perform that action at this time.
0 commit comments