validatingcomputerizedsystem com - Validating number in perl

validating number in perl-52validating number in perl-79

By default, this module exports a hash called that you index into, according to which kind of regular expression you're looking for.

Besides saving you from having to figure out the patterns on your own, it also makes your code more legible.

But, for further self-improvement or just for fun, you may want to to read perlop re 'Unary "-", or use Super Search or Google for "Na N" and "looks_like_number".

Bottom line: toolic's advice is probably about as good as any you'll find.

Here are some precooked solutions (the Cookbook's equivalent of just-add-water meals) for most common cases: warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d $/; # rejects -3 warn "not an integer" unless /^-? \d*$/; # rejects .2 warn "not a decimal number" unless /^-? General documentation for the module can be found in the Regexp:: Common manpage, but more detailed documentation for just the numeric patterns is in the Regexp:: Common::number manpage.