Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

matching a variable containing metacharacter treated as character

by docdurdee (Beadle)
on Feb 21, 2013 at 16:10 UTC ( #1019974=perlquestion: print w/ replies, xml ) Need Help??
docdurdee has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, I have a humble question: How do you match a variable containing a metacharacter that should be treated as a regular boring character? I'm processing some files (@foos) with dots in them. I want:
my @subfoos = grep{m/$var/} @foos;
I ended up with something awful:
my $regvar = $var; $regsol =~ s/\./___/g; my @subfoos = map {s/___/\./g; $_} grep{m/$regsol/} map{s/\./___/g; $_}@foos; }
is there a better way?

Comment on matching a variable containing metacharacter treated as character
Select or Download Code
Re: matching a variable containing metacharacter treated as character
by choroba (Abbot) on Feb 21, 2013 at 16:20 UTC
    Use \Q:
    grep /\Q$var/, @foos;
    See Escape sequences for details.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you!
Re: matching a variable containing metacharacter treated as character
by Anonymous Monk on Feb 22, 2013 at 00:30 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019974]
Approved by toolic
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-08-22 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls