This is an oddball issue I\'ve encountered (and probably have seen before but never paid attention to).
Here\'s the gist of the code:
my $url = \'htt
To quote perlop on Regexp Quote Like Operators:
In scalar context, each execution of
m//g
finds the next match, returning true if it matches, and false if there is no further match. The position after the last match can be read or set using thepos()
function; see pos. A failed match normally resets the search position to the beginning of the string, but you can avoid that by adding the/c
modifier (e.g.m//gc
). Modifying the target string also resets the search position.
So in scalar context (which you're using), /g
does not mean "search from the beginning", it means "search starting from the string's pos". "Search from the beginning" is the default (without /g
).
/g
is normally used when you want to find all matches for a regex in a string, instead of just the first match. In list context, it does that by returning a list of all the matches. In scalar context it does that by starting the search from where the previous search left off (usually done in a loop).