Skip to content

Commit bc61a6d

Browse files
authored
Document using C<|> or C<||> as a trap
#1141
1 parent 83c7646 commit bc61a6d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doc/Language/traps.pod6

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,24 @@ without L<MONKEY-SEE-NO-EVAL|
588588
In theory, careless use of C«<{}>» will only result in an exception being
589589
thrown, and should not introduce security issues.
590590
591+
=head2 C<|> vs C<||>: which branch will win
592+
593+
To match one of several possible alternatives, C<||> or C<|> will be used. But
594+
they are so different.
595+
596+
While there are more alternations than one matched, for those separated by
597+
C<||>, the first matching alternation wins; for those separated by C<|>,
598+
which to win is decided by LTM strategy. See also:
599+
L<documentation on C<||>|/language/regexes#Alternation:_||> and
600+
L<documentation on C<|>|/language/regexes#Longest_Alternation:_|>.
601+
602+
For advice of which to use: for simple regexes just using C<||> instead of C<|>
603+
will get you familiar semantics, but if writing grammars then it's useful to
604+
learn about LTM and declarative prefixes and prefer C<|>. And keep yourself
605+
away from using them in one regex. When you have to do that, add parentheses
606+
and ensure that you know how LTM strategy works to make the code
607+
do what you want.
608+
591609
=head1 Common Precedence Mistakes
592610
593611
=head2 Adverbs and Precedence

0 commit comments

Comments
 (0)