Skip to content

Instantly share code, notes, and snippets.

@karupanerura
Created June 18, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karupanerura/223b2d862f0c0c966e706e7062cb8cc9 to your computer and use it in GitHub Desktop.
Save karupanerura/223b2d862f0c0c966e706e7062cb8cc9 to your computer and use it in GitHub Desktop.
TPCiP LT Executable Example Codes
use strict;
use warnings;
use utf8;
use feature qw/say/;
use JSON::XS;
use B;
my $j = JSON::XS->new->allow_nonref;
sub json_type {
my $sv = shift;
my $json = $j->encode($sv);
return 'null' if $json eq 'null';
return 'boolean' if $json eq 'true' || $json eq 'false';
return 'string' if $json =~ /^"/;
return 'number';
}
sub perl_type {
my $sv = shift;
my $o = B::svref_2object(\$sv);
return 'PV' if ref $o eq 'B::PV';
return 'NV' if ref $o eq 'B::NV';
return 'IV' if ref $o eq 'B::IV';
return 'PVIV' if ref $o eq 'B::PVIV';
return 'PVNV' if ref $o eq 'B::PVIV';
die "Unknwon type: ". ref $o;
}
sub show_type {
my $sv = shift;
say $sv, ': ', perl_type($sv), ' -> ', json_type($sv);
}
show_type(do {
my $x = 'a';
$x;
});
show_type(do {
my $x = 1;
$x;
});
show_type(do {
my $x = '1';
$x;
});
show_type(do {
my $x = 1;
print "DEBUG: x = $x\n"; # <-- Creates PV slot implicitly
$x;
});
// JavaScript
const response = JSON.parse(`{"expiresTime":"3600"}`);
const expiresTime = response.expiresTime;
const nowEpoch = Math.floor(Date.now() / 1000);
const expiresAt = expiresTime + nowEpoch; // String+Number is string concat
console.log(expiresAt); // 3600XXXXXXXX
use strict;
use warnings;
use utf8;
use feature qw/say/;
use JSON::XS qw/encode_json/;
use JSON::Types;
my $json = encode_json([bool 0, number 1, string 2]);
say $json; # => [false,1,"2"]
use strict;
use warnings;
use utf8;
use feature qw/say/;
use Cpanel::JSON::XS;
use Cpanel::JSON::XS::Type;
my $schema = [JSON_TYPE_BOOL, JSON_TYPE_INT, JSON_TYPE_STRING];
my $json = encode_json([0, 1, 2], $schema);
say $json; # => [false,1,"2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment