They seem to be used interchangeably in the documentation. Is there any difference, even in intent?
As the documentation you link to states,
m/abc/; # a regex that is immediately matched against $_ rx/abc/; # a Regex object /abc/; # a Regex object
[...] Example of difference between
m/ /
and/ /
operators:my $match; $_ = "abc"; $match = m/.+/; say $match; say $match.^name; # OUTPUT: «「abc」Match» $match = /.+/; say $match; say $match.^name; # OUTPUT: «/.+/Regex»
So /.../
returns a Regex
object that can be passed around as a value, and be used for matching later on and multiple times, and m/.../
returns a Match
object having immediately performed the match. When you print a Match
object, you get the result of the match, and when you print a Regex
object, you get a textual representation of the regex. Using m/.../
in Perl 6 lets you access the implicit Match
object, $/
:
Match results are stored in the
$/
variable and are also returned from the match. The result is of typeMatch
if the match was successful; otherwise it isNil
.
The distinction is comparable to Python's re.compile
vs. re.match
/re.search
, and a similar distinction exists in Perl 5 where you can store and re-use a regex with qr/.../
vs. m/.../
and /.../
for direct matching. As @raiph points out, not all occurrences of m/.../
and /.../
result in direct matching. Conversely, Perl 5 precompiles literal (static) regexes even when not explicitly asking it to. (Presumably, Perl 6 performs this optimization, too.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With