Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: question mark in there

by kilinrax (Deacon)
on Dec 12, 2000 at 21:19 UTC ( #46277=note: print w/replies, xml ) Need Help??

in reply to question mark in there

Just use \Q to quote out the '?' character:
abowley@lave:~$ perl -e '$a = $b = "word?dude"; print "$a $b\n" if $a +=~ m/\Q$b/' word?dude word?dude

Replies are listed 'Best First'.
Re: Re: question mark in there
by floopy (Sexton) on Dec 12, 2000 at 22:05 UTC
    That's not necessarily a bad way to do it, but everyone should be aware that if there are any metacharacters in $b and \E precedes them, the metacharacters will be parsed as metacharcters.
    IOW, if $b == "???\E?", the fourth '?' will be not be matched literally.

    The only real advantage to punk music is that nobody can whistle it.
      I was skeptical about this warning, and a simple experiment shows that it is not accurate:
      DB<1> $x = '???\E???' DB<2> p "\Q$x" \?\?\?\\E\?\?\? DB<3>
      A \Q cannot be ended by a \E in an interpolated string. And, using B::Deparse's -q option in 5.6, we can see why:
      ~> perl -MO=Deparse,-q -e '$x = q{?\E?}; $y = "a\Q$x\Eb"; print "$y\n" +' $x = '?\\E?'; $y = quotemeta $x; print $y . "\n"; -e syntax OK
      This shows that \Q\E inside a double-quoted string is actually compiled as concatenation and quotemeta. The value of $x at runtime won't change the scope of the quotemeta().

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://46277]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-05-25 17:19 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.