Skip to content

Commit a3ce572

Browse files
committed
Data::Dumper - dump booleans as booleans
Use the support for tracking booleans to dump boolean values as !!1 and !!0.
1 parent 95c346b commit a3ce572

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

dist/Data-Dumper/Dumper.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use 5.008_001;
1818
require Exporter;
1919

2020
use constant IS_PRE_516_PERL => $] < 5.016;
21+
use constant SUPPORTS_CORE_BOOLS => defined &builtin::is_bool;
2122

2223
use Carp ();
2324

@@ -551,6 +552,12 @@ sub _dump {
551552
elsif (!defined($val)) {
552553
$out .= "undef";
553554
}
555+
elsif (SUPPORTS_CORE_BOOLS && do {
556+
BEGIN { SUPPORTS_CORE_BOOLS and warnings->unimport("experimental::builtin") }
557+
builtin::is_bool($val)
558+
}) {
559+
$out .= $val ? '!!1' : '!!0';
560+
}
554561
# This calls the XSUB _vstring (if the XS code is loaded). I'm not *sure* if
555562
# if belongs in the "Pure Perl" implementation. It sort of depends on what
556563
# was meant by "Pure Perl", as this subroutine already relies Scalar::Util

dist/Data-Dumper/Dumper.xs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,17 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
12791279
}
12801280
}
12811281

1282+
#ifdef SvIsBOOL
1283+
if (SvIsBOOL(val)) {
1284+
if (SvTRUE(val)) {
1285+
sv_catpvs(retval, "!!1");
1286+
}
1287+
else {
1288+
sv_catpvs(retval, "!!0");
1289+
}
1290+
}
1291+
else
1292+
#endif
12821293
if (DD_is_integer(val)) {
12831294
STRLEN len;
12841295
if (SvIsUV(val))

dist/Data-Dumper/t/dumper.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,26 @@ EOT
15221522
$want);
15231523
}
15241524

1525+
#############
1526+
{
1527+
if (!Data::Dumper::SUPPORTS_CORE_BOOLS) {
1528+
SKIP_BOTH("Core booleans not supported on older perls");
1529+
last;
1530+
}
1531+
my $want = <<'EOT';
1532+
#$VAR1 = [
1533+
# !!1,
1534+
# !!0
1535+
#];
1536+
EOT
1537+
1538+
$foo = [ !!1, !!0 ];
1539+
TEST_BOTH(q(Data::Dumper::DumperX($foo)),
1540+
'Booleans',
1541+
$want);
1542+
}
1543+
1544+
15251545
#############
15261546
{
15271547
# If XS cannot load, the pure-Perl version cannot deparse vstrings with

0 commit comments

Comments
 (0)