[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] TMTOWTDI #2



Brian Keefer wrote:
> Vicki Brown wrote:
> > # Problem - we want code to validate input.
> > # In this particular case, the possible input values are
> > #     blastx, blastn, blastp, tblastx, tblastn
> > # so it looks like a job for a regexp
> >
> 
> while (<>){
>         /t?blast[xn]/ && next;
>         /blastp/ && next;
>         die "$_ : not valid";
> }

This (and all the other answers using regexp) has the problem
that it doesn't take care of the *rest* of the input string.
Is leading whitespace allowed? Trailing garbage? Separated
from the name by whitespace?

The above example would also allow `XXXtblastx<<>>W#$$@Q#%$DR'
as program name, right?

> I think that's closer to what the holy camel tome wants of us.

For a `switch'-like approach, yes. But the problem is the regexp.

I think this is a case where the "holy book of the owl" is more
appropriate. It teaches you how to construct a RE that really
matches what you want. And it will open your eyes for cases
were you just *thought* your RE would match perfectly but didn't
consider the exotic cases.

So I'll give it a shot:

  use Term::ReadLine;

  $term = new Term::ReadLine "name of application";
  while(1) {
    $_ = $term->readline("Enter program name: ");
    last if m/^ \s* ( blast[xnp] | tblast[xn] ) \s* $/x;
    print "Error in program name, try again.\n";
  }
  $program = $1;

(assuming that leading and trailing whitespace is OK, but no 
other garbage allowed).

Roland
--
perl -e '@_=@ARGV;$_="@_[-3,-2,-1]";s{([@{[$_[-1]]}])}#($0=$1.
${[qw[>$%, ()%{&", &"=, |}+&" *]]}[$@++])=~y/*=->$%&"(-){-}+,/
lusternohack /,$0#eg; print; '        -- Roland Giersig, JAPH.

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org