Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rohanc opened this issue Mar 10, 2023 · 0 comments
Open

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

rohanc opened this issue Mar 10, 2023 · 0 comments

Comments

@rohanc
Copy link

rohanc commented Mar 10, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant