πŸ“˜ The match $/ object in Perl 6

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s