You could also eval it directly - depends on your application
# eval returns the value of the last evaluated expression
if (eval "\$String =~ $RegEx") {
print "Matched";
}
else {
print "Not matched";
}
However this doesn't provide you with access to the regex variables (like
$&, $1, $', ...) as they are dynamically scoped - imagine the eval being a separate block of code.
From your post I'm not sure if you are aware of the difference between eval BLOCK and eval EXPR so I'd suggest reading up on that ...
-- Hofmator