in reply to variable type

You don't want to know if a value is a string or number, since you're building SQL so they're always converted to strings. You want to know if the string only contains numbers digits. You should probably use DBI's $dbh->quote or placeholders. But otherwise my $is_num = $value =~ /^-?\d+(\.\d+)?$/;

Replies are listed 'Best First'.
Re^2: variable type
by imp (Priest) on Dec 07, 2006 at 14:55 UTC
    That's almost correct. $ will match an optional newline as well, so both "5" and "5\n" are valid. Better to use \z

    print "1. number\n" if "5" =~ /^\d+$/; print "1. number and newline\n" if "5\n" =~ /^\d+$/; print "2. number\n" if "5" =~ /^\d+\z/; print "2. number and newline\n" if "5\n" =~ /^\d+\z/
    Update
    The advice to use a \z fixes the problem of bad data being submitted, but it might block otherwise good data, depending on the input source. Data read from a filehandle could have the newline present, without it being from a misbehaving user.
    chomping the field in question is a better approach than being stricter with the regex.