(/me dons suit of --proof armor)

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:

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. }
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."

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.


In reply to Validating User Input by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.