tfoertsch has asked for the wisdom of the Perl Monks concerning the following question:

I get passed an SV* and want to make sure it is a valid string. How to do that?

Consider this sv:

SV = PVMG(0x2492968) at 0x1e759c8 REFCNT = 1 FLAGS = (PADMY,GMG,SMG,pIOK) IV = 123456 NV = 0 PV = 0x1f76868 "123foobar"\0 CUR = 9 LEN = 16 MAGIC = 0x1f83b18 MG_VIRTUAL = &PL_vtbl_taint MG_TYPE = PERL_MAGIC_taint(t)
It is an IV and has an invalid buffer assigned.

How can I do something like $x.=''?

I thought SvUPGRADE($buffer, SVt_PV) is the answer but that changes nothing.

Thanks

Replies are listed 'Best First'.
Re: XS question
by BrowserUk (Patriarch) on Jul 16, 2008 at 15:31 UTC

    Just using SvPV will stringify a numeric value if needed. From PerlAPI:

    SvPV

    Returns a pointer to the string in the SV, or a stringified form of the SV if the SV does not contain a string.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: XS question
by Anonymous Monk on Jul 16, 2008 at 20:38 UTC
    I don't know what you mean with valid string, but in Perl all scalar values are capable of being stringified and as such it has no concept of 'valid string'. Calling SvPV will give you this string value. If you want to force stringification before calling SVPV you could simply do sv_catpvn_mg(sv, "", 0).