in reply to Confusion over B::PV

So you want $sv->PV() to return a true value while you also want PV to not be set in the Dump() output? I am going to assume that your "should this be set?" comment just gave me an incorrect impression.

DB<1> $x= 100 DB<2> Dump $x SV = IV(0x2f10ee0) at 0x2f10ee4 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 100 DB<3> $y = "$x" DB<4> Dump $x SV = PVIV(0x424424) at 0x2f10ee4 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 100 # ^^^ ^^^^ These are important PV = 0x2eeda6c "100"\0 CUR = 3 LEN = 4 DB<5>

$sv->PV() is not returning a value because your scalar isn't marked as having a valid PV value (no POK nor pPOK flags set). I don't know what $sv->PV() actually translates to in C code.

It appears that the scalar had previously been upgraded to type PVIV and Perl doesn't waste resources to downgrade it to type IV when sv_setiv() marks the IV as the only value currently in sync with the SV's value. Perl doesn't even waste time free()ing the PV's buffer or putting a '\0' character at the front of it.

You don't show enough code for me to be able to determine why the "100" string was already sitting cached (it appears) in that SV. You might want to Dump($r) in between each of your steps to gain better insights.

If you want a PV to be cached in the SV, then it looks like $sv->PV() doesn't do that work so you should use the scalar as a string before that.

- tye        

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

    Also, to explain why there was 100 already in the pv it was because a previous test (prior to the one I showed) did a sv_setpvn. Sorry, I tried to omit other stuff for brevity and in this case probably added to the confusion.

Re^2: Confusion over B::PV (POK)
by mje (Curate) on May 19, 2012 at 17:29 UTC

    I think ikegami answered my question. I thought PV simply returned the string pointer which is really what PVX does. As such I was surprised PV was false but the scalar had a pv.

    The should be set test comment was left over from the fact that another bug masked the fact that test should have failed. Now that is fixed I am expecting PV to return undef and don't care there is a pv pointer.