diff --git a/examples/try_me.pl b/examples/try_me.pl index a936464..aedf3ce 100644 --- a/examples/try_me.pl +++ b/examples/try_me.pl @@ -30,12 +30,14 @@ package main; baz => 789, }, readonly => \2, + boolean => [1 == 1, 1 == 2], regexp => qr/foo.*bar/i, glob => \*STDOUT, code => sub { return 42 }, class => $obj, }; + $sample->{weakref} = $sample; weaken $sample->{weakref}; diff --git a/lib/Data/Printer/Filter/SCALAR.pm b/lib/Data/Printer/Filter/SCALAR.pm index b96c5d4..419d427 100644 --- a/lib/Data/Printer/Filter/SCALAR.pm +++ b/lib/Data/Printer/Filter/SCALAR.pm @@ -4,6 +4,8 @@ use warnings; use Data::Printer::Filter; use Scalar::Util; +my $HAS_BOOLEAN = $] ge '5.036000' && eval 'use builtin; 1'; + filter 'SCALAR' => \&parse; filter 'LVALUE' => sub { my ($scalar_ref, $ddp) = @_; @@ -23,6 +25,13 @@ sub parse { if (not defined $value) { $ret = $ddp->maybe_colorize('undef', 'undef'); } + elsif ($HAS_BOOLEAN && _is_bool($value)) { + if ($value) { + $ret = $ddp->maybe_colorize('true', 'true'); + } else { + $ret = $ddp->maybe_colorize('false', 'false'); + } + } elsif ( $ddp->show_dualvar ne 'off' ) { my $numified; $numified = do { no warnings 'numeric'; 0+ $value } if defined $value; @@ -122,4 +131,10 @@ sub _is_number { return $is_number; } +sub _is_bool { + no warnings 'experimental::builtin'; + return builtin::is_bool($_[0]); +} + + 1; diff --git a/lib/Data/Printer/Theme.pm b/lib/Data/Printer/Theme.pm index 73bf230..22a76b5 100644 --- a/lib/Data/Printer/Theme.pm +++ b/lib/Data/Printer/Theme.pm @@ -228,6 +228,8 @@ Data::Printer::Theme - create your own color themes for DDP! vstring => '#aabbcc', # version strings (v5.30.1, etc) lvalue => '#aabbcc', # lvalue label format => '#aabbcc', # format type + true => '#aabbcc', # boolean type (true) + false => '#aabbcc', # boolean type (false) repeated => '#aabbcc', # references to seen values caller_info => '#aabbcc', # details on what's being printed weak => '#aabbcc', # weak references flag diff --git a/lib/Data/Printer/Theme/Classic.pm b/lib/Data/Printer/Theme/Classic.pm index f21cf81..0ef91a3 100644 --- a/lib/Data/Printer/Theme/Classic.pm +++ b/lib/Data/Printer/Theme/Classic.pm @@ -18,6 +18,8 @@ sub colors { vstring => 'bright_blue', # version strings (v5.16.0, etc) lvalue => '', # lvalue label format => '', # format type + true => 'bright_cyan', # boolean type (true) + false => 'bright_cyan', # boolean type (false) repeated => 'white on_red', # references to seen values caller_info => 'bright_cyan', # details on what's being printed weak => 'cyan', # weak references diff --git a/lib/Data/Printer/Theme/Material.pm b/lib/Data/Printer/Theme/Material.pm index 57a6e0a..8abb6bf 100644 --- a/lib/Data/Printer/Theme/Material.pm +++ b/lib/Data/Printer/Theme/Material.pm @@ -1,6 +1,6 @@ package Data::Printer::Theme::Material; # inspired by Mattia Astorino's Material theme: -# https://github.com/equinusocio/vsc-material-theme +# https://github.com/material-theme/vsc-material-theme use strict; use warnings; @@ -37,6 +37,8 @@ sub colors { vstring => $code_for{strong_orange}, # version strings (v5.16.0, etc) lvalue => $code_for{strong_orange}, # lvalue label format => $code_for{strong_orange}, # format type + true => $code_for{blue}, # boolean type (true) + false => $code_for{blue}, # boolean type (false) repeated => $code_for{red}, # references to seen values caller_info => $code_for{gray}, # details on what's being printed weak => $code_for{green}, # weak references flag diff --git a/lib/Data/Printer/Theme/Monokai.pm b/lib/Data/Printer/Theme/Monokai.pm index cc9b973..282a3d3 100644 --- a/lib/Data/Printer/Theme/Monokai.pm +++ b/lib/Data/Printer/Theme/Monokai.pm @@ -28,6 +28,8 @@ sub colors { vstring => $code_for{cyan}, # version strings (v5.16.0, etc) lvalue => $code_for{green}, # lvalue label format => $code_for{violet}, # format type + true => $code_for{violet}, # boolean type (true) + false => $code_for{violet}, # boolean type (false) repeated => $code_for{pink}, # references to seen values caller_info => $code_for{grey}, # details on what's being printed weak => $code_for{green}, # weak references flag diff --git a/lib/Data/Printer/Theme/Solarized.pm b/lib/Data/Printer/Theme/Solarized.pm index fa8a864..0902327 100644 --- a/lib/Data/Printer/Theme/Solarized.pm +++ b/lib/Data/Printer/Theme/Solarized.pm @@ -38,8 +38,10 @@ sub colors { vstring => $code_for{base1}, # version strings (v5.16.0, etc) lvalue => $code_for{green}, # lvalue label format => $code_for{green}, # format type - repeated => $code_for{red}, # references to seen values - caller_info => $code_for{cyan}, # details on what's being printed + true => $code_for{blue}, # boolean type (true) + false => $code_for{blue}, # boolean type (false) + repeated => $code_for{red}, # references to seen values + caller_info => $code_for{cyan}, # details on what's being printed weak => $code_for{violet}, # weak references flag tainted => $code_for{violet}, # tainted flag unicode => $code_for{magenta}, # utf8 flag diff --git a/t/002-scalar.t b/t/002-scalar.t index 6f1ab3f..ac761b5 100644 --- a/t/002-scalar.t +++ b/t/002-scalar.t @@ -2,11 +2,12 @@ # ^^ taint mode must be on for taint checking. use strict; use warnings; -use Test::More tests => 67; +use Test::More tests => 72; use Data::Printer::Object; use Scalar::Util; test_basic_values(); +test_boolean_values(); test_tainted_values(); test_unicode_string(); test_escape_chars(); @@ -57,6 +58,23 @@ sub test_basic_values { is $object->parse(\$var), '123', 'integer 123 in variable'; } +sub test_boolean_values { + SKIP: { + skip 'booleans only exist after 5.36', 5 unless $] ge '5.036000'; + my $object = Data::Printer::Object->new( colored => 0 ); + my $var = 1 == 1; + is $object->parse(\$var), 'true', 'boolean true is "true"'; + $var = 1 == 2; + is $object->parse(\$var), 'false', 'boolean false is "false"'; + $var = 1; + is $object->parse(\$var), '1', '1 is 1, not "true"'; + $var = ''; + is $object->parse(\$var), '""', 'empty string is "", not "false"'; + $var = 0; + is $object->parse(\$var), '0', '0 is 0, not "false"'; + }; +} + sub test_tainted_values { SKIP: { # only use 1 char substring to avoid leaking