hankache / rakuguide

The Raku Guide

Home Page:https://raku.guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

For regex definitions explain the differences

trosel opened this issue · comments

http://perl6intro.com/#_regex_definition

Explain the differences between

/light/
m/light/
rx/light/

Are they all the same in the context of smart matching (~~) ?

This language doc shows that m/thing/ is matched immediately against $_.

The other two are just regex objects. Perhaps they're better used when saving the regex in a variable?

commented

Are they all the same in the context of smart matching (~~) ?

Yes, except that you can only specify matching adverbs using the m... variant.

This language doc shows that m/thing/ is matched immediately against $_.

All three variants will immediately match if used with smart matching (~~) as you suspected but also if used in sink context, i.e. on their own as a statement:

/.../;   # matches immediately
rx/.../; # so does this
m/.../;  # and this too

The other two are just regex objects. Perhaps they're better used when saving the regex in a variable?

That sort of thing, yes:

my $regex = /.../; # puts a regex object (`/.../`) in `$var`.
my $match = m/.../; # evaluates the regex within the `m/.../` and puts the resulting match object in `$match`