http://www.perlmonks.org?node_id=1027738


in reply to What does 'global' (/g) do in a regexp in this particular case?

/g in scalar context iterates over all matches and keeps track of the position, see also pos

Without /g the match restarts from the beginning, i.e. the string is stateless.

DB<116> $a="a a" => "a a" DB<117> scalar ($a =~ /a/g) => 1 DB<118> scalar ($a =~ /a/g) => 1 DB<119> scalar ($a =~ /a/g) => "" # exhausted DB<120> $a="a a" => "a a" DB<121> scalar ($a =~ /a/) => 1 DB<122> scalar ($a =~ /a/) => 1 DB<123> scalar ($a =~ /a/) => 1 # ... till infinity

Cheers Rolf

( addicted to the Perl Programming Language)