http://www.perlmonks.org?node_id=427036


in reply to Using constants in regexes?

There's a few ways of tackling this problem, depending on your goals. To use a constant as an interpolated value within a regular expression, use the following notation:
if ($string =~ /${\(TEST)}/) {
This has the unfortunate consequence of slowing down your regular expression, since it can't be compiled. Using the qr operator is your best bet. You can use japhy's suggested format:
use constant PATTERN => qr/def/; if ($string =~ PATTERN) { ... }
An alternative is to leave the constant in string format, and compile it just prior to executing the regex:
use constant PATTERN => 'def'; my $regex = qr/${\(PATTERN)}/; #options such as /m can go here. if ($string =~ regex) { ... }
This gives you the added benefit of being able to easily print the regex out for debugging purposes.