Instantly share code, notes, and snippets.

Embed
What would you like to do?
<html>
<head>
<title>Perl Numbers in JSON and YAML</title>
<style type="text/css">
body {
font-family: Verdana, Arial;
}
table.numbers {
border: 1px solid #bbb;
border-collapse: collapse;
box-shadow: 2px 2px 4px 1px grey;
margin-left: 3px;
margin-right: 3px;
max-height: 98%;
}
table.numbers tr th {
background-color: #d0d0d0;
}
table.numbers tr th, table.numbers tr td {
border: 1px solid #999;
padding: 1px 2px 1px 2px;
vertical-align: bottom;
text-align: right;
font-family: monospace;
}
td.index {
background-color: #d0d0d0;
}
td.flagx {
background-color: #d8d844;
}
td.code {
white-space: pre;
background-color: #ff99ff;
text-align: left !important;
}
td.json {
background-color: #bbbbe8;
}
td.yaml {
background-color: #d8bbbb;
}
</style>
</head>
<body>
<table class="numbers">
<tr>
<th></th>
<th></th>
<th>INT</th>
<th>FLOAT</th>
<th>STRING</th>
<th>JSON::PP</th>
<th>JSON::XS</th>
<th>Cpanel::JSON::XS</th>
<th>JSON::Tiny</th>
<th>Mojo::JSON</th>
<th>YAML</th>
<th>YAML::XS</th>
<th>YAML::Syck</th>
<th>YAML::PP</th>
</tr>
<tr>
<td class="version"></td>
<td class="version"></td>
<td class="version"></td>
<td class="version"></td>
<td class="version"></td>
<td class="version">2.97001</td>
<td class="version">3.04</td>
<td class="version">4.02</td>
<td class="version">0.58</td>
<td class="version">7.75</td>
<td class="version">1.26</td>
<td class="version">0.72</td>
<td class="version">1.30</td>
<td class="version">0.007</td>
</tr>
<tr>
<td class="index">1</td>
<td class="code">$x = 10</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="json">10</td>
<td class="json">10</td>
<td class="json">10</td>
<td class="json">10</td>
<td class="json">10</td>
<td class="yaml">10
</td>
<td class="yaml">10
</td>
<td class="yaml">10
</td>
<td class="yaml">10
</td>
</tr>
<tr>
<td class="index">2</td>
<td class="code">$x = 11;
$x .= "";
$x
</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">"11"</td>
<td class="json">"11"</td>
<td class="json">"11"</td>
<td class="json">"11"</td>
<td class="json">"11"</td>
<td class="yaml">11
</td>
<td class="yaml">'11'
</td>
<td class="yaml">11
</td>
<td class="yaml">'11'
</td>
</tr>
<tr>
<td class="index">3</td>
<td class="code">$x = 12;
$x .= "x";
$x += 0;
$x
</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">12</td>
<td class="json">12</td>
<td class="json">12.0</td>
<td class="json">12</td>
<td class="json">12</td>
<td class="yaml">12
</td>
<td class="yaml">12
</td>
<td class="yaml">'12'
</td>
<td class="yaml">12.0
</td>
</tr>
<tr>
<td class="index">4</td>
<td class="code">$x = 13;
$y = $x . "string";
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">13</td>
<td class="json">"13"</td>
<td class="json">13</td>
<td class="json">13</td>
<td class="json">13</td>
<td class="yaml">13
</td>
<td class="yaml">13
</td>
<td class="yaml">13
</td>
<td class="yaml">13
</td>
</tr>
<tr>
<td class="index">5</td>
<td class="code">$x = 14;
$x .= "";
$x += 0;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="json">14</td>
<td class="json">14</td>
<td class="json">14</td>
<td class="json">14</td>
<td class="json">14</td>
<td class="yaml">14
</td>
<td class="yaml">14
</td>
<td class="yaml">14
</td>
<td class="yaml">14
</td>
</tr>
<tr>
<td class="index">6</td>
<td class="code">$x = 15;
$x .= "";
$y = $x + 1;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">15</td>
<td class="json">"15"</td>
<td class="json">15</td>
<td class="json">15</td>
<td class="json">15</td>
<td class="yaml">15
</td>
<td class="yaml">15
</td>
<td class="yaml">15
</td>
<td class="yaml">15
</td>
</tr>
<tr>
<td class="index">7</td>
<td class="code">$x = "16"</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">"16"</td>
<td class="json">"16"</td>
<td class="json">"16"</td>
<td class="json">"16"</td>
<td class="json">"16"</td>
<td class="yaml">16
</td>
<td class="yaml">'16'
</td>
<td class="yaml">16
</td>
<td class="yaml">'16'
</td>
</tr>
<tr>
<td class="index">8</td>
<td class="code">$x = "17";
$x += 0;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="json">17</td>
<td class="json">17</td>
<td class="json">17</td>
<td class="json">17</td>
<td class="json">17</td>
<td class="yaml">17
</td>
<td class="yaml">17
</td>
<td class="yaml">17
</td>
<td class="yaml">17
</td>
</tr>
<tr>
<td class="index">9</td>
<td class="code">$x = "18";
$y = $x + 0;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">18</td>
<td class="json">"18"</td>
<td class="json">18</td>
<td class="json">18</td>
<td class="json">18</td>
<td class="yaml">18
</td>
<td class="yaml">18
</td>
<td class="yaml">18
</td>
<td class="yaml">18
</td>
</tr>
<tr>
<td class="index">10</td>
<td class="code">$x = 19;
$x .= "x";
$y = $x + 0;
$x
</td>
<td class="flagx">x</td>
<td class="flagx">x</td>
<td class="flagx">x</td>
<td class="json">"19x"</td>
<td class="json">"19x"</td>
<td class="json">"19x"</td>
<td class="json">"19x"</td>
<td class="json">"19x"</td>
<td class="yaml">19x
</td>
<td class="yaml">19x
</td>
<td class="yaml">19x
</td>
<td class="yaml">19x
</td>
</tr>
<tr>
<td class="index">11</td>
<td class="code">$x = 2e1</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">20</td>
<td class="json">20</td>
<td class="json">20.0</td>
<td class="json">20</td>
<td class="json">20</td>
<td class="yaml">20
</td>
<td class="yaml">20
</td>
<td class="yaml">'20'
</td>
<td class="yaml">20.0
</td>
</tr>
<tr>
<td class="index">12</td>
<td class="code">$x = "021";
$y = $x + 0;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">"021"</td>
<td class="json">"021"</td>
<td class="json">"021"</td>
<td class="json">"021"</td>
<td class="json">"021"</td>
<td class="yaml">021
</td>
<td class="yaml">021
</td>
<td class="yaml">'021'
</td>
<td class="yaml">'021'
</td>
</tr>
<tr>
<td class="index">13</td>
<td class="code">$x = 22.1;
$x
</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">22.1</td>
<td class="json">22.1</td>
<td class="json">22.1</td>
<td class="json">22.1</td>
<td class="json">22.1</td>
<td class="yaml">22.1
</td>
<td class="yaml">22.1
</td>
<td class="yaml">'22.1'
</td>
<td class="yaml">22.1
</td>
</tr>
<tr>
<td class="index">14</td>
<td class="code">$x = 23.1;
$y = $x . '';
$x
</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">23.1</td>
<td class="json">23.1</td>
<td class="json">23.1</td>
<td class="json">23.1</td>
<td class="json">23.1</td>
<td class="yaml">23.1
</td>
<td class="yaml">23.1
</td>
<td class="yaml">'23.1'
</td>
<td class="yaml">23.1
</td>
</tr>
<tr>
<td class="index">15</td>
<td class="code">$x = 24.1;
$x .= '';
$x
</td>
<td class="flag"></td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">"24.1"</td>
<td class="json">"24.1"</td>
<td class="json">"24.1"</td>
<td class="json">"24.1"</td>
<td class="json">"24.1"</td>
<td class="yaml">24.1
</td>
<td class="yaml">'24.1'
</td>
<td class="yaml">'24.1'
</td>
<td class="yaml">'24.1'
</td>
</tr>
<tr>
<td class="index">16</td>
<td class="code">$x = "25.0";
$y = $x + 0;
$x
</td>
<td class="flagx">x</td>
<td class="flagx">x</td>
<td class="flagx">x</td>
<td class="json">"25.0"</td>
<td class="json">"25.0"</td>
<td class="json">"25.0"</td>
<td class="json">"25.0"</td>
<td class="json">"25.0"</td>
<td class="yaml">25.0
</td>
<td class="yaml">25.0
</td>
<td class="yaml">'25.0'
</td>
<td class="yaml">'25.0'
</td>
</tr>
<tr>
<td class="index">17</td>
<td class="code">$x = 0 + "inf"</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">Inf</td>
<td class="json">inf</td>
<td class="json">null</td>
<td class="json">"Inf"</td>
<td class="json">"Inf"</td>
<td class="yaml">Inf
</td>
<td class="yaml">Inf
</td>
<td class="yaml">'Inf'
</td>
<td class="yaml">.inf
</td>
</tr>
<tr>
<td class="index">18</td>
<td class="code">$x = 0 - "inf"</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">-Inf</td>
<td class="json">-inf</td>
<td class="json">null</td>
<td class="json">"-Inf"</td>
<td class="json">"-Inf"</td>
<td class="yaml">-Inf
</td>
<td class="yaml">-Inf
</td>
<td class="yaml">'-Inf'
</td>
<td class="yaml">-.inf
</td>
</tr>
<tr>
<td class="index">19</td>
<td class="code">$x = 0 + "nan"</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="json">NaN</td>
<td class="json">-nan</td>
<td class="json">null</td>
<td class="json">"NaN"</td>
<td class="json">"NaN"</td>
<td class="yaml">NaN
</td>
<td class="yaml">NaN
</td>
<td class="yaml">'NaN'
</td>
<td class="yaml">.nan
</td>
</tr>
<tr>
<td class="index">20</td>
<td class="code">$x = "0 but true";
$y = 1 + $x;
$x
</td>
<td class="flagx">x</td>
<td class="flag"></td>
<td class="flagx">x</td>
<td class="json">"0 but true"</td>
<td class="json">"0 but true"</td>
<td class="json">"0 but true"</td>
<td class="json">"0 but true"</td>
<td class="json">"0 but true"</td>
<td class="yaml">0 but true
</td>
<td class="yaml">0 but true
</td>
<td class="yaml">'0 but true'
</td>
<td class="yaml">0 but true
</td>
</tr>
</table>
</body>
</html>
INT FLOAT STRING JSON::PP JSON::XS Cpanel::JSON::XS JSON::Tiny Mojo::JSON YAML YAML::XS YAML::Syck YAML::PP
2.97001 3.04 4.02 0.58 7.75 1.26 0.72 1.30 0.007
1 $x = 10 x 10 10 10 10 10 10
10
10
10
2 $x = 11;
$x .= "";
$x
x "11" "11" "11" "11" "11" 11
'11'
11
'11'
3 $x = 12;
$x .= "x";
$x += 0;
$x
x 12 12 12.0 12 12 12
12
'12'
12.0
4 $x = 13;
$y = $x . "string";
$x
x x 13 "13" 13 13 13 13
13
13
13
5 $x = 14;
$x .= "";
$x += 0;
$x
x 14 14 14 14 14 14
14
14
14
6 $x = 15;
$x .= "";
$y = $x + 1;
$x
x x 15 "15" 15 15 15 15
15
15
15
7 $x = "16" x "16" "16" "16" "16" "16" 16
'16'
16
'16'
8 $x = "17";
$x += 0;
$x
x 17 17 17 17 17 17
17
17
17
9 $x = "18";
$y = $x + 0;
$x
x x 18 "18" 18 18 18 18
18
18
18
10 $x = 19;
$x .= "x";
$y = $x + 0;
$x
x x x "19x" "19x" "19x" "19x" "19x" 19x
19x
19x
19x
11 $x = 2e1 x 20 20 20.0 20 20 20
20
'20'
20.0
12 $x = "021";
$y = $x + 0;
$x
x x "021" "021" "021" "021" "021" 021
021
'021'
'021'
13 $x = 22.1;
$x
x 22.1 22.1 22.1 22.1 22.1 22.1
22.1
'22.1'
22.1
14 $x = 23.1;
$y = $x . '';
$x
x 23.1 23.1 23.1 23.1 23.1 23.1
23.1
'23.1'
23.1
15 $x = 24.1;
$x .= '';
$x
x "24.1" "24.1" "24.1" "24.1" "24.1" 24.1
'24.1'
'24.1'
'24.1'
16 $x = "25.0";
$y = $x + 0;
$x
x x x "25.0" "25.0" "25.0" "25.0" "25.0" 25.0
25.0
'25.0'
'25.0'
17 $x = 0 + "inf" x Inf inf null "Inf" "Inf" Inf
Inf
'Inf'
.inf
18 $x = 0 - "inf" x -Inf -inf null "-Inf" "-Inf" -Inf
-Inf
'-Inf'
-.inf
19 $x = 0 + "nan" x NaN -nan null "NaN" "NaN" NaN
NaN
'NaN'
.nan
20 $x = "0 but true";
$y = 1 + $x;
$x
x x "0 but true" "0 but true" "0 but true" "0 but true" "0 but true" 0 but true
0 but true
'0 but true'
0 but true
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use FindBin '$Bin';
use Getopt::Long;
use JSON::PP ();
use JSON::XS ();
use Cpanel::JSON::XS ();
use Mojolicious ();
use Mojo::JSON ();
use JSON::Tiny ();
use YAML ();
use YAML::Syck ();
$YAML::Syck::ImplicitTyping = 1;
use YAML::XS ();
use YAML::PP ();
use Text::Table;
use Devel::Peek ();
use HTML::Template::Compiled;
GetOptions(
"format=s" => \my $format,
);
$format ||= 'text';
my @data;
my @codes = (
q{$x = 10},
<<'EOM',
$x = 11;
$x .= "";
$x
EOM
<<'EOM',
$x = 12;
$x .= "x";
$x += 0;
$x
EOM
<<'EOM',
$x = 13;
$y = $x . "string";
$x
EOM
<<'EOM',
$x = 14;
$x .= "";
$x += 0;
$x
EOM
<<'EOM',
$x = 15;
$x .= "";
$y = $x + 1;
$x
EOM
q{$x = "16"},
<<'EOM',
$x = "17";
$x += 0;
$x
EOM
<<'EOM',
$x = "18";
$y = $x + 0;
$x
EOM
<<'EOM',
$x = 19;
$x .= "x";
$y = $x + 0;
$x
EOM
q{$x = 2e1},
<<'EOM',
$x = "021";
$y = $x + 0;
$x
EOM
<<'EOM',
$x = 22.1;
$x
EOM
<<'EOM',
$x = 23.1;
$y = $x . '';
$x
EOM
<<'EOM',
$x = 24.1;
$x .= '';
$x
EOM
<<'EOM',
$x = "25.0";
$y = $x + 0;
$x
EOM
q{$x = 0 + "inf"},
q{$x = 0 - "inf"},
q{$x = 0 + "nan"},
<<'EOM',
$x = "0 but true";
$y = 1 + $x;
$x
EOM
);
for my $code (@codes) {
push @data, eval 'no warnings; my ($x, $y);' . $code;
}
my @json = qw/ JSON::PP JSON::XS Cpanel::JSON::XS JSON::Tiny Mojo::JSON /;
my @yaml = qw/ YAML YAML::XS YAML::Syck YAML::PP /;
my @headers;
my @versions;
my @rows;
if ($format eq 'html') {
@headers = ('', '', qw/ INT FLOAT STRING /, @json, @yaml);
@versions = ('','','','','');
}
else {
@headers = ('', '', qw/ INT FLOAT STRING /, @json, @yaml);
@versions = ('','','','','');
}
my $i = 0;
for my $fw (@json, @yaml) {
my $version = $fw eq 'Mojo::JSON' ? Mojolicious->VERSION: $fw->VERSION;
push @versions, $version;
}
if ($format eq 'html') {
push @rows, [map {
+{ type => (length $? ? 'version' : ''), value => $_ }
} @versions];
}
else {
push @rows, \@versions;
}
for my $i (0 .. $#data) {
my $item = $data[ $i ];
my $code = $codes[ $i ] // '-';
# Devel::Peek::Dump $item;
my $flags = B::svref_2object(\$item)->FLAGS;
my $intflag = $flags & B::SVp_IOK ? 'x' : '';
my $floatflag = $flags & B::SVp_NOK ? 'x' : '';
my $stringflag = $flags & B::SVp_POK ? 'x' : '';
my @row;
$i++;
if ($format eq 'html') {
push @row, { type => 'index', value => $i };
push @row, { type => 'code', value => $code };
push @row, { type => "flag$intflag", value => $intflag };
push @row, { type => "flag$floatflag", value => $floatflag };
push @row, { type => "flag$stringflag", value => $stringflag };
}
else {
push @row, $i;
push @row, $code;
push @row, $intflag, $floatflag, $stringflag;
}
for my $fw (@json) {
my $can = $fw->can("encode_json");
my $json = $can->([$item]);
my $version = $fw eq 'Mojo::JSON' ? Mojolicious->VERSION: $fw->VERSION;
$json =~ s/^\[(.*)\]/$1/;
if ($format eq 'html') {
push @row, { type => 'json', value => $json };
}
else {
push @row, $json;
}
}
for my $fw (@yaml) {
my $can = $fw->can("Dump");
my $yaml = $can->($item);
my $version = $fw->VERSION;
$yaml =~ s/^---\s//;
if ($format eq 'html') {
push @row, { type => 'yaml', value => $yaml };
}
else {
push @row, $yaml;
}
}
push @rows, \@row;
push @rows, [(' ') x @row] unless $format eq 'html';
}
if ($format eq 'text') {
my $tb = Text::Table->new( @headers );
$tb->load( @rows );
print $tb;
}
elsif ($format eq 'markdown') {
say '|', (join '|', @headers), '|';
say '|', ('-|' x @headers);
for my $row (@rows) {
s/\n/<br>/g for @$row;
say '|', (join '|', @$row), '|';
}
}
elsif ($format eq 'html') {
my $htc = HTML::Template::Compiled->new(
paths => "$Bin",
filename => "table.html",
tagstyle => [ qw/ -asp -comment +tt / ],
loop_context_vars => 1,
);
$htc->param(
header => \@headers,
rows => \@rows,
);
print $htc->output;
}
INT FLOAT STRING JSON::PP JSON::XS Cpanel::JSON::XS JSON::Tiny Mojo::JSON YAML YAML::XS YAML::Syck YAML::PP
2.97001 3.04 4.02 0.58 7.75 1.26 0.72 1.30 0.007
1 $x = 10 x 10 10 10 10 10 10 10 10 10
2 $x = 11; x "11" "11" "11" "11" "11" 11 '11' 11 '11'
$x .= "";
$x
3 $x = 12; x 12 12 12.0 12 12 12 12 '12' 12.0
$x .= "x";
$x += 0;
$x
4 $x = 13; x x 13 "13" 13 13 13 13 13 13 13
$y = $x . "string";
$x
5 $x = 14; x 14 14 14 14 14 14 14 14 14
$x .= "";
$x += 0;
$x
6 $x = 15; x x 15 "15" 15 15 15 15 15 15 15
$x .= "";
$y = $x + 1;
$x
7 $x = "16" x "16" "16" "16" "16" "16" 16 '16' 16 '16'
8 $x = "17"; x 17 17 17 17 17 17 17 17 17
$x += 0;
$x
9 $x = "18"; x x 18 "18" 18 18 18 18 18 18 18
$y = $x + 0;
$x
10 $x = 19; x x x "19x" "19x" "19x" "19x" "19x" 19x 19x 19x 19x
$x .= "x";
$y = $x + 0;
$x
11 $x = 2e1 x 20 20 20.0 20 20 20 20 '20' 20.0
12 $x = "021"; x x "021" "021" "021" "021" "021" 021 021 '021' '021'
$y = $x + 0;
$x
13 $x = 22.1; x 22.1 22.1 22.1 22.1 22.1 22.1 22.1 '22.1' 22.1
$x
14 $x = 23.1; x 23.1 23.1 23.1 23.1 23.1 23.1 23.1 '23.1' 23.1
$y = $x . '';
$x
15 $x = 24.1; x "24.1" "24.1" "24.1" "24.1" "24.1" 24.1 '24.1' '24.1' '24.1'
$x .= '';
$x
16 $x = "25.0"; x x x "25.0" "25.0" "25.0" "25.0" "25.0" 25.0 25.0 '25.0' '25.0'
$y = $x + 0;
$x
17 $x = 0 + "inf" x Inf inf null "Inf" "Inf" Inf Inf 'Inf' .inf
18 $x = 0 - "inf" x -Inf -inf null "-Inf" "-Inf" -Inf -Inf '-Inf' -.inf
19 $x = 0 + "nan" x NaN -nan null "NaN" "NaN" NaN NaN 'NaN' .nan
20 $x = "0 but true"; x x "0 but true" "0 but true" "0 but true" "0 but true" "0 but true" 0 but true 0 but true '0 but true' 0 but true
$y = 1 + $x;
$x
<html>
<head>
<title>Perl Numbers in JSON and YAML</title>
<style type="text/css">
body {
font-family: Verdana, Arial;
}
table.numbers {
border: 1px solid #bbb;
border-collapse: collapse;
box-shadow: 2px 2px 4px 1px grey;
margin-left: 3px;
margin-right: 3px;
max-height: 98%;
}
table.numbers tr th {
background-color: #d0d0d0;
}
table.numbers tr th, table.numbers tr td {
border: 1px solid #999;
padding: 1px 2px 1px 2px;
vertical-align: bottom;
text-align: right;
font-family: monospace;
}
td.index {
background-color: #d0d0d0;
}
td.flagx {
background-color: #d8d844;
}
td.code {
white-space: pre;
background-color: #ff99ff;
text-align: left !important;
}
td.json {
background-color: #bbbbe8;
}
td.yaml {
background-color: #d8bbbb;
}
</style>
</head>
<body>
<table class="numbers">
<tr>
[%loop header %]
<th>[%= _ %]</th>
[%/loop header %]
</tr>
[%loop rows %]
<tr>
[%loop _ %]
<td class="[%= type %]">[%= value %]</td>
[%/loop _ %]
</tr>
[%/loop rows %]
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment