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

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, any of $/.list, @$/, or @() can be used.

say @().join# OUTPUT: «bbbbbdddddeff␤» 

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, any of $/.hash, %$/, or %() can be used.

say %().join;       # OUTPUT: «"punctuation     ....final-word  see?"␤»