Skip to content

Commit 491f703

Browse files
committed
Document :our
Also adds :my and :our to index. Examples provided by the OP (thanks, @lefth) and [roast](https://github.com/perl6/roast/blob/d1baf2e7a3e56cd6619c46040d04ed6daebc1d02/S05-modifier/my.t). There does not seem to be many other uses of :our in the wild. Anyway, this closes #1475.
1 parent ef6e19e commit 491f703

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

doc/Language/regexes.pod6

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,29 @@ it in a variable first:
10261026
say "Matched $c"; # OUTPUT: «␤Matched 1␤»
10271027
10281028
1029+
X<|:my>
10291030
C<:my> helps scoping the C<$c> variable within the regex and beyond; in this case we can use it in the next sentence to show what has been matched inside the regex. This can be used for debugging inside regular expressions, for instance:
10301031
1031-
my $paragraph="line\nline2\nline3";
1032+
my $paragraph="line\nline2\nline3";
10321033
$paragraph ~~ rx| :my $counter = 0; ( \V* { ++$counter } ) *%% \n |;
10331034
say "Matched $counter lines"; # OUTPUT: «Matched 3 lines␤»
10341035
1036+
X<|:our>
1037+
The C<:our>, similarly to L<C<our>|/syntax/our> in classes, can be used in L<Grammar>s to declare variables that can be accessed, via its fully qualified name, from outside the grammar:
1038+
1039+
=begin code
1040+
grammar HasOur {
1041+
token TOP {
1042+
:our $our = 'þor';
1043+
$our \s+ is \s+ mighty
1044+
}
1045+
}
1046+
1047+
say HasOur.parse('þor is mighty'); # OUTPUT: «「þor is mighty」␤»
1048+
say $HasOur::our; # OUTPUT: «þor␤»
1049+
=end code
1050+
1051+
Once the parsing has been done successfully, we use the FQN name of the C<$our> variable to access its value, that can be none other than C<þor>
10351052
10361053
=head2 X<Named captures|regex, Named captures>
10371054

0 commit comments

Comments
 (0)