in reply to Search for text from user input
Perhaps try using the value of $search somewhere in your code? A second grep would be good.
Also, be sure to sanitize your input or it will run arbitrary code many times.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Search for text from user input
by sierpinski (Chaplain) on May 12, 2010 at 19:28 UTC | |
I also recommend 'use strict;' to force proper programming technique. Incorporating your $search in a grep or in your original regex would fix your problem. Right now you're not looking for what was typed, only things with 'jpg' in the title. You can also reduce your regex by using the 'i' operator after the last slash to make your search case-insensitive. I'd suggest you do some searching on google for regexs (you might find a good tutorial here, I haven't looked lately) and how they work. | [reply] |
|
Re^2: Search for text from user input
by Nathan_84 (Acolyte) on May 12, 2010 at 19:46 UTC | |
How do i sanitize my input?
Ive tried using the <STDIN> and added it to grep but im unable to get it to work. Im not sure how im ment to add the input to grep. Thanks | [reply] [d/l] |
by ww (Archbishop) on May 13, 2010 at 02:30 UTC | |
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 -wTI 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. | [reply] [d/l] [select] |
by ww (Archbishop) on May 13, 2010 at 13:56 UTC | |
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:
| [reply] [d/l] [select] |
by Nathan_84 (Acolyte) on May 12, 2010 at 21:23 UTC | |
This is my attempt however is doesnt work and i get the error message: Use of uninitialized value $_ in pattern match (m//) at search2.pl line 11, <STDIN> line 1. Any ideas?
| [reply] [d/l] |