Skip to content

Commit

Permalink
More elegant way to test for numeric scalars
Browse files Browse the repository at this point in the history
Now we can also dump floats without quoting.
  • Loading branch information
gisle committed Oct 15, 2011
1 parent 96cb57c commit c771f4e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
10 changes: 2 additions & 8 deletions lib/Data/Dump.pm
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,8 @@ sub _dump
if (!defined $$rval) {
$out = "undef";
}
elsif (my $v = substr($$rval, 0, 12), $$rval =~ /^-?[1-9]\d{0,9}\z/ || $$rval eq "0") {
# The assignment to $v above is required because the fact that we use
# a regexp here will actually modify $$rval if it happens to be an alias
# for one of the regexp special vars, like $1. We use substr instead of
# regular stringification, just to make it cheaper in case the scalar
# we're dumping is huge. The prefix length extracted must be longer
# than what the regexp might match for this code to be correct.
$out = $v;
elsif (do {no warnings 'numeric'; $$rval + 0 eq $$rval}) {
$out = $$rval;
}
else {
$out = str($$rval);
Expand Down
7 changes: 5 additions & 2 deletions t/dump.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use strict;
use Test qw(plan ok);
plan tests => 30;
plan tests => 33;

use Data::Dump qw(dump);

Expand All @@ -14,8 +14,11 @@ ok(dump(0), "0");
ok(dump(1234), "1234");
ok(dump(12345), "12345");
ok(dump(12345678), "12345678");
ok(dump(123456789012345), "123456789012345");
ok(dump(0.333), "0.333");
ok(dump(1/3), qr/^0\.3+\z/);
ok(dump(-33), "-33");
ok(dump(-1.5), "\"-1.5\"");
ok(dump(-1.5), "-1.5");
ok(dump("0123"), qq("0123"));
ok(dump(1..2), "(1, 2)");
ok(dump(1..3), "(1, 2, 3)");
Expand Down

0 comments on commit c771f4e

Please sign in to comment.