📘 The match $/ object in Perl 6

📘 The match $/ object in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

As we have just seen, the smartmatch operator comparing a string with a regex returns an object of the Match type. This object is stored in the $/ variable. It also contains all the matching substrings. To keep (catch) the substring a pair of parentheses is used. The first match is indexed as 0, and you may access it as an array element either using the full syntax $/[0] or the shortened one: $0.

Remember that even the separate elements like $0 or $0 still contain objects of the Match type. To cast them to strings or numbers, coercion syntax can be used. For example, ~$0 converts the object to a string, and +$0 converts it to an integer.

'Wed 15' ~~ /(\w+) \s (\d+)/;
say ~$0; # Wed
say +$1; # 15

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code