Skip to content

Instantly share code, notes, and snippets.

@perlpunk
Last active April 21, 2018 11:24
Show Gist options
  • Save perlpunk/8a4d497133b43d82f10395aef216efe1 to your computer and use it in GitHub Desktop.
Save perlpunk/8a4d497133b43d82f10395aef216efe1 to your computer and use it in GitHub Desktop.
boolean vs. JSON::PP
#/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use JSON::PP;
use boolean;
use Text::Table;
my @headers = ('', qw/ boolean::true JSON::PP::true boolean::false JSON::PP::false /);
my $tb = Text::Table->new(@headers);
my @rows = (
['"$b"'],
['if ($b)'],
['! $b'],
['ref(! $b)'],
['++$b'],
['ref(++$b)'],
['--$b'],
['ref(--$b)'],
['$b + 0'],
['ref($b + 0)'],
);
for my $item (
[ 'boolean true' => true ],
[ 'JSON::PP true' => JSON::PP::true ],
[ 'boolean false' => false ],
[ 'JSON::PP false' => JSON::PP::false ],
) {
my ($name, $b) = @$item;
my $str = "$b";
push @{ $rows[0] }, "$b";
my $if = '';
if ($b) {
$if = "is true";
}
else {
$if = "is false";
}
push @{ $rows[1] }, "$if";
my $not = ! $b;
push @{ $rows[2] }, "$not";
push @{ $rows[3] }, ref $not;
my $plus = $b;
++$plus;
push @{ $rows[4] }, "$plus";
push @{ $rows[5] }, ref $plus;
my $minus = $b;
--$minus;
push @{ $rows[6] }, "$minus";
push @{ $rows[7] }, ref $minus;
my $plus0 = $b + 0;
push @{ $rows[8] }, "$plus0";
push @{ $rows[9] }, ref $plus0;
}
for my $row (@rows) {
map {
$_ //= 'undef';
$_ = "''" unless length $_;
} @$row;
}
$tb->load(@rows);
print $tb;
boolean::true JSON::PP::true boolean::false JSON::PP::false
"$b" 1 1 0 0
if ($b) is true is true is false is false
! $b 0 '' 1 1
ref(! $b) boolean '' boolean ''
++$b 94562854343665 2 94562854343641 1
ref(++$b) '' '' '' ''
--$b 94562854343663 0 94562854343639 -1
ref(--$b) '' '' '' ''
$b + 0 1 1 0 0
ref($b + 0) '' '' '' ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment