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

John--

The eq operator looks for an exact match of the left and right value, whilst the =~ operator checks to see if the string on the left contains the regular expression on the right side.

The problem is that the string you're using contains characters in it that have special meaning when they're inside a regular expression. In this case, these are the parenthesis. For your regular expression argument, try escaping the special characters, like so:
perl -e '$a="MIN.(NERR_VOL2)"; $b="MIN.\(NERR_VOL2\)" ; $x = ( qq/$a/ +=~ qq/$b/ ) ; print "$a == $b ? $x \n" ;'
You'll want to read the 'perlre' documentation to see what's going on here.

Note 1: There's another character in the expression that has special meaning in your regular expression, but it didn't hurt you in this case.

Note 2: Another thing: You should never assign a variable like so:
$b = "$a";
You're telling perl to create a string, and since it has an expression inside the string, you're telling it to interpolate the value. The end result is that you're asking Perl to do a lot more work to get the result you really want:
$b = $a;
which tells Perl to simply copy the value of $a into $b.

--roboticus

Replies are listed 'Best First'.
Re^2: Matching quoted variables. Cant make it work..
by Fletch (Bishop) on Mar 16, 2006 at 05:01 UTC
    You should never assign a variable like so:

    Well, not necessarily "never". In general for 99.99999% of the cases you don't want the extra quotes; however there's times when you may want to explicitly "stringify" something (say an object with an overloaded q// method). They're few and far between, but not never ever.</pedant>

      Fletch--

      I'm often a pedant myself, so thanks for the correction. I don't quite follow what you mean, though. Could you provide or point me to a trivial example, perhaps?

      --roboticus

        Suppose you've got a roman numeral class. If you treat an instance as a number it works as a number ($fourty_two = $rn_six * $rn_seven;); however when you print it out it represents itself as the roman numeral form (print "The answer is $fourty_two\n"). To get the string version you might explicitly use $as_roman = "$roman_instance" to force stringification (granted in most cases you'd probably be either interpolating it with something else or concatenating it with another string). Or another plausible occurrence might be some operation that just returns the string value, i.e. return "$roman_instance".

        Again, it's a very contrived case, but there are occasions where you will use $a = "$b".

Re^2: Matching quoted variables. Cant make it work..
by Melly (Chaplain) on Mar 16, 2006 at 16:44 UTC

    BTW although the '.' meta-character doesn't hurt you in this case, it does mean you are risking a match-positive when you would expect a match negative

    e.g.

    $a = 'hello.world'; $b = 'helloxworld'; print "matched" if $b =~ /$a/;

    You will get a match in the above example (because '.' means any single character in a regex).

    Tom Melly, tom@tomandlu.co.uk