Skip to content

Instantly share code, notes, and snippets.

Created August 26, 2013 10:42
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 anonymous/6340200 to your computer and use it in GitHub Desktop.
Save anonymous/6340200 to your computer and use it in GitHub Desktop.
Funny Mojo::JSON FLAGS handling
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'uninitialized';
use Mojo::JSON;
use Data::Dump 'pp';
use B;
my $data = { 'string' => 'Mojolicious', 'number' => 42, };
# run as:
# perl flagsbug.pl
# perl flagsbug.pl pp
# perl flagsbug.pl num
# The first one is OK, the pp one exhibits the problem,
# the num one shows why the problem appears...
my $flags = B::svref_2object(\$data->{string})->FLAGS;
printf("Original: flags: %08x, SVp_IOK: %08x, SVp_NOK: %08x, flags&(IOK|NOK): %08x\n",
$flags, B::SVp_IOK, B::SVp_NOK, $flags & (B::SVp_IOK | B::SVp_NOK));
if ($ARGV[0] eq "pp") {
pp($data);
} elsif ($ARGV[0] eq "num") {
no warnings 'numeric';
if ($data->{string} + 0 eq $data->{string}) {
print "String is a number\n";
} else {
print "String is not a number\n";
}
}
$flags = B::svref_2object(\$data->{string})->FLAGS;
printf("Possibly modified: flags: %08x, SVp_IOK: %08x, SVp_NOK: %08x, flags&(IOK|NOK): %08x\n",
$flags, B::SVp_IOK, B::SVp_NOK, $flags & (B::SVp_IOK | B::SVp_NOK));
print Mojo::JSON->new->encode($data), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment