Perl, strings, floats, unit testing and regexps!
Posted
by
Chris R
on Stack Overflow
See other posts from Stack Overflow
or by Chris R
Published on 2011-02-09T22:30:33Z
Indexed on
2011/02/09
23:25 UTC
Read the original article
Hit count: 233
perl
OK, as a preface this question potentially is 'stupider' than my normal level of question - however this problem has been annoying me for the last few days so I'll ask it anyway. I'll give a mock example of what my problem is so I can hope to generalize it to my current problem.
#!/usr/bin/perl -w use strict;
use Test::More 'no_plan';
my $fruit_string = 'Apples cost $1.50';
my ($fruit, $price) = $fruit_string =~ /(\w+)s cost \$(\d+\.\d+)/;
# $price += 0; # Uncomment for Great Success
is ($price, 1.50, 'Great Success');
Now when this is run I get the message
# Failed test 'Great Success'
# got: '1.50'
# expected: '1.5'
To make the test work - I either uncomment the commented line, or use is ($price, '1.50', 'Great Success')
. Both options do not work for me - I'm testing a huge amount of nested data using Test::Deep and cmp_deeply. My question is, how can you extract a double from a regexp then use it immediately as a double - or if there is a better way altogether let me know - and feel free to tell me to take up gardening or something lol, learning Perl is hard.
© Stack Overflow or respective owner