I think this question is repeated, but searching wasn’t helpful for me.
my $pattern = "javascript:window.open\('([^']+)'\);";
$mech->content =~ m/($pattern)/;
print $1;
I want to have an external $pattern in the regular expression. How can I do this? The current one returns:
Use of uninitialized value $1 in print at main.pm line 20.
$1was empty, so the match did not succeed. I’ll make up a constant string in my example of which I know that it will match the pattern.Declare your regular expression with qr, not as a simple string. Also, you’re capturing twice, once in
$patternfor theopencall’s parentheses, once in themoperator for the whole thing, therefore you get two results. Instead of$1,$2etc. I prefer to assign the results to an array.