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

Let me first explain the situation. A dropdown menu is created using @var. When a value is selected from the dropdown menu and submitted to the form, that submitted value is checked with the array to see if anything was tampered (nothing is considered acceptable input). Problem is what with grep, i can't seem to get metacharacters to match. The following is a snippet of the problem.
$input = '$b'; @var = ('a','$b','$c','d','e','$f','g'); unless (grep /^$input$/, @var or $var eq "") { print "error"; }
if i replace $b with e (with no metacharacters), the program snippet works fine. maybe i'm approaching this the wrong way. Does anyone have a better solution, something simple, yet flexible?

Replies are listed 'Best First'.
Re: checking input using grep()
by chip (Curate) on Dec 28, 2001 at 05:04 UTC
    Well, if you insist on using a pattern, you have to use the \Q escape to "Q"uote all the metachars, and \E to end the quoting. Thus:

      die unless grep { /^\Q$input\E$/ } @var;

    But you'd really be better off using eq:

      die unless grep { $_ eq $input } @var;

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      die unless grep $_ eq $input, @var;BLOCKs are ugly :) but I second the eq notion.

      Parham, look at quotemeta also.
        I usually avoid BLOCK constructs when I can, but even more than BLOCKs I dislike having similar-looking code that acts very different. So I like the BLOCK versions of grep and map.

            -- Chip Salzenberg, Free-Floating Agent of Chaos