in reply to Confusion over B::PV

PV checks whether the scalar is a string. Use PVX to get the string buffer regardless of whether the scalar is a string. Since PV and PVX can only be called on subclasses of B::PV, you might want to check if you have the right class first.
my $x = $sv->can('PVX') ? $sv->PVX : undef;

But why are you checking that?! It's equally correct for it to be present and absent.


use strict; use warnings; use Test::More tests => 3; use Devel::Peek; use B qw( svref_2object SVf_IOK SVf_NOK SVf_POK ); sub is_iv { my $sv = svref_2object(my $ref = \$_[0]); my $flags = $sv->FLAGS; my $pv = $sv->can('PVX') ? $sv->PVX : undef; if (wantarray) { return ($flags & SVf_IOK, $pv); } else { return $flags & SVf_IOK; } } my $r = "100"; $r = 100; Dump($r); my ($iv, $pv) = is_iv($r); is($r, 100, "correct value returned SQL_INTEGER"); ok($iv, "ivok bind integer"); ok($pv, "pv not null bind integer");

Replies are listed 'Best First'.
Re^2: Confusion over B::PV
by mje (Curate) on May 19, 2012 at 17:14 UTC

    Thank you for the explanation. The test is looking to see that the scalar does not look like a string because it wants to behave like a number because it was a number in the database. The test is simply ensuring the scalar was either created with sv_setiv or sql_type_cast_svpv was run on it.

    Before sql_type_cast_svpv or DBDs like DBD::ODBC (here) set the scalar with sv_setiv the scalar looks like a string then you have to add 0 to all your numbers retrieved from the database before passing them to modules like JSON::XS or they will be turned into "NNN" instead of NNN. Another example was people doing select a_int, b_int from table then performing a_int & b_int which gives the wrong answer unless you add 0 to them first. See http://search.cpan.org/~timb/DBI-1.620/DBI.pm#sql_type_cast and various discussions on dbi-dev mailing list.

      The test is looking to see that the scalar does not look like a string

      You want SvPOK(sv) which is defined to be SvFLAGS(sv) & SVf_POK and is available as $sv->POK.