Modifiers

Modifiers alter the meaning of the pattern syntax. The standard position for modifiers is at the beginning of the rule, right after the m, s, or rx, or after the name in a named rule. Modifiers cannot attach to the outside of a bare / . . . /. For example:

m:i/marvin/ # case insensitive
rule names :i { marvin | ford | arthur }

The single-character modifiers can be grouped, but the others must be separated by a colon:

m:wig/ zaphod /                        # OK
m:words:ignorecase:globally / zaphod / # OK
m:wordsignorecaseglobally / zaphod /   # Not OK

Most of the modifiers can also go inside the rule, attached to the rule delimiters or to grouping delimiters. Internal modifiers are lexically scoped to their enclosing delimiters, so you get a temporary alteration of the pattern:

m/:w I saw [:i zaphod] / # only 'zaphod' is case insensitive

The repetition modifiers (:N x, :N th, :once, :globally, and :exhaustive) and the continue modifier (:cont) can’t be lexically scoped, because they alter the return value of the entire rule.

The :N x modifier matches the rule a counted number of times. If the modifier expects more matches than the string has, the match fails. It has an alternate form :x( N), which can take a variable in place of the number.

The :once modifier on a rule only allows it to match once. The rule will not match again until you call the .reset method on the rule object.

The :globally modifier matches as many times as possible. The :exhaustive modifier also matches as many times as possible, but ...

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.