in reply to How to ignore special characters in a variable ?

quotemeta or \Q...\E
my $line = "CCCC--CCCC--CCC(--CC(O"; my $string = "CCC("; if ($line =~ m/\Q$string\E/) {print "match";}

Replies are listed 'Best First'.
Re^2: How to ignore special characters in a variable ?
by Anonymous Monk on Feb 17, 2011 at 05:21 UTC

    /Q.../E turns out to be better option than quotemeta. Just in case a beginner happens to read this node - remember - quotemeta modifies your string by adding "\" before special characters. If your string is a variable (like $string in above example), it becomes unusable.

    Having fun with perl... :-)

      \Q is quotemeta, so I don't see how it could be better.

      >perl -MO=Concise,-exec -e"qq{\Q$x}" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <1> quotemeta[t2] sK/1 5 <@> stringify[t3] vK/1 6 <@> leave[1 ref] vKP/REFC -e syntax OK >perl -MO=Concise,-exec -e"quotemeta($x)" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <1> quotemeta[t2] vK/1 5 <@> leave[1 ref] vKP/REFC -e syntax OK

      If you're just talking about how it looks in the code, ok.

      /Q /E isn't real syntax, but \Q \E is :)
        Oops.. my mistake