... $2 is still blank.
You have to properly account for all capturing groups in the overall regex, properly counting capture groups in any interpolated regex. If you have a capturing group within the interpolated recursive regex (not, IMHO, necessary), then you want to access the 3rd capture group variable. However, a capture group in the recursive regex screws up the more general
@p = $s =~ m{ $r4 }xmsg;
extraction regex. See examples below.
>perl -wMstrict -le
"my $s = 'x(y) (a(b)) ()() q (a(b)c()(d(e(f)g))h) q';
;;
our $r3 = qr{
\(
(?:
[^()]+
|
(??{ our $r3 })
)*
\)
}xms;
;;
my @p = $s =~ m{ $r3 }xmsg;
print qq{'$_'} for @p;
print '--------';
;;
$s =~ m{ ($r3) [^()]* ($r3) }xms;
print qq{1 '$1' 2 '$2'};
print '--------';
;;
our $r4 = qr{
\(
(
[^()]*
|
(??{ our $r4 })
)*
\)
}xms;
;;
@p = $s =~ m{ $r4 }xmsg;
print qq{'$_'} for @p;
print '--------';
;;
$s =~ m{ ($r4) [^()]* ($r4) }xms;
print qq{1 '$1' 3 '$3'};
print '--------';
"
'(y)'
'(a(b))'
'()'
'()'
'(a(b)c()(d(e(f)g))h)'
--------
1 '(y)' 2 '(a(b))'
--------
''
''
''
''
''
--------
1 '(y)' 3 '(a(b))'
--------
Have you tried the very neat "(?PARNO)" operator available with Perl 5.10+ and discussed in the example referred to here?