variable $/

Documentation for variable $/ assembled from the following types:

language documentation Variables

From Variables

(Variables) variable $/

$/ is the match variable. It stores 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.