in reply to Re-use of a global match

It is better not to use /g modifier while performing match operation.

Replies are listed 'Best First'.
Re^2: Re-use of a global match
by mr_mischief (Monsignor) on Aug 01, 2007 at 14:11 UTC
    That statement needs to be qualified. It is far from always true. The /g modifier is part of Perl for a reason and there are many things one can do with it that are much more difficult without it.
    $foo = (join ', ', 'abacadaeafagahaiaj' =~ m/a/g ); print $foo . "\n";
      /g can be useful. But if you just want to know if something matches (which for me is most of the time), you don't need it.

      The cases where m///g is useful are where:

      • you need to save the position of the last match, or
      • you're counting or saving the matches for later use (assigning the result of m/// to a variable)

      In practice, I find myself using /g only in substitutions.