T
Tore Aursand
Hi!
Not having written many tests myself, I started yesterday looking av some
of the Test modules. Boring stuff, but an excellent way of keeping your
code clean from those annoying bugs.
However. In one of the modules, there was a 'is_decimal' function, and it
failed some of my tests. Not surprisingly, though. But. Is there a way
to check if a number is a decimal number even when you declare the value
as a "plain number"?
Example:
my $nr = 5.00;
if ( is_decimal($nr) ) {
print "is_decimal\n";
}
sub is_decimal {
my $nr = shift;
return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
}
This always fails, of course, as Perl seems to evaluate _the number_ 5.00
as the number 5 as soon as the variable $nr gets set. Setting $nr to a
string - ie. '5.00' - works fine.
Is there a way round this "problem"? No big deal, really, but just out of
curiousity.
Not having written many tests myself, I started yesterday looking av some
of the Test modules. Boring stuff, but an excellent way of keeping your
code clean from those annoying bugs.
However. In one of the modules, there was a 'is_decimal' function, and it
failed some of my tests. Not surprisingly, though. But. Is there a way
to check if a number is a decimal number even when you declare the value
as a "plain number"?
Example:
my $nr = 5.00;
if ( is_decimal($nr) ) {
print "is_decimal\n";
}
sub is_decimal {
my $nr = shift;
return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
}
This always fails, of course, as Perl seems to evaluate _the number_ 5.00
as the number 5 as soon as the variable $nr gets set. Setting $nr to a
string - ie. '5.00' - works fine.
Is there a way round this "problem"? No big deal, really, but just out of
curiousity.