in reply to calling subroutines within a regular expression?

if ($s =~ /${\REGEX}/) {
or:
my $re = REGEX; if ( $s =~ /$re/ ) {
But then what's the point of using constant? May as well:
my $REGEX = qr/cat/; #... if ( $s =~ /$REGEX/ ) {

Replies are listed 'Best First'.
Re^2: calling subroutines within a regular expression?
by Your Mother (Archbishop) on Apr 03, 2009 at 23:17 UTC

    One more for "fun"-

    if ( $s =~ /${+REGEX}/ ) { ...
      This doesn't seem to work:
      >perl -wMstrict -le "use constant RX => qr{ [atc]{3} }xms; my $s = 'the cat in the hat'; print 'found' if $s =~ /${+RX}/; print qq{found a $1} if $s =~ /(${+RX})/; " Use of uninitialized value in regexp compilation ... found Use of uninitialized value in concatenation (.) or string ... found a

        You're right. It looked like it worked on the original to me because it matched "cat" against "" which "found" it. The + is a way to force constants to evaluate, also works, in some contexts, with spelling it out as the sub it is, RX(). Doesn't work here though. :(

Re^2: calling subroutines within a regular expression?
by AnomalousMonk (Archbishop) on Apr 04, 2009 at 00:51 UTC
    And then there is the always useful (if ever-homely):
    >perl -wMstrict -le "use constant RX => qr{ [atc]{3} }xms; my $s = 'the cat in the hat'; print qq{found a $1} if $s =~ m{ ( @{[ RX ]} ) }xms; " found a cat