Skip to content

Commit

Permalink
Data::Dumper - dump booleans as booleans
Browse files Browse the repository at this point in the history
Use the support for tracking booleans to dump boolean values as !!1 and
!!0.
  • Loading branch information
haarg committed Jun 25, 2022
1 parent 95c346b commit a3ce572
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dist/Data-Dumper/Dumper.pm
Expand Up @@ -18,6 +18,7 @@ use 5.008_001;
require Exporter;

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

use Carp ();

Expand Down Expand Up @@ -551,6 +552,12 @@ sub _dump {
elsif (!defined($val)) {
$out .= "undef";
}
elsif (SUPPORTS_CORE_BOOLS && do {
BEGIN { SUPPORTS_CORE_BOOLS and warnings->unimport("experimental::builtin") }
builtin::is_bool($val)
}) {
$out .= $val ? '!!1' : '!!0';
}
# This calls the XSUB _vstring (if the XS code is loaded). I'm not *sure* if
# if belongs in the "Pure Perl" implementation. It sort of depends on what
# was meant by "Pure Perl", as this subroutine already relies Scalar::Util
Expand Down
11 changes: 11 additions & 0 deletions dist/Data-Dumper/Dumper.xs
Expand Up @@ -1279,6 +1279,17 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
}
}

#ifdef SvIsBOOL
if (SvIsBOOL(val)) {
if (SvTRUE(val)) {
sv_catpvs(retval, "!!1");
}
else {
sv_catpvs(retval, "!!0");
}
}
else
#endif
if (DD_is_integer(val)) {
STRLEN len;
if (SvIsUV(val))
Expand Down
20 changes: 20 additions & 0 deletions dist/Data-Dumper/t/dumper.t
Expand Up @@ -1522,6 +1522,26 @@ EOT
$want);
}

#############
{
if (!Data::Dumper::SUPPORTS_CORE_BOOLS) {
SKIP_BOTH("Core booleans not supported on older perls");
last;
}
my $want = <<'EOT';
#$VAR1 = [
# !!1,
# !!0
#];
EOT

$foo = [ !!1, !!0 ];
TEST_BOTH(q(Data::Dumper::DumperX($foo)),
'Booleans',
$want);
}


#############
{
# If XS cannot load, the pure-Perl version cannot deparse vstrings with
Expand Down

0 comments on commit a3ce572

Please sign in to comment.