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:
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:if ($string =~ /${\(TEST)}/) {
An alternative is to leave the constant in string format, and compile it just prior to executing the regex:use constant PATTERN => qr/def/; if ($string =~ PATTERN) { ... }
This gives you the added benefit of being able to easily print the regex out for debugging purposes.use constant PATTERN => 'def'; my $regex = qr/${\(PATTERN)}/; #options such as /m can go here. if ($string =~ regex) { ... }
In Section
Seekers of Perl Wisdom