in reply to Pattern matching against a variable

Perl tries to guess whether $ is an end-of-line test or a variable to interpolate. It almost always guesses right; your problem is something different.

By the way, if you do a match like /$usersuppliedpattern/, there are few things to be aware of:

  • if you want characters such as '*' to be treated literally, quotemeta the variable: /\Q$userpat/
  • if the pattern will never change, use the //o flag
  • or if the match is in a loop and the pattern isn't changed in it, set $userpat = qr/$userpat/ and match with =~ $userpat to avoid recompilation (assuming your perl version supports qr//).
  • if the var may be empty be aware that m/$emptyvar/ (or s/$emptyvar//) will not always succeed (as an empty regex should) but rather reuse the last successful pattern your program has matched (a particularly confusing kind of action at a distance only allowed for hysterical raisins). You can foil this with /(?:)$var/