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


in reply to Using constants in regexes?

Use the (?{ CODE }) experimental feature at your own risk:
use constant TEST => 'def'; my $string = 'abcdefg'; if ($string =~ /(?{TEST})/) { print "The string contains the constant.\n"; }else{ print "The string doesn't contain the constant.\n"; }

Aziz,,,

Replies are listed 'Best First'.
Re: Answer: Using CODEconstant/CODEs in regexes?
by japhy (Canon) on Aug 11, 2001 at 03:11 UTC
    Err, I think you were thinking of (??{ ... }). That's overkill, in my opinion. Your approach doesn't work, since (?{ ... }) merely executes code, it doesn't use it as part of the regex.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      You are absolutely right: it was $$ not $. And yes it is an overkill, but it works. I just couldn't remember the syntax of @{[...]}.

      Thanks for the correction

      Aziz,,,