in reply to using constants in regex/search

I typically use:
my $NAME = "abc"; ... if ($input =~ /$NAME/) { ... }
It does use a variable, but it saves a sub. The advantage of being able to interpolate variables to me outweight the advantages 'constants' give me.

Replies are listed 'Best First'.
Re^2: using constants in regex/search
by spmlingam (Scribe) on Dec 02, 2008 at 11:34 UTC
    You can use like this
    if ($input =~ /${\NAME}/) { print "Matched!"; }
      That's identical to
      if ($input =~ /$NAME/) {...}
      which is what I used, so I don't get your point.
        You have copied the NAME value to a variable and used as $NAME.
        i have specified a method, which will use the constant NAME itself in the regular expression
        use strict; use constant NAME => 'abc'; if ($ARGV[0] =~ /${\NAME}/) { print "match"; };
        You're thinking of ${NAME}. ${\NAME} is the scalar version of @{[ NAME ]}.