What is difference between /.../
and m/.../
?
use strict;
use warnings;
my $str = "This is a testing for modifier";
if ($str =~ /This/i) { print "Modifier...\n"; }
if ($str =~ m/This/i) { print "W/O Modifier...\n"; }
However, I checked with this site for Reference not clearly understand with the theory
There's no difference. If you just supply /PATTERN/
then it assumes m
. However, if you're using an alternative delimiter, you need to supply the m
. E.g. m|PATTERN|
won't work as |PATTERN|
.
In your example, i
is the modifier as it's after the pattern. m
is the operation. (as opposed to s
, tr
, y
etc.)
Perhaps slightly confusingly - you can use m
as a modifier, but only if you put if after the match.
m/PATTERN/m
will cause ^
and $
to match differently than in m/PATTERN/
, but it's the trailing m
that does this, not the leading one.
Perl has a number of quote-like operators where you can choose the delimiter to suit the data you're passing to the operator.
q(...)
creates a single-quoted stringqq(...)
creates a double-quoted stringqw(...)
creates a list by splitting its arguments on white-spaceqx(...)
executes a command and returns the outputqr(...)
compiles a regular expressionm(...)
matches its argument as a regular expression(There's also s(...)(...)
but I've left that off the list as it has two arguments)
For some of these, you can omit the letter at the start of the operator if you choose the default delimiter.
q
if you use single quote characters ('...'
).qq
if you use double quote characters ("..."
).qx
if you use backticks (`...`
).m
if you use slashes (/.../
).So, to answer your original question, m/.../
and /.../
are the same, but because slashes are the default delimitor for the match operator, you can omit the m
.
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