Data-Printer icon indicating copy to clipboard operation
Data-Printer copied to clipboard

support native booleans in Data::Printer::Filter::Web

Open rohanc opened this issue 2 years ago • 0 comments

Perl 5.36 comes with stable boolean tracking. Could you please update Data::Printer::Filter::Web so that it prints “true” or “false” when it encounters builtin::true? It can do this easily using builtin::is_bool.

Here’s my test code:

use warnings;
use strict;
no warnings (qw/experimental::builtin/);
use builtin (qw/is_bool true false/);
use Data::Printer filters => ["Web"],
use Types::Serialiser;
 
my $apple=Types::Serialiser::true;
p($apple); # prints "true"
my $pear=builtin::true;
p($pear); # prints 1

I don't think any of the major Perl JSON libraries emit builtin::true and builtin::false yet, but it's possible to make JSON::XS emit them from decode_json by calling:

my $coder=JSON::XS->new;
$coder->boolean_values(builtin::false,builtin::true);

Here's a demonstration script:

no warnings (qw/experimental::builtin/);
use builtin (qw/is_bool true false/);
use JSON::XS;
use Data::Printer;
use Devel::Peek qw( Dump );

my $coder=JSON::XS->new;
$coder->boolean_values(builtin::false,builtin::true);
my $data = $coder->decode("[0,1,false,true]");
Dump $$data[2];

for which the output is

SV = PVNV(0xaaaac272d7a0) at 0xaaaac2752e80
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,IsCOW,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0xaaaab2090cb0 "" [BOOL PL_No]
  CUR = 0
  LEN = 0

You might even want to modify Data::Printer to always write "true" or false when a scalar satisfies builtin::is_bool. That might be risky for other tools, but defensible for Data::Printer given that you have the stated aim of only producing human-readable output.

rohanc avatar Mar 10 '23 02:03 rohanc