syntax |

Documentation for syntax | assembled from the following types:

language documentation Regexes

From Regexes

(Regexes) regex |

In short, in regex branches separated by |, the longest token match wins, independent of the textual ordering in the regex. However, what | really does is more than that. It does not decide which branch wins after finishing the whole match, but follows the longest-token matching (LTM) strategy.

Briefly, what | does is this:

say "abc" ~~ /ab | a.* /;                 # Output: ⌜abc⌟ 
say "abc" ~~ /ab | a {} .* /;             # Output: ⌜ab⌟ 
say "if else" ~~ / if | if <.ws> else /;  # Output: 「if」 
say "if else" ~~ / if | if \s+   else /;  # Output: 「if else」 

As is shown above, a.* is a declarative prefix, while a {} .* terminates at {}, then its declarative prefix is a. Note that non-declarative atoms terminate declarative prefix. This is quite important if you want to apply | in a rule, which automatically enables :s, and <.ws> accidentally terminates declarative prefix.

say "abc" ~~ /a. | ab { print "win" } /;  # Output: win「ab」 

When two alternatives match at the same length, the tie is broken by specificity. That is, ab, as an exact match, counts as closer than a., which uses character classes.

say "abc" ~~ /a\w| a. { print "lose" } /# Output: ⌜ab⌟ 

If the tie breaker above doesn't work, then the textually earlier alternative takes precedence.

For more details, see the LTM strategy.

Quoted lists are LTM matches

Using a quoted list in a regex is equivalent to specifying the longest-match alternation of the list's elements. So, the following match:

say 'food' ~~ /< f fo foo food >/;      # OUTPUT: «「food」␤» 

is equivalent to:

say 'food' ~~ / f | fo | foo | food /;  # OUTPUT: «「food」␤» 

Note that the space after the first < is significant here: <food> calls the named rule food while < food > and < food> specify quoted lists with a single element, 'food'.

Arrays can also be interpolated into a regex to achieve the same effect:

my @increasingly-edible = <f fo foo food>;
say 'food' ~~ /@increasingly-edible/;   # OUTPUT: «「food」␤» 

This is documented further under Regex Interpolation, below.

class Signature

From Signature

(Signature) parameter |

Prefixing a parameter with a vertical bar | makes the parameter a Capture, using up all the remaining positional and named arguments.

This is often used in proto definitions (like proto foo (|) {*}) to indicate that the routine's multi definitions can have any type constraints. See proto for an example.

If bound to a variable arguments can be forwarded as a whole using the slip operator |.

sub a(Int $iStr $s{ say $i.^name ~ ' ' ~ $s.^name }
sub b(|c{ say c.^namea(|c}
# OUTPUT: «Capture␤Int Str␤»