Skip to content

Commit d15e32a

Browse files
committed
General improvements to Match documentation
* mention that Match can be an in-progress match as well * mention that method pos only makes sense for in-progress matches * orig can return a non-string
1 parent 435336c commit d15e32a

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

doc/Type/Match.pod6

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,25 @@ Submatches are also C<Match> objects (or lists of C<Match> objects,
5050
if the corresponding regex was quantified), so each match object
5151
can be seen as the root of a tree of match objects.
5252
53+
A C<Match> object can also hold the result of a match in progress
54+
(while the grammar engine is running), in which case the C<pos> method
55+
returns the current position. This view on C<Match> objects is only visible
56+
if you call code from within a regex.
57+
5358
=head1 Methods
5459
5560
=head2 method pos
5661
5762
method pos()
5863
59-
Returns the current position as a string index into C<Match.target>.
64+
Returns the current position as a string index into C<Match.target> for a regex match
65+
in progress:
66+
67+
my $a = 'abcdef';
68+
$a ~~ /b. {say $/.pos }../; say $/.pos; # OUTPUT: «3␤»
6069
61-
my $a = "þor" ~~ /o/;
62-
say $a.pos; # OUTPUT: «2␤»
70+
You should not use this method on finished Match, as the output can be implementation
71+
specific.
6372
6473
=head2 method target
6574
@@ -80,7 +89,14 @@ Clones the C<Match> object.
8089
8190
=head2 method orig
8291
83-
Returns the original string that the regex was matched against.
92+
Returns the original input to the regex engine, which is usually a string,
93+
but doesn't need to be (could be anything that can be coerced to a string):
94+
95+
42 ~~ /.+/;
96+
say $/.orig; # OUTPUT: «42»
97+
say $/.orig.^name; # OUTPUT: «Int»
98+
99+
See L<#method target> for a close equivalent that always returns a string.
84100
85101
=head2 method from
86102

0 commit comments

Comments
 (0)