syntax %

Documentation for syntax % assembled from the following types:

language documentation Regexes

From Regexes

(Regexes) regex %

To more easily match things like comma separated values, you can tack on a % modifier to any of the above quantifiers to specify a separator that must occur between each of the matches. For example, a+ % ',' will match a or a,a or a,a,a, etc. To also match trailing delimiters ( a, or a,a, ), you can use %% instead of %.

The quantifier interacts with % and controls the number of overall repetitions that can match successfully, so a* % ',' also matches the empty string. If you want match words delimited by commas, you might need to nest an ordinary and a modified quantifier:

say so 'abc,def' ~~ / ^ [\w+** 1 % ',' $ /;  # Output: «False» 
say so 'abc,def' ~~ / ^ [\w+** 2 % ',' $ /;  # Output: «True» 

declarator state

From state

(state) variable %

In addition, there's an Associative anonymous state variable %.

sub foo($x{
    say (%).push($x => $x);
}
 
foo($_for ^3;
 
# OUTPUT: «{0 => 0} 
#          {0 => 0, 1 => 1} 
#          {0 => 0, 1 => 1, 2 => 2}␤» 

The same caveat about disambiguation applies. As you may expect, indexed access is also possible (with copying to make it useful).

sub foo($x{
    my $v = %;
    $v{$x} = $x;
    say $v;
}
 
foo($_for ^3;
 
# OUTPUT: «{0 => 0} 
#          {0 => 0, 1 => 1} 
#          {0 => 0, 1 => 1, 2 => 2}␤» 

As with the other anonymous state variables, each mention of % within a given scope will effectively introduce a separate variable.