sploiber
sploiber

Reputation: 621

perl case insensitive switch as variable?

In perl, I would like to be able to do this:

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

In other words, how can I make "$switches" optional at run-time? (The user can choose from a variety of options for the search)

Upvotes: 7

Views: 922

Answers (1)

ikegami
ikegami

Reputation: 385754

if ($string =~ /(?$flags:$pattern)/) {
   ...
}

Note: This won't work if $pattern is a compiled pattern (i.e. produced by qr//) rather than a string since it's the flags passed to qr// that affect a pattern compiled with qr//. You would have to pass the flags to qr// rather than m//.

Upvotes: 11

Related Questions