-4
$_ = ~m [($substr)]g;

I have three questions:

  1. What's the difference between ~m[] and ~m//?
  2. Why if deleted () the results would be different?
  3. What does it mean by the g at the end?
serenesat
  • 4,524
  • 10
  • 32
  • 51
  • 1
    I would recommend taking a look at http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – hwnd Mar 26 '15 at 01:13

2 Answers2

2
  1. 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://]
    
  2. () 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
    
  3. 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.

ikegami
  • 322,729
  • 15
  • 228
  • 466
1
  1. The ~m[] and ~m// are same, and they are boundary symbols.

  2. Scalar variable $substr is embed into parentheses, it means that you can get matched value into parentheses through print $1.

  3. The symbol g means global match.
serenesat
  • 4,524
  • 10
  • 32
  • 51
sol
  • 200
  • 2
  • 14