I think yours is working a little harder than necessary. (Update: I didn't recognize the cache.) Also note that you ought to localize $_ if you're going to use it where Perl isn't automatically localizing it.