Re: When is a string a number?



On 10/4/06, Martin J. Evans <martin.evans@xxxxxxxxxxxx> wrote:

With DBI/DBD::Oracle all values read from the database are scalars. As
everyone will know, whether something read from the database is a string
or a number in Perl purely depends on the context it is used in so:

internally, there are flags in the SV structure that hint as to what conversions
have been done on the SV already, for efficiency's sake.

I have to admit I don't know how the JSON module knows what is a number
and what is a string in Perl but I see the same issue with Data::Dumper
so I presume there must be some way to find out if a perl scalar is a
number or a string.

It seems like the pure perl Dumper tests values with a regex and optimizes
to numbers when the stringification matches a number template, while
the XS version checks the flags in the SV structure.

The problem gets a lot worse for me since I do some arithmetic on values
pulled from the database before converting them to JSON and this is
where Perl seems to change them into numbers e.g.

I don't want to have to do ($var +0) on all the number fields I pull
from the database (to turn them into numbers) and neither do I want to
do a '$var .= ""' (to turn all the fields into strings).

you might have to do exactly that. "$var" will produce a string version. Sorry.
Apparently you can tell the JSON module to make everything strings:
http://search.cpan.org/~makamaka/JSON-1.07/lib/JSON.pm#AUTOCONVERT

As an aside (and probably a perl question rather than a DBI one) does
anyone know why the type of a scalar changes when you use it on the
right side of an assignment:

perl -MData::Dumper -le '$a="1"; print Dumper($a); $b += $a; print
Dumper($a);'
$VAR1 = '1';
$VAR1 = 1;

The has-been-evaluated-as-a-number flag got set on $a when it was
evaluated as a number; then Dumper, with both available, chose the
number format.

How does JSON and Data::Dumper know whether Perl thinks something is a
number or a string?

inspecting the flags; except pure-perl Dumper apparently uses a
regular expression
to identify numbers. Those are guesses. The source is available for
your inspection.


--
The Country Of The Blind, by H.G. Wells
http://cronos.advenge.com/pc/Wells/p528.html
.



Relevant Pages

  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: When is a string a number?
    ... whether something read from the database is a string ... or a number in Perl purely depends on the context it is used in so: ... number or a string. ... I moved from JSON to JSON Syck. ...
    (perl.dbi.users)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Database insertion, escape issue
    ... I have a Perl script where I try to strip some data from a web page ... I am capturing the paragraphs of text and inserting ... I am currently trying this with a SQLite database. ... # Create the file string ...
    (perl.beginners)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)