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 = 'http://twitter.com/' . $handle;
my $page = get($url);
if($page =~ m/Web<\/span>\s*<a href=\"(.+?)\"/gi) {
$website = $1;
}
if($page =~ m/follower_count\" class=\"stats_count numeric\">(.+?)\s*</g) {
$num_followers = $1;
}
It gets a twitter url and does a bit of regex to capture the # of followers and the website of the user. This code actually works fine. But when you switch the order and search for the website AFTER you search for follower, website comes up empty. As it turns out, when you regex a string, it seems to sort of save the location of where that last match was made. In the html, the follower count comes up after the website display. If you do the follower count regex first, it’s like it starts up the website regex where the follower count left off (like an index reference to the string).
What has me baffled is that i have the “g” operator at the end, signifying “global”, as in “search the string globally… from the beginning”.
Am I missing something here? I can’t seem to figure out why it’s resuming the last regex position on the string (if that makes sense).
The
/gmodifier, in scalar context, doesn’t do what you think it does. Get rid of it.As perlretut explains,
/gin scalar context cycles over each match in turn. It’s designed for use in a loop, like so:The other way to use
/gis in list context:If you’re using
/gin scalar context and you’re not iterating over it, you’re almost certainly not using it right.