in reply to Re^2: Search for text from user input
in thread Search for text from user input

Update: This was intended as an answer to re ^2; specifically, how to untaint. Apologies for any confusion caused by my confusion. :-)
 

Anonymonk gave you the bullet version; sierpinski provided the details. Very simply, write a regular expression to reject anything which is NOT acceptable -- for your purposes, acceptable input might well be constrained to

/^[A-Za-z0-9]+\.jpg$/i

...that is, a name beginning with an upper or lowercase alpha character or a digit, followed by any number of alphas or digits, followed by a period and "jpg". The "^" and "$"mark the beginning and end of your $search string, thus preventing someone from sending you a file called

foo.jpg.delete_everything.exe.

Alternately, your could reject everything except the char set just discussed by using

/^[^A-Za-z0-9]+\.jpg$/i

...which is the inverse set-- anything that is NOT an upper or lowercase alpha or digit matches, in which case you would want to reject anything that DOES match this one. (if you wish to accept "*.jpeg" you'll need to extend these regexen.)

BTW, the shebang is better written as

#!/usr/bin/perl -wT

I suspect your version will fail. And, for your own sanity and safety:

And, as to your question in re ^3, consider: Where do you expect the value of $_ to come from? Again, see walkingthecow's answer, below.

Replies are listed 'Best First'.
Re^4: Search for text from user input
by ww (Archbishop) on May 13, 2010 at 13:56 UTC
    Warning about the above for the future reader:

    Both regexen were originally written without the /i. Then (belatedly) recognition of the need for case insensitivity on the file extension set in. /me (insufficient thought) just added /i... without fixing the rest of the regexen.

    Duh. That kind of thoughtlessness during late-night (or early morning) code revision has bit me before. Perhaps this will warn others.

    The regexen don't need the a-z when the i is added. Obvious? Yes, but not at the (befuddled) time.

    And the second version would be better and more clearly written with a negated match:  if ($item !~/^[A-Z0-9]+\.jpg$/i) {

    Tested:

    #!/usr/bin/perl use strict; use warnings; # 839761 my @foo=("a.jpg", "b.txt", "Cde.jpg", "abc.jpg.delete_everything.exe", + "123.jpg", "|#&.exe"); print "Using negative match, '!~'- items which match should be exclude +d:\n\n"; for my $item(@foo) { if ($item !~/^[A-Z0-9]+\.jpg$/i) { print "\t--> (negated) match: $item \n"; }else{ print "no match in \$item: $item \n"; } } print "\n\n". 'Now using /^[A-Z0-9]+\..+$/i' . " so matched should be +accepted: \n\n"; for my $item(@foo) { if ($item =~/^[A-Z0-9]+\.jpg$/i) { print "\t --> match: $item \n"; }else{ print "no match in \$item: $item \n"; } }