10. Example VII: Regexp::Common

Stricter validation can be necessary when using the modules Params::ValidationCompiler and Getopt::Long module. E.g. in these modules will a number be 'casted' to a string but not vice versa (in fact a number is a string of digits and some other characters: see test set @exprs below). The module Regexp::Common can be helpful:


#!/usr/bin/perl

use Regexp::Common;

@exprs = qw(1 5.25 0.001 1.3e8 foo bar 1dd inf infinity 0x12 1_000_000 1,000,000);

foreach $expr (@exprs) {
    print "$expr is",  ($expr =~ /$RE{num}{real}/) ? '' : ' not', " a number\n";
}

Output:
1 is a number
5.25 is a number
0.001 is a number
1.3e8 is a number
foo is not a number
bar is not a number
1dd is a number
inf is not a number
infinity is not a number
0x12 is a number
1_000_000 is a number
1,000,000 is a number