It would be helpful if you posted the script output and especially error messages you get as well. Then we don't need to copy and execute your code just to see what happens
With your script I get an error message: "Unmachted ( in regex; marked by <-- HERE in m/mozilla/5.0 ( <-- HERE macintosh;...". I assume you see the same. It means you have a regex special character in the string you are using as pattern.
The culprit is an opening parenthesis without a closing one, leading to an error message. Solution: Use the function quotemeta() to escape any possible character that could have a special meaning in regexes