Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For regex definitions explain the differences #135

Open
trosel opened this issue Mar 10, 2017 · 1 comment
Open

For regex definitions explain the differences #135

trosel opened this issue Mar 10, 2017 · 1 comment

Comments

@trosel
Copy link
Contributor

trosel commented Mar 10, 2017

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?

@raiph
Copy link
Contributor

raiph commented Nov 15, 2017

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`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants