robkinyon / dbm-deep

DBM::Deep Perl module
http://search.cpan.org/dist/DBM-Deep/
11 stars 16 forks source link

Numerical values are stored as strings #18

Open NickHibma opened 7 years ago

NickHibma commented 7 years ago

When storing a number (e.g. 42) in DBM::Deep, it is retrieved as a string (e.g. "42"). This is generally not a problem unless you generate JSON and pass that onto Javascript which trips on this.

Example code:

use DBM::Deep;
use Devel::Peek;

my $v = 3.14;

my $db = DBM::Deep->new( "foo.db" );
$db->{s} = $v;

my $n_scalar = $v;
my $n_db = $db->{s};
my $n_db_add = $db->{s} + 0.0;

print "=====> Value:\n";
print Dump $n_scalar;
print "=====> Value run through db:\n";
print Dump $n_db;
print "=====> Value after conversion to number again:\n";
print Dump $n_db_add;

resulting in the following output:

SV = NV(0x7faed302d810) at 0x7faed302d828
  REFCNT = 1
  FLAGS = (NOK,pNOK)
  NV = 3.14
=====> Value run through db:
SV = PVNV(0x7faed30034f0) at 0x7faed302d840
  REFCNT = 1
  FLAGS = (POK,pPOK)
  IV = 0
  NV = 0
  PV = 0x7faed2d5d110 "3.14"\0
  CUR = 4
  LEN = 10
=====> Value after conversion to number again:
SV = NV(0x7faed302d858) at 0x7faed302d870
  REFCNT = 1
  FLAGS = (NOK,pNOK)
  NV = 3.14

The crucial bit is the NOK in the FLAGS section (IOK for integers, etc.).

DrHyde commented 1 year ago

This repo is no longer in use, I've replicated this ticket in the new repo at https://github.com/DBM-Deep/DBM-Deep/issues/1