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.

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␤»