in reply to Matching quoted variables. Cant make it work..

Because there are characters in $b that are interpreted differently when used in a regex. The ".", "(", and ")" are such characters. You'd have to do $a =~ /\Q$b\E/ to get it work, or else use quotemeta(). If you want to see if they're equal, though, use 'eq'. If you want to see if one contains the other, use index(). If you want to match a pattern, THEN use a regex.

Can I ask why you're quoting $a and $b?


Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Matching quoted variables. Cant make it work..
by johntweed (Initiate) on Mar 16, 2006 at 20:44 UTC
    I'm using variables because I'm doing a context based search for a string in a file. This string changes at different places in the file.

    As you rightly pointed out.. quotemeta($a) is what I was really looking for, but missed it. Too many languages in my head.. F77,lisp,skill,c,tcl,perl,csh,ksh.......etc etc. I knew I wanted to 'quote' the contents of the variable to prevent regex interpretation, but didn't see how.

    And yes, everyone is correct, that in the example I chose to show, the matching is redundant and 'eq' or index() is a better choice. But once I got stuck on the problem I wanted to understand the generalities of quoting & regex with variables for future use.

    Thanks to everyone, I can see clearly now ...