perl case insensitive switch as variable?

spl*_*ber 7 regex perl

在perl中,我希望能够这样做:

$switches = "is";
$regexp = "\\d";
if ($string =~ m/$regexp/$switches) {
 do something...
}
Run Code Online (Sandbox Code Playgroud)

换句话说,如何在运行时使"$ switches"可选?(用户可以从各种搜索选项中进行选择)

ike*_*ami 11

if ($string =~ /(?$flags:$pattern)/) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

注意:如果$pattern是编译模式(即由...生成qr//)而不是字符串,这将不起作用,因为传递给它的标志qr//会影响编译的模式qr//.你必须将标志传递给qr//而不是m//.