Backtracking Control

Backtracking is triggered whenever part of the pattern fails to match. You can also explicitly trigger backtracking by calling the fail function within a closure. Table 7-8 shows some metacharacters and built-in rules relevant to backtracking.

Table 7-8. Backtracking controls

Operator

Meaning

:

Don’t retry the previous atom; fail to the next earlier atom.

::

Don’t backtrack over this point; fail out of the closest enclosing group (( . . . ), [ . . . ], or the rule delimiters).

:::

Don’t backtrack over this point; fail out of the current rule or subrule.

<commit>

Don’t backtrack over this point; fail out of the entire match (even from within a subrule).

<cut>

Like <commit>, but also cuts the string matched. The current matching position at this point becomes the new beginning of the string.

Get Perl 6 and Parrot Essentials, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.