In general I'd use the qr operator:
my $pattern = qr/one/i;
But from your code it is unclear to me exactly how you want to do your "toggling" between case sensitive and case insensitive. Depending on what you actually want to do, one solution might be:
my $sensitive = qr/one/; my $insensitive = qr/one/i; foreach (@words) { my $pattern = some_condition( $_ ) ? $sensitive : $insensitive; if (/$pattern/) { print "Matched: pattern was $pattern and token was $_\n"; } else { print "Unmatched: pattern was $pattern and token was $_\n"; } }
Regarding the use of qr, the posts by japhy on this recent thread are very instructive.
the lowliest monk
In reply to Re: Regex help
by tlm
in thread Regex help
by kiat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |