in reply to check if its a number

Why is a regular expression bad? They're extremely efficient and simple to understand and the right tool for the job.
if ( defined($x) && length($x) && $x !~ /\D/ && 0 <= $x && $x <= 10 ) +{ # It matches }
Of course, if this is homework, be prepared to explain why each of those is important. :-)

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: check if its a number
by m.y (Novice) on Apr 14, 2008 at 18:47 UTC
    I think I will go with the regexp as several people suggested. Originally I didn't want to do that because I remembered a while back I did some benches on using regexp vs substr+index and the regexp was always slower. This code is to run in a mod_perl handler which handles a ton of requests and I was worried about it slowing everything down. thanks