Subject: | Zero value not printing correctly |
Date: | Wed, 25 Jan 2017 11:13:48 +0000 |
To: | "bug-Perl6-Form [...] rt.cpan.org" <bug-Perl6-Form [...] rt.cpan.org> |
From: | "Long, Stuart" <Stuart.Long [...] hsn.net> |
Perl6-Form-0.06
perl 5.16.3 on RHEL7
Example:
use Perl6::Form;
use strict;
use warnings;
my @vals = (0.23, 1.14);
print form
"{]]].[}", \@vals ;
Output:
#perl foo
0.23.
1.14
Investigation:
Culprit appears to be sub jnum, lines 385-398.
if ($integral) {
local $SIG{__WARN__} = sub { $fail = 1 };
$str = sprintf('%*d',$val{width},int($orig));
}
else {
local $SIG{__WARN__} = sub { $fail = 1 };
$str = sprintf('%*.*f',$val{width},$places,$orig);
}
if ($fail) {
$_[0] = $huh;
}
else {
my ($w,$p) = ($str =~ /^\s*(.*)\.(.*)$/g); # floating point
($w,$p) = ($str =~ /^\s*(.*)$/,"") if !$w; # integer
If the substring to the left of the decimal point ($w from the first RE) is zero, then the !$w triggers, and the string is interpreted as an integer, with bogus results.
Possible solution:
Since the preceding logic already branches on whether or not the value is floating point or integer, move the $w and $p assignments there, and eliminate the cascading RE decision:
my ($w, $p);
if ($integral) {
local $SIG{__WARN__} = sub { $fail = 1 };
$str = sprintf('%*d',$val{width},int($orig));
($w,$p) = ($str =~ /^\s*(.*)$/,""); # integer
}
else {
local $SIG{__WARN__} = sub { $fail = 1 };
$str = sprintf('%*.*f',$val{width},$places,$orig);
($w,$p) = ($str =~ /^\s*(.*)\.(.*)$/g); # floating point
}
if ($fail) {
$_[0] = $huh;
}
else {
# my ($w,$p) = ($str =~ /^\s*(.*)\.(.*)$/g); # floating point
# ($w,$p) = ($str =~ /^\s*(.*)$/,"") if !$w; # integer
This results in properly formatted output:
#perl foo
0.23
1.14
http://www.hsn.com<http://www.hsn.com/>
[http://images.hsn.com/images/corpemail/2013_hsn_logo_prod.jpg]
Message body is not shown because it is too large.