equant has asked for the wisdom of the Perl Monks concerning the following question:

I believe that this should work, but it doesn't, and I can't figure out why. Can someone clue me in? I want to have saved regexps that may or may not include an 'ix' at the end. I thought this would be the way to do it?
my $word = "Apple"; my $pattern = "qr/^(Orange| Apple| Grape| Halibut)$/ix"; print "qr// Match\n" if ($word =~ m/$pattern/); print "non qr// Match\n" if ($word =~ m/^(Orange| Apple| Grape| Halibut)$/ix);
Thanks, Nathan

20050204 Edit by castaway: Changed title from 'qr//'

Replies are listed 'Best First'.
Re Why doesn't my qr// work?
by Tanktalus (Canon) on Feb 03, 2005 at 21:49 UTC

    Remove the quotes around the qr ...

    my $pattern = qr/^(Orange|Apple|Grape|Halibut)$/ix;
    (I removed the spaces because I'm lazy, not because I had to)

Re: Why doesn't my qr// work?
by crashtest (Curate) on Feb 03, 2005 at 23:56 UTC
    Just to expand a little on what Tanktalus said... instead of creating a regular expression with the qr// quote, you just created a normal string that happened to contain a perl expression.
    my $word = "Apple"; my $pattern_quoted = "qr/^(Orange|Apple|Grape|Halibut)$/i"; print "Pattern matched\n" if ($word =~ m/$pattern_quoted/); print "Eval'd pattern matched\n" if ($word =~ eval($pattern_quoted));