variable $/

Documentation for variable $/ assembled from the following types:

language documentation Variables

From Variables

(Variables) variable $/

$/ is the match variable. A fresh one is created in every routine. It is set to the result of the last Regex match and so usually contains objects of type Match.

'abc 12' ~~ /\w+/;  # sets $/ to a Match object 
say $/.Str;         # OUTPUT: «abc␤»

The Grammar.parse method also sets the caller's $/ to the resulting Match object. For the following code:

use XML::Grammar# zef install XML 
XML::Grammar.parse("<p>some text</p>");
say $/;
 
# OUTPUT: «「<p>some text</p>」 
#           root => 「<p>some text</p>」 
#            name => 「p」 
#            child => 「some text」 
#             text => 「some text」 
#             textnode => 「some text」 
#           element => 「<p>some text</p>」 
#            name => 「p」 
#            child => 「some text」 
#             text => 「some text」 
#             textnode => 「some text」␤» 

Prior to the 6.d version, you could use $() shortcut to get the ast value from $/ Match if that value is truthy, or the stringification of the Match object otherwise.

'test' ~~ /.../;
# 6.c language only: 
say $(); # OUTPUT: «tes␤»; 
$/.make: 'McTesty';
say $(); # OUTPUT: «McTesty␤»;

This (non-)feature has been deprecated as of version 6.d.

Positional attributes

$/ can have positional attributes if the Regex had capture-groups in it, which are just formed with parentheses.

'abbbbbcdddddeffg' ~~ / a (b+) c (d+ef+) g /;
say $/[0]; # OUTPUT: «「bbbbb」␤» 
say $/[1]; # OUTPUT: «「dddddeff」␤»

These can also be accessed by the shortcuts $0, $1, $2, etc.

say $0# OUTPUT: «「bbbbb」␤» 
say $1# OUTPUT: «「dddddeff」␤»

To get all of the positional attributes, you can use $/.list or @$/. Before 6.d, you can also use the @() shortcut (no spaces inside the parentheses).

say @$/.join# OUTPUT: «bbbbbdddddeff␤» 
 
# 6.c language only: 
say @().join# OUTPUT: «bbbbbdddddeff␤»

This magic behavior of @() has been deprecated as of 6.d

Named attributes

$/ can have named attributes if the Regex had named capture-groups in it, or if the Regex called out to another Regex.

'I... see?' ~~ / \w+ $<punctuation>=[ <-[\w\s]>+ ] \s* $<final-word> = [ \w+ . ] /;
say $/<punctuation># OUTPUT: «「....」␤» 
say $/<final-word>;  # OUTPUT: «「see?」␤» 

These can also be accessed by the shortcut $<named>.

say $<punctuation># OUTPUT: «「....」␤» 
say $<final-word>;  # OUTPUT: «「see?」␤»

To get all of the named attributes, you can use $/.hash or %$/. Before 6.d language, you could also use the %() shortcut (no spaces inside the parentheses).

say %$/.join;       # OUTPUT: «"punctuation     ....final-word  see?"␤» 
 
# 6.c language only 
say %().join;       # OUTPUT: «"punctuation     ....final-word  see?"␤»

This behavior has been deprecated as of the 6.d version.

Thread-safety issues

Because $/ is only defined per routine, you are in fact re-using the same $/ when you do matching in a loop. In a single threaded program, this is not an issue. However, if you're going to use hyper or race to have multiple threads do matching in parallel, the sharing of the "outer" $/ becomes an issue, because then it is being shared between threads! Fortunately, the solution is very simple: define your own $/ inside the scope where you are doing the matching. For example, taking a source of text, running a regex on it, and map that to a hash using parallel execution:

my %mapped = @source.race.map: {
    my $/;  # need one in this block to prevent racing issues 
    m/foo (.*?) bar (.*)/;  # matches on $_, stores in $/ 
    $0 => $1   # short for $/[0] / $/[1] 
}