All pastes #16154 Raw Edit

isNumber( $value )

public text v1 · immutable
#16154 ·published 2005-06-28 20:55 UTC
rendered paste body
sub isNumeric {
    my $value = shift;

    # Only a number with value of zero, OR a string equal to '0' will eq '0',
    # (since 0.0000 is represented by just 0)
    # but a real number and a string other than '0' will not eq '0'.
    if( $value eq '0' ) {
        return 1; # Return perl true (1) since even a string equal to '0' is still a number.
    } else {
        # Here $value is either a number not equal to zero, or a string not equal to '0'.
        $value *= 1;
        if( $value == 0 ) {
            # If we multiply a string by 1, it will equal zero.
            return 0;
        } else {
            # $value must be a number not equal to zero.
            return 1;
        }
    }
}