$_ = ~m [($substr)]g;
I have three questions:
- What's the difference between
~m[]and~m//? - Why if deleted
()the results would be different? - What does it mean by the
gat the end?
$_ = ~m [($substr)]g;
I have three questions:
~m[] and ~m//? () the results would be different?g at the end?There is no difference between /.../, m/.../ and m[...]. You can use the delimiter of your choice when you specify the m. /.../ is standard, but sometimes it's more readable to use something else. For example, compare
/^http:\/\//
with
m[^http://]
() are captures. They capture the text matched by the pattern within. You can access the captured text of the first capture via $1, the second via $2, etc. In list context, the match operator returns the captured strings.
$ perl -E'say for 'abcd' =~ /(.).(.)/;'
a
c
m//g is used to find all the matches. m// is documented in perlop.
$ perl -E'say for "abc" =~ /(.)/;'
a
$ perl -E'say for "abc" =~ /(.)/g;'
a
b
c
Note that $var = ~m[...] and $var =~ m[...] are very different, and that you surely meant to use the latter. =~ followed by a match operator specifies the variable against which the match operator is matching.