deprecated has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to validate user input against an array or ignore it in a catchall function.
We just recently had a discussion about reinventing the wheel, and this strikes me as something that's reinvented often. However, I wasnt able to find anything quickly using the search, so I hacked this together:
I interpret this as "get a line from STDIN. if caller gave us an array, we check it against all the elements of the array and set a false value if it doesnt check out. if we have false at the end of the loop, we return false. otherwise we return what we got. if we didnt get an array, we return what we got from STDIN."sub getline { my $line; chomp ($line = <>); if (@_) { my $bool; foreach (@_) { if ($line =~ /$_/) { undef $bool } else { $bool++ } } return $bool ? $line : 0 ; } $line; # we got here because caller didnt care about what they get. }
The problem with this is the code doesnt work. Rather, it does, but its too fascist. If I pass qw(foo bar baz bletch) and my user gives me "foo bletch" I want to accept it. I get the feeling here that I am forgetting something that is criminally stupid.
Shaking my head,
brother dep.
--
transcending "coolness" is what makes us cool.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Validating User Input
by mirod (Canon) on Feb 18, 2001 at 14:42 UTC | |
|
Re: Validating User Input
by chipmunk (Parson) on Feb 19, 2001 at 00:28 UTC |